Skip to content

Runtime: CreateSampler2 on the Vulkan device (D3D-28, part 2) - #538

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
MrCheese5:d3d28-sampler2
Nov 22, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
MrCheese5:d3d28-sampler2

Conversation

@MrCheese5

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D-28 (part 2), in #374

Depends on: #472

What and why

ID3D12Device11::CreateSampler2 on the Vulkan-backed device, as a wrapper over CreateSampler. CreatePlacedResource1/2 and CreateHeap1, the rest of part 2, still wait for #286.

  • No flags: the same S# as CreateSampler.
  • D3D12_SAMPLER_FLAG_UINT_BORDER_COLOR: reads the union as integers. (0,0,0,0), (0,0,0,1) and (1,1,1,1) get the S#'s transparent-black, opaque-black and opaque-white types. The hardware reads those types by the image's format, so they serve integer textures too, and radv gives VK_BORDER_COLOR_INT_* and _FLOAT_* the same SQ_TEX_BORDER_COLOR. Any other colour is a table colour, still transparent black like CreateSampler's custom colours.
  • D3D12_SAMPLER_FLAG_NON_NORMALIZED_COORDINATES: sets FORCE_UNNORMALIZED (word 0, bit 15) and changes nothing else.
  • CheckFeatureSupport: now answers D3D12_OPTIONS17 with NonNormalizedCoordinateSamplersSupported = TRUE and ManualWriteTrackingResourceSupported = FALSE, as the RTX 4090 and WARP do. Vulkan has non-normalized samplers in core, with the same restrictions. The field is in OPTIONS17, not OPTIONS19.

Refusals, measured on the host. I probed the PC runtime on an RTX 4090 and on WARP, with the debug layer off. For a description it refuses, the runtime leaves the slot as it was, and the layer now does the same. Refused:

  • a null description;
  • flag bits other than the two;
  • a non-normalized sampler with unequal min/mag filters, a linear mip filter, anisotropy or a comparison filter;
  • a non-normalized sampler whose AddressU or AddressV is not CLAMP/BORDER;
  • a non-normalized sampler with MinLOD, MaxLOD or MipLODBias other than 0.

Accepted: AddressW is free (as in Vulkan), -0 counts as 0, MINIMUM/MAXIMUM filters work, and MaxAnisotropy/ComparisonFunc on a point filter are ignored. The table is in docs/d3d12_vulkan.md §3.2, next to part 1's.

Not measured: what the runtime does when the feature is unreported. Both devices here report it, so there was nothing to probe; the layer now reports it too.

Tested

RTX 4090, driver 621.14, Windows 11 build 26200, MSVC 19.44, Vulkan SDK 1.4.363.0:

  • cmake --build runtime/build, then ctest --test-dir runtime/build: 19/19 pass.
  • New d3d12_sampler2_test: 10 descriptions without flags, 6 border colours and 3 non-normalized samplers match CreateSampler byte for byte (with bit 15 set for the non-normalized ones). The 22 edge cases match the host's PC runtime live, and the D3D12_OPTIONS17 answer is checked. It passes with and without LEONIDA_VK_VALIDATION=1, with no validation errors.
  • d3d12_method_variants_test, d3d12_vulkan_device_test, d3d12_descriptor_test and d3d12_com_test still pass with LEONIDA_VK_VALIDATION=1.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • 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

@evilclanker9000

Copy link
Copy Markdown
Contributor

UINT opaque-white border reads back as zero on RTX 5080 at 23ca4c172374507868bb4a6d80f60c03160e4e68 (driver 616.92). The checked-in d3d12_sampler2_test passes on the same build.

I adapted the existing descriptor readback fixture locally. It uploads a synthetic 8×8 DXGI_FORMAT_R8G8B8A8_UINT texture via the D3D12 queue and calls the runtime's ID3D12Device11::CreateSampler2 twice. Both descriptions use point sampling, U=BORDER, V/W=CLAMP, LOD 0 and UINT_BORDER_COLOR. The two border values are (0,0,0,0) and (1,1,1,1). A Vulkan compute shader looks up the actual heap T#/S# in the runtime's bindless set, samples with usampler2D at explicit LOD 0, and stores one full uint per invocation. The 8×8 grid samples texel centers; its right half is displaced +1 normalized unit in U. The selected channel cycles through RGBA across each four-pixel half-row.

Sampler 32 in-range channel reads 32 out-of-range channel reads
UINT transparent black 32 matched upload 32 returned 0 as expected
UINT opaque white 32 matched upload 32 returned 0, expected 1
UINT opaque white, reviewer-only Vulkan enum control 32 matched upload 32 returned 1

For the control I changed only the two Vulkan border enumerants from FLOAT to their INT counterparts before vkCreateSampler, in a separately linked copy of the device object. Both the shader and its input stayed the same. The runtime passes just the four S# words to bind_sampler, caches by that key, and reconstructs VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE. The cache also needs to distinguish float and UINT requests that encode the same S# if both can be bound. Sampler creation and cache · Border mapping

As a separate check, ordinary normalized CLAMP and CreateSampler2(NON_NORMALIZED_COORDINATES) each matched all 64 predicted RGBA8_UNORM texels at coordinates (x+0.5, y+0.5). Their outputs differed at 63/64 positions. All three runs reported zero validation errors. Descriptor creation, lookup and texture upload use the runtime; dispatch uses the descriptor test's synthetic Vulkan compute pipeline.

@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested 23ca4c1 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_sampler2_test exit 0: "10 plain descriptions, 6 border colours and 3 non-normalized samplers match CreateSampler; 22 edge cases match the PC runtime"
  • d3d12_method_variants_test exit 0

MrCheese5 added a commit to MrCheese5/Leonida that referenced this pull request Nov 22, 2026
… review)

A UINT opaque-white border read back as 0 (Vonsek, on an RTX 5080). The S# has
no integer border types, since the hardware reads them by the image's format.
So bind_sampler gave Vulkan VK_BORDER_COLOR_FLOAT_*, which an integer image
reads as 0. The float and UINT samplers also shared one S# key and so one
VkSampler.

An integer border is now marked in the S#: BORDER_COLOR_PTR = 0x800
(gfx10::kIntegerBorderPtr), a field the hardware reads only for table colours.
That gives it its own key, and vulkan_sampler picks VK_BORDER_COLOR_INT_*.

d3d12_descriptor_test runs Vonsek's case: an R8G8B8A8_UINT texture sampled past
its edge through UINT black and white border samplers reads 0 and 1, and a
float white border reads 255. The case fails with the float enums on an RTX
4090. gfx10_descriptors_test and d3d12_sampler2_test check the mark.

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

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed report, Vonsek. Your diagnosis was right on both counts, and fdf59bb fixes both.

  • FLOAT enums: bind_sampler rebuilt the sampler from the four S# words, so it always got VK_BORDER_COLOR_FLOAT_*, and an integer image reads that border as 0.
  • Shared cache entry: the UINT and float white samplers encoded the same S#, so they shared one cache key and one VkSampler.

Fix: a UINT border is now marked in the S# with BORDER_COLOR_PTR = 0x800 (gfx10::kIntegerBorderPtr). The hardware reads that field only for table colours (SQ_TEX_BORDER_COLOR_REGISTER), so it's free with the three built-in types. The mark gives the UINT sampler its own key in the descriptor hash table, and vulkan_sampler turns it into VK_BORDER_COLOR_INT_*. Custom UINT colours stay table colours (still transparent black, same as float custom colours) with no mark for now.

Your case is in d3d12_descriptor_test now: an 8×8 R8G8B8A8_UINT texture, sampled one tile to the right in U through CreateSampler2, with point sampling, U=BORDER, V/W=CLAMP and UINT_BORDER_COLOR. A usampler2D path in cs_sample.comp reads it.

  • The UINT (0,0,0,0) border reads 0 and (1,1,1,1) reads 1, in all 256 channel reads each.
  • A float white control on the UNORM texture reads 255, and its S# must differ from the UINT white one only in the mark.

With vulkan_sampler forced back to the FLOAT enums, the test fails on an RTX 4090 the way you saw (UINT opaque white border, texel 0 channel 0: got 0, want 1).

Retested on an RTX 4090, driver 621.14, Windows 11 26200, MSVC 19.44:

  • ctest passes 23/23.
  • With LEONIDA_VK_VALIDATION=1, these pass with no validation errors: d3d12_descriptor_test, d3d12_sampler2_test, gfx10_descriptors_test, d3d12_method_variants_test, d3d12_vulkan_device_test and d3d12_com_test.

docs/d3d12_vulkan.md §3.2 explains the mark.

(AI-assisted reply.)

MrCheese5 and others added 3 commits November 22, 2026 01:03
CreateCommittedResource1/2/3, CreateCommandQueue1 and GetCopyableFootprints1
as thin wrappers over the existing methods. D3D12_RESOURCE_DESC1 drops to
D3D12_RESOURCE_DESC; the initial state or barrier layout is unused, because new
textures start UNDEFINED and move on first use. The newer arguments are answered
as the PC runtime answers them (measured on an RTX 4090, driver 621.14):

- a protected resource session: E_INVALIDARG (no session support reported);
- a sampler-feedback mip region: E_INVALIDARG on textures, ignored on buffers;
  GetCopyableFootprints1 returns its failure answer for such a texture;
- a buffer's initial layout other than UNDEFINED: E_INVALIDARG;
- a castable count with a null list: no list;
- a castable list: E_INVALIDARG, since it needs RelaxedFormatCastingSupported,
  which the layer does not report (D3D12_OPTIONS12 is unanswered);
- CreateCommandQueue1's creator ID: ignored.

d3d12_method_variants_test compares each variant with its base method, runs the
edge cases and, when the host has a D3D12 device, compares each answer with the
PC runtime live. Thanks to ezwinxdlmao for the probe notes in ItsNotRexed#156.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CreateSampler2 wraps CreateSampler. The UINT border colour maps (0,0,0,0),
(0,0,0,1) and (1,1,1,1) to the S#'s border types, and anything else to the
table. Non-normalized coordinates set FORCE_UNNORMALIZED. A description the PC
runtime refuses (unknown flags; a non-normalized sampler with an unequal
min/mag filter, a linear mip filter, anisotropy, comparison, a U/V address mode
other than clamp/border, or a non-zero LOD or bias) leaves the slot as it was,
as the runtime does on an RTX 4090 and on WARP. The layer now answers
D3D12_OPTIONS17 with NonNormalizedCoordinateSamplersSupported, as both report.

d3d12_sampler2_test compares the S# bytes with CreateSampler's and the 22
edge cases with the host's PC runtime live.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… review)

A UINT opaque-white border read back as 0 (Vonsek, on an RTX 5080). The S# has
no integer border types, since the hardware reads them by the image's format.
So bind_sampler gave Vulkan VK_BORDER_COLOR_FLOAT_*, which an integer image
reads as 0. The float and UINT samplers also shared one S# key and so one
VkSampler.

An integer border is now marked in the S#: BORDER_COLOR_PTR = 0x800
(gfx10::kIntegerBorderPtr), a field the hardware reads only for table colours.
That gives it its own key, and vulkan_sampler picks VK_BORDER_COLOR_INT_*.

d3d12_descriptor_test runs Vonsek's case: an R8G8B8A8_UINT texture sampled past
its edge through UINT black and white border samplers reads 0 and 1, and a
float white border reads 255. The case fails with the float enums on an RTX
4090. gfx10_descriptors_test and d3d12_sampler2_test check the mark.

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

Copy link
Copy Markdown
Contributor Author

Note: after rebasing onto main (22590c7) the fix is b3e71d7 (previously fdf59bb), with no change to its content. ctest passes 24/24, and the six D3D12/GFX10 GPU tests pass with LEONIDA_VK_VALIDATION=1 on the rebased head. (AI-assisted.)

@ItsNotRexed
ItsNotRexed merged commit 536ba3a into ItsNotRexed:main Nov 22, 2026
8 of 9 checks passed
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