Compute shader values

Started by
3 comments, last by Nik02 9 years, 8 months ago

Hello,

I wanted to know how to return compute shader results back to CPU so that I can use it for application logics level computations. I have running compute shader on DirectX11 but I don't know how to convert results from unordered access view to my base structure on CPU. How do I do that?

Advertisement

Once you've written your computation results to a buffer (or texture) you can Map() the buffer/texture and read back the results.

Note that for some resource types (depending on the creation flags) you may first need to copy your results to a staging resource and then Map() this intermediate resource.

This link (even though for DX10) contains some pointers to related topics: http://msdn.microsoft.com/en-us/library/windows/desktop/bb205132(v=vs.85).aspx

One thing to keep in mind: If you read back the results in the same frame as the computation happens you will probably introduce stalls in your CPU-GPU communication which can lead to performance problems. So wait at least a frame before reading back data. Even better would be to issue a query after your Dispatch() call and then asynchronously ask DirectX if the query is finished so you know your computation you did before is finished as well.

Thanks for information. Looking forward to implement them.

On the other hand I see that you can specify wether directx should wait for result of compute shader or not... So maybe problematic result reading is not necessary?

You can either wait, or accept the fact that the results may not yet be ready by the time you try to read them.

If you choose not to wait, the methods can return an error, and you don't get the actual results any faster.

If you can cope with a frame of latency, it is best to use a double buffer; that is, map and copy one buffer while the GPU is filling the other, and then swap the two for the next frame (or dispatch). This works with more buffers too, but additional buffers cause additional latency to the results.

Niko Suni

This topic is closed to new replies.

Advertisement