# How does the following code generate a full screen quad?

How does this

struct Output
{
float4 position_cs : SV_POSITION;
float2 texcoord : TEXCOORD;
};

Output main(uint id: SV_VertexID)
{
Output output;
output.texcoord = float2((id << 1) & 2, id & 2);
output.position_cs = float4(output.texcoord * float2(2, -2) + float2(-1, 1), 0, 1);
return output;
}


and this

pImmediateContext->VSSetShader(fxaaVS, ..., ...);
pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
pImmediateContext->Draw(3, 0);


1. It has only three vertices b(provided quad has 4 vertices)
2. the positions produced are also not like a (half of a) quad.

Looking at this, i might be confused between a quad and a full screen triangle.

• It generates a fullscreen quad because it's a 'big triangle' that is clipped by the hardware to a quad. This is preferable because it avoids some duplicated work along what would otherwise be the diagonal seam if you draw it as two triangles. – Chuck Walbourn Mar 8 '18 at 17:36
• Can you post this as an answer? Your comment answers my question – Basit Anwer Mar 9 '18 at 4:38
• I think ratchet freak beat me to it :) – Chuck Walbourn Mar 10 '18 at 23:41

The outputs end up as:

output[0].texcoord = float2(0,0);
output[0].position_cs = float4(-1, 1, 0, 1);

output[1].texcoord = float2(2,0);
output[1].position_cs = float4(3, 1, 0, 1);

output[2].texcoord = float2(0,2);
output[2].position_cs = float4(-1, -3, 0, 1);


It goes beyond the edges of the screen but any part outside will get clipped by the rasterizer stage. Doing this avoid double coverage of evaluated pixel on the diagonal you get with 2 triangles.

• Be aware that both the 'self-generating quad' and the 'big triangle' rely on the VertexID system-generated value which requires Direct3D Hardware Feature Level 10.0 or better hardware if you are using Direct3D. See this blog post – Chuck Walbourn Mar 8 '18 at 17:37
• DirectX 11 is the API... Direct3D Hardware Feature Level is what GPU is attached to the computer. Be sure you know the difference. – Chuck Walbourn Mar 10 '18 at 23:40