1
$\begingroup$

I am relatively new to compute shaders and gpgpu in general so bear with me if i am missing the point somewhere.

The main thing i do not yet understand / see is how to get memory from a compute shader to global shader memory usable in a pixel shader without passing it back and forth between GPU and CPU.

I am implementing an irradiance caching lighting solution with Unity3D for learning purposes. I have written a compute shader that calculates the spherical harmonic coefficients for each volume. Now i can go ahead and read those values back to gpu using buffer.GetData() on the cpu side and shader.SetGlobalVectorArray(), but as far as I understand that will copy the memory to CPU resulting in allocation overhead.

I have seen similar posts where people were suggesting just doing the computation in a pixel shader to begin with, but i don't see how that is supposed to help? Which pixel shader and how? I only want these computations to be done once for each volume and they require parallel reduction.

Potentially my search terms are inadequate but i cannot find any answer to how to get results from compute shaders to global shader memory. Thanks in advance

$\endgroup$
2
$\begingroup$

well, if anyone is stumbling across this and needs to know what solved it for me: We can bind a compute buffer to global shader memory if we just know what exactly we need to do

within the Compute shader the RWstructuredBuffer is declared as (without the "\" infront of float):

uniform RWStructuredBuffer<\float3> buffer : register(u1);

(not certain whether register(u1) is really needed

within the vert/frag shader the buffer is defined as:

uniform StructuredBuffer<\float3> buffer;   

In a c# script before the first kernel dispatch (we only need to assign this buffer once), I assign the buffer via

buffer = new ComputeBuffer(size, sizeof(float)*3,ComputeBufferType.Default);
Shader.SetGlobalBuffer(Shader.PropertyToID("buffer"), buffer);

It took me ages to find this and it works as far as I can tell. The article I finally stumbled over that did something similar. I do not have to read the data back to cpu with buffer.GetData(container) and put it into the material manually. If the compute shader changes the buffer data this will be reflected in the vert/frag shader immediately.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Thanks for coming back to answer your own question and to help people with the same problem in the future. Really appreciate this. $\endgroup$ – wychmaster May 27 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.