Skip to content

Commit 5fb8cf2

Browse files
authored
Support layer: Add acceleration structure build serialization (#89)
1 parent d1f04bb commit 5fb8cf2

File tree

7 files changed

+259
-0
lines changed

7 files changed

+259
-0
lines changed

layer_gpu_support/README_LAYER.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ irrespective of other settings.
103103
"pre": false, // Insert full barrier before render passes
104104
"post": false // Insert full barrier after render passes
105105
},
106+
"asbuild": {
107+
"pre": false, // Insert full barrier before acceleration structure builds
108+
"post": false // Insert full barrier after acceleration structure builds
109+
},
106110
"tracerays": {
107111
"pre": false, // Insert full barrier before trace rays
108112
"post": false // Insert full barrier after trace rays

layer_gpu_support/layer_config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"pre": false,
1313
"post": false
1414
},
15+
"asbuild": {
16+
"pre": false,
17+
"post": false
18+
},
1519
"tracerays": {
1620
"pre": false,
1721
"post": false

layer_gpu_support/source/layer_config.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ void LayerConfig::parse_serialization_options(const json& config)
5757
bool s_stream_rp_pre = s_stream.at("renderpass").at("pre");
5858
bool s_stream_rp_post = s_stream.at("renderpass").at("post");
5959

60+
bool s_stream_asb_pre = s_stream.at("asbuild").at("pre");
61+
bool s_stream_asb_post = s_stream.at("asbuild").at("post");
62+
6063
bool s_stream_rt_pre = s_stream.at("tracerays").at("pre");
6164
bool s_stream_rt_post = s_stream.at("tracerays").at("post");
6265

@@ -65,12 +68,19 @@ void LayerConfig::parse_serialization_options(const json& config)
6568

6669
// Write after all options read from JSON so we know it parsed correctly
6770
conf_serialize_queues = (!s_none) && (s_all || s_queue);
71+
6872
conf_serialize_dispatch_pre = (!s_none) && (s_all || s_stream_c_pre);
6973
conf_serialize_dispatch_post = (!s_none) && (s_all || s_stream_c_post);
74+
7075
conf_serialize_render_pass_pre = (!s_none) && (s_all || s_stream_rp_pre);
7176
conf_serialize_render_pass_post = (!s_none) && (s_all || s_stream_rp_post);
77+
78+
conf_serialize_as_build_pre = (!s_none) && (s_all || s_stream_asb_pre);
79+
conf_serialize_as_build_post = (!s_none) && (s_all || s_stream_asb_post);
80+
7281
conf_serialize_trace_rays_pre = (!s_none) && (s_all || s_stream_rt_pre);
7382
conf_serialize_trace_rays_post = (!s_none) && (s_all || s_stream_rt_post);
83+
7484
conf_serialize_transfer_pre = (!s_none) && (s_all || s_stream_tx_pre);
7585
conf_serialize_transfer_post = (!s_none) && (s_all || s_stream_tx_post);
7686

@@ -81,6 +91,8 @@ void LayerConfig::parse_serialization_options(const json& config)
8191
LAYER_LOG(" - Serialize compute post: %d", conf_serialize_dispatch_post);
8292
LAYER_LOG(" - Serialize render pass pre: %d", conf_serialize_render_pass_pre);
8393
LAYER_LOG(" - Serialize render pass post: %d", conf_serialize_render_pass_post);
94+
LAYER_LOG(" - Serialize acceleration structure build pre: %d", conf_serialize_as_build_pre);
95+
LAYER_LOG(" - Serialize acceleration structure build post: %d", conf_serialize_as_build_post);
8496
LAYER_LOG(" - Serialize trace rays pre: %d", conf_serialize_trace_rays_pre);
8597
LAYER_LOG(" - Serialize trace rays post: %d", conf_serialize_trace_rays_post);
8698
LAYER_LOG(" - Serialize transfer pre: %d", conf_serialize_transfer_pre);
@@ -281,6 +293,18 @@ bool LayerConfig::serialize_cmdstream_render_pass_post() const
281293
return conf_serialize_render_pass_post;
282294
}
283295

296+
/* See header for documentation. */
297+
bool LayerConfig::serialize_cmdstream_as_build_pre() const
298+
{
299+
return conf_serialize_as_build_pre;
300+
}
301+
302+
/* See header for documentation. */
303+
bool LayerConfig::serialize_cmdstream_as_build_post() const
304+
{
305+
return conf_serialize_as_build_post;
306+
}
307+
284308
/* See header for documentation. */
285309
bool LayerConfig::serialize_cmdstream_trace_rays_pre() const
286310
{

layer_gpu_support/source/layer_config.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ class LayerConfig
9696
*/
9797
bool serialize_cmdstream_transfer_post() const;
9898

99+
/**
100+
* @brief True if config wants to serialize before acceleration structure build workloads.
101+
*/
102+
bool serialize_cmdstream_as_build_pre() const;
103+
104+
/**
105+
* @brief True if config wants to serialize after acceleration structure build workloads.
106+
*/
107+
bool serialize_cmdstream_as_build_post() const;
108+
99109
// Config queries for shaders
100110

101111
/**
@@ -202,6 +212,16 @@ class LayerConfig
202212
*/
203213
bool conf_serialize_trace_rays_post {false};
204214

215+
/**
216+
* @brief True if we force serialize before acceleration structure build workloads.
217+
*/
218+
bool conf_serialize_as_build_pre {false};
219+
220+
/**
221+
* @brief True if we force serialize after acceleration structure build workloads.
222+
*/
223+
bool conf_serialize_as_build_post {false};
224+
205225
/**
206226
* @brief True if we force serialize before transfer workloads.
207227
*/

layer_gpu_support/source/layer_device_functions.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,36 @@ VKAPI_ATTR void VKAPI_CALL
133133
uint32_t height,
134134
uint32_t depth);
135135

136+
// Commands for acceleration structure builds
137+
138+
/* See Vulkan API for documentation. */
139+
template<>
140+
VKAPI_ATTR void VKAPI_CALL layer_vkCmdBuildAccelerationStructuresIndirectKHR<user_tag>(
141+
VkCommandBuffer commandBuffer,
142+
uint32_t infoCount,
143+
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
144+
const VkDeviceAddress* pIndirectDeviceAddresses,
145+
const uint32_t* pIndirectStrides,
146+
const uint32_t* const* ppMaxPrimitiveCounts);
147+
148+
/* See Vulkan API for documentation. */
149+
template<>
150+
VKAPI_ATTR void VKAPI_CALL layer_vkCmdBuildAccelerationStructuresKHR<user_tag>(
151+
VkCommandBuffer commandBuffer,
152+
uint32_t infoCount,
153+
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
154+
const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos);
155+
156+
/* See Vulkan API for documentation. */
157+
template<>
158+
VKAPI_ATTR void VKAPI_CALL layer_vkCmdBuildAccelerationStructuresIndirectKHR<user_tag>(
159+
VkCommandBuffer commandBuffer,
160+
uint32_t infoCount,
161+
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
162+
const VkDeviceAddress* pIndirectDeviceAddresses,
163+
const uint32_t* pIndirectStrides,
164+
const uint32_t* const* ppMaxPrimitiveCounts);
165+
136166
// Commands for transfers
137167

138168
/* See Vulkan API for documentation. */
@@ -229,6 +259,24 @@ VKAPI_ATTR void VKAPI_CALL layer_vkCmdCopyImageToBuffer<user_tag>(VkCommandBuffe
229259
uint32_t regionCount,
230260
const VkBufferImageCopy* pRegions);
231261

262+
/* See Vulkan API for documentation. */
263+
template<>
264+
VKAPI_ATTR void VKAPI_CALL
265+
layer_vkCmdCopyAccelerationStructureKHR<user_tag>(VkCommandBuffer commandBuffer,
266+
const VkCopyAccelerationStructureInfoKHR* pInfo);
267+
268+
/* See Vulkan API for documentation. */
269+
template<>
270+
VKAPI_ATTR void VKAPI_CALL
271+
layer_vkCmdCopyAccelerationStructureToMemoryKHR<user_tag>(VkCommandBuffer commandBuffer,
272+
const VkCopyAccelerationStructureToMemoryInfoKHR* pInfo);
273+
274+
/* See Vulkan API for documentation. */
275+
template<>
276+
VKAPI_ATTR void VKAPI_CALL
277+
layer_vkCmdCopyMemoryToAccelerationStructureKHR<user_tag>(VkCommandBuffer commandBuffer,
278+
const VkCopyMemoryToAccelerationStructureInfoKHR* pInfo);
279+
232280
/* See Vulkan API for documentation. */
233281
template<>
234282
VKAPI_ATTR void VKAPI_CALL

layer_gpu_support/source/layer_device_functions_trace_rays.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,58 @@ static void postTraceRays(Device* layer, VkCommandBuffer commandBuffer)
8282
nullptr);
8383
}
8484

85+
/**
86+
* @brief Pre-build-acceleration-structure code injection point.
87+
*
88+
* @param layer The layer context for the device.
89+
* @param commandBuffer The command buffer we are recording.
90+
*/
91+
static void preAccelerationStructureBuild(Device* layer, VkCommandBuffer commandBuffer)
92+
{
93+
if (!layer->instance->config.serialize_cmdstream_as_build_pre())
94+
{
95+
return;
96+
}
97+
98+
// Execution dependency
99+
layer->driver.vkCmdPipelineBarrier(commandBuffer,
100+
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
101+
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
102+
0,
103+
0,
104+
nullptr,
105+
0,
106+
nullptr,
107+
0,
108+
nullptr);
109+
}
110+
111+
/**
112+
* @brief Post-build-acceleration-structure code injection point.
113+
*
114+
* @param layer The layer context for the device.
115+
* @param commandBuffer The command buffer we are recording.
116+
*/
117+
static void postAccelerationStructureBuild(Device* layer, VkCommandBuffer commandBuffer)
118+
{
119+
if (!layer->instance->config.serialize_cmdstream_as_build_post())
120+
{
121+
return;
122+
}
123+
124+
// Execution dependency
125+
layer->driver.vkCmdPipelineBarrier(commandBuffer,
126+
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
127+
VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
128+
0,
129+
0,
130+
nullptr,
131+
0,
132+
nullptr,
133+
0,
134+
nullptr);
135+
}
136+
85137
/* See Vulkan API for documentation. */
86138
template<>
87139
VKAPI_ATTR void VKAPI_CALL layer_vkCmdTraceRaysIndirect2KHR<user_tag>(VkCommandBuffer commandBuffer,
@@ -162,3 +214,52 @@ VKAPI_ATTR void VKAPI_CALL
162214
depth);
163215
postTraceRays(layer, commandBuffer);
164216
}
217+
218+
/* See Vulkan API for documentation. */
219+
template<>
220+
VKAPI_ATTR void VKAPI_CALL layer_vkCmdBuildAccelerationStructuresIndirectKHR<user_tag>(
221+
VkCommandBuffer commandBuffer,
222+
uint32_t infoCount,
223+
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
224+
const VkDeviceAddress* pIndirectDeviceAddresses,
225+
const uint32_t* pIndirectStrides,
226+
const uint32_t* const* ppMaxPrimitiveCounts)
227+
{
228+
LAYER_TRACE(__func__);
229+
230+
// Hold the lock to access layer-wide global store
231+
std::unique_lock<std::mutex> lock {g_vulkanLock};
232+
auto* layer = Device::retrieve(commandBuffer);
233+
234+
// Release the lock to call into the driver
235+
lock.unlock();
236+
preAccelerationStructureBuild(layer, commandBuffer);
237+
layer->driver.vkCmdBuildAccelerationStructuresIndirectKHR(commandBuffer,
238+
infoCount,
239+
pInfos,
240+
pIndirectDeviceAddresses,
241+
pIndirectStrides,
242+
ppMaxPrimitiveCounts);
243+
postAccelerationStructureBuild(layer, commandBuffer);
244+
}
245+
246+
/* See Vulkan API for documentation. */
247+
template<>
248+
VKAPI_ATTR void VKAPI_CALL layer_vkCmdBuildAccelerationStructuresKHR<user_tag>(
249+
VkCommandBuffer commandBuffer,
250+
uint32_t infoCount,
251+
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
252+
const VkAccelerationStructureBuildRangeInfoKHR* const* ppBuildRangeInfos)
253+
{
254+
LAYER_TRACE(__func__);
255+
256+
// Hold the lock to access layer-wide global store
257+
std::unique_lock<std::mutex> lock {g_vulkanLock};
258+
auto* layer = Device::retrieve(commandBuffer);
259+
260+
// Release the lock to call into the driver
261+
lock.unlock();
262+
preAccelerationStructureBuild(layer, commandBuffer);
263+
layer->driver.vkCmdBuildAccelerationStructuresKHR(commandBuffer, infoCount, pInfos, ppBuildRangeInfos);
264+
postAccelerationStructureBuild(layer, commandBuffer);
265+
}

layer_gpu_support/source/layer_device_functions_transfer.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,61 @@ VKAPI_ATTR void VKAPI_CALL
400400
layer->driver.vkCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
401401
postTransfer(layer, commandBuffer);
402402
}
403+
404+
/* See Vulkan API for documentation. */
405+
template<>
406+
VKAPI_ATTR void VKAPI_CALL
407+
layer_vkCmdCopyAccelerationStructureKHR<user_tag>(VkCommandBuffer commandBuffer,
408+
const VkCopyAccelerationStructureInfoKHR* pInfo)
409+
{
410+
LAYER_TRACE(__func__);
411+
412+
// Hold the lock to access layer-wide global store
413+
std::unique_lock<std::mutex> lock {g_vulkanLock};
414+
auto* layer = Device::retrieve(commandBuffer);
415+
416+
// Release the lock to call into the driver
417+
lock.unlock();
418+
419+
preTransfer(layer, commandBuffer);
420+
layer->driver.vkCmdCopyAccelerationStructureKHR(commandBuffer, pInfo);
421+
postTransfer(layer, commandBuffer);
422+
}
423+
424+
/* See Vulkan API for documentation. */
425+
template<>
426+
VKAPI_ATTR void VKAPI_CALL
427+
layer_vkCmdCopyAccelerationStructureToMemoryKHR<user_tag>(VkCommandBuffer commandBuffer,
428+
const VkCopyAccelerationStructureToMemoryInfoKHR* pInfo)
429+
{
430+
LAYER_TRACE(__func__);
431+
432+
// Hold the lock to access layer-wide global store
433+
std::unique_lock<std::mutex> lock {g_vulkanLock};
434+
auto* layer = Device::retrieve(commandBuffer);
435+
436+
// Release the lock to call into the driver
437+
lock.unlock();
438+
preTransfer(layer, commandBuffer);
439+
layer->driver.vkCmdCopyAccelerationStructureToMemoryKHR(commandBuffer, pInfo);
440+
postTransfer(layer, commandBuffer);
441+
}
442+
443+
/* See Vulkan API for documentation. */
444+
template<>
445+
VKAPI_ATTR void VKAPI_CALL
446+
layer_vkCmdCopyMemoryToAccelerationStructureKHR<user_tag>(VkCommandBuffer commandBuffer,
447+
const VkCopyMemoryToAccelerationStructureInfoKHR* pInfo)
448+
{
449+
LAYER_TRACE(__func__);
450+
451+
// Hold the lock to access layer-wide global store
452+
std::unique_lock<std::mutex> lock {g_vulkanLock};
453+
auto* layer = Device::retrieve(commandBuffer);
454+
455+
// Release the lock to call into the driver
456+
lock.unlock();
457+
preTransfer(layer, commandBuffer);
458+
layer->driver.vkCmdCopyMemoryToAccelerationStructureKHR(commandBuffer, pInfo);
459+
postTransfer(layer, commandBuffer);
460+
}

0 commit comments

Comments
 (0)