Anteru's blog
  • Consulting
  • Research
    • Assisted environment probe placement
    • Assisted texture assignment
    • Edge-Friend: Fast and Deterministic Catmull-Clark Subdivision Surfaces
    • Error Metrics for Smart Image Refinement
    • High-Quality Shadows for Streaming Terrain Rendering
    • Hybrid Sample-based Surface Rendering
    • Interactive rendering of Giga-Particle Fluid Simulations
    • Quantitative Analysis of Voxel Raytracing Acceleration Structures
    • Real-time Hybrid Hair Rendering
    • Real-Time Procedural Generation with GPU Work Graphs
    • Scalable rendering for very large meshes
    • Spatiotemporal Variance-Guided Filtering for Motion Blur
    • Subpixel Reconstruction Antialiasing
    • Tiled light trees
    • Towards Practical Meshlet Compression
  • About
  • Archive

Minimal setup screen space quads: No buffers/layouts required

March 03, 2012
  • Direct3d
  • Graphics
approximately 4 minutes to read

A neat trick that Rory Driscollmentioned on Twitter is to use vertex IDs to generate screen space quads. I just recently came around to use that in my stuff, so here’s some ready-to-use code. The advantage of using the vertex id is that you don’t need a vertex layout, vertex buffer or index buffer.

Here’s the complete vertex shader:

float4 VS_main (uint id : SV_VertexID) : SV_Position
{
    switch (id) {
    case 0: return float4 (-1, 1, 0, 1);
    case 1: return float4 (-1, -1, 0, 1);
    case 2: return float4 (1, 1, 0, 1);
    case 3: return float4 (1, -1, 0, 1);
    default: return float4 (0, 0, 0, 0);
    }
}

All you need for rendering now is to issue a single draw call which renders a triangle strip containing four vertices, and you’re done. Note: This quad is for RH rendering, if your culling order is reversed, you’ll need to swap the second and third vertex. In the pixel shader, you can now use SV_Position, or alternatively, you can also generate UV coordinates in the vertex shader.

Update: As noted in the comments by Martins, there is also a way to do it with a single triangle that spans the whole screen. Here’s the code for that, again for a RH system:

// Without the explicit casts, this does not compile correctly using the
// D3D Compiler (June 2010 SDK)
float x = float ((id & 2) << 1) - 1.0;
float y = 1.0 - float ((id & 1) << 2);

return float4 (x, y, 0, 1);

There’s a weird compiler bug that requires the explicit float cast, otherwise, the D3D compiler from the June 2010 SDK fails to compile this code.

Previous post
Next post

Recent posts

  • Data formats: Why CSV and JSON aren't the best
    Posted on 2024-12-29
  • Replacing cron with systemd-timers
    Posted on 2024-04-21
  • Open Source Maintenance
    Posted on 2024-04-02
  • Angular, Caddy, Gunicorn and Django
    Posted on 2023-10-21
  • Effective meetings
    Posted on 2022-09-12
  • Older posts

Find me on the web

  • GitHub
  • GPU database
  • Projects

Follow me

Anteru NIV_Anteru
Contents © 2005-2025
Anteru
Imprint/Impressum
Privacy policy/Datenschutz
Made with Liara
Last updated August 19, 2023