Computations with DirectCompute. How to get data back?

Started by
2 comments, last by Hodgman 8 years, 6 months ago

Hello,

I'm trying to use my gpu for computations only (I don't even create a swap chain). But I'm not sure how to retrieve a data after shader do it's job. I want to modify the same data I passed. For this I'm using single structured buffer binded as D3D11_BIND_UNORDERED_ACCESS.

As far as I know I need to use D3D11_CPU_ACCESS_READ together with D3D11_USAGE_STAGING. But in this case I have an error:


A D3D11_USAGE_STAGING Resource cannot be bound to any parts of the graphics pipeline, so therefore cannot have any BindFlags bits set.

My next approach was to create a structured unordered buffer with D3D11_USAGE_DEFAULT usage. I applied calculations on it, next I called


ID3D11DeviceContext::CopyResource()

to copy this buffer to the one with D3D11_CPU_ACCESS_READ and finally read the data. But I'm not sure this is a correct way - I don't like this extra copying between resources.

Advertisement

It's correct and actually the only way: Have a staging buffer (no bind flags, cpu read), copy, then map/unmap to read data back.

Good to know. Thank you. And just for curiosity - why is it forbidden to use staging resource in pipeline?

The DEFAULT resource will be physically located on the GPU. Technically, the CPU could read the data from there, but it's ao slow that you really wouldn't want to.
The CPU_ACCESS_READ resource will be located in system memory (like malloc would).
The Copy operation will schedule an efficient, asynchronous DMA transfer from the GPU-resident resource to the CPU-resident one, where the CPU can then efficiently read the data from.

This is all very good :D

[edit] Staging resources are located in memory thats accessible by both the CPU and GPU, but optimized for CPU read/write speed and GPU DMA access only.
Some GPUs might be able to use them directly, but it would be very slow. So, D3D forces you to do the right thing(tm), and perform an async DMA transfer into an actual GPU-resident location before use.

This topic is closed to new replies.

Advertisement