Search code examples
glslshadermeshvulkanvoxel

Can I access the mesh output of a mesh shader back to CPU?


So I’m making a voxel game in Vulkan where the bulk of the computation is not only in rendering the faces of each block, but also generating those blocks as in terrain generator. After hearing all the flexibility a mesh shader can have, since it’s treated like a compute shader, I’m intrigued to put more of such procedural generation code into GPU—mesh shader. I have yet to touch the mesh shader at all, but if I were to generate terrain data (whereas I would have left it for the CPU to do it under the traditional vertex shader pipeline), I would need the terrain data sent back to CPU for testing collision against the player.

Note: there won’t be much player interactivity other than collision.

Are there general support for mesh shader to return some mesh data back to CPU? If not, is there any chance to achieve that on Nvidia? (Without going back to compute shader w/ traditional rendering pipeline)


Solution

  • Transform feedback does not work with NV_mesh_shader. This is, or at least was, a hardware issue. I don't know of any statement about them fixing this issue.

    And even if you could use it... you really shouldn't.

    Mesh shaders are for rendering triangles; that's what they produce. But your CPU doesn't need triangles; what it needs are the more abstract data structures that those triangles represent.

    When doing collision detection in, say, Minecraft, you would not do object-triangle collision tests. You don't need to test against triangles; you test against blocks. Axis-aligned blocks. Knowing that this is what you're testing against makes the test that much easier and faster to compute.

    Giving mesh shaders abstract representations of the world and having them compute triangles from them makes sense. Giving those triangles to the CPU does not. The CPU should be using its own abstract representation of the world. It may be that you can share this representation directly with GPU mesh shaders, but all of the serious "procedural generation code" ought to be on the CPU.

    Having to do a CPU->GPU->CPU round-trip just to figure out how collision works is not a great idea.