Drawing

Face culling and winding order

In a closed mesh, about half the triangles face away from the camera and are hidden by the other half. Face culling throws them away before they are rasterized. The GPU decides which side a triangle shows from the order of its corners on screen. This page explains that test, the conventions around it, and the cases that reverse it. The last part shows how RASM does it.

In short

  • A triangle's winding is the order of its three corners on screen: counter-clockwise or clockwise. Seen from behind, the same triangle has the opposite winding.
  • The GPU finds the winding from the sign of the triangle's signed area in screen space, after projection.
  • The application says which winding is the front (glTF and OpenGL use counter-clockwise), and which faces to cull: none, back or front.
  • A mirroring transform (negative determinant) reverses the winding of every triangle. So does showing the image upside down.
  • Double-sided surfaces (leaves, cloth, glass panes) are not culled, and their back side is lit with the normal reversed.

1Why cull back faces

Every triangle of a closed mesh (a box, a character, a car body) has an outside. Its normal points out. From any camera position, the triangles whose normals point away from the camera are behind the ones that point toward it. Drawing them only costs time: the depth test hides them anyway.

front face: its normal points toward the camera back face: its normal points away
No culling: 6 faces rasterized drawn, then hidden by depth camera Back-face culling: 3 faces rasterized skipped before rasterization camera
Seen from above. In a closed mesh, about half of the triangles face away from any camera. Culling them saves their rasterization and their pixel shading, with no change to the image.

Back-face culling is only correct for closed meshes seen from outside:

always covered by the front

Closed mesh

Every back face has a front face before it. Nothing is lost.

normal a back face with nothing behind it

Open surface

A single wall or a leaf has nothing behind its back side. Culling makes it vanish when seen from behind.

2Winding: the order of the corners

A triangle is stored as three indices into the vertex list, in a fixed order: 0, 1, 2. Look at the triangle and follow the corners in that order. You go either counter-clockwise or clockwise. Walk around to its back and look again: the same order now goes the other way.

Seen from the front 0 1 2 0 → 1 → 2: counter-clockwise stored: the index order 0 1 2 walk around to the back same triangle, same indices Seen from behind 0 1 2 0 → 1 → 2: clockwise
So "front" can be defined as "counter-clockwise on screen" (or clockwise, by convention). No normal is needed: the test uses only the three projected corners.
The winding is not stored anywhere

It comes from the index order and from where the camera is. Tools write the indices so that the outside is the front.

3The test: the signed area

After the vertex shader, the divide by w and the viewport transform, each corner has a 2D screen position (xi, yi). The shoelace formula gives the area of the triangle with a sign:

a = ½ · [ (x0·y1 − x1·y0) + (x1·y2 − x2·y1) + (x2·y0 − x0·y2) ]

    = ½ · ((x1 − x0)·(y2 − y0) − (x2 − x0)·(y1 − y0))

With y pointing up, the sign is the winding:

0 1 2

a > 0

Counter-clockwise.

0 1 2

a < 0

Clockwise.

0 1 2

a = 0

A line: never drawn.

y points up +x +y p0 (0, 0.5) p1 (−0.5, −0.5) p2 (0.5, −0.5) a = ½ · (0.25 + 0.5 + 0.25) = +0.5 positive, and counter-clockwise on screen y points down (pixel rows) +x +y p0 p1 p2 a = +0.5: the same numbers but clockwise on screen: every sign flips
APIs with a Y-down framebuffer put a minus sign in front to compensate. The same number is twice the 2D cross product of two edges. It is the test the rasterizer already needs: its edge functions use the same products.

The screen-space test gives the same answer as the 3D question "does the normal point toward the eye?", that is (p0 − eye) · n < 0 for a front face, where n is the cross product of two edges. Projection keeps the side of the eye a plane is seen from.

(p0 − eye) · n < 0: front eye p0 − eye p0 n triangle, edge-on (p0 − eye) · n > 0: back eye p0 − eye p0 n
The 3D form is what a program uses before the GPU, for example to cull whole groups of triangles.

4Conventions: which winding is front

The GPU has two settings: which winding counts as front, and which faces to cull.

API or formatFront-face settingDefault front
glTF 2.0, OpenGL tradition— / glFrontFacecounter-clockwise
Direct3D 11 and 12FrontCounterClockwise in the rasterizer stateclockwise (the flag is false)
Metalfront-facing winding on the render encoderclockwise
VulkanfrontFace in the pipeline, or dynamic state (core since 1.3)no default: the pipeline must say

The four cull modes, on the closed mesh of section 1 (seen from above, camera below, thick lines are drawn):

none

Drops nothing.

Used for: double-sided surfaces, 2D and UI, debug views.

back

Drops faces with the "back" winding.

Used for: almost every closed, opaque mesh.

front

Drops faces with the "front" winding.

Used for: mirrored meshes (section 5), inside-out volumes such as a sky box seen from inside, some shadow-map techniques.

front and back

Drops every triangle.

Used for: rare: points and lines still draw.

Try the whole test on one triangle. The picture computes the signed area of section 3 from the three corners, then applies the two settings.

front face, drawn back face, drawn culled the direction of 0 → 1 → 2
Drag corner 0 down through the edge between 1 and 2: the area changes sign and the triangle is culled. Then change the front-face setting or the cull mode to bring it back.
Mixing conventions is a common bug

A model exported clockwise into a counter-clockwise engine appears inside out, with its far inner walls visible and its near outer walls missing: the "front" card above, when "back" was meant.

5What reverses the winding

A mirroring transform

A mirror (a reflection) turns a counter-clockwise triangle into a clockwise one. A scale of −1 along one axis is a mirror, and so is any matrix whose 3×3 part has a negative determinant.

As modeled 0 1 2 counter-clockwise: front scale.x = −1 det = −1 Mirrored, on screen 0 1 2 clockwise: the GPU says "back"
The side we see is the authored front, the outside. Only its on-screen winding changed. Without the fix, back culling removes exactly the faces that should be visible, and the object looks inside out.
The fix, per object

det ≥ 0 → cull back. det < 0 → cull front (or keep back culling and swap the front-face setting). For T · R · S, det = sx · sy · sz: a rotation has det = +1.

Two mirrors cancel:

0 1 2

scale (1, 1, 1)

det = +1. Counter-clockwise, as modeled.

0 1 2

scale (−1, 1, 1)

det = −1. One mirror: clockwise.

0 1 2

scale (−1, −1, 1)

det = +1. Two mirrors: a 180° rotation, counter-clockwise again.

Try it. The front face is counter-clockwise and the camera looks at the outside of the triangle.

drawn culled by mistake the direction of 0 → 1 → 2
Make one scale negative: the triangle is mirrored and back culling removes it. Switch the cull mode to follow the determinant. Then make both scales negative.

Mirrors are common: a level designer reuses a left door as a right door with a negative scale, and glTF files may contain nodes with negative scale. Because the determinant is a product, a hierarchy is mirrored when an odd number of its matrices are.

An upside-down image

Showing an image upside down is also a mirror (across the horizontal axis). This is what happens when Y-up clip coordinates reach Vulkan, whose clip space is Y-down, without a fix: every winding reverses.

0 1 2

Y-up clip space on Vulkan, no fix

The image is upside down and every winding is reversed.

0 1 2

With a fix

Both usual fixes, a negative viewport height or a flipped projection, give an upright image and keep the windings as authored (Coordinate spaces).

6Double-sided surfaces

A leaf, a flag or a sheet of paper is one layer of triangles. It must be visible from both sides, so its material turns culling off. The back side then needs care in lighting: its normal still points out of the front.

one layer of triangles n −n viewer A sees the front: lit with n viewer B sees the back: lit with −n
The rasterizer tells each pixel whether its triangle was front-facing (SV_IsFrontFace, gl_FrontFacing). For a back face, the pixel shader uses −n, so the light falls on the side being looked at. glTF requires exactly this.
n: away from the light viewer dark

Back face lit with n

The back is dark, or lit from the wrong side.

−n viewer lit

Back face lit with −n

The light falls on the side being looked at.

The flag is about the on-screen winding

Under a mirroring transform the "front-facing" flag reports the authored front as "back". A shader that also handles mirrors must combine the flag with the sign of the determinant.

7Where culling sits in the pipeline

Vertex shader Assemble, clip, ÷ w Face cull signed area Rasterize Pixel shader saved for a culled triangle already paid
Face culling is nearly free: the rasterizer computes the signed area anyway. It mostly saves pixel shading, which is where most of the cost of a triangle is.
The vertices were already shaded

They may be shared with front-facing triangles. To skip vertex work too, engines cull whole clusters of triangles earlier, with a cone that bounds their normals (cluster cone culling).

8Worked example

The triangle of section 3, drawn with Vulkan into an 800 × 600 image with back-face culling and a counter-clockwise front face. Vulkan computes the area in framebuffer coordinates (rows go down) with a minus sign in front: a = −½ · Σ (xi · yi+1 − xi+1 · yi). A positive a is counter-clockwise.

1. In Y-up clip space

0 1 2 y = +1 y = −1 p0 = (0, 0.5) p1 = (−0.5, −0.5) p2 = (0.5, −0.5) a = ½ · (0.25 + 0.5 + 0.25) = +0.5 → counter-clockwise The triangle points up and was modeled as a front face.

This is how the projection matrix produced it (clip space with Y up, as in OpenGL-style math).

2. With a negative viewport height

0 1 2 row 0 row 600 pixel y = (1 − y_ndc) / 2 · 600 (flipped: +1 is row 0) (400, 150) (200, 450) (600, 450) a = −½ · (150000 − 180000 − 90000) = +60000 → front: kept The image is upright and the front face is drawn.

The flip of the viewport and the minus sign of Vulkan's formula cancel: Y-up counter-clockwise stays counter-clockwise.

3. Without the flip

0 1 2 row 0 row 600 pixel y = (1 + y_ndc) / 2 · 600 (Vulkan's own direction) (400, 450) (200, 150) (600, 150) a = −60000 → back: culled The image is upside down, and back culling now removes the faces that should be seen.

This is why a Y-up projection on Vulkan needs a fix, or a clockwise front face.

4. A mirrored object

0 1 2 row 0 row 600 scale (−1, 1, 1): det = −1 p0 = (0, 0.5) p1 = (0.5, −0.5) p2 = (−0.5, −0.5) a = −0.5 pixels: (400, 150) (600, 450) (200, 450) a = −60000 → "back" It is the outside of the object, and it is in front. Cull front faces for this object instead.

The determinant decides the cull mode per object, before drawing. Nothing about the triangle itself changes.

9Pitfalls

A model looks inside out
Its winding convention differs from the engine's, or it has a mirroring transform and the cull mode was not flipped.
Everything vanishes on Vulkan
A Y-up projection without a Y flip, with a counter-clockwise front face.
A wall or a leaf disappears from one side
An open surface without a double-sided material.
The back of a double-sided surface is dark or lit from the wrong side
Its normal was not reversed for back faces.
Lighting is wrong only on mirrored double-sided objects
The front-facing flag used without the determinant's sign.
Holes in some models only
Mixed winding inside one mesh; it needs fixing in the tool, or a double-sided material.

10In RASM

RASM uses glTF's convention: counter-clockwise is front. The cull mode is chosen per instance on the CPU from the material and the determinant, and the draw list is grouped so that each group of equal modes is one indirect draw. Because the Vulkan backend flips Y with a negative viewport height, clip space and the screen agree on the winding (D8).

CPU · when a transform or material changes getCullMode doubleSided, BLEND, can mirror → NONE sx · sy · sz · det(mesh) < 0 → FRONT else → BACK instanceCullModes one per instance row rebuildDrawList sort by alpha, cull mode → runs Draw list (one buffer; the opaque runs shown) NONE FRONT BACK (most draws) MASK and BLEND runs follow; empty runs are left out GPU · each scene pass per run: setCullMode, one indirect draw, then NONE Rasterizer front = counter-clockwise fragMain backFace → normal reversed Cull mode is dynamic pipeline state, so one pipeline per alpha mode serves all its cull modes.
Within an alpha mode the runs are in the order of CullMode: NONE, FRONT, BACK. The one BLEND run is always NONE. GPU culling keeps each run's visible draws at the start of its range (GPU-driven rendering).

Where RASM follows the textbook, and where it differs

PartThis pageRASM
Front facea conventionCounter-clockwise (VK_FRONT_FACE_COUNTER_CLOCKWISE in createGraphicsPipeline), like glTF. Imported data is not mirrored (D8).
Y flipkeeps the winding if done rightA negative viewport height in VulkanCommandBuffer::setViewport, so Y-up counter-clockwise stays counter-clockwise on screen.
Cull modea pipeline settingDynamic state (VK_DYNAMIC_STATE_CULL_MODE, core since Vulkan 1.3). Pipelines are created with VK_CULL_MODE_NONE; RenderContext::setCullMode sets it while recording.
Mirrorscull front, or swap the front faceCull front: getCullMode returns FRONT when scale.x * scale.y * scale.z * det(mat3(subMesh.transform)) < 0. The front-face setting never changes. This is the full determinant of the instance's world matrix, because the entity's rotation has determinant 1. A sub-mesh that a glTF clip can mirror (a scale that reaches 0 or less on its node, a node above it or one of its joints) gets NONE, because its determinant can change sign on the GPU from frame to frame (animationCanMirror); the shader's handedness comes from the posed matrix, so its lighting stays right.
Double-sidedno culling, reversed normal on the backNONE when the material's doubleSided is set (glTF doubleSided, or createMaterial's last argument). fragMain sets backFace = isFrontFace == (handedness < 0), and the PBR shader then uses −n.
Mirror + front-facing flagcombine with the determinantDone: vertMain passes handedness, the sign of determinant(modelMat3), to the fragment shader.
Granularityper objectPer instance row (one sub-mesh of one entity). updateDrawModes recomputes it, with the material's alpha mode, after updateTransform and after a material change; a changed mode rebuilds the draw lists of the entity's scenes. A see-through (BLEND) material always gets NONE, so its draws stay in one sorted run; it drops its back faces in the shader instead (transparency).
Draw callsone state per drawDraws are sorted into at most seven runs (three cull modes for opaque and for cut-out draws, one see-through run), one indirect draw per run, instead of one draw per object.
Other passes—beginPass sets NONE, drawSceneView restores NONE after its runs, and the stats overlay draws with NONE.
Cluster cone cullingsection 7Not done. Culling on the GPU works per draw (frustum, occlusion), not per cluster of triangles.

Where it lives in code

WhatWhere
The modesCullMode in include/rasm/types.h
Choosing a modegetCullMode, Renderer::updateDrawModes in src/renderer/gpuScene.cpp
Runs in the draw listRenderer::rebuildDrawList in src/renderer/gpuScene.cpp; DrawRun in src/renderer/gpuScene.h; MAX_DRAW_RUNS in src/renderer/uberMaterialTypes.h
Drawing the runsRenderer::drawSceneView in src/renderer/gpuScene.cpp
Front face, dynamic cull modeVulkanDevice::createGraphicsPipeline in src/rhi/vulkan/vulkanDevice.cpp
Setting the mode, the Y flipVulkanCommandBuffer::setCullMode, VulkanCommandBuffer::setViewport in src/rhi/vulkan/vulkanCommandBuffer.cpp
Double-sided flagMaterial in include/rasm/material.h; read from glTF in src/assets/material.cpp
Back-face lightingvertMain, fragMain in shaders/passes/rasm.slang; the normal flip in shaders/lib/pbr.slang
Keep these true
  • The counter-clockwise front face and the negative-height viewport are a pair. Change the Y flip, and the winding rule changes with it.
  • getCullMode and the shader's handedness must agree: both come from the sign of the world matrix's determinant.
  • Every pipeline takes its cull mode from the command buffer. A pass that draws must set it (beginPass sets NONE); code that changes it puts NONE back.
  • Anything that changes a transform or a material's doubleSided or alphaMode must go through updateDrawModes, or a draw stays in the wrong run.
  • The draw list has one run per alpha mode and cull mode: adding a mode means changing MAX_DRAW_RUNS on both the C++ and the Slang side (Struct layout).

Further reading