Skip to content

Occlusion culling: do not draw meshes that other meshes hide (G-buffer pass) #252

Closed

Description

@BenjaFriend

Problem

Frustum culling (MeshCuller, #249) skips the meshes that are outside the camera's view volume. It does not skip a mesh that another mesh hides. The G-buffer pass still draws every mesh that is inside the view volume, and the GPU then throws most of their pixels away at the depth test.

Seen in the Levels/LightingTest.json level (3136 meshes in a grid, 1.5 units apart): with the camera against one sphere, so that it fills the screen, the GPU Options window still says about 1300 meshes drawn and about 1950 outside the view. The view volume reaches forward past the sphere over the rest of the grid, so all of those meshes count as "in view" although none of them can be seen.

This is not a bug in frustum culling. It is the limit of the technique, and dense scenes seen along their rows (a grid, a city street, a room full of props) are where it helps least.

Proposal

Add occlusion culling to the G-buffer pass, so that a mesh that is fully hidden behind others is not drawn. It runs after the frustum test and only removes meshes, so the picture stays the same.

Options

  • Hierarchical depth buffer (Hi-Z), on the GPU. Suggested first. Build a depth pyramid (a mip chain where each texel holds the farthest depth of the texels below it) from the depth of a previous pass or frame. A compute shader tests each mesh's bounds against it and writes the survivors into the indirect draw buffer. The pass already draws with vkCmdDrawIndexedIndirect (OffscreenSubpass, one VkDrawIndexedIndirectCommand per mesh slot), so the draw side may need little change. The culling itself moves from the CPU to the GPU, which scales to many thousands of meshes.
  • Hardware occlusion queries. The CPU asks the GPU how many pixels a bounding box would have drawn. Results come back a frame late and the cost grows with the number of meshes, so this suits a few hundred large objects and not thousands.
  • Software occlusion. The CPU rasterizes a few large occluders into a small depth buffer and tests the other meshes against it. It needs no GPU work, but it needs the scene author (or the engine) to say which meshes are occluders.

Design notes

  • Where the depth comes from. The common Hi-Z approach uses last frame's depth (reprojected with the camera change) and tests against it. This can wrongly hide a mesh for one frame when the camera moves fast or something moves into view. The two-pass variant fixes it: draw what was visible last frame, build the pyramid from that depth, test the rest against it, and draw those that pass. It costs a second draw call set but has no popping. A decision is needed on which to start with.
  • Meshes that move. Rotators and movers change position between ticks, and the renderer blends the last two ticks (render interpolation). The test must use the same blended world matrix the pass draws with, as MeshCuller does today.
  • Conservative bounds. The test must never hide something that is visible: use the mesh's bounding box (an Aabb is already stored per slot), the nearest depth of the box, and the pyramid level that covers its screen size.
  • Transparent and special meshes. Only opaque meshes in the G-buffer pass can be occluders and be occluded by depth. Check what the skybox and any later passes need before they use the result.
  • Stats. CullStats has Drawn and Culled. Add a third count (for example Occluded) and show it in the GPU Options window, so that the effect is visible.
  • A switch. A control next to Frustum culling in GPU Options, and a --occlusion-culling on|off flag and [Engine] OcclusionCulling key like the other run settings (RunSettings), so that a run can compare on and off.
  • Software GPUs. Compute work needs to run on llvmpipe too, because headless screenshot runs use it. Or occlusion culling turns itself off when the device is a CPU driver.
  • Not in scope. Distance culling and level of detail are separate, cheaper ways to cut the draw count in a scene this size. They are not part of this issue.

Acceptance criteria

  • With the camera against a large mesh in Levels/LightingTest.json, the drawn count falls to a small number, and the picture (an offscreen screenshot with occlusion culling on and off) is the same.
  • A scene with nothing hidden draws the same meshes as before, and the cost of the test is small.
  • No mesh is hidden that is visible: a randomized test (like FrustumCullingTests) with boxes in front of and behind occluders, and the camera moving, never hides a visible mesh.
  • A moving camera and moving meshes show no popping or flicker in a recording, and a resize or a swap chain recreation rebuilds the pyramid.
  • The Vulkan validation layer reports no errors with the new pass on.
  • The test for the pure part (the bounds-versus-depth test, written so that it runs on the CPU too) runs in GraphicsTests without a window.

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions