Skip to content

Runtime: Variable Rate Shading on VK_KHR_fragment_shading_rate (D3D-32) - #519

Closed
0xTehuti wants to merge 1 commit into
ItsNotRexed:mainfrom
0xTehuti:runtime/d3d12-variable-rate-shading
Closed

0xTehuti wants to merge 1 commit into
ItsNotRexed:mainfrom
0xTehuti:runtime/d3d12-variable-rate-shading

Conversation

@0xTehuti

Copy link
Copy Markdown
Contributor

Implements D3D-32 from #374 (coordinating with #313).

Summary of Changes

1. CommandList IR (runtime/src/command_list.h)

  • Defined shading_rate namespace with standard D3D12 rate constants (rate_1x1 through rate_4x4) and validation helpers (shading_rate::valid).
  • Defined shading_rate_combiner namespace (passthrough, override_val, min, max, sum) with validation helpers (shading_rate_combiner::valid).
  • Added SetShadingRate and SetShadingRateImage command structs to the Command variant and methods rs_set_shading_rate / rs_set_shading_rate_image on CommandList.

2. D3D12 COM Interface (runtime/shims/d3d12_com.h, runtime/shims/d3d12_com.cpp)

  • Implemented RSSetShadingRate and RSSetShadingRateImage on GraphicsCommandList (ID3D12GraphicsCommandList5).
  • Enforces list type gating: permitted only on DIRECT or BUNDLE lists; rejected on COMPUTE or other list types.
  • Validates shading rate enum values and combiners; null combiner pointers default to passthrough for both combiners.
  • Supports unbinding shading rate image with nullptr.
  • Restores shading rate defaults (1x1, passthrough, unbound image) on Reset().
  • Exported test inspectors command_list_shading_rate() and command_list_shading_rate_image().

3. Vulkan Device & Backend (runtime/shims/d3d12_vulkan_device.cpp, runtime/src/vulkan_backend.h, runtime/src/vulkan_replay.h)

  • Probed and enabled VK_KHR_fragment_shading_rate device extension and physical device features (pipelineFragmentShadingRate, primitiveFragmentShadingRate, attachmentFragmentShadingRate).
  • Added vkCmdSetFragmentShadingRateKHR device function pointer loading.
  • Implemented D3D12_FEATURE_D3D12_OPTIONS6 support query in CheckFeatureSupport(), reporting VariableShadingRateTier (Tier 1 / Tier 2), ShadingRateImageTileSize, AdditionalShadingRatesSupported, and PerPrimitiveShadingRateSupportedWithViewportIndexing.
  • Added shading_rate_extent helper converting D3D12 shading rates to VkExtent2D.
  • Replays dynamic fragment shading rate via vkCmdSetFragmentShadingRateKHR. Validates image resource binding and rejects buffers.

4. Testing & Verification

  • Unit Tests:
    • command_list_test.cpp: tests valid/invalid rates, combiners, image resource IDs, and closed-list recording rejection.
    • d3d12_com_test.cpp: tests QueryInterface for IID_ID3D12GraphicsCommandList5, default state verification, argument rejection, compute list rejection, and queue replay.
    • d3d12_vulkan_device_test.cpp: tests CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS6), tier reporting, tile size, background processing, and parameter validation.
    • vk_command_replay_test.cpp: tests replay with and without VK_KHR_fragment_shading_rate, 1x1 rate tolerance, extent/combiner verification, and buffer image rejection.
  • Diagnostics: Clean build across all Ninja targets under MSVC /W4 /permissive- /utf-8 with 0 warnings and 0 errors.
  • Test Suite: 18 / 18 CTest suites passed (100%), Python test suites passed.
  • Documentation: Updated docs/d3d12_vulkan.md with D3D-32 details.

Environment & Disclosure

  • AI-assisted: Yes (Claude / Antigravity).
  • OS: Windows 11 Pro x64 (24H2)
  • Compiler: MSVC 2022 (v14.44.35207 / cl.exe 19.44.35225)
  • GPU: NVIDIA GeForce RTX 2070 SUPER (8203 MB VRAM)
  • Driver: NVIDIA 595.79 (Vulkan 1.4.321, WDDM 3.2)

@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested 0d2f9ca on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200: pass.

  • runtime built with MSVC 19.50 (build_runtime.ps1, Vulkan SDK 1.4.363.0); ctest 23/23
  • d3d12_vulkan_device_test exit 0; d3d12_com_test exit 0 ("16 colour, depth and stencil texels matched; queue wait held the GPU")

ezwinxdlmao added a commit to ezwinxdlmao/Leonida that referenced this pull request Nov 22, 2026
RSSetShadingRate now refuses a combiner outside D3D12_SHADING_RATE_COMBINER:
Close returns E_INVALIDARG, where the combiner used to act as passthrough.
Either VRS call on a compute or copy list makes Close return E_FAIL, and
bundles take both. When a list has several invalid calls, the first one
decides what Close returns. These are the host PC D3D12 runtime's results,
measured on a Radeon 680M, an RTX 3050 and WARP.

The stricter checks follow 0xTehuti's ItsNotRexed#519, which also refuses invalid
combiners and these calls on compute lists (there with E_INVALIDARG).

d3d12_vrs_test checks the Close result after 14 call sequences, plus 2 more
at tier 2. They run before the test's PSO check, so they also run on a device
that refuses PSOs. The draw case for out-of-range combiners is gone. The docs
list where the shading-rate image is still checked at replay rather than at
Close.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@0xTehuti
0xTehuti force-pushed the runtime/d3d12-variable-rate-shading branch from 0d2f9ca to c6d88c0 Compare November 22, 2026 01:29
@woreksami

Copy link
Copy Markdown

Tested 0d2f9ca on an RTX 3060 (driver 610.88), Windows 11: pass. ctest 23/23; d3d12_vulkan_device_test (including the VariableShadingRateTier checks: this GPU exposes VK_KHR_fragment_shading_rate with attachment rates), vk_command_replay_test, d3d12_com_test, d3d12_descriptor_test pass.

Setup: Windows 11 Pro, NVIDIA GeForce RTX 3060 (Ampere), driver 610.88, Vulkan 1.4.341, MSVC (VS 18 Build Tools), Ninja, RelWithDebInfo. No validation layer installed, so not run with LEONIDA_VK_VALIDATION=1. AI-assisted (Claude Code).

@MrCheese5

Copy link
Copy Markdown
Contributor

Tested c6d88c0 on RTX 4090 (driver 621.14), Windows 11 26200, MSVC 19.44: pass

No game files used. AI-assisted run (Claude Code).

@matheuskshn

Copy link
Copy Markdown
Contributor

Code review at c6d88c0. The external-device probe can report Tier 2 from the physical device's pipeline and attachment capabilities, while standalone creation also requires primitive rate support. Neither constructor path can make SetShadingRateImage attach an image during replay today; that branch only records it for future draw support. It would be useful to keep advertised Tier 2 separate from physical support until the attachment path is implemented. AI-assisted code review.

ezwinxdlmao added a commit to ezwinxdlmao/Leonida that referenced this pull request Nov 22, 2026
RSSetShadingRate now refuses a combiner outside D3D12_SHADING_RATE_COMBINER:
Close returns E_INVALIDARG, where the combiner used to act as passthrough.
Either VRS call on a compute or copy list makes Close return E_FAIL, and
bundles take both. When a list has several invalid calls, the first one
decides what Close returns. These are the host PC D3D12 runtime's results,
measured on a Radeon 680M, an RTX 3050 and WARP.

The stricter checks follow 0xTehuti's ItsNotRexed#519, which also refuses invalid
combiners and these calls on compute lists (there with E_INVALIDARG).

d3d12_vrs_test checks the Close result after 14 call sequences, plus 2 more
at tier 2. They run before the test's PSO check, so they also run on a device
that refuses PSOs. The draw case for out-of-range combiners is gone. The docs
list where the shading-rate image is still checked at replay rather than at
Close.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ezwinxdlmao added a commit to ezwinxdlmao/Leonida that referenced this pull request Nov 22, 2026
RSSetShadingRate now refuses a combiner outside D3D12_SHADING_RATE_COMBINER:
Close returns E_INVALIDARG, where the combiner used to act as passthrough.
Either VRS call on a compute or copy list makes Close return E_FAIL, and
bundles take both. When a list has several invalid calls, the first one
decides what Close returns. These are the host PC D3D12 runtime's results,
measured on a Radeon 680M, an RTX 3050 and WARP.

The stricter checks follow 0xTehuti's ItsNotRexed#519, which also refuses invalid
combiners and these calls on compute lists (there with E_INVALIDARG).

d3d12_vrs_test checks the Close result after 14 call sequences, plus 2 more
at tier 2. They run before the test's PSO check, so they also run on a device
that refuses PSOs. The draw case for out-of-range combiners is gone. The docs
list where the shading-rate image is still checked at replay rather than at
Close.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@0xTehuti
0xTehuti force-pushed the runtime/d3d12-variable-rate-shading branch from c6d88c0 to c163f70 Compare November 22, 2026 15:04
@0xTehuti

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (003e7812) and updated commit to c163f704.

Changes & Conflict Resolutions:

  • Merge conflict resolution: Resolved conflicts across runtime/shims/d3d12_vulkan_device.cpp, runtime/shims/d3d12_com.cpp, runtime/shims/d3d12_com.h, runtime/src/command_list.h, runtime/src/vulkan_backend.h, runtime/src/vulkan_replay.h, runtime/test/command_list_test.cpp, runtime/test/vk_test_device.h, and docs/d3d12_vulkan.md.
  • Review feedback (@matheuskshn):
    • Unifed capability probing between external-device probe (create_vulkan_device) and standalone creation (create_vulkan_device_standalone): both now evaluate pipelineFragmentShadingRate for base VRS support and attachmentFragmentShadingRate && primitiveFragmentShadingRate for physical Tier 2 capability.
    • Separated physical device capability (physical_variable_shading_rate_tier) from the advertised D3D12 tier (variable_shading_rate_tier): because SetShadingRateImage currently records the image resource without binding an attachment during replay, CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS6) advertises D3D12_VARIABLE_SHADING_RATE_TIER_1 until attachment replay is active.
    • Closed command list after query verification in command_list_test.cpp to ensure clean state reset for VRS test cases.

Verification Suite:

  • python -m compileall -q make_game.py tools tests: Pass (exit 0).
  • python -I make_game.py --help: Pass (exit 0).
  • python -m unittest discover -s tests/semantic -v: 1,567 / 1,567 tests passed (exit 0).
  • cmake -S runtime -B runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_VULKAN_HEADERS_FETCH=ON: Pass (exit 0).
  • cmake --build runtime/build: 280 / 280 targets built cleanly under MSVC /W4 /permissive- /utf-8 with 0 warnings and 0 errors.
  • ctest --test-dir runtime/build --output-on-failure: 98 / 98 tests passed (100% pass rate, 0 failures, 7 skipped/disabled).

Test Environment:

  • OS: Windows 11 Pro x64 (24H2)
  • Compiler: MSVC 2022 (v14.44.35207 / cl.exe 19.44.35225)
  • Vulkan Headers: 1.4.363.0
  • GPU: NVIDIA GeForce RTX 2070 SUPER (driver 595.79)
  • Clean-room compliant: No game files used. AI-assisted (Antigravity).

@0xTehuti

Copy link
Copy Markdown
Contributor Author

Closing in favor of #469.

@ezwinxdlmao's #469 builds on top of draw paths, hooks the shading-rate attachment into the render pass, and incorporates our combiner validation and command list gating from #519. We tested e4f06d1d natively on an NVIDIA GeForce RTX 2070 SUPER with LEONIDA_VK_VALIDATION=1, verified full feature query and status contract parity, and approved #469.

Closing to keep coordination clean and avoid duplicate PRs on D3D-32.

@0xTehuti 0xTehuti closed this Nov 22, 2026
ezwinxdlmao added a commit to ezwinxdlmao/Leonida that referenced this pull request Nov 23, 2026
RSSetShadingRate now refuses a combiner outside D3D12_SHADING_RATE_COMBINER:
Close returns E_INVALIDARG, where the combiner used to act as passthrough.
Either VRS call on a compute or copy list makes Close return E_FAIL, and
bundles take both. When a list has several invalid calls, the first one
decides what Close returns. These are the host PC D3D12 runtime's results,
measured on a Radeon 680M, an RTX 3050 and WARP.

The stricter checks follow 0xTehuti's ItsNotRexed#519, which also refuses invalid
combiners and these calls on compute lists (there with E_INVALIDARG).

d3d12_vrs_test checks the Close result after 14 call sequences, plus 2 more
at tier 2. They run before the test's PSO check, so they also run on a device
that refuses PSOs. The draw case for out-of-range combiners is gone. The docs
list where the shading-rate image is still checked at replay rather than at
Close.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

5 participants