Fullscreen triangle pass and texcoords

So I have a fullscreen triangle shader like this:

#ifndef FULLSCREEN_TRIANGLE_VERTEX_HLSL
#define FULLSCREEN_TRIANGLE_VERTEX_HLSL

// Variation on Timothy Lottes FXAA vertex shader, for CCW frontface
float4 vs_main(uint VertexID: SV_VertexID) : SV_Position
{
return float4(float2(((VertexID << 1) & 2) * 2.0f, (VertexID == 0) * -4.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
}

#endif


This helps me during deferred shading, since I can just do like this in the pixel shader:

float4 ps_main(float4 position : SV_Position) : SV_Target0
{
float ssao = 1.0;
if (gUseSSAO)
{
ssao = gSSAOTexture[uint2(position.xy)].r;
ssao = (clamp(1.0 - (1.0 - ssao), 0.0, 1.0) + 0.1) / (1.0 + 0.1);
}

return gDiffuseTexture[uint2(position.xy)] * ssao * gAmbientLight;
}


SV_Position automatically gives me the screenspace position of the pixel. Now some of my alghorithms require texture coordinates. I know this is simply (screenspacePosition.xy / textureSize.xy) but I want to avoid those instructions in my pixel shader. Is there some system-provided value (like SV_Position) or any neat way to enhance the fullscreen vertex shader to provide me with texture coordinates aswell?

I do it like this, so no divide in the PS. Vertex Shader code follows:

struct VertextoPixel
{
float4 pos              : SV_POSITION;
float2 tex              : TEXCOORD0;
};

VertextoPixel main(uint vI : SV_VERTEXID)
{
float2 inTex = float2(vI%2,vI%4/2);
VertextoPixel Out = (VertextoPixel)0;
Out.pos=float4((inTex.x-0.5f)*2,-(inTex.y-0.5f)*2,0,1);
Out.tex=inTex;
return Out;
}

• the divide that he had will be optimized in the pixel shader, as it's a constant – Babis Nov 7 '14 at 7:28
• @babis: Are you sure? I have to pass in the texture size as a uniform, it is not a constant value – KaiserJohaan Nov 7 '14 at 7:53
• Sorry, morning confusion, I was talking about the divide by "1/ (1.0 + 0.1)" which might be a bit irrelevant as Janos was probably talking about the divide that you would have to do to get the UV. – Babis Nov 7 '14 at 8:03
• Janos, if bitwise ops are supported you can change the inTex calculation to float2 inTex = float2(vI&1,vI>>1) which is much faster, if it's not automatically optimized that is. – Babis Nov 7 '14 at 8:05
• Yeah good point, I am just not used to those. :) – János Turánszki Nov 7 '14 at 9:41