Return value from Shader

Started by
6 comments, last by XoBoR 14 years ago
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.
Advertisement
Your pixel shaders write data to render-targets. It is possible to read these render-targets (textures) back onto the CPU (though it's slow).
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
Crazy dude smoking D3D11, AngelScript, PhysX, 3D Sound, Network, DB, FBX, and some other weird things, doing that on Visual Studio 2010 + Visual Assist X on Overclocked CPU
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.
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.


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.
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?

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

This topic is closed to new replies.

Advertisement