0

Is there any way to use two buffers in compute shader in HLSL in Unity? I need to use one to input some scalar values on a grid and one to get back vertex array generated by marching cubes.

2
  • I used multiple StructuredBuffer<float4> and it works just fine. Without further details I'd expect no problems. Commented Aug 26, 2022 at 17:48
  • @KYL3R but how do I know which buffer do I pass data to? and which buffer do i get data from? Commented Aug 26, 2022 at 17:50

1 Answer 1

1

You can define a RWStructuredBuffer - RW means Read+Write A normal StructuredBuffer is write-protected: you can't write TO it from the shader, but read it after passing it from C#.

You can define multiple in shader:

RWStructuredBuffer<float4> myFirstBuffer : register(u1); // index1 !
RWStructuredBuffer<float4> mySecondBuffer : register(u2); // index2 !

And access them like this in C#:

Graphics.ClearRandomWriteTargets();
material.SetPass(0);
material.SetBuffer("myFirstBuffer", myFirstBuffer);
Graphics.SetRandomWriteTarget(1, myFirstBuffer, false); // index 1!
compute_buffer.GetData(myFirstBuffer);

material.SetBuffer("mySecondBuffer", mySecondBuffer);
Graphics.SetRandomWriteTarget(2, mySecondBuffer, false); // index 2!
compute_buffer.GetData(mySecondBuffer);
Sign up to request clarification or add additional context in comments.

2 Comments

Is specyfying material really needed when using compute shader?
I didn't test the code, but it should give you an idea. You will find out the rest :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.