Access vertices & indies buffer from the compute shader

Started by
3 comments, last by jamesxli 10 years, 3 months ago

Hi everyone,

I am trying to implement a compute shader (with DX11&SlimDX) to sort a list vertices based their depth (z-coordinates). In order to minimize the data traffics between CPU and GPU, I would like to do the sorting completely on GPU with a compute shader. Does anyone know a way to access the vertices and indices buffer from a compute shader? Does anyone even by chance know a sample HLSL implementation of a sorting algorithm? It is really hard to find anything other than trivial on Internet about compute shader. Any help would greatly appreciated.

James

Advertisement


Does anyone know a way to access the vertices and indices buffer from a compute shader?

The only way to get data into a compute shader is through a UAV, an SRV, or a constant buffer - so your input to the shader has to be one of those three options. Constant buffers are probably not a good choice, since their data can't change (hence the constant name :) ). SRVs are probably also not a great choice, since you will need to eventually store the data back to a buffer after you have sorted it. So most likely you would want to use a UAV to access the data, sort it, and then store it back to the UAV resource. Another option is to read the data from the SRV and write it to a UAV, which may help you with managing the data in flight.

Be aware that there are restrictions about which bind and usage flags a vertex buffer can have (test them out yourself - try creating a buffer resource with multiple combinations of bind flags, and also usage flags as well). This may force you to make an extra buffer that serves as a go-between for your algorithm passes.

Thanks for the response. But I am still some what confused. If I create an RWStructuredBuffer<uint> with an UAV to access the index buffer, how can I move the content of the index buffer to the UAV buffer, and how do I store the modified data back to the index buffer? I have tried to call CopyResource(idxBuffer, idxUavBuffer) and CopyResource(idxUavBuffer, idxBuffer) on the CPU side, but these calls seemed to do nothing (and the don't cause any errors or exceptions either.) I have tried some different option combinations for the UAV buffer (BindFlags, OptionFlags, Usage, etc), but without luck so far. Should I just try more different combinations?

RWStructuredBuffer<uint> is the resource representation in HLSL, and you use a UAV to bind the resource to the pipeline on the C++ side. So in that case, the UAV and the RWStructuredBuffer<uint> are representing the same resource, just in different domains.

CopyResource is a way to copy the contents of one resource to another. If you are starting out with an index buffer, and you want to copy it to another resource that has the ability to be bound as a UAV, then you would use copy resource to do that prior to invoking your compute shader.

The RWStructuredBuffer is actually the source of your data within the compute shader, and you will also need to write the sorted data back into it. That's where the name RW comes from - it is for reading and writing.

It sounds to me like you are unfamiliar with resources in D3D11 - I would recommend spending a bit of time with a good book to get familiar. Otherwise it will be a long hard struggle to get something trivially working... Any time that you invest in learning about resources is time well spent, so go for it!

Thanks, Jason. It is indeed frustrating to code for GPU without good understanding of all those new concepts. After thoroughly cleaned up my code, I got the code working. I basically creates RW buffers (UAVs) for the sorting algorithm, and used the CopyResource() to copy data between compute and vertex shader.

This topic is closed to new replies.

Advertisement