Computing mesh data with compute Shaders

Is it possible to tell a unity mesh object that it’s buffers are the result of a compute function?

Or perhaps when I declare a new unity mesh I could somehow point the vertex and index buffers at some pre-created compute buffer results?

Here’s what I need to do …

On CPU:

  • Generate a “basic” set of data.
  • Pass to mesh object.

On GPU:
Only once …

  • Take mesh buffers
  • Run compute function on them

Each frame …

  • Render mesh

Is this possible to do WITHOUT having to pull the computed data back to the system ram and declare a new mesh with the compute results.

The reason I ask is that later on in the life cycle of this mesh I may want to run other compute functions on the mesh data.

Due to the size of my dataset I don’t think my solution will work with a lot of gfx ram to system ram transfers of data.

Unity mesh - no, but OpenGL allows you to do that (i assume Direct3D too). Actually, ComputeBuffer, Vertex array object, which stores vertex data and Element array object which stores triangles or whatever, are similar - they are array of user-defined structs in GPU, so you can create computebuffer and VAO or EAO pointing on same memory on GPU. But, unfortunately, you have not any access to VBOs and other OpenGL (or Direct3D) native objects in Unity, so you can’t take their handles or pointers to GPU memory. So it is possible, but not in Unity.

UPD:
I found some hack to avoid copying data. You need to create in regular shader uniform buffer variable just like in compute shader, and assign to it all buffers from compute shader using Material.SetBuffer() method. Next, once on initialization pass to every vertex its index, for example, assign index to red channel of vertex color data. In vertex shader, assign vertex data from compute buffer using index, passed from pervertex data, for example, from red channel of vertex color.