#Graphics Buffers, Meshes and rendering

1 messages · Page 1 of 1 (latest)

tribal siren
#

Ive been working on tools to make it easier to work with compute shadera with a goal of setting up a gpu driven procedural mesh system and I'm trying to better understand how to take advantage of unity features.

I would like to avoid sending mesh data back to the cpu from the gpu as much as possible as part of the editing experience.

In trying to figure this out I had looked into the New Mesh API and Graphics Buffers to figure out how I could read mesh data I generate on the cpu and hand it off to the rendering system without having to sync between the cpu and gpu.

How can we accomplish this using Unitys apis?

fringe bane
#

It depends on which kind of data you generate from the compute shaders, if you generate mesh data directly, then you can use RenderPrimitivesIndirect or RenderPrimitivesIndexedIndirect if you have an index buffer as well to directly render your meshes from the compute shader.

Of course, your vertex shader needs to be able to read the data from this vertex buffer to write its attributes like any indirect/instanced rendering.

For writing from the CPU to GPU, you can use LockBufferForWrite, it is the most efficient option but it has some platform limitations that could get annoying. GraphicsBuffer.SetData is the simplest option.

Regarding the readback, I recommend using the AsyncGPUReadback API to do a non-blocking read from GPU to CPU (it takes a couple of frames to get the data)

https://docs.unity3d.com/ScriptReference/Graphics.RenderPrimitivesIndirect.html
https://docs.unity3d.com/ScriptReference/Graphics.RenderPrimitivesIndexedIndirect.html
https://docs.unity3d.com/ScriptReference/Rendering.AsyncGPUReadback.Request.html
https://docs.unity3d.com/ScriptReference/GraphicsBuffer.LockBufferForWrite.html
https://docs.unity3d.com/ScriptReference/GraphicsBuffer.SetData.html

tribal siren
#

The goal is actually to avoid the cpu/gpu sync entirely, generating new geometry on the gpu and then rendering it on the gpu without needing to bring the mesh data back to the cpu

quick sage
fringe bane
#

GetVertexBuffer allows the modification of the vertex data in a computer but does not allow adding new vertices. If you want to completely avoid syncs and stay on the GPU, then you need to use only pre-allocated GraphicsBuffers, compute shaders, and indirect draw calls like so:

  • Distach compute to update / modify vertex data
  • Dispatch compute to generate the indirect argument buffer (you can do frustum culling on the GPU on top of this step too)
  • Indirect Draw with the indirect argument buffer + bind the vertex data buffer to your material before the draw
tribal siren
# fringe bane GetVertexBuffer allows the modification of the vertex data in a computer but doe...

I think I need to formulate a much more specific question, but this explains why a prior approach i tried did not work, and clarifies that I specifically don't want to use GetVertexBuffer.
I'm currently working (sadly not on games) but after work ill try to out together a much more targeted question.

Your comments about RenderPrimitivesIndirect may be what I'm looking for, I just didn't want to respond until I actually had time to take a close look.