410 likes | 1.12k Views
Guard Band Clipping. Advanced D3D Programming Sim Dietrich SDietrich@nvidia.com. Guard Band Clipping. What is Guard Band Clipping? Why would you want to use it? Using Guard Band Clipping 2D Clip Testing and Guard Band 3D Culling and Guard Band How to Detect it.
E N D
Guard Band Clipping Advanced D3D Programming Sim Dietrich SDietrich@nvidia.com
Guard Band Clipping • What is Guard Band Clipping? • Why would you want to use it? • Using Guard Band Clipping • 2D Clip Testing and Guard Band • 3D Culling and Guard Band • How to Detect it
What is Guard Band Clipping? • The ability for hardware to accept screen coordinates outside of the current viewport range. • The basic idea of guard band clipping is that the hardware can accept triangles that are partially or totally off-screen. • Some graphics processors support 2D coordinates within the range [-2048,-2048 to 2047,2047].
Guard Band and Viewport Viewport ( 1024x768 ) Guard Band ( 4096x4096 )
Guard Band and Viewport This triangle can be trivially accepted or rejected These triangles can be trivially accepted This triangle must be clipped
3D Frustum Clipping is Slow • Requires dot products with 6 frustum planes for each vertex • Produces extra vertices • Each value at vertex ( u,v, fog, alpha, diffuse color, specular color ) requires interpolation • More bandwidth required for each new vertex • Breaks vertex cache coherency • Clipping can break up strips and fans
Clip Testing is Faster • 2D or 3D Clip testing combined with Guard band addresses these issues • Most triangles that normally require clipping could be passed through to HW instead • Keeps more strips and fans intact • Maintain vertex cache coherency • Apps must still clip to near and far clip planes
Guard Band Clipping • Only in the rare case of a primitive crossing both the viewport and a Guard Band boundary is 3D frustum clipping necessary • Either clip to the guard band boundary or the viewport boundary • D3D clips triangles to the guard band • Clipping to the viewport is preferred • It’s smaller than the guard band, so less pixels • Clipper will reject triangles outside viewport
Using Guard Band Clipping • Direct3D takes advantage of Guard Band clipping automatically if present and Direct3D performs clipping • If the app performs own transforms • Detect the Guard Band extents • If found, use 3D cull testing or 2D clip testing after culling and perspective transform • If apps pass 2D coordinates to D3D, they should perform their own clipping, to keep Direct3D from de-projecting during its clipping
2D Clip Testing • A version of Cohen-Sutherland clip-testing (FOLEY90) • Generate outcodes for the viewport and the guard band’s borders for each vertex • Outcode is a Bitfield : 0 means in, 1 means out • Bitwise AND (&) and Bitwise OR ( | ) the outcodes in parallel to test various cases • If primitive is within viewport accept • If primitive is outside of viewport on at least 1 side, reject • If primitive is in GB, accept • If primitive crosses a GB boundary, clip to viewport
3D Culling and Guard Band • An alternative to 2D clip testing is to use a second, larger frustum extending through the guard band borders for your 3D culling test. • This eliminates the need to project the vertices to 2D before clip testing • It is easily added to the end of your existing 3D culling
Top View of Guard Band and View Frustums Far Clip Plane Guard Band Frustum Left Guard Band Frustum Plane Right View Frustum Plane Near Clip Plane View Frustum Screen
3D Culling and Guard Bands • Create a 3D view frustum that passes through the guard band borders • Cull boxes or spheres of objects to this frustum and the normal viewing frustum • If the object is within the guard band frustum and crosses an edge of the view frustum, you can trivially accept it instead of clipping it to the view frustum
3D Culling and Guard Bands • For each object, calculate signed distance from the center of bounding sphere to each view frustum plane and compare to radius of sphere • If it’s completely outside view frustum, reject • If it’s completely inside view frustum, accept • If it’s partially in, test against guard band frustum • If completely in, accept it - This is where you save performance in avoiding clipping • If it crosses a guard band frustum plane, clip it to view frustum as normal
Top View of Guard Band and View Frustums, and Bounding Spheres Far Clip Plane Guard Band Frustum B Clipped A Accepted C Rejected Near Clip Plane Screen View Frustum
Trivially Accepting Polygons • If you trivially accept a set of polygons, render them with • D3DDP_DONOTCLIP | D3DDP_DONOTUPDATEEXTENTS • If you omit this, Direct3D will clip test to guard band for you • If you must clip a set of polygons that straddle both the view and guard band frustums, clip them to the view frustum
Detecting a Guard Band • Call the DIRECT3DDEVICE3 interface GetCaps method to get the guard band extents • hr = device->GetCaps( &aD3DHWDevDesc, &aD3DSWDevDesc ); • aD3DHWDevDesc.dvGuardBandLeft; • aD3DHWDevDesc.dvGuardBandRight; • aD3DHWDevDesc.dvGuardBandTop; • aD3DHWDevDesc.dvGuardBandBottom; • These extents will all be set to 0 to indicate no guard band support
Questions ? ? ? ? ? ? ? Sim Dietrich SDietrich@nvidia.com www.nvidia.com