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:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 15:21:28 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 15:21:28 +0100
commit2d2eec9a75e3f98cf26b6a55df58d0bd90366ba3 (patch)
tree20dbe81d7273809675ee002c646b7c00a9619a82 /runtime
parentc2f39447b611f6c5df7bd283ae41e44a6f78d458 (diff)
M4 review: storage-image init only on shader-running non-copy submissions, ordered for other queues
- The zero-fill/GENERAL init of images with a UAV view no longer drains on a copy queue or in a submission that runs no dispatch or draw; every other queue's next submission waits on the initializing one's progress semaphore. - storage_pending_ is a set (UAV descriptors are rewritten every frame). - write_buffer_uav: FirstElement and element count are checked against Width / unit, so the byte range cannot overflow. - docs: M4 row names the remaining limitation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
-rw-r--r--runtime/shims/d3d12_vulkan_device.cpp2
-rw-r--r--runtime/src/vulkan_backend.h39
-rw-r--r--runtime/test/d3d12_m4_compute_test.cpp4
3 files changed, 38 insertions, 7 deletions
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp
index 69b8956f7..9be9a5fa4 100644
--- a/runtime/shims/d3d12_vulkan_device.cpp
+++ b/runtime/shims/d3d12_vulkan_device.cpp
@@ -3963,7 +3963,7 @@ public:
3963 } 3963 }
3964 const UINT64 unit = structured ? view.StructureByteStride : 4; 3964 const UINT64 unit = structured ? view.StructureByteStride : 4;
3965 if (!unit || unit > 0x3fff || view.CounterOffsetInBytes || 3965 if (!unit || unit > 0x3fff || view.CounterOffsetInBytes ||
3966 (UINT64(view.FirstElement) + view.NumElements) * unit > rd.Width || 3966 view.FirstElement > rd.Width / unit || (UINT64(view.FirstElement) + view.NumElements) > rd.Width / unit ||
3967 UINT64(view.NumElements) * (structured ? 1 : 4) > ~0u) { 3967 UINT64(view.NumElements) * (structured ? 1 : 4) > ~0u) {
3968 log_once("unordered-access view of a buffer outside the resource or with a bad stride; elements", 3968 log_once("unordered-access view of a buffer outside the resource or with a bad stride; elements",
3969 int(view.NumElements)); 3969 int(view.NumElements));
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index a79bf47be..59d5e2038 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -27,6 +27,7 @@
27#include <map> 27#include <map>
28#include <memory> 28#include <memory>
29#include <mutex> 29#include <mutex>
30#include <unordered_set>
30#include <shared_mutex> 31#include <shared_mutex>
31#include <span> 32#include <span>
32#include <thread> 33#include <thread>
@@ -820,7 +821,7 @@ public:
820 // zero-filled, ahead of its lists. An image already tracked beyond UNDEFINED is left as it is. 821 // zero-filled, ahead of its lists. An image already tracked beyond UNDEFINED is left as it is.
821 void note_storage_image(ResourceId id) { 822 void note_storage_image(ResourceId id) {
822 std::lock_guard lock(storage_mutex_); 823 std::lock_guard lock(storage_mutex_);
823 storage_pending_.push_back(id); 824 storage_pending_.insert(id);
824 } 825 }
825 826
826 void remove_resource(ResourceId id) { 827 void remove_resource(ResourceId id) {
@@ -1129,7 +1130,13 @@ private:
1129 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_; 1130 std::unordered_map<std::uint64_t, VkAccelerationStructureKHR> acceleration_structures_;
1130 LayoutTracker tracker_; 1131 LayoutTracker tracker_;
1131 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only 1132 std::mutex storage_mutex_; // a leaf lock: guards storage_pending_ only
1132 std::vector<ResourceId> storage_pending_; 1133 std::unordered_set<ResourceId> storage_pending_;
1134 // The last submission that initialized storage images (its queue's progress semaphore and ticket) and a counter
1135 // that says which one: every other queue's next submission waits for it, so no queue uses such an image before
1136 // the zero-fill that put it in GENERAL has run.
1137 VkSemaphore storage_init_semaphore_ = VK_NULL_HANDLE;
1138 std::uint64_t storage_init_value_ = 0;
1139 std::uint64_t storage_init_version_ = 0;
1133 std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_; 1140 std::map<std::tuple<VkImage, VkImageAspectFlags, std::uint32_t, std::uint32_t, std::uint32_t, VkFormat>, VkImageView> views_;
1134 std::function<bool(VkCommandBuffer)> bindless_prepare_; 1141 std::function<bool(VkCommandBuffer)> bindless_prepare_;
1135 std::unique_ptr<Depth24Converter> depth24_; 1142 std::unique_ptr<Depth24Converter> depth24_;
@@ -1445,10 +1452,25 @@ public:
1445 return ReplayResult::unsupported; 1452 return ReplayResult::unsupported;
1446 } 1453 }
1447 auto next_tracker = context_.tracker_; 1454 auto next_tracker = context_.tracker_;
1455 // Images with a UAV view are moved to GENERAL (zeroed) by the first submission that can use a shader on them:
1456 // not a copy queue (its family may not clear images), and one whose lists dispatch or draw.
1457 bool runs_shaders = false;
1458 for (const CommandList* list : lists)
1459 for (const auto& command : list->commands())
1460 runs_shaders |= std::holds_alternative<Dispatch>(command) || std::holds_alternative<Draw>(command) ||
1461 std::holds_alternative<ExecuteIndirect>(command);
1448 std::vector<ResourceId> storage_images; 1462 std::vector<ResourceId> storage_images;
1449 { 1463 {
1450 std::lock_guard lock(context_.storage_mutex_); 1464 std::lock_guard lock(context_.storage_mutex_);
1451 storage_images.swap(context_.storage_pending_); 1465 if (context_.storage_init_semaphore_ && context_.storage_init_semaphore_ != progress_ &&
1466 context_.storage_init_version_ != storage_init_seen_) {
1467 waits_.push_back(context_.storage_init_semaphore_);
1468 wait_values_.push_back(context_.storage_init_value_);
1469 storage_init_seen_ = context_.storage_init_version_;
1470 }
1471 if (kind_ != QueueKind::copy && runs_shaders)
1472 storage_images.assign(context_.storage_pending_.begin(), context_.storage_pending_.end());
1473 if (!storage_images.empty()) context_.storage_pending_.clear();
1452 } 1474 }
1453 for (const ResourceId id : storage_images) { 1475 for (const ResourceId id : storage_images) {
1454 const ResourceBinding* image = context_.resolve(id); 1476 const ResourceBinding* image = context_.resolve(id);
@@ -1501,8 +1523,14 @@ public:
1501 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0); 1523 const bool submitted = ended && submit(command_buffer, VK_NULL_HANDLE, 0);
1502 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission 1524 if (!submitted && !storage_images.empty()) { // not initialized after all: try again with the next submission
1503 std::lock_guard lock(context_.storage_mutex_); 1525 std::lock_guard lock(context_.storage_mutex_);
1504 context_.storage_pending_.insert(context_.storage_pending_.end(), storage_images.begin(), 1526 context_.storage_pending_.insert(storage_images.begin(), storage_images.end());
1505 storage_images.end()); 1527 }
1528 if (submitted && !storage_images.empty()) {
1529 std::lock_guard lock(context_.storage_mutex_);
1530 context_.storage_init_semaphore_ = progress_;
1531 context_.storage_init_value_ = submitted_;
1532 ++context_.storage_init_version_;
1533 storage_init_seen_ = context_.storage_init_version_;
1506 } 1534 }
1507 if (submitted) { 1535 if (submitted) {
1508 context_.tracker_ = std::move(next_tracker); 1536 context_.tracker_ = std::move(next_tracker);
@@ -1655,6 +1683,7 @@ private:
1655 std::uint64_t submitted_ = 0; 1683 std::uint64_t submitted_ = 0;
1656 std::vector<CommandSlot> buffers_; 1684 std::vector<CommandSlot> buffers_;
1657 std::unique_ptr<OcclusionResolve> occlusion_resolve_; 1685 std::unique_ptr<OcclusionResolve> occlusion_resolve_;
1686 std::uint64_t storage_init_seen_ = 0; // the storage_init_version_ this queue has waited for
1658 std::vector<VkSemaphore> waits_; 1687 std::vector<VkSemaphore> waits_;
1659 std::vector<std::uint64_t> wait_values_; 1688 std::vector<std::uint64_t> wait_values_;
1660 std::uint32_t open_labels_ = 0; // queue labels begun and not ended 1689 std::uint32_t open_labels_ = 0; // queue labels begun and not ended
diff --git a/runtime/test/d3d12_m4_compute_test.cpp b/runtime/test/d3d12_m4_compute_test.cpp
index cb322c5e5..bd3a555fd 100644
--- a/runtime/test/d3d12_m4_compute_test.cpp
+++ b/runtime/test/d3d12_m4_compute_test.cpp
@@ -266,7 +266,9 @@ int main() {
266 EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&copy_fence.p)))); 266 EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&copy_fence.p))));
267 EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&compute_fence.p)))); 267 EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&compute_fence.p))));
268 268
269 // Copy list 1: stream the input in (the buffer is in COMMON and is promoted to COPY_DEST by the copy). 269 // Copy list 1: stream the input in (the buffer is in COMMON and is promoted to COPY_DEST by the copy). This is the
270 // first submission after the UAV views were written, and a copy queue: it must not initialize the storage image
271 // (the compute queue does, in the submission that dispatches), or the data written later would be at risk.
270 stream_list->CopyBufferRegion(input.p, 0, upload.p, 0, in_bytes); 272 stream_list->CopyBufferRegion(input.p, 0, upload.p, 0, in_bytes);
271 EXPECT(stream_list->Close() == S_OK); 273 EXPECT(stream_list->Close() == S_OK);
272 274