upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
author73N1X <115502401+73N1X@users.noreply.github.com>2026-11-22 00:45:01 -0400
committer73N1X <115502401+73N1X@users.noreply.github.com>2026-11-22 05:28:28 -0400
commit9360ec9ec0ef5f21ae30334c63fdc3196cf5d03d (patch)
treed34bb9f6e10786649c0bf83f5a6d1d7c23a7026c /runtime/src
parente7584c5207e44ed51472e6aa05c781f39ea975f6 (diff)
Runtime: predicate byte-granular CopyBufferRegion on the GPU
Build on the full 64-bit predicate snapshots from #856. Replay predicated buffer copies through a storage8 compute helper, retain descriptor batches through timeline completion, and reject missing capabilities or active statistics queries before submitting work. Validate byte offsets, high predicate words, bounds, skipped destinations and pending descriptor lifetimes with CPU checks, 180 native WARP comparisons and 64 gated/reused GPU submissions. The existing OPTIONS19 host-capability mismatch also reproduces on base 4a36bb1; no tests are disabled.
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/command_list.h3
-rw-r--r--runtime/src/predicated_copy.comp11
-rw-r--r--runtime/src/predicated_copy_spv.h54
-rw-r--r--runtime/src/vulkan_backend.h21
-rw-r--r--runtime/src/vulkan_predicated_copy.h154
-rw-r--r--runtime/src/vulkan_replay.h30
6 files changed, 268 insertions, 5 deletions
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h
index 25b2db1e8..080f94a84 100644
--- a/runtime/src/command_list.h
+++ b/runtime/src/command_list.h
@@ -585,7 +585,8 @@ public:
585 // Copies. The rules D3D12 states for the call itself are checked here; ranges against the resources are 585 // Copies. The rules D3D12 states for the call itself are checked here; ranges against the resources are
586 // checked at replay, when the resources are known. 586 // checked at replay, when the resources are known.
587 bool copy_buffer(const CopyBuffer& copy) { 587 bool copy_buffer(const CopyBuffer& copy) {
588 if (!copy.dst || !copy.src || !copy.size) return false; 588 if (!copy.dst || !copy.src) return false;
589 if (!copy.size) return !closed_; // Native D3D12 accepts a zero-byte CopyBufferRegion as a no-op.
589 return append(copy); 590 return append(copy);
590 } 591 }
591 592
diff --git a/runtime/src/predicated_copy.comp b/runtime/src/predicated_copy.comp
new file mode 100644
index 000000000..0a105554d
--- /dev/null
+++ b/runtime/src/predicated_copy.comp
@@ -0,0 +1,11 @@
1// Synthetic byte-granular buffer copy, executed inside the captured conditional-rendering scope.
2#version 450
3#extension GL_EXT_shader_8bit_storage : require
4layout(local_size_x=64) in;
5layout(set=0,binding=0,std430) readonly buffer Source { uint8_t bytes[]; } source;
6layout(set=0,binding=1,std430) buffer Destination { uint8_t bytes[]; } destination;
7layout(push_constant) uniform Copy { uint source_offset; uint destination_offset; uint size; } copy;
8void main() {
9 uint i=gl_GlobalInvocationID.x;
10 if(i<copy.size) destination.bytes[copy.destination_offset+i]=source.bytes[copy.source_offset+i];
11}
diff --git a/runtime/src/predicated_copy_spv.h b/runtime/src/predicated_copy_spv.h
new file mode 100644
index 000000000..3cf9f30fe
--- /dev/null
+++ b/runtime/src/predicated_copy_spv.h
@@ -0,0 +1,54 @@
1// glslang 16.6.0: glslang -V --target-env vulkan1.2 -Os --spirv-val predicated_copy.comp -o predicated_copy.spv
2#pragma once
3#include <cstdint>
4namespace leonida { inline constexpr std::uint32_t predicated_copy_spirv[] = {
5 0x07230203u, 0x00010500u, 0x0008000bu, 0x00000037u, 0x00000000u, 0x00020011u, 0x00000001u, 0x00020011u,
6 0x00001160u, 0x0006000bu, 0x00000001u, 0x4c534c47u, 0x6474732eu, 0x3035342eu, 0x00000000u, 0x0003000eu,
7 0x00000000u, 0x00000001u, 0x0009000fu, 0x00000005u, 0x00000004u, 0x6e69616du, 0x00000000u, 0x0000000bu,
8 0x00000013u, 0x00000021u, 0x0000002bu, 0x00060010u, 0x00000004u, 0x00000011u, 0x00000040u, 0x00000001u,
9 0x00000001u, 0x00030003u, 0x00000002u, 0x000001c2u, 0x00080004u, 0x455f4c47u, 0x735f5458u, 0x65646168u,
10 0x62385f72u, 0x735f7469u, 0x61726f74u, 0x00006567u, 0x00040005u, 0x00000004u, 0x6e69616du, 0x00000000u,
11 0x00080005u, 0x0000000bu, 0x475f6c67u, 0x61626f6cu, 0x766e496cu, 0x7461636fu, 0x496e6f69u, 0x00000044u,
12 0x00040005u, 0x00000011u, 0x79706f43u, 0x00000000u, 0x00070006u, 0x00000011u, 0x00000000u, 0x72756f73u,
13 0x6f5f6563u, 0x65736666u, 0x00000074u, 0x00080006u, 0x00000011u, 0x00000001u, 0x74736564u, 0x74616e69u,
14 0x5f6e6f69u, 0x7366666fu, 0x00007465u, 0x00050006u, 0x00000011u, 0x00000002u, 0x657a6973u, 0x00000000u,
15 0x00040005u, 0x00000013u, 0x79706f63u, 0x00000000u, 0x00050005u, 0x0000001fu, 0x74736544u, 0x74616e69u,
16 0x006e6f69u, 0x00050006u, 0x0000001fu, 0x00000000u, 0x65747962u, 0x00000073u, 0x00050005u, 0x00000021u,
17 0x74736564u, 0x74616e69u, 0x006e6f69u, 0x00040005u, 0x00000029u, 0x72756f53u, 0x00006563u, 0x00050006u,
18 0x00000029u, 0x00000000u, 0x65747962u, 0x00000073u, 0x00040005u, 0x0000002bu, 0x72756f73u, 0x00006563u,
19 0x00040047u, 0x0000000bu, 0x0000000bu, 0x0000001cu, 0x00030047u, 0x00000011u, 0x00000002u, 0x00050048u,
20 0x00000011u, 0x00000000u, 0x00000023u, 0x00000000u, 0x00050048u, 0x00000011u, 0x00000001u, 0x00000023u,
21 0x00000004u, 0x00050048u, 0x00000011u, 0x00000002u, 0x00000023u, 0x00000008u, 0x00040047u, 0x0000001eu,
22 0x00000006u, 0x00000001u, 0x00030047u, 0x0000001fu, 0x00000002u, 0x00050048u, 0x0000001fu, 0x00000000u,
23 0x00000023u, 0x00000000u, 0x00040047u, 0x00000021u, 0x00000021u, 0x00000001u, 0x00040047u, 0x00000021u,
24 0x00000022u, 0x00000000u, 0x00040047u, 0x00000028u, 0x00000006u, 0x00000001u, 0x00030047u, 0x00000029u,
25 0x00000002u, 0x00040048u, 0x00000029u, 0x00000000u, 0x00000018u, 0x00050048u, 0x00000029u, 0x00000000u,
26 0x00000023u, 0x00000000u, 0x00030047u, 0x0000002bu, 0x00000018u, 0x00040047u, 0x0000002bu, 0x00000021u,
27 0x00000000u, 0x00040047u, 0x0000002bu, 0x00000022u, 0x00000000u, 0x00040047u, 0x00000036u, 0x0000000bu,
28 0x00000019u, 0x00020013u, 0x00000002u, 0x00030021u, 0x00000003u, 0x00000002u, 0x00040015u, 0x00000006u,
29 0x00000020u, 0x00000000u, 0x00040017u, 0x00000009u, 0x00000006u, 0x00000003u, 0x00040020u, 0x0000000au,
30 0x00000001u, 0x00000009u, 0x0004003bu, 0x0000000au, 0x0000000bu, 0x00000001u, 0x0004002bu, 0x00000006u,
31 0x0000000cu, 0x00000000u, 0x00040020u, 0x0000000du, 0x00000001u, 0x00000006u, 0x0005001eu, 0x00000011u,
32 0x00000006u, 0x00000006u, 0x00000006u, 0x00040020u, 0x00000012u, 0x00000009u, 0x00000011u, 0x0004003bu,
33 0x00000012u, 0x00000013u, 0x00000009u, 0x00040015u, 0x00000014u, 0x00000020u, 0x00000001u, 0x0004002bu,
34 0x00000014u, 0x00000015u, 0x00000002u, 0x00040020u, 0x00000016u, 0x00000009u, 0x00000006u, 0x00020014u,
35 0x00000019u, 0x00040015u, 0x0000001du, 0x00000008u, 0x00000000u, 0x0003001du, 0x0000001eu, 0x0000001du,
36 0x0003001eu, 0x0000001fu, 0x0000001eu, 0x00040020u, 0x00000020u, 0x0000000cu, 0x0000001fu, 0x0004003bu,
37 0x00000020u, 0x00000021u, 0x0000000cu, 0x0004002bu, 0x00000014u, 0x00000022u, 0x00000000u, 0x0004002bu,
38 0x00000014u, 0x00000023u, 0x00000001u, 0x0003001du, 0x00000028u, 0x0000001du, 0x0003001eu, 0x00000029u,
39 0x00000028u, 0x00040020u, 0x0000002au, 0x0000000cu, 0x00000029u, 0x0004003bu, 0x0000002au, 0x0000002bu,
40 0x0000000cu, 0x00040020u, 0x00000030u, 0x0000000cu, 0x0000001du, 0x0004002bu, 0x00000006u, 0x00000034u,
41 0x00000040u, 0x0004002bu, 0x00000006u, 0x00000035u, 0x00000001u, 0x0006002cu, 0x00000009u, 0x00000036u,
42 0x00000034u, 0x00000035u, 0x00000035u, 0x00050036u, 0x00000002u, 0x00000004u, 0x00000000u, 0x00000003u,
43 0x000200f8u, 0x00000005u, 0x00050041u, 0x0000000du, 0x0000000eu, 0x0000000bu, 0x0000000cu, 0x0004003du,
44 0x00000006u, 0x0000000fu, 0x0000000eu, 0x00050041u, 0x00000016u, 0x00000017u, 0x00000013u, 0x00000015u,
45 0x0004003du, 0x00000006u, 0x00000018u, 0x00000017u, 0x000500b0u, 0x00000019u, 0x0000001au, 0x0000000fu,
46 0x00000018u, 0x000300f7u, 0x0000001cu, 0x00000000u, 0x000400fau, 0x0000001au, 0x0000001bu, 0x0000001cu,
47 0x000200f8u, 0x0000001bu, 0x00050041u, 0x00000016u, 0x00000024u, 0x00000013u, 0x00000023u, 0x0004003du,
48 0x00000006u, 0x00000025u, 0x00000024u, 0x00050080u, 0x00000006u, 0x00000027u, 0x00000025u, 0x0000000fu,
49 0x00050041u, 0x00000016u, 0x0000002cu, 0x00000013u, 0x00000022u, 0x0004003du, 0x00000006u, 0x0000002du,
50 0x0000002cu, 0x00050080u, 0x00000006u, 0x0000002fu, 0x0000002du, 0x0000000fu, 0x00060041u, 0x00000030u,
51 0x00000031u, 0x0000002bu, 0x00000022u, 0x0000002fu, 0x0004003du, 0x0000001du, 0x00000032u, 0x00000031u,
52 0x00060041u, 0x00000030u, 0x00000033u, 0x00000021u, 0x00000022u, 0x00000027u, 0x0003003eu, 0x00000033u,
53 0x00000032u, 0x000200f9u, 0x0000001cu, 0x000200f8u, 0x0000001cu, 0x000100fdu, 0x00010038u,
54}; }
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index 084e000e1..d5cb96368 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -688,6 +688,11 @@ public:
688 predicate_family_ = family; 688 predicate_family_ = family;
689 predicate_reducer_ = std::make_unique<PredicateReducer>(get, device_, memory, max_range); 689 predicate_reducer_ = std::make_unique<PredicateReducer>(get, device_, memory, max_range);
690 } 690 }
691 // Configure before queue creation, only when storageBuffer8BitAccess is enabled on the device.
692 void configure_predicated_copy(PFN_vkGetDeviceProcAddr get, const VkPhysicalDeviceLimits& limits) {
693 std::unique_lock lock(resource_mutex_);
694 predicated_copier_ = std::make_unique<PredicatedBufferCopier>(get, device_, limits);
695 }
691 VkDevice device() const { return device_; } 696 VkDevice device() const { return device_; }
692 697
693 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets 698 // Debug markers become labels from here on; call before any queue or list uses the context. Incomplete sets
@@ -938,6 +943,7 @@ private:
938 VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{}; 943 VkPhysicalDeviceMemoryProperties occlusion_resolve_memory_{};
939 ReplayFunctions replay_; 944 ReplayFunctions replay_;
940 std::unique_ptr<PredicateReducer> predicate_reducer_; 945 std::unique_ptr<PredicateReducer> predicate_reducer_;
946 std::unique_ptr<PredicatedBufferCopier> predicated_copier_;
941 std::uint32_t predicate_family_ = UINT32_MAX; 947 std::uint32_t predicate_family_ = UINT32_MAX;
942 DebugLabelFunctions labels_; 948 DebugLabelFunctions labels_;
943}; 949};
@@ -1071,6 +1077,7 @@ public:
1071 for (const auto& command : list->commands()) 1077 for (const auto& command : list->commands())
1072 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures; 1078 if (const auto* p = std::get_if<Predication>(&command); p && p->buffer) ++captures;
1073 std::unique_ptr<PredicateReducer::Batch> batch; 1079 std::unique_ptr<PredicateReducer::Batch> batch;
1080 std::unique_ptr<PredicatedBufferCopier::Batch> copy_batch;
1074 if (captures) { 1081 if (captures) {
1075 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_) 1082 if (kind_ == QueueKind::copy || !context_.predicate_reducer_ || family_ != context_.predicate_family_)
1076 return ReplayResult::unsupported; 1083 return ReplayResult::unsupported;
@@ -1085,6 +1092,17 @@ public:
1085 replay.capture_predicate = [&](VkCommandBuffer cb, const PredicateCapture& p) { 1092 replay.capture_predicate = [&](VkCommandBuffer cb, const PredicateCapture& p) {
1086 context_.predicate_reducer_->capture(cb, p); 1093 context_.predicate_reducer_->capture(cb, p);
1087 }; 1094 };
1095 if (context_.predicated_copier_) {
1096 copy_batch = context_.predicated_copier_->batch();
1097 replay.prepare_predicated_copy = [&](VkBuffer source, VkBuffer destination, VkDeviceSize src,
1098 VkDeviceSize dst, VkDeviceSize size,
1099 std::vector<PredicatedCopyDispatch>& out) {
1100 return copy_batch->prepare(source, destination, src, dst, size, out);
1101 };
1102 replay.emit_predicated_copy = [&](VkCommandBuffer cb, const std::vector<PredicatedCopyDispatch>& jobs) {
1103 context_.predicated_copier_->emit(cb, jobs);
1104 };
1105 }
1088 } 1106 }
1089 const auto& occlusion_memory = context_.occlusion_resolve_memory_; 1107 const auto& occlusion_memory = context_.occlusion_resolve_memory_;
1090 const bool can_resolve_occlusion = occlusion_memory.memoryTypeCount != 0; 1108 const bool can_resolve_occlusion = occlusion_memory.memoryTypeCount != 0;
@@ -1204,6 +1222,7 @@ public:
1204 if (submitted) { 1222 if (submitted) {
1205 context_.tracker_ = std::move(next_tracker); 1223 context_.tracker_ = std::move(next_tracker);
1206 slot->predicate = std::move(batch); 1224 slot->predicate = std::move(batch);
1225 slot->predicated_copy = std::move(copy_batch);
1207 queries.commit(); 1226 queries.commit();
1208 slot->segments = std::move(queries.retained); 1227 slot->segments = std::move(queries.retained);
1209 } else { 1228 } else {
@@ -1255,6 +1274,7 @@ private:
1255 VkCommandBuffer buffer; 1274 VkCommandBuffer buffer;
1256 std::uint64_t ticket; 1275 std::uint64_t ticket;
1257 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments; 1276 std::vector<std::shared_ptr<const OcclusionSegmentPool>> segments;
1277 std::unique_ptr<PredicatedBufferCopier::Batch> predicated_copy;
1258 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes 1278 std::unique_ptr<PredicateReducer::Batch> predicate; // retained until this slot completes
1259 }; 1279 };
1260 1280
@@ -1272,6 +1292,7 @@ private:
1272 if (reuse) { 1292 if (reuse) {
1273 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr; 1293 if (context_.vk.vkResetCommandBuffer(reuse->buffer, 0) != VK_SUCCESS) return nullptr;
1274 reuse->predicate.reset(); 1294 reuse->predicate.reset();
1295 reuse->predicated_copy.reset();
1275 reuse->ticket = UINT64_MAX; // in use until submitted 1296 reuse->ticket = UINT64_MAX; // in use until submitted
1276 return reuse; 1297 return reuse;
1277 } 1298 }
diff --git a/runtime/src/vulkan_predicated_copy.h b/runtime/src/vulkan_predicated_copy.h
new file mode 100644
index 000000000..6a225ced7
--- /dev/null
+++ b/runtime/src/vulkan_predicated_copy.h
@@ -0,0 +1,154 @@
1// Byte-granular conditional CopyBufferRegion. Requires enabled storageBuffer8BitAccess, not shaderInt8.
2#pragma once
3#ifndef VK_NO_PROTOTYPES
4#define VK_NO_PROTOTYPES
5#endif
6#include <vulkan/vulkan.h>
7#include <algorithm>
8#include <cstdint>
9#include <memory>
10#include <initializer_list>
11#include <vector>
12#include "predicated_copy_spv.h"
13namespace leonida {
14struct PredicatedCopyDispatch {
15 VkDescriptorSet set = VK_NULL_HANDLE;
16 std::uint32_t source = 0, destination = 0, bytes = 0;
17};
18class PredicatedBufferCopier {
19public:
20 PredicatedBufferCopier(PFN_vkGetDeviceProcAddr get, VkDevice device, const VkPhysicalDeviceLimits& limits)
21 : get_(get), device_(device), limits_(limits) {}
22 ~PredicatedBufferCopier() {
23 if(pipeline_)fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_,pipeline_,nullptr);
24 if(layout_)fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_,layout_,nullptr);
25 if(set_layout_)fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_,set_layout_,nullptr);
26 }
27 PredicatedBufferCopier(const PredicatedBufferCopier&)=delete;
28 PredicatedBufferCopier& operator=(const PredicatedBufferCopier&)=delete;
29 struct Batch {
30 PredicatedBufferCopier& owner;
31 std::vector<VkDescriptorPool> pools;
32 explicit Batch(PredicatedBufferCopier& value):owner(value){}
33 ~Batch(){for(auto pool:pools)owner.fn<PFN_vkDestroyDescriptorPool>("vkDestroyDescriptorPool")(owner.device_,pool,nullptr);}
34 bool prepare(VkBuffer source,VkBuffer destination,VkDeviceSize src,VkDeviceSize dst,VkDeviceSize bytes,
35 std::vector<PredicatedCopyDispatch>& out) {
36 out.clear();
37 if(!bytes)return true;
38 if(!source||!destination||bytes>UINT64_MAX-src||bytes>UINT64_MAX-dst)return false;
39 if(source==destination && src<dst+bytes && dst<src+bytes)return false;
40 if(!owner.make_pipeline())return false;
41 // Align each descriptor base down, keeping byte prefixes in push constants. Split at the smaller
42 // remaining descriptor range or dispatch limit, so no uint32 shader index can wrap or overread.
43 struct Window{VkDescriptorBufferInfo source,destination;std::uint32_t src,dst,bytes;};
44 std::vector<Window> windows;
45 const auto alignment=std::max<VkDeviceSize>(1,owner.limits_.minStorageBufferOffsetAlignment);
46 const VkDeviceSize maximum=owner.limits_.maxStorageBufferRange;
47 const VkDeviceSize threads=VkDeviceSize(owner.limits_.maxComputeWorkGroupCount[0])*64;
48 for(VkDeviceSize done=0;done<bytes;){
49 const auto s=src+done,d=dst+done,sp=s%alignment,dp=d%alignment;
50 if(sp>=maximum||dp>=maximum||!threads)return false;
51 const auto n=std::min({bytes-done,maximum-sp,maximum-dp,threads});
52 if(!n||n>UINT32_MAX)return false;
53 windows.push_back({{source,s-sp,sp+n},{destination,d-dp,dp+n},
54 static_cast<std::uint32_t>(sp),static_cast<std::uint32_t>(dp),static_cast<std::uint32_t>(n)});
55 if(windows.size()>UINT32_MAX/2)return false;
56 done+=n;
57 }
58 const auto count=static_cast<std::uint32_t>(windows.size());
59 const VkDescriptorPoolSize size{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,count*2};
60 VkDescriptorPoolCreateInfo pool_info{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
61 pool_info.maxSets=count;pool_info.poolSizeCount=1;pool_info.pPoolSizes=&size;
62 VkDescriptorPool pool=VK_NULL_HANDLE;
63 if(owner.fn<PFN_vkCreateDescriptorPool>("vkCreateDescriptorPool")(owner.device_,&pool_info,nullptr,&pool)!=VK_SUCCESS)return false;
64 // The batch is retained with the command buffer until its timeline ticket completes.
65 pools.push_back(pool);
66 const std::vector<VkDescriptorSetLayout> layouts(count,owner.set_layout_);
67 std::vector<VkDescriptorSet> sets(count);
68 VkDescriptorSetAllocateInfo allocate{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
69 allocate.descriptorPool=pool;allocate.descriptorSetCount=count;allocate.pSetLayouts=layouts.data();
70 if(owner.fn<PFN_vkAllocateDescriptorSets>("vkAllocateDescriptorSets")(owner.device_,&allocate,sets.data())!=VK_SUCCESS)return false;
71 for(std::uint32_t i=0;i<count;++i){
72 VkWriteDescriptorSet writes[2]{};
73 const VkDescriptorBufferInfo* infos[]={&windows[i].source,&windows[i].destination};
74 for(std::uint32_t binding=0;binding<2;++binding){
75 auto& w=writes[binding];w.sType=VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;w.dstSet=sets[i];
76 w.dstBinding=binding;w.descriptorCount=1;w.descriptorType=VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;w.pBufferInfo=infos[binding];
77 }
78 owner.fn<PFN_vkUpdateDescriptorSets>("vkUpdateDescriptorSets")(owner.device_,2,writes,0,nullptr);
79 out.push_back({sets[i],windows[i].src,windows[i].dst,windows[i].bytes});
80 }
81 return true;
82 }
83 };
84 std::unique_ptr<Batch> batch(){return std::make_unique<Batch>(*this);}
85 void emit(VkCommandBuffer cb,const std::vector<PredicatedCopyDispatch>& jobs)const{
86 const auto barrier=fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier");
87 // Barriers are unconditional even when the dispatch is skipped by conditional rendering. They order
88 // transfer producers, dependent copies, and adjacent windows whose aligned descriptors may overlap.
89 for(const auto& job:jobs){
90 VkMemoryBarrier before{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
91 before.srcAccessMask=VK_ACCESS_MEMORY_WRITE_BIT|VK_ACCESS_MEMORY_READ_BIT;
92 before.dstAccessMask=VK_ACCESS_SHADER_READ_BIT|VK_ACCESS_SHADER_WRITE_BIT;
93 barrier(cb,VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,0,1,&before,0,nullptr,0,nullptr);
94 fn<PFN_vkCmdBindPipeline>("vkCmdBindPipeline")(cb,VK_PIPELINE_BIND_POINT_COMPUTE,pipeline_);
95 fn<PFN_vkCmdBindDescriptorSets>("vkCmdBindDescriptorSets")(cb,VK_PIPELINE_BIND_POINT_COMPUTE,layout_,0,1,&job.set,0,nullptr);
96 const std::uint32_t push[]={job.source,job.destination,job.bytes};
97 fn<PFN_vkCmdPushConstants>("vkCmdPushConstants")(cb,layout_,VK_SHADER_STAGE_COMPUTE_BIT,0,sizeof(push),push);
98 fn<PFN_vkCmdDispatch>("vkCmdDispatch")(cb,(job.bytes-1u)/64u+1u,1,1);
99 VkMemoryBarrier after{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
100 after.srcAccessMask=VK_ACCESS_SHADER_WRITE_BIT|VK_ACCESS_SHADER_READ_BIT;
101 after.dstAccessMask=VK_ACCESS_MEMORY_READ_BIT|VK_ACCESS_MEMORY_WRITE_BIT;
102 barrier(cb,VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,0,1,&after,0,nullptr,0,nullptr);
103 }
104 }
105private:
106 template<class T>T fn(const char* name)const{return reinterpret_cast<T>(get_(device_,name));}
107 bool make_pipeline() {
108 if (pipeline_) return true;
109 // No entry-point assumptions for partial/mock devices: fail before allocating or recording anything.
110 for (const char* name : {"vkCreateDescriptorPool", "vkDestroyDescriptorPool",
111 "vkAllocateDescriptorSets", "vkUpdateDescriptorSets", "vkCreateShaderModule", "vkDestroyShaderModule",
112 "vkCreateDescriptorSetLayout", "vkDestroyDescriptorSetLayout", "vkCreatePipelineLayout",
113 "vkDestroyPipelineLayout", "vkCreateComputePipelines", "vkDestroyPipeline",
114 "vkCmdPipelineBarrier", "vkCmdBindPipeline", "vkCmdBindDescriptorSets", "vkCmdPushConstants", "vkCmdDispatch"})
115 if (!get_(device_, name)) return false;
116 VkDescriptorSetLayout set = VK_NULL_HANDLE;
117 VkPipelineLayout layout = VK_NULL_HANDLE;
118 VkShaderModule shader = VK_NULL_HANDLE;
119 VkPipeline pipeline = VK_NULL_HANDLE;
120 const VkDescriptorSetLayoutBinding bindings[]={{0,VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,1,VK_SHADER_STAGE_COMPUTE_BIT,nullptr},{1,VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,1,VK_SHADER_STAGE_COMPUTE_BIT,nullptr}};
121 VkDescriptorSetLayoutCreateInfo set_info{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
122 set_info.bindingCount = 2; set_info.pBindings = bindings;
123 bool ok = fn<PFN_vkCreateDescriptorSetLayout>("vkCreateDescriptorSetLayout")(device_, &set_info, nullptr, &set) == VK_SUCCESS;
124 const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, 12};
125 VkPipelineLayoutCreateInfo layout_info{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
126 layout_info.setLayoutCount = 1; layout_info.pSetLayouts = &set;
127 layout_info.pushConstantRangeCount = 1; layout_info.pPushConstantRanges = &push;
128 ok = ok && fn<PFN_vkCreatePipelineLayout>("vkCreatePipelineLayout")(device_, &layout_info, nullptr, &layout) == VK_SUCCESS;
129 VkShaderModuleCreateInfo module{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
130 module.codeSize = sizeof(predicated_copy_spirv); module.pCode = predicated_copy_spirv;
131 ok = ok && fn<PFN_vkCreateShaderModule>("vkCreateShaderModule")(device_, &module, nullptr, &shader) == VK_SUCCESS;
132 VkComputePipelineCreateInfo info{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO};
133 info.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
134 info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT; info.stage.module = shader; info.stage.pName = "main";
135 info.layout = layout;
136 ok = ok && fn<PFN_vkCreateComputePipelines>("vkCreateComputePipelines")(device_, VK_NULL_HANDLE, 1, &info, nullptr, &pipeline) == VK_SUCCESS;
137 if (shader) fn<PFN_vkDestroyShaderModule>("vkDestroyShaderModule")(device_, shader, nullptr);
138 if (!ok) {
139 if (pipeline) fn<PFN_vkDestroyPipeline>("vkDestroyPipeline")(device_, pipeline, nullptr);
140 if (layout) fn<PFN_vkDestroyPipelineLayout>("vkDestroyPipelineLayout")(device_, layout, nullptr);
141 if (set) fn<PFN_vkDestroyDescriptorSetLayout>("vkDestroyDescriptorSetLayout")(device_, set, nullptr);
142 return false;
143 }
144 set_layout_ = set; layout_ = layout; pipeline_ = pipeline;
145 return true;
146 }
147 PFN_vkGetDeviceProcAddr get_;
148 VkDevice device_;
149 VkPhysicalDeviceLimits limits_;
150 VkDescriptorSetLayout set_layout_=VK_NULL_HANDLE;
151 VkPipelineLayout layout_=VK_NULL_HANDLE;
152 VkPipeline pipeline_=VK_NULL_HANDLE;
153};
154}
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 77eba9612..6df4beba5 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -24,6 +24,8 @@
24// rectangles need color_attachment usage on the image (ResourceBinding::color_attachment); 24// rectangles need color_attachment usage on the image (ResourceBinding::color_attachment);
25// - query commands need the query functions, fill_buffer and query_pool; resolve destinations have TRANSFER_DST 25// - query commands need the query functions, fill_buffer and query_pool; resolve destinations have TRANSFER_DST
26// usage and binary occlusion resolves need resolve_binary_occlusion. 26// usage and binary occlusion resolves need resolve_binary_occlusion.
27// - predicated buffer copies need the byte-copy helper and buffers with STORAGE_BUFFER usage; helper descriptors
28// must remain alive through submission completion (VulkanQueue owns them).
27// A list that needs a missing function fails with ReplayResult::unsupported before anything is written. 29// A list that needs a missing function fails with ReplayResult::unsupported before anything is written.
28 30
29#include "command_list.h" 31#include "command_list.h"
@@ -32,6 +34,7 @@
32 34
33#include <vulkan/vulkan.h> 35#include <vulkan/vulkan.h>
34#include "vulkan_predicate.h" 36#include "vulkan_predicate.h"
37#include "vulkan_predicated_copy.h"
35 38
36#include <algorithm> 39#include <algorithm>
37#include <array> 40#include <array>
@@ -678,6 +681,9 @@ struct ReplayFunctions {
678 // Preparation only allocates a slot; capture records the GPU snapshot at the SetPredication command. 681 // Preparation only allocates a slot; capture records the GPU snapshot at the SetPredication command.
679 std::function<bool(VkBuffer, VkDeviceSize, PredicateCapture&)> prepare_predicate; 682 std::function<bool(VkBuffer, VkDeviceSize, PredicateCapture&)> prepare_predicate;
680 std::function<void(VkCommandBuffer, const PredicateCapture&)> capture_predicate; 683 std::function<void(VkCommandBuffer, const PredicateCapture&)> capture_predicate;
684 std::function<bool(VkBuffer, VkBuffer, VkDeviceSize, VkDeviceSize, VkDeviceSize,
685 std::vector<PredicatedCopyDispatch>&)> prepare_predicated_copy;
686 std::function<void(VkCommandBuffer, const std::vector<PredicatedCopyDispatch>&)> emit_predicated_copy;
681 // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears. 687 // Returns a 2D-array view of the range (caller-owned, alive until execution completes); rectangle clears.
682 std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view; 688 std::function<VkImageView(const ResourceBinding&, const VkImageSubresourceRange&)> image_view;
683 // Footprint copies of a depth24_as_float texture (depth24.h): `depth24_prepare` makes the scratch and descriptors 689 // Footprint copies of a depth24_as_float texture (depth24.h): `depth24_prepare` makes the scratch and descriptors
@@ -725,6 +731,7 @@ struct PreparedQuery {
725 731
726struct Prepared { 732struct Prepared {
727 PredicateCapture predicate; 733 PredicateCapture predicate;
734 std::vector<PredicatedCopyDispatch> predicated_copy;
728 ResourceBinding binding; 735 ResourceBinding binding;
729 VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only) 736 VkImageSubresourceRange range{}; // the barrier range, or the cleared view (cleared aspects only)
730 VkImageView view = VK_NULL_HANDLE; 737 VkImageView view = VK_NULL_HANDLE;
@@ -1577,13 +1584,14 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
1577 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) || 1584 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) ||
1578 std::holds_alternative<CopyResource>(command)) { 1585 std::holds_alternative<CopyResource>(command)) {
1579 if (!copies) return ReplayResult::unsupported; 1586 if (!copies) return ReplayResult::unsupported;
1580 if (predicated) ++out.unpredicated_copies; 1587 if (predicated && !std::holds_alternative<CopyBuffer>(command)) ++out.unpredicated_copies;
1581 const auto [dst_id, src_id] = copy_ids(command); 1588 const auto [dst_id, src_id] = copy_ids(command);
1582 const ResourceBinding* source = resolve(src_id); 1589 const ResourceBinding* source = resolve(src_id);
1583 if (!bind(dst_id) || !source) return ReplayResult::unresolved_resource; 1590 if (!bind(dst_id) || !source) return ReplayResult::unresolved_resource;
1584 if (p.binding.host_readable) { 1591 if (p.binding.host_readable) {
1585 out.wrote_readback = true; 1592 out.wrote_readback = true;
1586 out.readback_src_stages |= VK_PIPELINE_STAGE_2_COPY_BIT; 1593 out.readback_src_stages |= predicated && std::holds_alternative<CopyBuffer>(command)
1594 ? VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT : VK_PIPELINE_STAGE_2_COPY_BIT;
1587 } 1595 }
1588 p.source = *source; 1596 p.source = *source;
1589 ReplayResult result = ReplayResult::success; 1597 ReplayResult result = ReplayResult::success;
@@ -1592,7 +1600,19 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
1592 !detail::within(buffer_copy->dst_offset, buffer_copy->size, p.binding.size) || 1600 !detail::within(buffer_copy->dst_offset, buffer_copy->size, p.binding.size) ||
1593 !detail::within(buffer_copy->src_offset, buffer_copy->size, p.source.size)) 1601 !detail::within(buffer_copy->src_offset, buffer_copy->size, p.source.size))
1594 return ReplayResult::invalid_copy; 1602 return ReplayResult::invalid_copy;
1595 p.buffer_copies.push_back({buffer_copy->src_offset, buffer_copy->dst_offset, buffer_copy->size}); 1603 if (buffer_copy->src_offset > UINT64_MAX - p.source.buffer_offset ||
1604 buffer_copy->dst_offset > UINT64_MAX - p.binding.buffer_offset) return ReplayResult::invalid_copy;
1605 const VkDeviceSize src = p.source.buffer_offset + buffer_copy->src_offset;
1606 const VkDeviceSize dst = p.binding.buffer_offset + buffer_copy->dst_offset;
1607 if (buffer_copy->size > UINT64_MAX - src || buffer_copy->size > UINT64_MAX - dst)
1608 return ReplayResult::invalid_copy;
1609 if (p.source.buffer == p.binding.buffer && src < dst + buffer_copy->size && dst < src + buffer_copy->size)
1610 return ReplayResult::invalid_copy;
1611 if (predicated) {
1612 if (statistics_active || !vk.prepare_predicated_copy || !vk.emit_predicated_copy ||
1613 !vk.prepare_predicated_copy(p.source.buffer, p.binding.buffer, src, dst, buffer_copy->size, p.predicated_copy))
1614 return ReplayResult::unsupported;
1615 } else p.buffer_copies.push_back({src, dst, buffer_copy->size});
1596 } else if (const auto* texture_copy = std::get_if<CopyTexture>(&command)) { 1616 } else if (const auto* texture_copy = std::get_if<CopyTexture>(&command)) {
1597 result = detail::prepare_texture_copy(*texture_copy, p); 1617 result = detail::prepare_texture_copy(*texture_copy, p);
1598 // A depth plane footprint of a D24 texture stored as D32: words of 24-bit UNORM, floats in the image. 1618 // A depth plane footprint of a D24 texture stored as D32: words of 24-bit UNORM, floats in the image.
@@ -2187,7 +2207,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2187 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) || 2207 } else if (std::holds_alternative<CopyBuffer>(command) || std::holds_alternative<CopyTexture>(command) ||
2188 std::holds_alternative<CopyResource>(command)) { 2208 std::holds_alternative<CopyResource>(command)) {
2189 const auto [dst_id, src_id] = copy_ids(command); 2209 const auto [dst_id, src_id] = copy_ids(command);
2190 if (!p.buffer_copies.empty()) { 2210 if (!p.predicated_copy.empty()) {
2211 vk.emit_predicated_copy(command_buffer, p.predicated_copy);
2212 } else if (!p.buffer_copies.empty()) {
2191 vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()), 2213 vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()),
2192 p.buffer_copies.data()); 2214 p.buffer_copies.data());
2193 } else if (!p.image_copies.empty()) { 2215 } else if (!p.image_copies.empty()) {