Skip to content

Runtime: SetSamplePositions and the centre sample pattern on VK_EXT_sample_locations - #848

Open
TheHoracio wants to merge 1 commit into
ItsNotRexed:mainfrom
TheHoracio:sample-positions
Open

TheHoracio wants to merge 1 commit into
ItsNotRexed:mainfrom
TheHoracio:sample-positions

Conversation

@TheHoracio

@TheHoracio TheHoracio commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in #634: #634 (comment) (with OMSetDepthBounds, #819)

Depends on: #831 (the PSO sample-quality change; this branch is #831's commit 8cbf2c2f on main 003e7812 plus one commit, 7e3f1f8e. Review that commit alone.)

Update after rebase: single-sample positions (Tier 2) are now programmable too, and D3D12_CENTER_MULTISAMPLE_PATTERN is implemented on the same path (every sample at the pixel centre), so #831 no longer refuses it where the extension is available. Details in the comment below.

What and why

SetSamplePositions was a logging stub. Engines use programmable sample positions for temporal and checkerboard techniques: they move the samples every frame, then resolve over several frames.

  • Recording (d3d12_com.cpp): the list keeps the positions and every draw carries them.
    • SetSamplePositions(0, 0, nullptr) returns to the standard pattern, and so do Reset and ClearState.
    • These are invalid calls, as D3D12 requires, and make Close return E_INVALIDARG: a count that is not a power of two ≤ 16, pixels other than 1 or 4, null positions, or a coordinate outside -8..7.
  • Pipeline (d3d12_vulkan_device.cpp): when the device has VK_EXT_sample_locations (VulkanDeviceInfo::sample_locations, its sampleLocationSampleCounts), a multisampled PSO at such a count gets sampleLocationsEnable and VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT. Without the extension nothing changes, and the positions are ignored.
  • Replay (vulkan_replay.h): each draw calls vkCmdSetSampleLocationsEXT.
    • With list positions: D3D12's 1/16 pixel from the centre becomes Vulkan's 0.5 + p/16, and four pixels are a 2x2 grid, ordered by pixel and then by sample in both APIs.
    • Without list positions: the standard pattern, from the spec's table of standard sample locations (standard_sample_locations). D3D's standard patterns are the same.
    • Positions for a count other than the PSO's fail the list with invalid_subresource. A 2x2 pattern at a count whose maxSampleLocationGridSize (vkGetPhysicalDeviceMultisamplePropertiesEXT) is below 2x2 is unsupported.
  • Depth: multisampled depth images get VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT where the extension covers their count, as the spec requires for a depth attachment drawn with custom locations.
  • Devices: the standalone device and vk_test_device.h enable the extension where present.
  • Docs: a "Sample positions" bullet in docs/d3d12_vulkan.md.

Not in this PR

Tested

AMD Radeon RX 7800 XT (RDNA3; sampleLocationSampleCounts 1/2/4/8, 2x2 grid), driver 32.0.32015.7029, Windows 11 Pro (26300), MSVC 14.44.35207, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo, LEONIDA_VK_VALIDATION=1.

d3d12_sample_positions_test (new, GPU), 0 validation errors. Every check draws one triangle into a multisampled target, then resolves and reads it back. The triangle's edge has a half-pixel end so that it crosses pixels at many offsets.

  • Standard tables: at 2x, 4x and 8x, a D3D12 device with dynamic locations gives exactly the image of a D3D12 device on the same GPU whose pipelines leave the positions to the driver.
  • Custom positions: with positions for every pixel, and with a 2x2 pattern, every resolved pixel equals the share of its samples on the inside of the edge, computed from the positions (±1 for resolve rounding).
  • Reset: SetSamplePositions(0, 0, nullptr) gives the standard image again, and that image differs from the custom one.
  • Invalid calls: pixels = 2 and a coordinate of 8 make Close return E_INVALIDARG. Positions for 2 samples on a 4x PSO fail the list with invalid_subresource.

Negative controls:

  • One 8x standard sample moved by 1/16 pixel: fails.
  • The 4x table transposed: fails. A first version of the test used a 45° edge through whole pixels and could not tell the 4x pattern from its mirror; the edge was changed so that it can.
  • The replay ignoring list positions: fails.

Regression: ctest 15/15. d3d12_msaa_draw_test, d3d12_draw_test and d3d12_compute_test pass with 0 validation errors.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. All fixtures are synthetic.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (N/A: runtime only.)

AI-assisted (Claude Code).

🤖 Generated with Claude Code

@TheHoracio

Copy link
Copy Markdown
Contributor Author

Note from the rebase onto 2aed144: the new base builds graphics pipelines later, from a saved Recipe (variants at draw time). The VkPipelineSampleLocationsStateCreateInfoEXT that create_graphics chained onto the multisample state through pNext was a local, so after the rebase it would have dangled when build() ran. It is now created in build() from the recipe's flag, next to the dynamic SAMPLE_LOCATIONS_EXT state, and the stride state stays last. d3d12_sample_positions_test passes again: the standard tables match the driver at 2x/4x/8x, custom and 2x2 positions match the computed coverage, and there are 0 validation errors. AI-assisted.

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

Tested f1f9a4c on AMD RDNA2 iGPU and RTX 5070: pass (first NVIDIA and RDNA2 runs; the PR was tested on an RX 7800 XT).

Windows 11 Home 10.0.26300, MSVC 14.44.35207, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo (scripts\build_runtime.ps1), LEONIDA_VK_VALIDATION=1, VK_LOADER_LAYERS_DISABLE=~implicit~. One PC, two GPUs, picked with VK_LOADER_DRIVERS_SELECT: AMD Radeon iGPU of a Ryzen 7 9800X3D (RDNA2, AMD 2026.04.17) and NVIDIA GeForce RTX 5070 (Blackwell, 617.42).

Run directly, exit 0 and no validation messages on both GPUs:

  • d3d12_sample_positions_test: standard tables match the driver at 3 counts; custom positions (52 partial pixels) and a 2x2 pattern match the computed coverage; reset, invalid calls and a mismatched count behave.
  • d3d12_msaa_draw_test: 4x draws resolved, 4096 pixels matched; 64 of 189 edge pixels are partial (red/blue mix); SampleMask 0x1 wrote one sample of four; 3 samples and a mismatched target refused.
  • d3d12_draw_test: five draws and a culled triangle matched all 4096 pixels ....

ctest: RTX 5070 33/33; AMD iGPU 32/33. The AMD failure is the base's OPTIONS19.SupportedSampleCountsWithNoOutputs mismatch (since fixed on main), not this PR. The branch is 356 commits behind main.

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

@evilclanker9000

Copy link
Copy Markdown
Contributor

I checked 4× sample index ordering at f1f9a4c75467cc04fa914f7c6ca9e6517b12eaef on RTX 5080 and AMD Radeon integrated graphics with Vulkan validation enabled. Using this PR's 64×64 red-triangle/blue-clear fixture, I drew with four separate graphics PSOs, setting SampleMask to 1, 2, 4 or 8. A fifth draw used all four bits. I compared the driver's standard pattern, the runtime's dynamic standard pattern and a custom pattern submitted through SetSamplePositions(4,1,…) with the same four positions in reverse order.

On each GPU, the driver's default and the runtime's dynamic standard image were byte-identical for all five masks. For each individual mask, all 4,096 resolved pixels also agreed (±1 UNORM) with an integer edge test at the corresponding standard offset from pixel centre, in index order (−2,−6), (6,−2), (−6,2), (2,6) sixteenths. No sample lay exactly on the diagonal.

Reversing the positions changed 60, 20, 20 and 60 edge pixels for masks 1, 2, 4 and 8 respectively, exactly as predicted. Mask 15 gave a byte-identical image after reversal. At (63,0) mask 1 changed from RGBA (64,0,191,255) to (0,0,255,255). Both adapters produced the same counts with no validation errors. This establishes the mask-bit/position association at 4× through the recorded draw and resolve; the 16× table remains untested here.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Separate 1× finding at f1f9a4c75467cc04fa914f7c6ca9e6517b12eaef, relevant to any future Tier 2 advertisement. The spec includes programmable single-sample positions at Tier 2. Both native D3D12 adapters here report Tier 2. Their Vulkan sampleLocationSampleCounts also includes 1× (RTX 5080: 0x1f; integrated Radeon: 0x0f). DirectX programmable sample positions, hardware tiers

I reused the PR's 64×64 sloping triangle with a 1× RGBA8 color target, no depth, and copied the target directly to readback. Three draws used the standard pixel centre, then SetSamplePositions(1,1,…) at (−6,−6) and (+6,+6) sixteenths. The CPU edge test used each requested position; no sample hit the diagonal exactly.

Red pixels out of 4,096 Centre (−6,−6) (+6,+6)
CPU reference 1,520 1,562 1,478
Native D3D12, RTX 5080 and Radeon 1,520 1,562 1,478
This PR's Vulkan runtime, both GPUs 1,520 1,520 1,520

Each native hardware image matched the CPU reference byte-for-byte. The PR returned the original centre image for both shifts, with 42 incorrect edge pixels each. As a diagnostic local A/B, removing only the samples > 1 restriction on graphics_.sample_locations made all three images byte-exact on both Vulkan GPUs with validation enabled and no errors. This tests color only; depth-attachment handling has a separate multisample guard. WARP agreed for the centre but diverged for custom positions, so the native references used here are the two physical adapters.

@TheHoracio TheHoracio changed the title Runtime: SetSamplePositions on VK_EXT_sample_locations (ID3D12GraphicsCommandList1) Runtime: SetSamplePositions and the centre sample pattern on VK_EXT_sample_locations Nov 22, 2026
@TheHoracio

Copy link
Copy Markdown
Contributor Author

@Vonsek, thanks for both checks. The index-order result is good to have on two GPUs. Rebased onto main 003e7812 (on top of #831's 8cbf2c2f) as 7e3f1f8e, with your 1× finding fixed:

  • 1× positions: the samples > 1 restriction is gone. Every PSO at a count in sampleLocationSampleCounts gets dynamic sample locations, 1× included, and 1× depth images at such a count get SAMPLE_LOCATIONS_COMPATIBLE_DEPTH. In the test:

    • the 1× standard image is byte-identical to the driver's;
    • your (−6, −6) and (+6, +6) shifts match the CPU coverage of the requested position, and differ from the centre image.

    With the old samples > 1 rule put back, the test fails at the first 1× shift.

  • D3D12_CENTER_MULTISAMPLE_PATTERN: native D3D12 accepts it for PSOs: RX 7800 XT, the Radeon iGPU, Basic Render Driver and WARP all return S_OK for 0, standard and centre, and E_INVALIDARG for quality 1. It is now implemented here, with every sample at (0.5, 0.5) when the list sets no positions. A 4× centre-pattern draw matches the coverage computed for four centre samples, with no partial pixels. With the centre flag ignored, the test fails. Without the extension at that count it is still refused with a log.

  • d3d12_sample_positions_test is now in ctest (SKIP_RETURN_CODE 77).

  • Run: RX 7800 XT, 26.10.1, LEONIDA_VK_VALIDATION=1, 0 validation errors.

  • Regression: ctest -j4 103/105. The two failures (d3d12_vulkan_device line 3403 and d3d12_draw's VUID-vkDestroyPipeline-pipeline-00765) are the same on clean main, and d3d12_draw_test shows no new VUID with 1× sample locations on.

OPTIONS2.ProgrammableSamplePositionsTier stays as main reports it; with 1× in, TIER_2 could be reported where the extension covers 1/2/4/8 with a 2×2 grid. I've left that as a follow-up. AI-assisted.

@TheHoracio

Copy link
Copy Markdown
Contributor Author

#831 is merged (03d81593), so this is now one commit on main 49475796: e43d4cb5. Conflicts were keep-both: the X-list (vkCmdSetSampleLocationsEXT next to vkQueueBindSparse) and the device-extension setup (VK_EXT_sample_locations next to main's custom border colour) in d3d12_vulkan_device.cpp and vk_test_device.h. Full ctest: 117/117 pass (d3d12_queue_wait_timeout timed out once while an LLVM build loaded the machine, then passed 3/3 alone in 0.2 s), 0 validation errors. The Depends on: #831 line no longer applies. Windows 11 Pro 26300, RX 7800 XT + Radeon iGPU (AMD Adrenalin), MSVC 19.4x, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1. AI-assisted.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Windows + GPU check: AMD Radeon RX 9070 (RDNA4), Windows 11, MSVC Release, Vulkan SDK 1.4.363. Tested head e43d4cb merged onto main 4947579 (clean merge), full build, full ctest -C Release -j 4: 100% tests passed out of 117 | new failures vs main: none. The main baseline on the same machine passes 116/116, so nothing regresses here.

AI-assisted (Claude Code); runs on my machine.

@TheHoracio

Copy link
Copy Markdown
Contributor Author

@Dom-in, thanks for the RX 9070 run. Rebased onto main ec8ec4c9 (round 40) as 45f7e4b1. All the conflicts kept both sides: d3d12_bundle_test next to d3d12_sample_positions_test, shader_output_layer/shader_output_viewport_index next to sample_locations in VulkanDeviceInfo, and the colour-attachment format callback next to set_sample_location_grid_2x2. The one exception was main's removal of the unused stencil_ref_set_, which stays removed. Full ctest -j 8 with LEONIDA_VK_VALIDATION=1: 122/122 pass. Windows 11 Pro 26300, RX 7800 XT + Radeon iGPU (AMD 26.10.1), MSVC 19.4x, Vulkan SDK 1.4.363.0. AI-assisted.

…ample_locations

With the extension, PSOs at its sample counts (1x included: D3D12's Tier 2
programs single-sample positions too) take dynamic sample locations; each
draw sets the list's SetSamplePositions (one pixel or a 2x2 grid), the
D3D12_CENTER_MULTISAMPLE_PATTERN (every sample at the pixel centre), or the
standard pattern. Invalid calls make Close return E_INVALIDARG; positions
for another count than the PSO's fail the list. Depth images at those counts
get SAMPLE_LOCATIONS_COMPATIBLE_DEPTH. The standalone and test devices
enable the extension where present.

d3d12_sample_positions_test (in ctest) compares the standard tables with the
driver's own at 1x, 2x, 4x and 8x, and custom, 2x2, single-sample and centre
positions with coverage computed from the positions.

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

Copy link
Copy Markdown
Contributor Author

Rebased onto main f847748c as dd477640. One real change came with it.

  • Draw: main's D3D-289 inlined it, so the per-draw std::vector<std::int8_t> sample_positions is now an std::array<std::int8_t, 2 * 16 * 4>. That covers 16 samples × 4 pixels, D3D12's limit.
  • SetSamplePositions: it still validates the whole set before keeping any of it.
  • Constant names: the quality checks use main's DXGI_*_MULTISAMPLE_QUALITY_PATTERN names.
  • Docs: the Multisampling/Sample positions bullets follow main's geometry-shader bullet. The refused list no longer names MSAA or the geometry stage, both supported now.

Full ctest -j 8: 133/133. Windows 11 Pro 26300, RX 7800 XT + Radeon iGPU (AMD 26.10.1), MSVC 19.4x, Vulkan SDK 1.4.363.0, LEONIDA_VK_VALIDATION=1. AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Ran this PR's head (dd477640, built as is, not merged onto current main) on the two GPUs the other reports lack, Windows 11, MSVC 14.51, Vulkan SDK, RelWithDebInfo, no validation layer installed here.

ctest -R "vulkan_device_filter|d3d12_vulkan_device|d3d12_draw|d3d12_msaa_draw|d3d12_sample_positions": 5/5 pass on both.

  • RTX 4080 SUPER: VK_EXT_sample_locations rev 1, sampleLocationSampleCounts 1/2/4/8/16, variableSampleLocations true, sub-pixel bits 4.
  • Intel UHD 770 (32.0.101.6129): VK_EXT_sample_locations rev 1, counts 1/2/4/8, variableSampleLocations false, sub-pixel bits 4. Per the spec, with variableSampleLocations false, draws in one render pass must use the same locations, so per-draw SetSamplePositions changes inside a pass are not guaranteed there. The test passes anyway, and the centre pattern is a constant, so it is not affected.

Negative control: graphics_.center_samples = false; at d3d12_vulkan_device.cpp:2714 (centre quality treated as the standard pattern). d3d12_sample_positions then fails on both GPUs at compare_coverage(..., centre, partial_centre) (test line 386): edge pixels come out partially covered (e.g. 128,0,128 instead of all-or-nothing). Restored, it passes again.
AI-assisted.

const std::uint32_t samples = info.samples;
// Custom sample locations on a depth attachment need this (VK_EXT_sample_locations).
if ((format.aspects & VK_IMAGE_ASPECT_DEPTH_BIT) && (device_.info.sample_locations & samples))
info.flags |= VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT;

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.

[P2] Preserve sample-location state in depth-image barriers. This enables VK_IMAGE_CREATE_SAMPLE_LOCATIONS_COMPATIBLE_DEPTH_BIT_EXT, which makes a depth image's contents depend on the last sample pattern. Vulkan requires explicit layout-transition barriers for these images to carry the matching VkSampleLocationsInfoEXT in pNext; the current VulkanContext barrier path leaves pNext null, so a transition poisons the contents as if the old layout were undefined. Please thread the locations through the barrier/layout tracker, or avoid setting this flag on images that path handles. Vulkan layout-transition rule.

VkPipelineSampleLocationsStateCreateInfoEXT locations{
VK_STRUCTURE_TYPE_PIPELINE_SAMPLE_LOCATIONS_STATE_CREATE_INFO_EXT};
locations.sampleLocationsEnable = VK_TRUE;
locations.sampleLocationsInfo.sType = VK_STRUCTURE_TYPE_SAMPLE_LOCATIONS_INFO_EXT;

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.

[P2] Initialize a valid sampleLocationsInfo for dynamic pipelines. sampleLocationsEnable is true, so sampleLocationsInfo must remain a valid VkSampleLocationsInfoEXT even though VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT supplies the draw-time pattern. Setting only sType leaves sampleLocationsPerPixel at 0 and the grid/count empty, violating VUID-VkSampleLocationsInfoEXT-sampleLocationsPerPixel-01526 and sampleLocationsCount-01527. Initialize a valid 1×1 placeholder using this PSO's sample count and a matching location array. Sample-location structure requirements.

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.

6 participants