Read variable back from Shader

Started by
10 comments, last by ankhd 10 years, 2 months ago

Dear All,

Is there any possibility of doing the following:

Create a Shader in HLSL
Define a variable in the shader that I would like to read back after processing
Load the Shader for drawing
Call DX11 functions to draw my stuff
After all GPU processing, read back the value of the variable to the program in CPU

Any hints if this is possible?

Thanks,

Advertisement

With a compute / pixel shader you can store data in a render targets / UAVs which can be copied back to CPU. You may use CopyResource with a D3D11_USAGE_STAGING resource as target with a CPU read flag. A single variable defined inside a shader cannot be read back to CPU.

Cheers!

With a compute / pixel shader you can store data in a render targets / UAVs which can be copied back to CPU. You may use CopyResource with a D3D11_USAGE_STAGING resource as target with a CPU read flag. A single variable defined inside a shader cannot be read back to CPU.

Cheers!

Thanks so much. I do a lot of calculations in the Shader before drawing, and I would like to retrieve the result of one of those calculations in a variable. How can I do this with the approach above?
Thanks
Not sure if I read correct, but I believe that you should save the variable you need as part of a rendertarget(texture), and maybe write the rest of the rendertarget to zero. And then read out the 1st position of the render target/ texture so retrieve your value. Not sure though if this works with a float

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Not sure if I read correct, but I believe that you should save the variable you need as part of a rendertarget(texture), and maybe write the rest of the rendertarget to zero. And then read out the 1st position of the render target/ texture so retrieve your value. Not sure though if this works with a float


Thanks, I will try this, but need to be a float.

Hello. dx11 maybe stream out to a buffer.

Just to reminder you that copying data from GPU to CPU (using staging buffer) is usually not very fast. You should check the performance, if you need to make a lot of round-triples.

Hello. dx11 maybe stream out to a buffer.

Thanks

Just to reminder you that copying data from GPU to CPU (using staging buffer) is usually not very fast. You should check the performance, if you need to make a lot of round-triples.

Thanks. But is it possible to plain read a single variable defined inside a shader back to CPU?


But is it possible to plain read a single variable defined inside a shader back to CPU?

No, it is not possible with current DX11 library, as kauna already pointed out. Apart from using render tagert, you can declare a RWStructuredBuffer in your HLSL code, then bind an UAV with single element to the buffer. Then, in your HLSL code save your float variable into the first element of the RWStructredBuffer. Then, you need to create a staging buffer, and copy the UAV buffer to the staging buffer ( with CopyResource.). Then, you can use MapSubresource() function to read the staging buffer to the CPU side.


But is it possible to plain read a single variable defined inside a shader back to CPU?

No, it is not possible with current DX11 library, as kauna already pointed out. Apart from using render tagert, you can declare a RWStructuredBuffer in your HLSL code, then bind an UAV with single element to the buffer. Then, in your HLSL code save your float variable into the first element of the RWStructredBuffer. Then, you need to create a staging buffer, and copy the UAV buffer to the staging buffer ( with CopyResource.). Then, you can use MapSubresource() function to read the staging buffer to the CPU side.

Thanks for our insights. I will workf on the suggested approaches.

Thanks!

This topic is closed to new replies.

Advertisement