I'm still new to shaders and I'm trying to create a glow a shader. I understand how it all works and I don't think I'll have too much trouble with the rest of it, but I'm stuck at one point in particular which is getting the texcoords of the render target into my pixel shader.

Right now I'm setting the rendertarget, saving it to the texture, passing it to the shader. Then I'm going to do tex2D(texture, coords) but I don't know HOW to get the coordinates into my pixel shader. I've created float2 TexCoord : TEXCOORD0; for my vertex shader output but I don't know how to assign the coordinates as it reaches the pixel shader. Everything else (color,pos, normal) is just automatically passed in through the vertex shader and I'm assuming I don't need to change the format of that to include texcoords and it's something within the actual shader itself I need to add?



Actually, you can use the VPOS built-in input to the pixel shader to get the coordinates of the pixel in the viewport. Like this:

void ps_main(float2 pixel : VPOS, /* ...other inputs... */)
    float2 uvPixel = (pixel + 0.5) * float2(1.0 / screenWidth, 1.0 / screenHeight);
    // ...sample a texture using uvPixel...

This includes the infamous half-texel offset that's needed to perfectly map a screen-space texture to the screen in D3D9. You don't need to write any outputs to VPOS from the vertex shader; it's handled for you automatically by the hardware. The float2(1.0 / screenWidth, 1.0 / screenHeight) can be precalculated and stored in a uniform. Some more info on VPOS is available in this gamedev.net thread.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.