Search Unity

Rendering Compute shader generated mesh.

Discussion in 'Shaders' started by JonRurka, Apr 11, 2020.

  1. JonRurka

    JonRurka

    Joined:
    Nov 19, 2012
    Posts:
    35
    Hello.

    To give some background, I am creating my own volumetric terrain engine, and working on adding grass and other plants. I am using a compute shader to generate the large grass patches based on the generated height map. I am aware I can extract this with getData() on the compute buffer in C# and set to the MeshFilter.mesh, however as extracting from GPU memory to CPU memory is slow, I am wonder if there is a way to render the mesh directly via the Compute Buffers native pointer (GetNativeBufferPtr()).

    EDIT: I have found that there is a function GetNativeVertexBufferPtr() (as well as an index one) in the Mesh class. Unfortunately, there does not seem to be a way to set it from the Mesh class. From what I know of low level graphics APIs, these should be compatible as long as the compute buffer has the same stride. From what I can tell there is no technical reason Unity cannot do this. It's a manner of having the correct API for it exposed.
     
    Last edited: Apr 11, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    So yes & no. You can't use the data generated from a compute shader in a mesh filter, or with a
    DrawMesh
    call. Instead you'd want to use
    DrawProcedural
    or
    DrawProceduralIndirect
    and use a custom shader that uses
    SV_VertexID
    to index from buffer directly.
     
    BlankMauser, JonRurka and Neto_Kokku like this.