upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 22:03:00 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-25 22:03:00 -0500
commitb3e541b224e1d0bb691d8b4b5cc719eeea7c7dd7 (patch)
tree53e0bbdcace9b8f7b83a550c07d402e03942828e /runtime
parent7b1fe62850f69bc3ad0b3176aea8dda8168867ea (diff)
d3d12_xs: drop the null-RTV marks of a descriptor heap when it is released
The null-view marks (#1504, by ubiquitarianism) are kept by the real CPU handle. They were moved by descriptor copies and cleared by a view made with a resource, but never dropped when the heap went away, so an RTV heap made later at the same address inherited stale marks and its clears were skipped. The heap proxy now records an RTV heap's real CPU range and heap_release drops the marks in it. The barrier conversion keeps no state across calls, so it needs nothing here. xs_proxy_mock_test: an RTV heap's marked view is skipped while the heap lives and cleared after it is released; a mark outside it stays; a second heap at the same address starts unmarked and marks again. docs/d3d12_vulkan.md says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/d3d12_xs_device.cpp24
-rw-r--r--runtime/test/xs_proxy_mock_test.cpp34
2 files changed, 58 insertions, 0 deletions
diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp
index 9069a73fa..87cc26dd3 100644
--- a/runtime/shims/d3d12_xs_device.cpp
+++ b/runtime/shims/d3d12_xs_device.cpp
@@ -203,6 +203,10 @@ struct HeapProxy {
203 SIZE_T custom_byte_size; 203 SIZE_T custom_byte_size;
204 SIZE_T real_cpu_start; 204 SIZE_T real_cpu_start;
205 DescriptorHeapBackingFree custom_free; 205 DescriptorHeapBackingFree custom_free;
206 // An RTV heap's real CPU range, [rtv_real_start, +rtv_real_bytes): the null-view marks in it
207 // (DeviceProxy::null_rtvs) are dropped when the heap goes, so a later heap at the same address starts unmarked.
208 SIZE_T rtv_real_start = 0;
209 SIZE_T rtv_real_bytes = 0;
206}; 210};
207 211
208struct DescriptorRange { 212struct DescriptorRange {
@@ -460,6 +464,16 @@ void copy_null_rtv_marks(DeviceProxy* device, UINT num_dest, const D3D12_CPU_DES
460 device->null_rtv_count.store(device->null_rtvs.size(), std::memory_order_release); 464 device->null_rtv_count.store(device->null_rtvs.size(), std::memory_order_release);
461} 465}
462 466
467// An RTV heap was released: its real handles can name another heap's views from now on, so no mark may outlive it.
468void drop_null_rtv_marks(DeviceProxy* device, SIZE_T start, SIZE_T bytes) {
469 if (!device || !start || !bytes || device->null_rtv_count.load(std::memory_order_acquire) == 0) return;
470 const SIZE_T end = start > (std::numeric_limits<SIZE_T>::max)() - bytes ? (std::numeric_limits<SIZE_T>::max)()
471 : start + bytes;
472 std::lock_guard lock(device->null_rtv_lock);
473 device->null_rtvs.erase(device->null_rtvs.lower_bound(start), device->null_rtvs.lower_bound(end));
474 device->null_rtv_count.store(device->null_rtvs.size(), std::memory_order_release);
475}
476
463std::atomic<std::uint64_t> g_hits[kMaxSlots]; 477std::atomic<std::uint64_t> g_hits[kMaxSlots];
464std::atomic<bool> g_first[kMaxSlots]; 478std::atomic<bool> g_first[kMaxSlots];
465std::atomic<bool> g_beyond_warned[kMaxSlots]; 479std::atomic<bool> g_beyond_warned[kMaxSlots];
@@ -898,6 +912,7 @@ unsigned long heap_release(HeapProxy* self) {
898 const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1; 912 const unsigned long refs = self->refs.fetch_sub(1, std::memory_order_acq_rel) - 1;
899 if (refs == 0) { 913 if (refs == 0) {
900 if (self->custom_cpu_start != 0) unregister_descriptor_range(self); 914 if (self->custom_cpu_start != 0) unregister_descriptor_range(self);
915 drop_null_rtv_marks(self->device, self->rtv_real_start, self->rtv_real_bytes);
901 void* const backing = reinterpret_cast<void*>(self->custom_cpu_start); 916 void* const backing = reinterpret_cast<void*>(self->custom_cpu_start);
902 const DescriptorHeapBackingFree free_fn = self->custom_free; 917 const DescriptorHeapBackingFree free_fn = self->custom_free;
903 ID3D12DescriptorHeap* real = self->real; 918 ID3D12DescriptorHeap* real = self->real;
@@ -4714,6 +4729,15 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H
4714 4729
4715 hr = create_heap_proxy(heap, self, *riid, out, custom_cpu_start, custom_byte_size, real_cpu_start, 4730 hr = create_heap_proxy(heap, self, *riid, out, custom_cpu_start, custom_byte_size, real_cpu_start,
4716 custom_cpu_start ? free_fn : nullptr, stride); 4731 custom_cpu_start ? free_fn : nullptr, stride);
4732 if (SUCCEEDED(hr) && desc && desc->Type == D3D12_DESCRIPTOR_HEAP_TYPE_RTV && is_heap_proxy(*out)) {
4733 // The null-view marks are kept by real handle (mark_null_rtv); heap_release drops those in this range.
4734 const UINT inc = self->real->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
4735 auto* proxy = static_cast<HeapProxy*>(*out);
4736 proxy->rtv_real_start =
4737 real_cpu_start ? real_cpu_start : raw_heap_get_cpu_handle(heap, *reinterpret_cast<void***>(heap)).ptr;
4738 if (inc && desc->NumDescriptors <= (std::numeric_limits<SIZE_T>::max)() / inc)
4739 proxy->rtv_real_bytes = static_cast<SIZE_T>(desc->NumDescriptors) * inc;
4740 }
4717 if (leonida::fsdraw::enabled()) { // LOCAL DIAGNOSTIC (fsdraw): which CPU start the title gets for this heap 4741 if (leonida::fsdraw::enabled()) { // LOCAL DIAGNOSTIC (fsdraw): which CPU start the title gets for this heap
4718 static std::atomic<int> logged{0}; 4742 static std::atomic<int> logged{0};
4719 if (logged.fetch_add(1) < 32) 4743 if (logged.fetch_add(1) < 32)
diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp
index 453be7cca..cb350820e 100644
--- a/runtime/test/xs_proxy_mock_test.cpp
+++ b/runtime/test/xs_proxy_mock_test.cpp
@@ -5107,6 +5107,40 @@ int check_console_null_barrier_and_clear() {
5107 EXPECT(real->calls[48] == 5 && real->args[48][0] == rtv_copy + kMockDescriptorInc); 5107 EXPECT(real->calls[48] == 5 && real->args[48][0] == rtv_copy + kMockDescriptorInc);
5108 EXPECT(!real->list_error && hr_of(raw(list, 9)) == S_OK); 5108 EXPECT(!real->list_error && hr_of(raw(list, 9)) == S_OK);
5109 5109
5110 // Releasing an RTV heap drops the marks in its real range, so a heap made later at the same address starts
5111 // unmarked; a mark outside that range stays.
5112 g_heap_hook.next_ptr = nullptr; // a plain PC heap even if a descriptor-heap hook is registered: real handles
5113 D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc{};
5114 rtv_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
5115 rtv_heap_desc.NumDescriptors = 4;
5116 const IID heap_iid = __uuidof(ID3D12DescriptorHeap);
5117 void* rtv_heap = nullptr;
5118 EXPECT(SUCCEEDED(hr_of(raw(device, 14, word(&rtv_heap_desc), word(&heap_iid), word(&rtv_heap)))));
5119 EXPECT(rtv_heap && xs::is_heap_proxy(rtv_heap) && raw(rtv_heap, 9) == kMockHeapCpuStart);
5120 const SIZE_T in_heap = kMockHeapCpuStart + 2 * kMockDescriptorInc;
5121 raw(device, 20, 0, 0, in_heap);
5122 const unsigned clears = real->calls[48];
5123 raw(list, 52, in_heap, word(color), 0, 0);
5124 EXPECT(real->calls[48] == clears); // marked while the heap lives: skipped
5125 EXPECT(raw(rtv_heap, 2) == 0);
5126 raw(list, 52, in_heap, word(color), 0, 0);
5127 EXPECT(real->calls[48] == clears + 1 && real->args[48][0] == in_heap); // the mark went with the heap
5128 real->list_error = false; // the mock list still takes its own view there as invalid
5129 raw(list, 52, rtv_none, word(color), 0, 0);
5130 EXPECT(real->calls[48] == clears + 1); // outside the released heap: still skipped
5131 // A second RTV heap at the same address: the view made there with a resource is cleared, and a view made with
5132 // no resource is marked again for as long as that heap lives.
5133 void* rtv_heap2 = nullptr;
5134 EXPECT(SUCCEEDED(hr_of(raw(device, 14, word(&rtv_heap_desc), word(&heap_iid), word(&rtv_heap2)))));
5135 raw(device, 20, word(resource_a), 0, in_heap);
5136 raw(list, 52, in_heap, word(color), 0, 0);
5137 EXPECT(real->calls[48] == clears + 2 && !real->list_error);
5138 raw(device, 20, 0, 0, in_heap);
5139 raw(list, 52, in_heap, word(color), 0, 0);
5140 EXPECT(real->calls[48] == clears + 2);
5141 EXPECT(raw(rtv_heap2, 2) == 0);
5142 EXPECT(!real->list_error && hr_of(raw(list, 9)) == S_OK);
5143
5110 // The marks are the device's own: another device's clear at a handle marked on the first is forwarded (its mock 5144 // The marks are the device's own: another device's clear at a handle marked on the first is forwarded (its mock
5111 // list then takes the view as invalid, which is what that device was told). 5145 // list then takes the view as invalid, which is what that device was told).
5112 Mock* other_mock = nullptr; 5146 Mock* other_mock = nullptr;