-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathContext.cpp
More file actions
368 lines (296 loc) · 13.1 KB
/
Copy pathContext.cpp
File metadata and controls
368 lines (296 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/commands/Commands.h>
#include <vsg/commands/CopyAndReleaseBuffer.h>
#include <vsg/commands/CopyAndReleaseImage.h>
#include <vsg/commands/PipelineBarrier.h>
#include <vsg/core/Version.h>
#include <vsg/io/Logger.h>
#include <vsg/nodes/Geometry.h>
#include <vsg/nodes/Group.h>
#include <vsg/nodes/LOD.h>
#include <vsg/nodes/QuadGroup.h>
#include <vsg/nodes/StateGroup.h>
#include <vsg/state/DescriptorSet.h>
#include <vsg/state/DynamicState.h>
#include <vsg/ui/UIEvent.h>
#include <vsg/vk/CommandBuffer.h>
#include <vsg/vk/Context.h>
#include <vsg/vk/DescriptorPools.h>
#include <vsg/vk/RenderPass.h>
#include <vsg/vk/State.h>
using namespace vsg;
/////////////////////////////////////////////////////////////////////////////////////////
//
// BuildAccelerationStructureCommand
//
BuildAccelerationStructureCommand::BuildAccelerationStructureCommand(Device* device, const VkAccelerationStructureBuildGeometryInfoKHR& info, const VkAccelerationStructureKHR& structure, const std::vector<uint32_t>& primitiveCounts) :
_device(device),
_accelerationStructureInfo(info),
_accelerationStructure(structure)
{
_accelerationStructureInfo.mode = VK_BUILD_ACCELERATION_STRUCTURE_MODE_BUILD_KHR;
_accelerationStructureInfo.dstAccelerationStructure = _accelerationStructure;
_accelerationStructureGeometries = std::vector<VkAccelerationStructureGeometryKHR>(_accelerationStructureInfo.pGeometries, _accelerationStructureInfo.pGeometries + _accelerationStructureInfo.geometryCount);
_accelerationStructureInfo.pGeometries = _accelerationStructureGeometries.data();
for (const auto c : primitiveCounts)
{
_accelerationStructureBuildRangeInfos.emplace_back();
_accelerationStructureBuildRangeInfos.back().firstVertex = 0;
_accelerationStructureBuildRangeInfos.back().primitiveCount = c;
_accelerationStructureBuildRangeInfos.back().primitiveOffset = 0;
_accelerationStructureBuildRangeInfos.back().transformOffset = 0;
}
}
void BuildAccelerationStructureCommand::record(CommandBuffer& commandBuffer) const
{
auto extensions = commandBuffer.getDevice()->getExtensions();
const VkAccelerationStructureBuildRangeInfoKHR* rangeInfos = _accelerationStructureBuildRangeInfos.data();
extensions->vkCmdBuildAccelerationStructuresKHR(
commandBuffer,
1,
&_accelerationStructureInfo,
&rangeInfos);
VkMemoryBarrier memoryBarrier;
memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
memoryBarrier.pNext = nullptr;
memoryBarrier.srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
memoryBarrier.dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR | VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR;
vkCmdPipelineBarrier(commandBuffer, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, 0, 1, &memoryBarrier, 0, 0, 0, 0);
}
void BuildAccelerationStructureCommand::setScratchBuffer(ref_ptr<Buffer> scratchBuffer)
{
_scratchBuffer = scratchBuffer;
auto extensions = _device->getExtensions();
VkBufferDeviceAddressInfo devAddressInfo{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, nullptr, _scratchBuffer->vk(_device->deviceID)};
_accelerationStructureInfo.scratchData.deviceAddress = extensions->vkGetBufferDeviceAddressKHR(*_device, &devAddressInfo);
}
/////////////////////////////////////////////////////////////////////////////////////////
//
// vsg::Context
//
Context::Context(Device* in_device, const ResourceRequirements& in_resourceRequirements) :
deviceID(in_device->deviceID),
device(in_device),
resourceRequirements(in_resourceRequirements),
scratchBufferSize(0)
{
//semaphore = vsg::Semaphore::create(device);
scratchMemory = ScratchMemory::create(4096);
vsg::debug("Context::Context() ", this);
deviceMemoryBufferPools = device->deviceMemoryBufferPools.ref_ptr();
if (!deviceMemoryBufferPools)
{
device->deviceMemoryBufferPools = deviceMemoryBufferPools = MemoryBufferPools::create("Device_MemoryBufferPool", device, in_resourceRequirements);
vsg::debug("Context::Context() creating new deviceMemoryBufferPools = ", deviceMemoryBufferPools);
}
else
{
vsg::debug("Context::Context() reusing deviceMemoryBufferPools = ", deviceMemoryBufferPools);
}
stagingMemoryBufferPools = device->stagingMemoryBufferPools.ref_ptr();
if (!stagingMemoryBufferPools)
{
device->stagingMemoryBufferPools = stagingMemoryBufferPools = MemoryBufferPools::create("Staging_MemoryBufferPool", device, in_resourceRequirements);
vsg::debug("Context::Context() creating new stagingMemoryBufferPools = ", stagingMemoryBufferPools);
}
else
{
vsg::debug("Context::Context() reusing stagingMemoryBufferPools = ", stagingMemoryBufferPools);
}
descriptorPools = device->descriptorPools.ref_ptr();
if (!descriptorPools)
{
device->descriptorPools = descriptorPools = DescriptorPools::create(device);
vsg::debug("Context::Context() creating new descriptorPools = ", descriptorPools);
}
else
{
vsg::debug("Context::Context() reusing descriptorPools = ", descriptorPools);
}
if ((resourceRequirements.viewportStateHint & DYNAMIC_VIEWPORTSTATE))
{
defaultPipelineStates.push_back(DynamicState::create(VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR));
}
}
Context::Context(const Context& context) :
Inherit(context),
deviceID(context.deviceID),
device(context.device),
view(context.view),
viewID(context.viewID),
mask(context.mask),
viewDependentState(context.viewDependentState),
renderPass(context.renderPass),
defaultPipelineStates(context.defaultPipelineStates),
overridePipelineStates(context.overridePipelineStates),
descriptorPools(context.descriptorPools),
graphicsQueue(context.graphicsQueue),
commandPool(context.commandPool),
deviceMemoryBufferPools(context.deviceMemoryBufferPools),
stagingMemoryBufferPools(context.stagingMemoryBufferPools),
scratchBufferSize(context.scratchBufferSize)
{
scratchMemory = ScratchMemory::create(4096);
}
Context::~Context()
{
if (requiresWaitForCompletion)
{
waitForCompletion();
}
}
ref_ptr<CommandBuffer> Context::getOrCreateCommandBuffer()
{
if (!commandBuffer)
{
commandBuffer = commandPool->allocate();
}
return commandBuffer;
}
ShaderCompiler* Context::getOrCreateShaderCompiler()
{
if (shaderCompiler) return shaderCompiler;
#if VSG_SUPPORTS_ShaderCompiler
shaderCompiler = ShaderCompiler::create();
if (device && device->getInstance())
{
shaderCompiler->defaults->vulkanVersion = device->getInstance()->apiVersion;
}
#endif
return shaderCompiler;
}
VkResult Context::reserve(ResourceRequirements& requirements)
{
CPU_INSTRUMENTATION_L2_NC(instrumentation, "Context reserve", COLOR_COMPILE)
VkResult result = VK_SUCCESS;
if (deviceMemoryBufferPools->compileTraversalUseReserve) result = deviceMemoryBufferPools->reserve(requirements);
resourceRequirements.maxSlots.update(requirements.maxSlots);
descriptorPools->reserve(requirements);
return result;
}
ref_ptr<DescriptorSet::Implementation> Context::allocateDescriptorSet(DescriptorSetLayout* descriptorSetLayout)
{
return descriptorPools->allocateDescriptorSet(descriptorSetLayout);
}
void Context::copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest)
{
CPU_INSTRUMENTATION_L2_NC(instrumentation, "Context copy", COLOR_COMPILE)
// info("Context::copy(", data, ", ", dest, ") ", this, ", ", transferTask);
if (!copyImageCmd)
{
copyImageCmd = CopyAndReleaseImage::create(stagingMemoryBufferPools);
commands.push_back(copyImageCmd);
}
copyImageCmd->copy(data, dest);
}
void Context::copy(ref_ptr<Data> data, ref_ptr<ImageInfo> dest, uint32_t numMipMapLevels)
{
CPU_INSTRUMENTATION_L2_NC(instrumentation, "Context copy", COLOR_COMPILE)
// info("Context::copy(", data, ", ", dest, ") ", this, ", ", transferTask);
if (!copyImageCmd)
{
copyImageCmd = CopyAndReleaseImage::create(stagingMemoryBufferPools);
commands.push_back(copyImageCmd);
}
copyImageCmd->copy(data, dest, numMipMapLevels);
}
void Context::copy(ref_ptr<BufferInfo> src, ref_ptr<BufferInfo> dest)
{
CPU_INSTRUMENTATION_L2_NC(instrumentation, "Context copy", COLOR_COMPILE)
// info("Context::copy(", src, ", ", dest, ") ", this, ", ", transferTask);
if (!copyBufferCmd)
{
copyBufferCmd = CopyAndReleaseBuffer::create();
commands.emplace_back(copyBufferCmd);
}
copyBufferCmd->add(src, dest);
}
bool Context::record()
{
CPU_INSTRUMENTATION_L1_NC(instrumentation, "Context record", COLOR_COMPILE)
if (commands.empty() && buildAccelerationStructureCommands.empty()) return false;
if (!fence)
{
fence = vsg::Fence::create(device);
}
else
{
fence->reset();
}
getOrCreateCommandBuffer();
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(*commandBuffer, &beginInfo);
{
COMMAND_BUFFER_INSTRUMENTATION(instrumentation, *commandBuffer, "Context record", COLOR_COMPILE)
// issue commands of interest
{
for (auto& command : commands) command->record(*commandBuffer);
}
// create scratch buffer and issue build acceleration structure commands
if (scratchBufferSize > 0)
{
VkMemoryAllocateFlagsInfo memFlags = {};
memFlags.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO;
memFlags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT;
ref_ptr<Buffer> scratchBuffer = vsg::createBufferAndMemory(device, scratchBufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &memFlags);
for (auto& command : buildAccelerationStructureCommands)
{
command->setScratchBuffer(scratchBuffer);
command->record(*commandBuffer);
}
}
}
vkEndCommandBuffer(*commandBuffer);
VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = commandBuffer->data();
if (semaphore)
{
vsg::info("Context::record() semaphore assigned to submitInfo ", semaphore);
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = semaphore->data();
submitInfo.pWaitDstStageMask = &waitDstStageMask;
}
else
{
submitInfo.signalSemaphoreCount = 0;
submitInfo.pSignalSemaphores = nullptr;
}
graphicsQueue->submit(submitInfo, fence);
requiresWaitForCompletion = true;
return true;
}
void Context::waitForCompletion()
{
CPU_INSTRUMENTATION_L1_NC(instrumentation, "Context waitForCompletion", COLOR_COMPILE)
if (!requiresWaitForCompletion || !commandBuffer || !fence)
{
return;
}
// auto start_point = vsg::clock::now();
// we must wait for the queue to empty before we can safely clean up the commandBuffer
uint64_t timeout = 1000000000;
VkResult result;
while ((result = fence->wait(timeout)) == VK_TIMEOUT)
{
info("Context::waitForCompletion() ", this, " fence->wait() timed out, trying again.");
}
if (result != VK_SUCCESS)
{
info("Context::waitForCompletion() ", this, " fence->wait() failed with error. VkResult = ", result);
}
//vsg::info("Context::waitForCompletion() ", std::chrono::duration<double, std::chrono::milliseconds::period>(vsg::clock::now() - start_point).count());
requiresWaitForCompletion = false;
commands.clear();
copyImageCmd = nullptr;
copyBufferCmd = nullptr;
}