Jump to content

  • Log In with Google      Sign In   
  • Create Account


Read variable back from Shader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 fs1   Members   -  Reputation: 264

Like
0Likes
Like

Posted 26 January 2014 - 06:00 AM

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,



Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2167

Like
2Likes
Like

Posted 26 January 2014 - 11:04 AM

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!



#3 fs1   Members   -  Reputation: 264

Like
0Likes
Like

Posted 26 January 2014 - 11:16 AM

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

#4 cozzie   Members   -  Reputation: 1492

Like
1Likes
Like

Posted 26 January 2014 - 12:47 PM

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

#5 fs1   Members   -  Reputation: 264

Like
0Likes
Like

Posted 26 January 2014 - 02:36 PM

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.

#6 ankhd   Members   -  Reputation: 1116

Like
1Likes
Like

Posted 26 January 2014 - 09:54 PM

Hello. dx11 maybe stream out to a buffer.



#7 jamesxli   Members   -  Reputation: 302

Like
2Likes
Like

Posted 27 January 2014 - 12:19 PM

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. 



#8 fs1   Members   -  Reputation: 264

Like
0Likes
Like

Posted 27 January 2014 - 02:19 PM

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, 27 January 2014 - 02:20 PM.


#9 jamesxli   Members   -  Reputation: 302

Like
2Likes
Like

Posted 27 January 2014 - 04:54 PM


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. 



#10 fs1   Members   -  Reputation: 264

Like
0Likes
Like

Posted 28 January 2014 - 06:29 AM

 


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!



#11 kauna   Crossbones+   -  Reputation: 2167

Like
0Likes
Like

Posted 29 January 2014 - 01:14 PM


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. 

 

The slowness may be effect of trying to access the copied data right-away after copy. You should do something else before using the results in order to avoid unnecessary GPU-CPU synchronizations. Ie. the CopyResource is asynchronious, but mapping the data right after will make the CPU to wait the GPU to finish it's tasks. 

 

Cheers!



#12 ankhd   Members   -  Reputation: 1116

Like
0Likes
Like

Posted 30 January 2014 - 08:17 PM

One thing stream out is not slow.
Not in dx10 anyway and a vb with 1 value would be way faster then a render target.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS