载入中...
搜索中...
未找到
VulkanComputeShader.cpp
浏览该文件的文档.
3
4#include "common/Exception.h"
5
6#include <array>
7
8namespace eve::gpgpu {
9
11 if (!device_ || !static_cast<VkDevice>(device_->instance)) return;
12 auto &dev = *device_;
13 if (pipeline_) {
14 dev->destroyPipeline(pipeline_, device_->allocation_callbacks);
15 pipeline_ = vk::Pipeline{};
16 }
17 if (pipelineLayout_) {
18 dev->destroyPipelineLayout(pipelineLayout_, device_->allocation_callbacks);
19 pipelineLayout_ = vk::PipelineLayout{};
20 }
21 if (setLayout_) {
22 dev->destroyDescriptorSetLayout(setLayout_, device_->allocation_callbacks);
23 setLayout_ = vk::DescriptorSetLayout{};
24 }
25 for (vk::DescriptorPool pool : retiredPools_) dev->destroyDescriptorPool(pool, device_->allocation_callbacks);
26 if (descriptorPool_) {
27 // Sets allocated from the pool are freed with it.
28 dev->destroyDescriptorPool(descriptorPool_, device_->allocation_callbacks);
29 descriptorPool_ = vk::DescriptorPool{};
30 descriptorSet_ = vk::DescriptorSet{};
31 }
32 if (module_) {
33 dev->destroyShaderModule(module_, device_->allocation_callbacks);
34 module_ = vk::ShaderModule{};
35 }
36 if (dummyBuffer_) {
37#if defined(VKB_ENABLE_VMA)
38 if (dummyVmaAllocation_) {
39 vmaDestroyBuffer(device_->vma_allocator, static_cast<VkBuffer>(dummyBuffer_),
40 dummyVmaAllocation_);
41 dummyVmaAllocation_ = VK_NULL_HANDLE;
42 dummyBuffer_ = vk::Buffer{};
43 dummyMemory_ = vk::DeviceMemory{};
44 } else
45#endif
46 {
47 dev->destroyBuffer(dummyBuffer_, device_->allocation_callbacks);
48 dummyBuffer_ = vk::Buffer{};
49 }
50 }
51 if (dummyMemory_) {
52 dev->freeMemory(dummyMemory_, device_->allocation_callbacks);
53 dummyMemory_ = vk::DeviceMemory{};
54 }
55 device_ = nullptr;
56}
57
59 if (binding < 0 || binding >= kMaxBindings) return;
60 bindings_[size_t(binding)] = buffer;
61 descriptorsDirty_ = true;
62}
63
65 if (binding < 0 || binding >= kMaxBindings) return nullptr;
66 return bindings_[size_t(binding)];
67}
68
70 if (index < 0 || index >= kMaxFloats) return;
71 push_[size_t(index)] = value;
72}
73
75 if (index < 0 || index >= kMaxFloats) return 0.f;
76 return push_[size_t(index)];
77}
78
80 bindings_.fill(nullptr);
81 descriptorsDirty_ = true;
82}
83
85 if (!descriptorsDirty_ && descriptorSet_) return;
86
87 if (descriptorSet_) {
88 if (activeSequenceCount_ > 0) {
89 // Keep each immutable binding snapshot alive until all sequences
90 // that may reference it have retired.
92 } else {
93 device->freeDescriptorSets(descriptorPool_, descriptorSet_);
95 }
96 descriptorSet_ = vk::DescriptorSet{};
97 }
98
99 constexpr uint32_t setsPerPool = 64;
100 if (!descriptorPool_ || allocatedSets_ == setsPerPool) {
101 // Long recording batches can retain more than one pool of snapshots.
102 // Grow before exhausting maxSets; some drivers report fragmentation
103 // while others allow allocations beyond the declared pool capacity.
104 vk::DescriptorPoolSize poolSize{vk::DescriptorType::eStorageBuffer, uint32_t(kMaxBindings) * setsPerPool};
105 vk::DescriptorPoolCreateInfo poolInfo{};
106 poolInfo.flags = vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet;
107 poolInfo.maxSets = setsPerPool;
108 poolInfo.poolSizeCount = 1;
109 poolInfo.pPoolSizes = &poolSize;
110 const auto nextPool = device->createDescriptorPool(poolInfo, device.allocation_callbacks);
111 try {
113 } catch (...) {
114 device->destroyDescriptorPool(nextPool, device.allocation_callbacks);
115 throw;
116 }
117 descriptorPool_ = nextPool;
118 allocatedSets_ = 0;
119 }
120
121 vk::DescriptorSetAllocateInfo alloc{};
122 alloc.descriptorPool = descriptorPool_;
123 alloc.descriptorSetCount = 1;
124 alloc.pSetLayouts = &setLayout_;
125 auto sets = device->allocateDescriptorSets(alloc);
126 descriptorSet_ = sets[0];
128
129 // Dummy 4-byte SSBO for unused bindings (validation requires all layout slots valid).
130 if (!dummyBuffer_) {
131 using buf = vk::BufferUsageFlagBits;
132 using pfb = vk::MemoryPropertyFlagBits;
133 vkb::GenericBuffer tmp(device, buf::eStorageBuffer | buf::eTransferDst, 4, pfb::eDeviceLocal);
134 dummyBuffer_ = tmp.buffer;
135 dummyMemory_ = tmp.memory;
136#if defined(VKB_ENABLE_VMA)
137 dummyVmaAllocation_ = tmp.vma_allocation;
138#endif
139 // Without detach(), tmp's destructor frees the handles, leaving the
140 // dummy SSBO dangling: descriptor writes reference a destroyed buffer
141 // (crashes MoltenVK) and ~VulkanComputeShader double-frees it.
142 tmp.detach();
143 }
144
145 std::array<vk::DescriptorBufferInfo, kMaxBindings> infos{};
146 std::array<vk::WriteDescriptorSet, kMaxBindings> writes{};
147 for (int i = 0; i < kMaxBindings; ++i) {
148 auto *vb = dynamic_cast<VulkanGpuBuffer *>(bindings_[size_t(i)]);
149 infos[size_t(i)].buffer = (vb && vb->buffer_) ? vb->buffer_ : dummyBuffer_;
150 infos[size_t(i)].offset = 0;
151 infos[size_t(i)].range = (vb && vb->buffer_) ? vb->size_ : vk::DeviceSize(4);
152 writes[size_t(i)].dstSet = descriptorSet_;
153 writes[size_t(i)].dstBinding = uint32_t(i);
154 writes[size_t(i)].dstArrayElement = 0;
155 writes[size_t(i)].descriptorCount = 1;
156 writes[size_t(i)].descriptorType = vk::DescriptorType::eStorageBuffer;
157 writes[size_t(i)].pBufferInfo = &infos[size_t(i)];
158 }
159 device->updateDescriptorSets(kMaxBindings, writes.data(), 0, nullptr);
160
161 descriptorsDirty_ = false;
162}
163
165
169
171 if (activeSequenceCount_ > 0) return;
172 if (pendingSets_.empty()) return;
173 for (const auto &lease : pendingSets_) {
174 if (lease.pool != descriptorPool_) continue;
175 device->freeDescriptorSets(lease.pool, lease.set);
177 }
178 pendingSets_.clear();
179 // Every set in a retired pool is now idle. Destroying the whole pool also
180 // releases its descriptors without retaining a high-water allocation.
181 for (vk::DescriptorPool pool : retiredPools_) device->destroyDescriptorPool(pool, device.allocation_callbacks);
182 retiredPools_.clear();
183}
184
185} // namespace eve::gpgpu
vkb::Device & device
std::unique_ptr< gpgpu::GpuBuffer > buffer
Definition OnnxGpgpu.cpp:26
eve::action::ActionVfxBinding binding
PrefabLease lease
Object::Ptr value
uint32_t index
static constexpr int kMaxBindings
static constexpr int kMaxFloats
std::array< float, kMaxFloats > push_
Backend-agnostic GPU buffer for compute (storage) or CPU staging transfers. Squirrel-owned; derived c...
Definition GpuBuffer.h:20
std::vector< vk::DescriptorPool > retiredPools_
float getFloat(int index) const override
Returns the float.
void releasePendingDescriptors(vkb::Device &device)
Release pending descriptors.
void clearBindings() override
清空全部绑定。
void bindBuffer(int binding, GpuBuffer *buffer) override
绑定/解绑 GpuBuffer 到指定 binding。
vk::DescriptorSetLayout setLayout_
std::array< GpuBuffer *, kMaxBindings > bindings_
std::vector< DescriptorLease > pendingSets_
void flushDescriptors(vkb::Device &device)
内部:把脏绑定刷成 descriptor set。
GpuBuffer * getBoundBuffer(int binding) const override
Returns the bound buffer.
~VulkanComputeShader() override
Releases VulkanComputeShader resources.
void setFloat(int index, float value) override
标量 uniform 读写。
Vulkan 计算缓冲区实现(storage/vertex buffer + device memory)。
Fluids module — interactive surface fluid simulation.
Definition Fluids.h:35