Advertisement Jump to content
Sign in to follow this  
fs1

DX11 Read variable back from Shader

This topic is 1815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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,

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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?

Edited by fs1

Share this post


Link to post
Share on other sites


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. 

Share this post


Link to post
Share on other sites

 


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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!