- Home /
Use contents of RWStructuredBuffer written by shader in another shader
Hello,
I use a vertex-fragment shader to write stuff into a RWStructuredBuffer
. I want to use the results inside another shader where I only need read access, so a StructuredBuffer
would be enough. I just seem not to be able to make it work.
Basically what I do for the first shader, let's call it ShaderA
Script
ComputeBuffer cf = new ComputeBuffer(nEntries, System.Runtime.InteropServices.Marshal.SizeOf<MyStruct>(), ComputeBufferType.Default);
Graphics.SetRandomWriteTarget(1, cf);
MyStruct[] myStructVals = new MyStruct[nEntries];
cf.SetData(myStructVals, 0, 0, myStructVals.Length);
Shader.SetGlobalBuffer("_CustomBuffer", cf);
Shader
#ifdef SHADER_API_D3D11
RWStructuredBuffer<MyStruct> _CustomBuffer : register(u1);
#endif
....
// write to it
What I write into it, I want to use in another shader, ShaderB. And here, I have struggle doing so. I have a working solution, but it is not optimal. So this is working although it gives an error (UAV registers live in the same name space as outputs, so they must be bound to at least u4, manual bind to slot u1 failed):
#ifdef SHADER_API_D3D11
RWStructuredBuffer<MyStruct> buffer : register(u1);
#endif
I guess it works because u1 is also used in the other shader, if I use the suggested u4, I cannot read values from there. I read somewhere, that the buffers stay in the same state unless altered by set buffer.
This also does not work:
#ifdef SHADER_API_D3D11
StructuredBuffer<MyStruct> buffer : register(t1);
#endif
But it works for another StructuredBuffer of another type which gets registered to t0.
On the C# end, the first approach works if I assign the ComputeBuffer for ShaderA directly and if I copy the values into another, new, ComputeBuffer. Both things do not work for the StructuredBuffer approach.
Copy approach
MyStruct[] temp = new MyStruct[ShaderAScript.BufferCount];
ShaderAScript.cf.GetData(temp);
buffer.SetData(temp);
shaderBMaterial.SetBuffer("buffer", buffer);
Direct assign
shaderBMaterial.SetBuffer("buffer", ShaderAScript.cf);
Your answer
Follow this Question
Related Questions
Shader: Access current pixel screen position from within #ifdef SHADER_API_D3D11 0 Answers
Adding a clip() to the default shader? 0 Answers
Native rendering plugin D3D11 device type not being set in x86 builds. 0 Answers
Making shader not ignore Light on transparent areas ? 0 Answers
cast shadow - export lightmap from 3ds max to unity 2 Answers