Skip to content

Runtime: variable rate shading on Vulkan, RSSetShadingRate and the shading-rate image (D3D-32) - #469

Open
ezwinxdlmao wants to merge 3 commits into
ItsNotRexed:mainfrom
ezwinxdlmao:d3d32-vrs
Open

ezwinxdlmao wants to merge 3 commits into
ItsNotRexed:mainfrom
ezwinxdlmao:d3d32-vrs

Conversation

@ezwinxdlmao

@ezwinxdlmao ezwinxdlmao commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #374 as: D3D-32 (claim). Listed in #313.

What and why

Until now, RSSetShadingRate and RSSetShadingRateImage (ID3D12GraphicsCommandList5) were generated stubs that only logged the call, and D3D12_OPTIONS6 was not answered. This PR implements variable rate shading on VK_KHR_fragment_shading_rate, on top of #291's draw path.

Recording. The rate, both combiners and the image are recorded with each draw (leonida::Draw).

  • Reset and ClearState return them to 1x1, passthrough and no image.

  • A null combiner array is {passthrough, passthrough}.

  • Close returns what the host PC D3D12 runtime returns. I measured that with a standalone probe (not committed) on a Radeon 680M, an RTX 3050 and WARP:

    • a rate or combiner outside its enum: E_INVALIDARG;
    • either call on a compute or copy list: E_FAIL, while bundles take both calls, as direct lists do;
    • several invalid calls in one list: the first one decides.

    The three agree on all of these, except that WARP, a tier-1 device, refuses RSSetShadingRateImage on every list (see the deviations below).

  • The combiner and list-type checks follow 0xTehuti's Runtime: Variable Rate Shading on VK_KHR_fragment_shading_rate (D3D-32) #519 and came in with 1c59ccc. Before that commit, a combiner outside the enum acted as passthrough.

Replay. Replay calls vkCmdSetFragmentShadingRateKHR at every draw, and the shading rate is dynamic state in every graphics pipeline. D3D12_SHADING_RATE and a Vulkan shading-rate texel share one encoding (log2 w << 2 | log2 h).

D3D12 Vulkan
Per-primitive rate (SV_ShadingRate) Not translated: #284's corpus scan finds no shader that writes it. It is therefore always 1x1, and the first combiner is folded on the CPU (override and min give 1x1, the rest keep the rate). Vulkan's first op is KEEP, so primitiveFragmentShadingRate is not needed
Second combiner, image bound passthrough, override, min, max and sum map to KEEP, REPLACE, MIN, MAX and MUL. Multiplying sizes is adding log2 rates. Sum is reported only with fragmentShadingRateStrictMultiplyCombiner, because without it 1 x 1 may give 2
Second combiner, no image D3D12 reads 1x1, so it is folded on the CPU too
Shading-rate image (R8_UINT, 2D, single-sampled) The rendering scope's VkRenderingFragmentShadingRateAttachmentInfoKHR, in GENERAL (what SHADING_RATE_SOURCE already maps to), with texels of the reported tile size. On a tier-2 device, R8_UINT textures get the attachment usage, and graphics pipelines get ..._RENDERING_FRAGMENT_SHADING_RATE_ATTACHMENT_BIT_KHR. Other formats, multisampled textures and volumes are refused at replay
A pixel shader that writes SV_Depth or SV_StencilRef shades per pixel Vulkan leaves this to fragmentShadingRateWithShaderDepthStencilWrites, which both test GPUs have. Such PSOs (FragDepth or FragStencilRefEXT in the SPIR-V) are forced to 1x1

Feature answers.

  • Tier 1: pipelineFragmentShadingRate.
  • Tier 2: the attachment feature as well, fragmentShadingRateNonTrivialCombinerOps, and an 8x8 or 16x16 texel in the device's range. The smaller one becomes ShadingRateImageTileSize.
  • AdditionalShadingRatesSupported: 2x4, 4x2 and 4x4 are all listed for one sample.
  • OPTIONS10 sum combiner: as above.
  • Reported FALSE: per-primitive rates with viewport indexing, mesh per-primitive rates and background processing, because the layer implements none of them. The host reports per-primitive rates with viewport indexing on both GPUs, and the other two on the RTX 3050.
  • Measured: the tier, tile, additional-rate and sum answers match the host PC D3D12 runtime on both GPUs, and the test checks this through the adapter's LUID:
GPU Tier Tile 2x4/4x2/4x4 Sum
RTX 3050 Laptop 2 16 yes yes
Radeon 680M 2 8 no yes

Without the extension, OPTIONS6 reports no VRS, and the rate and the image are recorded but ignored. The standalone device (LEONIDA_D3D12=vulkan) enables the extension where the GPU has it.

Deviations. All four are listed under known gaps in docs/d3d12_vulkan.md.

  • An image smaller than the render area. D3D12 reads 1x1 past the image's edge, while Vulkan requires the image to cover the render area. An image that is too small is left out of the draw, which then shades as if no image were bound.
  • The image is checked at replay, not at Close. An image that is not an R8_UINT texture, or is multisampled or 3D, fails ExecuteCommandLists. One with several mips or array layers is used through mip 0 and layer 0. The host runtime fails Close for each of these except a 3D image. Checking at Close would mean reading the resource while recording, which the command list does not do today.
  • Tier 1. On a device that reports tier 1, RSSetShadingRateImage is accepted. The host runtime fails Close with E_FAIL there (WARP).
  • List types. Only the two VRS calls check the list type so far.

Vertex strides. Earlier heads of this PR also stopped pipelines without vertex input from declaring VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE (VUID-vkCmdDraw-pStrides-04913). #291's merge candidate now has that fix, so this PR no longer carries it.

Docs. docs/d3d12_vulkan.md is updated in several places:

  • section 3.2: replay, list state, known gaps and the new test;
  • section 4.1: the OPTIONS6/OPTIONS10 answers;
  • the VRS row of the Xbox-feature table;
  • open question 10.

Depends on: #291. This branch is rebased onto #291's merge candidate, Ryx69#6 (70178d3: #291 merged with main at 426023a), plus three commits: 5190251, 571a694 and cfe02e4 (a doc wording fix). Only those three are this PR's. Against today's main (c0ac5fd), this branch conflicts only where the candidate itself does: d3d12_vulkan_device.cpp and vulkan_replay.h. I'll rebase again once #291 lands.

The rebase kept the candidate's structure and re-added only the VRS parts:

  • Pipelines are built through the candidate's recipe, so unbound-slot variants get the shading-rate dynamic state and attachment flag too.
  • The shading-rate image usage moved into texture_create_info.
  • The standalone device now adds the fragment-shading-rate features to the candidate's feature chain. Before, it replaced enable13.pNext, which would have dropped the other extension features the candidate chains there.
  • Close keeps the candidate's tracing and its open-query check, which now goes through the same first-error rule.

Tested

Windows 11 Pro 26300, MSVC 19.44, Vulkan SDK 1.4.363.0, CMake + Ninja.

After the rebase onto 70178d3 (same GPUs and drivers as below):

  • Build: clean at /W4, no warnings.
  • ctest: 61 of 62 pass. The failure is d3d12_sampler2, which fails on main (e7584c5) too, on both GPUs: the host runtime reports no non-normalized samplers, so its answers differ from the layer's.
  • RTX 3050, with LEONIDA_VK_VALIDATION=1:
    • d3d12_vrs_test: every case matches on every pixel.
    • d3d12_draw_test and d3d12_state_map_test pass.
    • 0 validation errors.
  • Radeon 680M, with validation:

The results below are from the pre-rebase heads (88d0672, 1c59ccc):

  • Build: clean at /W4, no warnings. ctest: 15/15 pass on both GPUs.
  • New d3d12_vrs_test:
    • Every draw goes through the Xbox-ABI proxy. A triangle covers a 64x64 target, and its pixel shader writes the pixel it ran at, so a coarse pixel reads back as a block of equal values.
    • For each case, every pixel must equal the first pixel of its expected block (so shading is not finer), and that position must lie inside the block (so it is not coarser).
    • 24 draw cases on a device with the extension: the default, Reset and ClearState; every rate; each first combiner; each second combiner over a four-quadrant image (1x1, 2x2, 2x1, 1x2); no image bound; and a depth-writing pixel shader.
    • 3 more cases on a device without the extension, which must ignore VRS.
    • Close results: 14 call sequences, plus 2 more at tier 2, must give the host runtime's result. They cover a rate or combiners outside their enums, each call on direct, bundle, compute and copy lists, and an invalid barrier before and after a VRS call on a compute list. They need no PSO, so they also run on the 680M.
  • NVIDIA GeForce RTX 3050 Laptop GPU (D3D12 driver 32.0.16.2101, Vulkan 621.01): with LEONIDA_VK_VALIDATION=1, every draw case passes on every pixel, all 16 Close results match, and there are 0 validation errors. d3d12_draw_test, d3d12_compute_test, d3d12_vulkan_device_test, d3d12_descriptor_test, d3d12_com_test and vk_command_replay_test also pass under validation.
  • AMD Radeon 680M (D3D12 driver 31.0.14084.6002, Vulkan 22.40.84.06, selected with VK_LOADER_DRIVERS_SELECT=amd-vulkan64.json):
    • Feature answers, Close results and the no-extension run: pass in the committed test, with all 16 Close results matching and 0 validation errors.
    • Draws: this driver reports 128 bytes of push constants, so Runtime: compute path and graphics draws on Vulkan (D3D-3, D3D-4, milestone M1) #291 refuses every PSO there. The committed test checks that the PSO is refused, and says it drew nothing on that device. To test the draws anyway, I used a local build that was not committed: the pipeline layout declared no push-constant range, and the three test shaders declared no push block (they read none). In that build, at 88d0672, all 28 cases passed under validation, with 2x4, 4x2 and 4x4 clamped to 2x2 as D3D12 requires without the additional rates. 1c59ccc changes only recording and drops the out-of-range-combiner draw case, so I did not repeat that run.
    • Other GPU tests: d3d12_draw_test and d3d12_compute_test fail at PSO creation (Runtime: compute path and graphics draws on Vulkan (D3D-3, D3D-4, milestone M1) #291's push-constant refusal), and d3d12_vulkan_device_test fails at the D3D-14 check !device_push_constants_need_fallback (Tests: the D3D-14 push-constant check accepts GPUs with 128 bytes (Radeon 680M, Intel Arc) #516, since merged, fixes that check on main). This PR changes neither. d3d12_descriptor_test, d3d12_com_test, vk_command_replay_test and ctest pass.
  • Mutation check:
    • 88d0672: I disabled the depth-writing rule and the first-combiner fold in a scratch build. The test then failed exactly the six cases those rules cover and passed the rest.

    • 1c59ccc: I made seven wrong versions, and the tests caught every one:

      Wrong version What failed
      combiners outside the enum accepted the 3 combiner cases
      RSSetShadingRate allowed on compute and copy lists its 4 cases
      RSSetShadingRateImage allowed on compute and copy lists its 2 cases
      E_INVALIDARG instead of E_FAIL for the list type the 6 list-type cases
      the last invalid call decides both ordering cases
      bundles refused the 3 bundle cases
      the sum combiner refused the build (a static assert in d3d12_state_map_test)

AI-assisted (Claude Code, Claude Opus 5.5).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. The test shaders are GLSL written for this test.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

🤖 Generated with Claude Code

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

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

  • Clean RelWithDebInfo Ninja build of runtime/ (Vulkan SDK 1.4.363.0), then ctest --test-dir runtime/build --output-on-failure: 15/15 passed.
  • GPU tests with LEONIDA_VK_VALIDATION=1 and VK_LAYER_KHRONOS_validation: d3d12_com_test, d3d12_compute_test, d3d12_descriptor_test, d3d12_draw_test, d3d12_present_test, d3d12_vrs_test, d3d12_vulkan_device_test, vk_command_replay_test all exit 0, with 0 validation messages.

That includes the new d3d12_vrs_test, with every case passing. This GPU reports 256 bytes of push constants, so the draws really run here, and d3d12_draw_test and d3d12_compute_test pass too. That covers the draw path you could only check with an uncommitted build on the 128-byte RTX 3050.

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

@ezwinxdlmao

Copy link
Copy Markdown
Contributor Author

Rebased onto #291's merge candidate, Ryx69#6 (70178d3), as asked. The three commits are now 5190251, 571a694 and cfe02e4. Against today's main, the branch conflicts only in the two files the candidate itself conflicts in.

The candidate has the vertex-stride fix, so I dropped this PR's copy. One fix came out of the rebase: the standalone device used to replace enable13.pNext when it enabled VRS, which would have dropped the extension features the candidate chains there. It now adds to that chain.

With validation on, d3d12_vrs_test passes every case on the RTX 3050 with 0 validation errors. On the 680M, the feature answers and Close results match; it draws nothing there because PSOs are refused (#224). The description has the details.

ezwinxdlmao and others added 3 commits November 22, 2026 16:44
RSSetShadingRate and RSSetShadingRateImage (ID3D12GraphicsCommandList5) on
VK_KHR_fragment_shading_rate:
- Draws record the rate, the two combiners and the image. Reset and
  ClearState return them to 1x1, passthrough and no image. A rate outside
  D3D12_SHADING_RATE makes Close return E_INVALIDARG.
- Replay sets the rate at every draw as dynamic state.
- First combiner: the per-primitive rate is always 1x1, because
  SV_ShadingRate is not translated. The first combiner is folded on the CPU
  and Vulkan's primitive op is KEEP.
- Second combiner: with an image it maps to KEEP/REPLACE/MIN/MAX/MUL.
  Without one it is folded against 1x1.
- The R8_UINT shading-rate image becomes the rendering scope's
  fragment-shading-rate attachment, in GENERAL, with texels of the reported
  tile size. An image smaller than the render area is left out.
- Pixel shaders that write depth or stencil shade at 1x1, as D3D12
  requires.
- D3D12_OPTIONS6 and OPTIONS10 are answered from the GPU's
  fragment-shading-rate properties. The standalone device enables the
  extension where the GPU has it.

d3d12_vrs_test draws through the proxy with a pixel shader that writes the
pixel it ran at, and checks the coarse-pixel size read back for every rate,
both combiners, a four-quadrant shading-rate image, ClearState, and a
depth-writing pixel shader. It also compares OPTIONS6/OPTIONS10 with the
host D3D12 runtime on the same adapter, and checks that a device without
the extension reports and applies no VRS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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>
WARP, a tier-1 device, refuses RSSetShadingRateImage on every list type, so
the sentence measured on all three adapters no longer says bundles take
both calls. The tier-1 case is already listed under known gaps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ezwinxdlmao

Copy link
Copy Markdown
Contributor Author

Rebased onto main 003e7812: the three D3D-32 commits only (681eeadd, d3a3217f, e4f06d1d). The stacked runtime commits under them have since been merged.

Conflicts resolved against main:

  • texture_create_info() is now main's TextureCreateInfo struct, so the shading-rate attachment usage (single-sampled 2D R8_UINT) moved into its constructor through a shading_rate_images parameter. Both callers pass shading_rate.tile != 0.
  • The pipeline's dynamic states stay a vector, so the shading-rate state can be appended. Main's VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE (from 432d9ab) is added to it.
  • In Close/Reset, main's stop_recording()/record_into() stay, with this PR's error_ HRESULT in place of invalid_.
  • CMake: the d3d12_vrs_test block moved into main's glslangValidator block, ahead of its else().
  • The device and test-device feature chains keep main's swapchain_maintenance1 and shader-FMA additions alongside VK_KHR_fragment_shading_rate.

Tested on Windows 11 with MSVC 19.4x (VS 2022 Build Tools) and Ninja, on an NVIDIA GeForce RTX 3050 Laptop GPU, driver 32.0.16.2101:

AI-assisted (Claude Code).

@0xTehuti 0xTehuti 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.

Tested e4f06d1 on NVIDIA GeForce RTX 2070 SUPER, driver 595.79, Windows 11: pass

Environment & Toolchain

  • Host / OS: Windows 11
  • GPU: NVIDIA GeForce RTX 2070 SUPER (Driver 595.79 / Vulkan 1.4.321)
  • Compiler / Build System: MSVC 19.44 (Visual Studio 2022 Community), Ninja 1.13.2
  • Vulkan Toolchain: Vulkan SDK 1.4.363 (Vulkan-Headers 1.4.363, glslangValidator 16.6.0, VK_LAYER_KHRONOS_validation 1.4.363)
  • Python: Python 3.13 (compileall and make_game.py --help verified clean)

Test Verification

  1. d3d12_vrs_test.exe under LEONIDA_VK_VALIDATION=1:
    • Validation: 0 Vulkan validation messages / errors.
    • Feature Support / Query Parity: OPTIONS6 (Tier 2, Tile size 16, Additional rates supported: yes) and OPTIONS10 (Sum combiner: yes) match the host PC D3D12 runtime on the same adapter LUID.
    • Close() Status Contract: All 16 call sequences matched host D3D12 behavior (correct rejection with E_INVALIDARG for out-of-enum rates and combiners, E_FAIL on compute/copy command lists, S_OK for direct lists and bundles; first invalid call precedence verified).
    • Dynamic Shading Rate & Image Attachment: All 24 draw cases with VK_KHR_fragment_shading_rate matched on every pixel (default 1x1, dynamic rates 1x2 through 4x4, state resets, combiners, depth-writing pixel shader clamping to 1x1, and rate image attachment with 4-quadrant layout).
    • No-extension Fallback: All 3 draw cases without the extension verified to ignore VRS and shade at 1x1.
  2. d3d12_vulkan_device_test.exe:
    • Clean pass with 0 mismatches across format info, allocation info, and command stream execution.
  3. CTest Suite:
    • Core runtime, mock proxies, device filters, descriptors, state mapping all passing.

Endorsing #469's draw-hooked VRS implementation.

AI-assisted review.

@Gabzinnn12

Copy link
Copy Markdown

Tested e4f06d1 on AMD Radeon RX 7600 (RDNA3, Navi 33), AMD proprietary driver 26.11.2 (Vulkan 1.4.359), Windows 11 26300, MSVC 19.44, Vulkan SDK 1.4.363.0: pass for VRS, no regressions. As far as I can see this is the first AMD run of this PR (the earlier ones were an RTX 4090 and an RTX 2070 SUPER). It also covers a different path than the NVIDIA runs: this device reports tile 8 with no additional rates, where the RTX 2070 SUPER reports tile 16 with additional rates. AI-assisted (Claude Code); synthetic tests only, no game files.

d3d12_vrs_test (run directly, it isn't registered with ctest), with LEONIDA_VK_VALIDATION=1: exit 0 in 3 of 3 runs, 0 validation messages.

  • The device reports VK_KHR_fragment_shading_rate (pipeline 1, attachment 1).
  • The layer answers OPTIONS6 tier 2, tile 8, additional rates 0, sum combiner 1. The host D3D12 runtime on the same adapter answers the same.
  • All 16 Close cases match the expected HRESULTs: E_INVALIDARG for out-of-enum rates and combiners, E_FAIL on compute and copy lists, first invalid call wins.
  • All 24 rate, combiner and image cases are ok, including ClearState, Reset, depth-writing pixel shaders, and the image with override, min, max and sum.
  • With additional rates 0, 2x4 (0x6) and 4x4 (0xa) come out as 2x2.
  • The 3 cases without VK_KHR_fragment_shading_rate (tier 0) are ok. The test ends with "variable rate shading matched on every pixel of every case".

Full runtime ctest (RelWithDebInfo, VS 2022 generator, LEONIDA_VK_VALIDATION=1): 105/107 pass, 1 disabled (gameinput, no SDL3). Two tests fail: d3d12_vulkan_device (line 3403: allocation_mismatches <= 0, tiled 4x MSAA allocation sizes against the AMD host) and d3d12_draw (bundle blend pixel (0, 0): got 128,0,128,255, expected red 64, after fence signal 4 is not above 30). Both fail identically on this PR's base 003e7812, 3 of 3 runs with the same build setup. They are not from this PR. On current main 03b09a08, both pass on this card (154 tests, 0 failed).

Two notes:

  1. The branch conflicts with current main (git merge-tree: docs/d3d12_vulkan.md, runtime/CMakeLists.txt, runtime/shims/d3d12_com.cpp, runtime/shims/d3d12_com.h, runtime/shims/d3d12_vulkan_device.cpp, runtime/src/command_list.h, runtime/src/vulkan_replay.h, runtime/test/vk_test_device.h). Happy to rerun on the rebased head.
  2. d3d12_vrs_test is built but has no add_test in runtime/CMakeLists.txt, so CI never runs it. Registering it (perhaps skipping when the device lacks VK_KHR_fragment_shading_rate) would keep it from regressing after merge.

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.

4 participants