1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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;
}
\$\endgroup\$
7
  • \$\begingroup\$ the divide that he had will be optimized in the pixel shader, as it's a constant \$\endgroup\$
    – Babis
    Nov 7, 2014 at 7:28
  • \$\begingroup\$ @babis: Are you sure? I have to pass in the texture size as a uniform, it is not a constant value \$\endgroup\$ Nov 7, 2014 at 7:53
  • \$\begingroup\$ 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. \$\endgroup\$
    – Babis
    Nov 7, 2014 at 8:03
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Babis
    Nov 7, 2014 at 8:05
  • \$\begingroup\$ Yeah good point, I am just not used to those. :) \$\endgroup\$ Nov 7, 2014 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .