Sign in to follow this  
XoBoR

Return value from Shader

Recommended Posts

XoBoR    100
Hi guys! I've got just a short question. Is there any way to return a value from my HLSL Pixelshader to use it in my program (i was trying to run some computations on the GPU)? Im new to HLSLa and shaders, so please forgive is this question sounds dumb to you :) greetings, Robert.

Share this post


Link to post
Share on other sites
e3d_ALiVE    209
you can try using 1x1 textures[if you have single float resulted value]
then copy that texture to staging resource and read data from it, that way performance will not suffer that much

Share this post


Link to post
Share on other sites
cgrant    1825
As e3d_ALiVE and Hodgman mention the only way is through a shader resource (render target, staging resource). You just can return a value like you set uniform input parameters.

Share this post


Link to post
Share on other sites
BlackSeeds    156
If you have a DX11 card you could use direct compute to do your GPU calculation as this is what it was designed for.

You could issue hardware occlusion queries like the histogram average luminance method used in HDR rendering. Some clever work with a shader and occlusion query could get your data back in a nice efficient manner.

Reading data from render target is slow, but depending on what you want could be suitable for your needs.


Share this post


Link to post
Share on other sites
XoBoR    100
First of all, thanks for the answers!

Second i think, i have to think of something else.
I was using a raytracing mechanism to determine my mouse position in world space, and came up with the idea that this could be done super-easy via a pixel shader (by comparing the Screen-Space coordinate of the current pixel, and if it machtes tge current mouse-position, just setting *some* data to indicate which point in World space is related to that). When i got that working in the Shader, i realized, that i couldn't change globals from within the shader :/.

I don't think that doing a whole render pass just to do this will really be a benefit against writing some optimized ray-tracing code, do you guys think i'm wrong? If not, i guess i'll start writing some kd-tree or grid-optimized ray-tracer for this.

Thanks! Robert.

Share this post


Link to post
Share on other sites
BlackSeeds    156
I've done a similar method to how you describe before, I rendered depth into a render target, locked the texture (this was d3d9) and read the texture at the current mouse coordinate from the mouse x,z and depth you can reconstruct your world space position using un-projection.

I certainly think this is a viable option for picking because your not continually reading values from an RT, your just doing it on click. but if you want to continually do that each frame you might want to look at other approaches, why are you determining mouse position in world space? for picking or for something else?

Share this post


Link to post
Share on other sites
XoBoR    100
Yep, its basically for picking, currently i'm doing it every frame, because i'm positioning a light above Ground at the mouse position, but thats probably not gonna be in the game (if I ever manage to finish something that deserves to be called a game).

Edit: i forgot to mention that I'm also using it in my terrain editor, where i alter the terrain by "drawing" to the terrain, in that case i'll need the WorldPosition of my mouse every frame too (or at least every 10th frame or some).

thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this