Skip to content

RT-432: cull forward-sandbox draws outside the camera frustum - #5830

Open
venetanji wants to merge 1 commit into
ItsNotRexed:mainfrom
venetanji:sandbox/frustum-culling
Open

venetanji wants to merge 1 commit into
ItsNotRexed:mainfrom
venetanji:sandbox/frustum-culling

Conversation

@venetanji

Copy link
Copy Markdown

Claimed in the coordination issue as: RT-432, carried into #5781. Original claim.

Depends on: #4119, #4436 (shared-file conflicts / merge ordering; this patch builds independently on current main).

What and why

The forward sandbox currently submits every scene draw on every frame, including objects wholly behind the camera. Compute conservative mesh bounds once, transform them per instance, and test them against the actual view-projection matrix before recording draws. Culling is enabled by default. --no-cull on vk_sandbox or tools/sandbox.py provides an image comparison, and headless output / the window title report submitted versus total draws.

Plane crossings remain visible. The bounds account for reflected, nonuniform and sheared transforms, use floating-point rounding margins, and fail open for empty or non-finite inputs. Original scene draw indices and descriptor-table addresses remain unchanged. The visibility helper is independent of Vulkan.

#4119 and #4436 move the shared scene/camera types into sandbox_common.h. Their published heads (9d9012261f97864951effcb6fc2b0e0b6945f88f and e5301e92ea3964c6128cafc8a3537bd8696a96dd) conflict with both current main and this branch in the same seven files, checked with git merge-tree. Listing them above records that overlap; neither is a build prerequisite. This change targets the forward camera pass. The new visibility helper stays separate for the eventual shared-code integration.

Tested

Base: 561b65a832218fbb9e6d2effd392a8eef093a3f8. Patch: a6aed1555928702cac19c7252e6dbeb9b75624eb.

Arch Linux, NVIDIA RTX 3080 Ti and RTX 3090, driver 615.78.08; GCC 16.2.1, Python 3.14.8, glslang 16.6.0 and the project-pinned Vulkan headers. Selected each adapter using the Mesa device-select implicit layer and verified the renderer's reported device; the sandbox does not currently honor LEONIDA_VK_DEVICE.

  • CPU visibility CTest passes, also compiled separately with -Wall -Wextra -Wpedantic -Werror. Covers all six planes, D3D near clipping, perspective, crossings/contact, enclosing bounds, transforms, cancellation and invalid inputs. Disabling rejection in an untracked copy makes the suite fail.
  • Optional synthetic Vulkan suite passes on both cards: ten individual clipping/transform cases, multiple camera poses, complete PPM equality with culling enabled/disabled, and a negative control requiring removal of visible geometry to change the image.
  • The larger fixture submits 3 of 1,003 draws, culling 1,000 behind the camera. Compared the changed renderer directly with the original renderer built from the base source on each card: both cover 413/4,096 pixels, with byte-identical complete PPMs on both cards. SHA-256: 03e01dedb4448ed55b703b5468578d96c736b957d6eb633a1b09529ddc40a81f.
  • Command forwarding test, six sandbox-selection tests, ten sandbox-scene tests, three text-writer checks and three documentation-path checks pass. git diff --check passes.

Reproduce the focused checks (requires Vulkan and glslangValidator for readback):

cmake -S runtime -B out/culling-build -G Ninja -DCMAKE_BUILD_TYPE=Release \
  -DLEONIDA_VULKAN_HEADERS_FETCH=ON -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build out/culling-build --target sandbox_visibility_test vk_sandbox --parallel 4
ctest --test-dir out/culling-build --no-tests=error -R '^sandbox_visibility$' --output-on-failure
LEONIDA_SANDBOX_BUILD=out/culling-build python -I -B tests/test_sandbox_culling.py -v
python -I -B tests/test_sandbox_pick.py
python -I -B tests/test_sandbox_scene.py
python -I -B tests/test_text_write_line_endings.py -q
python -I -B tests/test_docs_repo_paths.py -q

The local build reused a local copy of the pinned headers with FETCHCONTENT_SOURCE_DIR_VULKANHEADERS; the fetch option above is the equivalent clean setup. Without LEONIDA_SANDBOX_BUILD, the Python test runs its command check and skips the two GPU checks.

All fixtures and GLSL in this patch are original synthetic inputs. Native Windows rendering and real-scene timing remain unverified; draw-count reduction is not an FPS measurement. This does not claim validation-layer cleanliness for the existing renderer.

AI-assisted: yes, implemented and tested with Codex.

Checklist

  • No game data, keys or decryption, including in logs or reports quoted here.
  • Translator contract update: not applicable; no translator rule changes.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested a6aed15 on NVIDIA GeForce RTX 4080, driver 617.42: sandbox_visibility_test exits 0 and prints "sandbox visibility checks passed". It prints no per-check count, and vk_sandbox and tests/test_sandbox_culling.py were not run.

AI-assisted.

@milas6669

Copy link
Copy Markdown

Native Windows check of a6aed155 (on current main 561b65a8, the same base), covering what the description lists as unverified: Windows rendering, and validation-layer behaviour. Result: pass, and the culling adds no new validation message.

Windows 11, MSVC 19.44 + Ninja, RelWithDebInfo, Vulkan SDK 1.4.363 (glslangValidator), NVIDIA GeForce RTX 3070 (Ampere, driver 32.0.16.2124). The earlier run on this head is the RTX 4080 CPU visibility test, and the description's GPU runs are Linux (RTX 3080 Ti, RTX 3090).

  • ctest -R ^sandbox_visibility$: 1/1 passed.
  • LEONIDA_SANDBOX_BUILD=runtime/build python -I -B tests/test_sandbox_culling.py -v: 3 tests OK, and the two GPU checks ran, not skipped: SandboxReadback.test_clip_boundaries_and_instance_transforms and test_draw_reduction_and_changed_visible_geometry (the culled and unculled PPMs compare as the test requires). tests/test_sandbox_pick.py 6 OK, test_sandbox_scene.py 10 OK, test_text_write_line_endings.py 3 OK, test_docs_repo_paths.py 3 OK.
  • Validation layer: vk_sandbox has no validation switch of its own (no VALIDATION in vk_sandbox.cpp), so I forced VK_LAYER_KHRONOS_validation on through the Vulkan loader (VK_INSTANCE_LAYERS) and ran the same suite with the vk_sandbox output captured: 27 vk_sandbox processes, all exit 0, the layer seen loading in 27 of 27, and one distinct validation message in all 27: VUID-VkSamplerCreateInfo-anisotropyEnable-01070 (anisotropy enabled without samplerAnisotropy).
  • That error is not from this PR: with only runtime/test/vk_sandbox.cpp reverted to origin/main and --no-cull stripped from the commands, the same suite and layer gave the same single error in 11 of 11 runs, and none of the files that enable anisotropy (runtime/src/gfx10_descriptors.h, runtime/shims/d3d12_vulkan_device.cpp, runtime/test/vk_harness.cpp) is in this PR's diff. vk_sandbox: enable shaderDrawParameters and samplerAnisotropy for the translated shaders #4776 ("vk_sandbox: enable shaderDrawParameters and samplerAnisotropy for the translated shaders") is the open change that targets it; I did not test that one.

So the description's "does not claim validation-layer cleanliness for the existing renderer" holds, and this PR neither adds nor removes anything there.

Not tested: real scenes and timing (the description says draw-count reduction is not an FPS measurement), --window, the larger 1,003-draw fixture's SHA-256 (the suite's 3-of-N draw reduction passes), and #4119/#4436 (the description lists them as conflicting, not as prerequisites).

AI-assisted. Synthetic fixtures only; no game data.

@DenRakEiw

Copy link
Copy Markdown
Contributor

Tested head a6aed155 (trial-merged cleanly onto origin/main 561b65a83) on Windows 11 x64, NVIDIA GeForce RTX 5090 (driver 32.0.16.1088), MSVC 2022 (19.44.35230.0), Ninja, Vulkan SDK 1.4.363.0 (glslangValidator), Python 3.12.10.

  • git diff --cached --check: clean (no whitespace or conflict issues).
  • cmake + cmake --build --target sandbox_visibility_test vk_sandbox (Release, MSVC+Ninja): built cleanly with 0 warnings/errors.
  • ctest -R sandbox_visibility --output-on-failure -V: 1/1 passed (0.03s, printed sandbox visibility checks passed).
  • python -m unittest -v tests/test_sandbox_culling.py with LEONIDA_SANDBOX_BUILD set and LEONIDA_VK_DEVICE="RTX 5090": 3/3 passed (7.220s), covering:
    • SandboxCommand::test_no_cull_reaches_both_renderer_modes
    • SandboxReadback::test_clip_boundaries_and_instance_transforms (10 clip/transform cases comparing culled vs --no-cull readback bit-for-bit on Vulkan)
    • SandboxReadback::test_draw_reduction_and_changed_visible_geometry (3 visible + 1000 hidden draws -> submitted=3 culled=1000 total=1003, pixel-identical to --no-cull across camera poses)

Looks good to merge.

AI-assisted (Gemini 4 Neon).

@MuratK20

Copy link
Copy Markdown

Tested a6aed15 (up to date with main 561b65a) on Windows 11 Pro 26100, NVIDIA GeForce RTX 5080 (driver 617.14), VS 2022 / MSVC, Vulkan SDK 1.4.363.0, Python 3.13.5: pass

  • Build: sandbox_visibility_test and vk_sandbox (RelWithDebInfo) built with no errors or warnings.
  • CPU test: ctest -R '^sandbox_visibility$' passed 1/1.
  • Python test: with LEONIDA_SANDBOX_BUILD set, python -I tests/test_sandbox_culling.py -v gives 3 tests OK and none skipped. Both Vulkan readback checks ran natively on Windows.
  • The 1,003-draw fixture at 640x360:
    • With culling: submitted=3 culled=1000 total=1003.
    • With --no-cull: submitted=1003 culled=0.
    • Both PPMs have the same SHA-256 (13,734 pixels covered).
  • Mutation check: I made Frustum::visible test the box's nearest corner instead of its farthest, so boxes crossing a plane get culled.
    • sandbox_visibility_test then fails ("far plane crossing", "camera inside bounds").
    • All 3 Python tests fail on the image comparison.
    • Restored, everything passes again. So the image check does catch over-culling.
  • Lint: compileall and git diff --check are clean.

Not tested:

  • AMD Radeon(TM) Graphics iGPU (driver 32.0.13036.4): vk_sandbox crashes there with 0xC0000005 inside amdvlk64 during vkCreateGraphicsPipelines (vk_sandbox.cpp:529 on main) on the PR's synthetic shaders. Main's build does the same with a single draw, so it's not from this PR.
  • GPU selection, also on main: vk_sandbox picks its GPU by rank only and ignores LEONIDA_VK_DEVICE, unlike the harnesses that use pick_device. On a machine with two GPUs it always takes the discrete one. I reached the iGPU with VK_LOADER_DRIVERS_SELECT=*amd*.
  • Not run: --window mode, and tools/sandbox.py --check on a real scene (needs game files).

AI-assisted (Claude Code). I ran these on my own machine.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at a6aed1555 on Windows 11 / RTX 5070 Ti.

  • ctest -R '^sandbox_visibility$': 1/1 passed (0.03 s).
  • LEONIDA_SANDBOX_BUILD=<build> python -I -B tests/test_sandbox_culling.py -v: 3/3 OK. The larger fixture asserts submitted=3 culled=1000 total=1003, PPM equality with --no-cull, a second camera, and that removing the visible geometry changes the image.
  • test_sandbox_pick.py 6/6, test_sandbox_scene.py 10/10, text-write line endings 3/3, docs paths 3/3.
  • Reviewed sandbox_visibility.h: six D3D clip planes from the row-vector view-projection, conservative transformed AABB with binary32 rounding margins, and fail-open empty/non-finite bounds. Draw order and descriptor addresses are unchanged.

Verdict: LGTM. The PR says native Windows was unverified; this run covers Windows 11 and the Vulkan readback path.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested a6aed15 on Intel UHD Graphics 770, driver 32.0.101.7088, MSVC 19.44, Vulkan SDK 1.4.357.0, with VK_LAYER_KHRONOS_validation loaded (loader prints "Insert instance layer "VK_LAYER_KHRONOS_validation""): sandbox_visibility_test and vk_sandbox build and sandbox_visibility_test exits 0 with "sandbox visibility checks passed" (ctest -R sandbox_visibility: 1/1 passed). It opens no Vulkan device, so the layer is not exercised there. tests/test_sandbox_culling.py with LEONIDA_SANDBOX_BUILD=runtime\build prints "Ran 3 tests" and "OK". vk_sandbox on a 3-draw synthetic scene prints "device: Intel(R) UHD Graphics 770" and exits 0 both ways: "culling: submitted=2 culled=1 total=3" with culling on, "culling: submitted=3 culled=0 total=3" with --no-cull. Each run prints one "Validation Error: [ VUID-VkSamplerCreateInfo-anisotropyEnable-01070 ]" line. It also appears when vk_sandbox.cpp is taken from main 561b65a and the other sources stay at this head, and vk_sandbox.cpp has no "anisotropy" line at either commit, so it does not come from this diff. I did not trace the sampler to its source and did not run a full main build. The PR's full 10-case synthetic suite and the -Werror standalone compile were not run.

AI-assisted.

@erickbobd

Copy link
Copy Markdown
Contributor

Additional draw-index/table-preservation check at a6aed1555928702cac19c7252e6dbeb9b75624eb, beyond the native Windows readback checks above.

Windows 11 25H2 (26200.9974), RTX 5070 Ti / NVIDIA 617.14, MSVC 19.51.36260, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0.

The submitted/culled counts and image comparisons also hold when the three visible instances are not the first three scene entries:

Synthetic draw order Visible source indices / construction Submitted / total Pixel comparison
All 1,000 hidden instances first 1000, 1001, 1002 3 / 1003 complete PPM identical to --no-cull
Hidden instances interleaved with visible ones 333, 667, 1002 3 / 1003 complete PPM identical to --no-cull
Deterministic shuffle Python random.Random(5830) over the same 1,003 instances 3 / 1003 complete PPM identical to --no-cull

Each image covers 413/4,096 pixels. All three have SHA-256 03e01dedb4448ed55b703b5468578d96c736b957d6eb633a1b09529ddc40a81f, also matching an independently built, unchanged main 561b65a8 renderer on this GPU. This exercises surviving instances after skipped descriptor-table entries, rather than only the original fixture's visible-first ordering.

Ran the PR's three Python tests with both Vulkan checks enabled (no skips), plus the three additional orders, normally and with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: 33 renderer processes per mode, all exit 0. The loader confirms the validation layer was inserted. CPU visibility, the six sandbox-pick tests, ten sandbox-scene tests, three text-writer checks and three documentation-path checks also pass. The existing test that removes visible geometry changes the image as expected.

Same validation caveat as @milas6669: VUID-VkSamplerCreateInfo-anisotropyEnable-01070 appears in each validation-enabled run and also on the unchanged main control. It is already covered by #4776; this is functional readback evidence, with that existing validation issue still present.

Native Windows headless rendering and original synthetic inputs only. No game files or timing/FPS measurements. AI-assisted (Codex). The PR worktree is unchanged and clean.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants