Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,16 @@ foreach(backend IN ITEMS warp hardware)
ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1;X2W_TEST_SUBMIT_FENCE_FAIL=1")
endforeach()

# A sampler-heap-indexed title root signature gets a sampler heap before it is set (debug layer ID 1419).
add_executable(startup_root_sampler_heap tests/startup_root_sampler_heap.cpp)
target_compile_features(startup_root_sampler_heap PRIVATE cxx_std_17)
target_link_libraries(startup_root_sampler_heap PRIVATE d3d12_xs_shim d3d12 dxgi)
foreach(backend IN ITEMS warp hardware)
add_test(NAME startup_root_sampler_heap_${backend} COMMAND startup_root_sampler_heap ${backend})
set_tests_properties(startup_root_sampler_heap_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77
ENVIRONMENT "X2W_D3D12_PERMISSIVE=1;X2W_D3D12_LIST_NOOP=1;X2W_D3D12_RENDER=1;X2W_D3D12_DEBUG=1")
endforeach()

add_executable(startup_nv12_draw tests/startup_nv12_draw.cpp tests/desktop_gpu.cpp)
target_compile_features(startup_nv12_draw PRIVATE cxx_std_17)
target_include_directories(startup_nv12_draw PRIVATE src)
Expand Down
9 changes: 9 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -10652,3 +10652,12 @@ Command-list slot 42 records the address of a root parameter declared as a CBV,
cmake --build --preset dev --target startup_stream_consumer
ctest --preset dev --no-tests=error -R "^startup_root_cbv_(warp|hardware)$" -V
```

## Default sampler heap for sampler-heap-indexed root signatures

A title root signature flagged `D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED` is set by command-list slot 34 with a one-entry default sampler heap bound first when the list has none, so the debug layer no longer reports message ID 1419 for it. The fixture (`tests/startup_root_sampler_heap.cpp`) is synthetic only: no title data, a root signature with no parameters, and a plain desktop list as the negative control that must produce the message. It checks the flagged root signature through the stand-in (0 messages), an unflagged one (0 messages) and the device state. The translated-draw and pipeline-creation occurrences of the message are not covered. Details are in [startup-runtime.md](startup-runtime.md#sampler-heap-indexed-root-signatures).

```powershell
cmake --build --preset dev --target startup_root_sampler_heap
ctest --preset dev --no-tests=error -R "^startup_root_sampler_heap_(warp|hardware)$" -V
```
8 changes: 8 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ by reading a render log. The device prints the counts by cause value, so
checks without a device that each cause lands in the slot printed under its label, and that the
header's names follow the same order. The control swaps the buffer and token slots and must fail.

## Sampler-heap-indexed root signatures

The desktop command list never receives the heaps a title sets with list slot 32: the stand-in only records the title's sampler source, and a translated draw binds heaps of its own. A title root signature created with `D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED` (`0x800`) was therefore set by slot 34 on a list with no sampler heap, which the debug layer reports as message ID 1419. A recent log set counted 394 of these (348 from the title's own root signatures, 43 on translated draws, 2 at translated pipeline creation).

The stand-in records the root signature flags in `CreateRootSignature`. When slot 34 sets a flagged root signature on a list that has no sampler heap bound, it binds one shared one-entry shader-visible sampler heap (the border sampler) first, keeping the view heap a translated draw already bound, and logs one line per run start (at most four): `sampler-heap-indexed root signature set with no sampler heap bound: default sampler heap bound first`. A sampler heap already bound on the list is never replaced, and a translated draw rebinds its own heaps as before.

Open scope: the translated root signature is created with no flags, so the 43 translated-draw and 2 pipeline-creation messages are not explained by this change and are not claimed fixed. `startup_root_sampler_heap_*` counts the ID 1419 messages through a debug layer message callback and skips (77) when the debug layer cannot report the control case.

## Root constant buffer views

Command-list slot 42 (desktop `SetGraphicsRootConstantBufferView` plus four) records the title address for a root
Expand Down
30 changes: 30 additions & 0 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,36 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
return drawn;
}

// One shared 1-entry sampler heap per device for lists that set a sampler-heap-indexed root signature with no
// sampler heap bound. Each heap lives for the lifetime of the process so submitted lists may still reference it.
SRWLOCK default_sampler_lock = SRWLOCK_INIT;
std::unordered_map<Wrapper*, x2w_startup::ComPtr<ID3D12DescriptorHeap>>& default_sampler_heaps()
{
static std::unordered_map<Wrapper*, x2w_startup::ComPtr<ID3D12DescriptorHeap>> map;
return map;
}

// Called before the real SetGraphicsRootSignature of a root signature that indexes the sampler heap. A
// sampler heap already bound on this list (the title's snapshot, or a translated draw's) is kept.
void ensure_sampler_heap_for_root(Wrapper* self, ID3D12GraphicsCommandList* list)
{
Pass& pass = pass_of(self);
Wrapper* device = self->own().list_device;
if (pass.bound_sampler_heap || !device || !device_of(device)) return;
x2w_startup::ComPtr<ID3D12DescriptorHeap> fallback;
AcquireSRWLockExclusive(&default_sampler_lock);
auto& slot = default_sampler_heaps()[device];
if (!slot) x2w_startup::create_default_sampler_heap(device_of(device), slot.GetAddressOf());
fallback = slot;
ReleaseSRWLockExclusive(&default_sampler_lock);
ID3D12DescriptorHeap* views = pass.heap_set && device == draw_heap_device ? draw_heap : nullptr;
if (!x2w_startup::bind_default_sampler_heap(list, views, pass.bound_sampler_heap, fallback.Get())) return;
pass.bound_sampler_heap = fallback.Get();
static LONG reports;
if (first_calls(reports, 4))
log("x2w-d3d12 sampler-heap-indexed root signature set with no sampler heap bound: default sampler heap bound first");
}

// List slot 32, SetDescriptorHeaps. Track the title's shader-visible sampler
// source while translated draws use their own physical descriptor heaps.
void __stdcall list_set_descriptor_heaps(Wrapper* self, UINT count, void* const* heaps)
Expand Down
30 changes: 29 additions & 1 deletion src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,9 @@ HRESULT __stdcall device_residency(Wrapper* self, UINT count, void* const* objec
// when the desktop GPU finishes, and the presented surface is read back and shown in the window.
// Everything else on a list is still dropped.
void release_targets(Wrapper* self);
void reset_pass(Wrapper* self);
void discard_draw_sampler_heaps(Wrapper* self);
void ensure_sampler_heap_for_root(Wrapper* self, ID3D12GraphicsCommandList* list);
void use_as_target(Wrapper* self, ID3D12Resource* resource);
bool is_container(const void* p);
bool shader_dump_dir(wchar_t* out, DWORD size);
Expand Down Expand Up @@ -1197,6 +1199,7 @@ HRESULT __stdcall list_reset(Wrapper* self, void* allocator)
ReleaseSRWLockExclusive(&allocator_lock);
}
if (SUCCEEDED(hr)) {
reset_pass(self);
self->own().list_ready = false;
discard_draw_sampler_heaps(self);
if (draw_dump_frame()) draw_dumps().discard(static_cast<ID3D12CommandList*>(self->own().real));
Expand Down Expand Up @@ -1415,6 +1418,20 @@ std::unordered_map<const Wrapper*, std::vector<RootParameter>>& root_infos()
static std::unordered_map<const Wrapper*, std::vector<RootParameter>> map;
return map;
}
std::unordered_map<const Wrapper*, UINT>& root_flag_map()
{
static std::unordered_map<const Wrapper*, UINT> map;
return map;
}
// The D3D12_ROOT_SIGNATURE_FLAGS a title root signature was created with (0 when unknown).
UINT root_flags_of(const Wrapper* root)
{
AcquireSRWLockShared(&root_lock);
const auto found = root_flag_map().find(root);
const UINT flags = found == root_flag_map().end() ? 0 : found->second;
ReleaseSRWLockShared(&root_lock);
return flags;
}
const std::vector<RootParameter>* root_info(const Wrapper* root)
{
AcquireSRWLockShared(&root_lock);
Expand Down Expand Up @@ -1442,10 +1459,12 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v
Wrapper* w = wrap(root, root_signature_kind);
note_root_blob(w, blob, size);
std::vector<RootParameter> parameters;
UINT flags = 0;
ID3D12VersionedRootSignatureDeserializer* deserializer = nullptr;
if (SUCCEEDED(D3D12CreateVersionedRootSignatureDeserializer(blob, size, IID_PPV_ARGS(&deserializer)))) {
const D3D12_VERSIONED_ROOT_SIGNATURE_DESC* desc = nullptr;
if (SUCCEEDED(deserializer->GetRootSignatureDescAtVersion(D3D_ROOT_SIGNATURE_VERSION_1_1, &desc)) && desc) {
flags = desc->Desc_1_1.Flags;
for (UINT i = 0; i < desc->Desc_1_1.NumParameters; ++i) {
const auto& parameter = desc->Desc_1_1.pParameters[i];
RootParameter info{parameter.ParameterType, 0, 0};
Expand All @@ -1470,6 +1489,7 @@ HRESULT __stdcall device_create_root_signature(Wrapper* self, UINT node, const v
}
AcquireSRWLockExclusive(&root_lock);
root_infos()[w] = std::move(parameters);
root_flag_map()[w] = flags;
ReleaseSRWLockExclusive(&root_lock);
*out = w;
return S_OK;
Expand Down Expand Up @@ -1689,6 +1709,10 @@ void release_targets(Wrapper* self)
transition(static_cast<ID3D12GraphicsCommandList*>(self->own().real), known.first, known.second, D3D12_RESOURCE_STATE_COMMON);
pass = Pass{};
}
void reset_pass(Wrapper* self)
{
pass_of(self) = Pass{};
}

// List slot 20, IASetPrimitiveTopology. 0x11 is a console-only rectangle list (no desktop draw yet).
void __stdcall list_topology(Wrapper* self, UINT topology)
Expand Down Expand Up @@ -1717,8 +1741,12 @@ void __stdcall list_graphics_root(Wrapper* self, void* root)
std::fill_n(pass.root_address, 16, 0);
pass.root = params;
pass.srv_bound = 0;
if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real)
if (auto* list = recording(self); list && w && w->own().kind == root_signature_kind && w->own().real) {
// The desktop list never receives the title's heaps, so a sampler-heap-indexed root signature
// would be set with no sampler heap bound (debug layer ID 1419): give the list a default one first.
if (x2w_startup::root_indexes_sampler_heap(root_flags_of(w))) ensure_sampler_heap_for_root(self, list);
list->SetGraphicsRootSignature(static_cast<ID3D12RootSignature*>(w->own().real));
}
}
DXGI_FORMAT view_format_of(u64 desktop_handle, ID3D12Resource* target)
{
Expand Down
34 changes: 34 additions & 0 deletions src/startup_stubs/translated_sampler_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,38 @@ inline void bind_translated_heaps(ID3D12GraphicsCommandList* list, ID3D12Descrip
list->SetDescriptorHeaps(samplers ? 2 : 1, heaps);
}

// D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED, spelled out so older headers build.
constexpr UINT root_flag_sampler_heap_indexed = 0x800;

inline bool root_indexes_sampler_heap(UINT root_flags)
{
return (root_flags & root_flag_sampler_heap_indexed) != 0;
}

// One shader-visible sampler (the border sampler) for a list that sets a sampler-heap-indexed root
// signature while no sampler heap is bound: the debug layer rejects that call (message ID 1419).
inline HRESULT create_default_sampler_heap(ID3D12Device* device, ID3D12DescriptorHeap** out)
{
const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 1,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
const HRESULT hr = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(out));
if (SUCCEEDED(hr)) {
const auto sampler = border_sampler();
device->CreateSampler(&sampler, (*out)->GetCPUDescriptorHandleForHeapStart());
}
return hr;
}

// Binds fallback as the list's sampler heap, keeping the view heap already bound (null: none).
// A sampler heap that is already bound is never replaced: returns false and records nothing.
inline bool bind_default_sampler_heap(ID3D12GraphicsCommandList* list, ID3D12DescriptorHeap* views,
ID3D12DescriptorHeap* bound_samplers, ID3D12DescriptorHeap* fallback)
{
if (bound_samplers || !fallback) return false;
ID3D12DescriptorHeap* heaps[]{views, fallback};
if (views) list->SetDescriptorHeaps(2, heaps);
else list->SetDescriptorHeaps(1, heaps + 1);
return true;
}

} // namespace x2w_startup
Loading