Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is it possible to send ComputeShader GPU data directly to mesh without retrieving it back to CPU?

Discussion in 'General Graphics' started by itisMarcii_, Apr 16, 2023.

  1. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    107
    Hey,

    Currently, I am using ComputeShader to retrieve displaced vertices data from the GPU to the CPU. UI then use this data to transform a mesh and obtain its exact world position via the ComputeBuffer.GetData method.

    However, as the GetData method doesnt scale very well, I am exploring alternative options to transform the mesh and seperate the required calculations from the mesh transformation process. The calculations for the mesh and the retrieved data are the same, i just dont need them for every mesh.

    My plan is to retrieve the data back to the CPU only when necessary and transform the mesh directly on the GPU. I am wondering if there is a way to send the ComputeShader GPU data directly to the mesh, rather than retrieving it back to the CPU to displace the vertices. Is this possible?

    PS: The mesh transformation is only visual, so the actual vertices displacement doesn't need to happen as long as the result would be visual the same.

    Thanks for reading :)
     
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    Displace vertices in compute shader and write results to structured buffer (you have already done this step). Make sure you have direct correspondance between vertex index of source mesh and result data in output buffer. Make a vertex shader modification that reads vertex position from structured buffer indexed by SV_VertexID. By this step you will get modified vertex position corresponding to every mesh vertex. Bind structured buffer made in "ComputeShader" step into material with new shader that used for mesh rendering. That's it.
     
    Sluggy, tmonestudio and itisMarcii_ like this.
  3. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    107
    Thanks thats exactly what i needed! :)