Search Unity

Accessing a ComputeBuffer from a surface shader after a ComputeShader writes to it [solved]

Discussion in 'Shaders' started by Alex_curiscope, Sep 28, 2017.

  1. Alex_curiscope

    Alex_curiscope

    Joined:
    Apr 4, 2017
    Posts:
    55
    Hey all.

    I have a compute shader writing values into a compute buffer. They're just positions. I then want to use those positions in the vertex shader of a surface shader.

    Here's my init code for the compute buffer. I'm making it a global shader buffer here but setting it as an explicit buffer on the surface shader material doesn't seem to work either.
    Code (CSharp):
    1.  
    2.         positionsBuffer = new ComputeBuffer(numNodes, 3 * sizeof(float));
    3.         kernel = computeShader.FindKernel("ComputeNodePositions");
    4.         positionsPID = Shader.PropertyToID("_KelpPositions");
    5.         computeShader.SetBuffer(kernel, positionsPID, positionsBuffer);
    6.         Shader.SetGlobalBuffer(positionsPID, positionsBuffer);
    7.  
    Here's the shader declaration:
    Code (CSharp):
    1.  
    2. #ifdef SHADER_API_D3D11
    3.         //uniform StructuredBuffer<float3> _KelpPositions : register(t1); // tried this too
    4.         uniform RWStructuredBuffer<float3> _KelpPositions : register(u1);
    5. #endif
    6.  
    Well, the positions buffer seems always to be zeroes. The compute shader is definitely putting data in there (its version of the buffer...?).

    Any ideas? What am I doing wrong?
     
    Last edited: Sep 28, 2017
  2. Alex_curiscope

    Alex_curiscope

    Joined:
    Apr 4, 2017
    Posts:
    55
    Wait! Stop!

    Code (CSharp):
    1.  
    2. #ifdef SHADER_API_D3D11
    3.         uniform StructuredBuffer<float3> _KelpPositions : register(t1); // this actually works after all!      
    4. #endif
    5.  
    False alarm folks, thanks for being the rubber duck.
     
  3. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    So? Maybe someone would love to know what happened with some more details. :) ... "someone"
    It would also be useful if you could share the compute shader.
    Also do you happen to know if this can be done via OpenGL API?
     
    Last edited: Feb 1, 2021
    deus0 likes this.