-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgpu_draw_dispatch.cpp
More file actions
646 lines (518 loc) · 24.1 KB
/
gpu_draw_dispatch.cpp
File metadata and controls
646 lines (518 loc) · 24.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
// Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
//
// 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.
#include "gpu_draw_dispatch.h"
#include "example_default.h"
#include "example_dynamic_state.h"
#include "common/vk_common.h"
#include "common/vk_initializers.h"
#include "gltf_loader.h"
#include "gui.h"
#include "platform/platform.h"
#include "scene_graph/components/sub_mesh.h"
#include "scene_graph/components/image.h"
#include "benchmark_mode/benchmark_mode.h"
#include "stop_after/stop_after.h"
#include <bitset>
#include <set>
#include <unordered_map>
namespace GpuDrawDispatch
{
PFN_vkCreateExecutionGraphPipelinesAMDX vkCreateExecutionGraphPipelinesAMDX;
PFN_vkGetExecutionGraphPipelineScratchSizeAMDX vkGetExecutionGraphPipelineScratchSizeAMDX;
PFN_vkGetExecutionGraphPipelineNodeIndexAMDX vkGetExecutionGraphPipelineNodeIndexAMDX;
PFN_vkCmdInitializeGraphScratchMemoryAMDX vkCmdInitializeGraphScratchMemoryAMDX;
PFN_vkCmdDispatchGraphAMDX vkCmdDispatchGraphAMDX;
PFN_vkCmdDispatchGraphIndirectAMDX vkCmdDispatchGraphIndirectAMDX;
PFN_vkCmdDispatchGraphIndirectCountAMDX vkCmdDispatchGraphIndirectCountAMDX;
static void load_extension_function_pointers(VkDevice device)
{
vkCreateExecutionGraphPipelinesAMDX = (PFN_vkCreateExecutionGraphPipelinesAMDX) vkGetDeviceProcAddr(device, "vkCreateExecutionGraphPipelinesAMDX");
vkGetExecutionGraphPipelineScratchSizeAMDX = (PFN_vkGetExecutionGraphPipelineScratchSizeAMDX)vkGetDeviceProcAddr(device, "vkGetExecutionGraphPipelineScratchSizeAMDX");
vkGetExecutionGraphPipelineNodeIndexAMDX = (PFN_vkGetExecutionGraphPipelineNodeIndexAMDX) vkGetDeviceProcAddr(device, "vkGetExecutionGraphPipelineNodeIndexAMDX");
vkCmdInitializeGraphScratchMemoryAMDX = (PFN_vkCmdInitializeGraphScratchMemoryAMDX) vkGetDeviceProcAddr(device, "vkCmdInitializeGraphScratchMemoryAMDX");
vkCmdDispatchGraphAMDX = (PFN_vkCmdDispatchGraphAMDX) vkGetDeviceProcAddr(device, "vkCmdDispatchGraphAMDX");
vkCmdDispatchGraphIndirectAMDX = (PFN_vkCmdDispatchGraphIndirectAMDX) vkGetDeviceProcAddr(device, "vkCmdDispatchGraphIndirectAMDX");
vkCmdDispatchGraphIndirectCountAMDX = (PFN_vkCmdDispatchGraphIndirectCountAMDX) vkGetDeviceProcAddr(device, "vkCmdDispatchGraphIndirectCountAMDX");
}
void GpuDrawDispatch::finish()
{
VulkanSample::finish();
if (device)
{
if (example)
{
example->free_resources();
}
for (const auto& gui_framebuffer : per_frame_gui_framebuffer)
{
vkDestroyFramebuffer(device->get_handle(), gui_framebuffer, nullptr);
}
for (const auto& shader_module : shader_module_cache)
{
vkDestroyShaderModule(device->get_handle(), shader_module.second, nullptr);
}
vkDestroyPipelineCache(device->get_handle(), pipeline_cache, nullptr);
vkDestroyRenderPass(device->get_handle(), gui_render_pass, nullptr);
}
}
GpuDrawDispatch::GpuDrawDispatch()
{
set_api_version(VK_MAKE_VERSION(1, 3, 0));
}
void GpuDrawDispatch::request_gpu_features(vkb::PhysicalDevice &gpu)
{
auto &requested_features = gpu.get_mutable_requested_features();
// Clamp if supported, it's better performance
requested_features.depthClamp = gpu.get_features().depthClamp;
requested_features.samplerAnisotropy = gpu.get_features().samplerAnisotropy;
requested_features.tessellationShader = gpu.get_features().tessellationShader;
requested_features.geometryShader = gpu.get_features().geometryShader;
// Not needed
requested_features.robustBufferAccess = VK_FALSE;
const auto& descriptorIndexingFeatures = gpu.request_extension_features<VkPhysicalDeviceDescriptorIndexingFeatures>(
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES);
// Required for non-uniform texture sampling in a workgroup
assert(descriptorIndexingFeatures.shaderSampledImageArrayNonUniformIndexing == VK_TRUE);
// Set up VK_AMDX_shader_enqueue extension
add_device_extension(VK_AMDX_SHADER_ENQUEUE_EXTENSION_NAME);
const auto& shaderEnqueueFeatures = gpu.request_extension_features<VkPhysicalDeviceShaderEnqueueFeaturesAMDX>(
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ENQUEUE_FEATURES_AMDX);
assert(shaderEnqueueFeatures.shaderEnqueue == VK_TRUE);
assert(shaderEnqueueFeatures.shaderMeshEnqueue == VK_TRUE);
// Request the BDA extension -- this is how the framework enables support in VMA
add_device_extension(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
const auto& bufferDeviceAddressFeatures = gpu.request_extension_features<VkPhysicalDeviceBufferDeviceAddressFeatures>(
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES);
assert(bufferDeviceAddressFeatures.bufferDeviceAddress == VK_TRUE);
add_device_extension(VK_EXT_MESH_SHADER_EXTENSION_NAME);
const auto& meshShaderFeatures = gpu.request_extension_features<VkPhysicalDeviceMeshShaderFeaturesEXT>(
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MESH_SHADER_FEATURES_EXT);
assert(meshShaderFeatures.meshShader == VK_TRUE);
memset(&shader_enqueue_properties, 0, sizeof(shader_enqueue_properties));
shader_enqueue_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ENQUEUE_PROPERTIES_AMDX;
VkPhysicalDeviceProperties2 physicalDeviceProperties = {};
physicalDeviceProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
physicalDeviceProperties.pNext = &shader_enqueue_properties;
vkGetPhysicalDeviceProperties2(gpu.get_handle(), &physicalDeviceProperties);
// Ensure the shader enqueue extension is available
uint32_t device_extension_count;
VK_CHECK(vkEnumerateDeviceExtensionProperties(gpu.get_handle(), nullptr, &device_extension_count, nullptr));
std::vector<VkExtensionProperties> available_device_extensions(device_extension_count);
VK_CHECK(vkEnumerateDeviceExtensionProperties(gpu.get_handle(), nullptr, &device_extension_count, available_device_extensions.data()));
auto compareExt = [](const VkExtensionProperties& props){
return std::strcmp(props.extensionName, VK_AMDX_SHADER_ENQUEUE_EXTENSION_NAME) == 0;
};
is_shader_enqueue_supported = (
std::find_if(available_device_extensions.cbegin(), available_device_extensions.cend(), compareExt)
!= available_device_extensions.cend());
};
void GpuDrawDispatch::input_event(const vkb::InputEvent &input_event)
{
Application::input_event(input_event);
if (gui)
{
gui->input_event(input_event);
}
if (input_event.get_source() == vkb::EventSource::Keyboard)
{
// Implement if needed
}
}
void GpuDrawDispatch::draw_gui()
{
gui->show_options_window(
/* body = */ [this]() {
{
ImGui::Text("%s", gui_message.c_str());
ImGui::SameLine();
}
},
/* lines = */ 1);
}
void GpuDrawDispatch::prepare_render_context()
{
// NOTE: Not sure how to use the framework correctly to change the swapchain properties after the app has been
// prepared. It would leak the previous swapchain.
// For now we just use the formats/usage bits that work for all rendering modes.
get_render_context().set_present_mode_priority({
VK_PRESENT_MODE_IMMEDIATE_KHR, // preferred
VK_PRESENT_MODE_FIFO_KHR,
});
// We have to use a non-SRGB format to use STORAGE image bit
get_render_context().set_surface_format_priority({
{VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
{VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR},
});
get_render_context().prepare(1, [this](vkb::core::Image &&swapchain_image) { return create_render_target(std::move(swapchain_image)); });
get_render_context().update_swapchain(std::set<VkImageUsageFlagBits>({
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
VK_IMAGE_USAGE_STORAGE_BIT,
VK_IMAGE_USAGE_TRANSFER_DST_BIT,
}));
}
std::unique_ptr<vkb::RenderTarget> GpuDrawDispatch::create_render_target(vkb::core::Image &&swapchain_image)
{
auto &device = swapchain_image.get_device();
auto &extent = swapchain_image.get_extent();
vkb::core::Image depth_image {
device,
extent,
vkb::get_suitable_depth_format(swapchain_image.get_device().get_gpu().get_handle()),
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VMA_MEMORY_USAGE_GPU_ONLY
};
std::vector<vkb::core::Image> images;
images.push_back(std::move(swapchain_image));
images.push_back(std::move(depth_image));
return std::make_unique<vkb::RenderTarget>(std::move(images));
}
bool GpuDrawDispatch::prepare(vkb::Platform &platform)
{
if (!VulkanSample::prepare(platform))
{
return false;
}
for (auto& arg : platform.get_arguments())
{
// platform.using_plugin<>() seems bugged in release mode
if (arg == "--benchmark")
{
is_benchmarking = true;
}
else if (arg.substr(0, 12) == "--stop-after")
{
is_stop_after = true;
}
}
// Keep these plugins disabled initially; we don't want to measure the resource loading time
if (is_benchmarking)
{
platform.get_plugin_2<::plugins::BenchmarkMode>()->set_enabled(false);
}
if (is_stop_after)
{
platform.get_plugin_2<::plugins::StopAfter>()->set_enabled(false);
}
enum {
USE_EXAMPLE_DEFAULT,
USE_EXAMPLE_DYNAMIC_STATE,
} example_to_use;
example_to_use = USE_EXAMPLE_DEFAULT;
DefaultExample::Config example_default_cfg {};
DynamicStateExample::Config example_dynamic_state_cfg {};
// Handle command line options
for (auto& arg : platform.get_generic_options())
{
if (arg == "no_animation")
{
example_default_cfg.rotateAnimation = false;
example_dynamic_state_cfg.rotateAnimation = false;
}
else if (arg == "draw_node")
{
example_default_cfg.drawMode = DefaultExample::OptDraw::WORKGRAPH_DRAW;
example_dynamic_state_cfg.drawMode = DynamicStateExample::OptDraw::WORKGRAPH_DRAW;
}
else if (arg == "compute_draw_node")
{
example_default_cfg.drawMode = DefaultExample::OptDraw::WORKGRAPH_COMPUTE_INTO_DRAW;
example_dynamic_state_cfg.drawMode = DynamicStateExample::OptDraw::WORKGRAPH_COMPUTE_INTO_DRAW;
}
else if (arg == "single")
{
example_default_cfg.instanceMode = DefaultExample::OptNodeInstance::SINGLE;
}
else if (arg == "multi")
{
example_default_cfg.instanceMode = DefaultExample::OptNodeInstance::MULTI;
}
else if (arg == "multi_all")
{
example_default_cfg.instanceMode = DefaultExample::OptNodeInstance::MULTI_ALL_AT_ONCE;
}
else if (arg == "node_info")
{
example_default_cfg.useNodeInfo = true;
}
else if (arg == "max_payload")
{
example_default_cfg.nodeLimits = DefaultExample::OptNodeLimits::MAX_SHADER_PAYLOAD_SIZE;
}
else if (arg == "max_draw")
{
example_default_cfg.nodeLimits = DefaultExample::OptNodeLimits::LARGE_NUMBER_PAYLOADS_DRAW;
}
else if (arg == "share_input")
{
example_default_cfg.instanceMode = DefaultExample::OptNodeInstance::MULTI_ALL_AT_ONCE;
example_default_cfg.shareInput = true;
}
else if (arg == "dynamic_state")
{
example_to_use = USE_EXAMPLE_DYNAMIC_STATE;
}
else
{
LOGE("Unrecognized option argument: {}", arg);
}
}
load_extension_function_pointers(device->get_handle());
switch (example_to_use)
{
case USE_EXAMPLE_DEFAULT:
example = std::make_unique<DefaultExample>(*this, example_default_cfg);
break;
case USE_EXAMPLE_DYNAMIC_STATE:
example = std::make_unique<DynamicStateExample>(*this, example_dynamic_state_cfg);
break;
}
if (!is_benchmarking)
{
gui = std::make_unique<vkb::Gui>(*this, platform.get_window(), nullptr, 15.0f, true);
std::vector<VkPipelineShaderStageCreateInfo> shader_stages;
shader_stages.emplace_back(load_shader("uioverlay/uioverlay.vert", VK_SHADER_STAGE_VERTEX_BIT));
shader_stages.emplace_back(load_shader("uioverlay/uioverlay.frag", VK_SHADER_STAGE_FRAGMENT_BIT));
{
VkAttachmentDescription attachment = {};
attachment.format = get_render_context().get_format();
attachment.samples = VK_SAMPLE_COUNT_1_BIT;
attachment.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
attachment.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference color_reference = {};
color_reference.attachment = 0;
color_reference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass_description = {};
subpass_description.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass_description.colorAttachmentCount = 1;
subpass_description.pColorAttachments = &color_reference;
subpass_description.pDepthStencilAttachment = nullptr;
subpass_description.inputAttachmentCount = 0;
subpass_description.pInputAttachments = nullptr;
subpass_description.preserveAttachmentCount = 0;
subpass_description.pPreserveAttachments = nullptr;
subpass_description.pResolveAttachments = nullptr;
VkRenderPassCreateInfo render_pass_create_info = {};
render_pass_create_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
render_pass_create_info.attachmentCount = 1;
render_pass_create_info.pAttachments = &attachment;
render_pass_create_info.subpassCount = 1;
render_pass_create_info.pSubpasses = &subpass_description;
render_pass_create_info.dependencyCount = 0;
render_pass_create_info.pDependencies = nullptr;
vkDestroyRenderPass(device->get_handle(), gui_render_pass, nullptr);
VK_CHECK(vkCreateRenderPass(device->get_handle(), &render_pass_create_info, nullptr, &gui_render_pass));
}
gui->prepare(pipeline_cache, gui_render_pass, shader_stages);
gui_message = example->get_gui_message();
per_frame_gui_framebuffer.resize(get_num_frames());
}
// Static resources
{
VkPipelineCacheCreateInfo pipeline_cache_create_info = {};
pipeline_cache_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
VK_CHECK(vkCreatePipelineCache(device->get_handle(), &pipeline_cache_create_info, nullptr, &pipeline_cache));
}
example->create_static_resources();
return true;
}
bool GpuDrawDispatch::resize(uint32_t width, uint32_t height)
{
auto ok = Application::resize(width, height);
device->wait_idle();
get_render_context().handle_surface_changes();
if (gui)
{
gui->resize(width, height);
}
resources_ready = false;
return ok;
}
void GpuDrawDispatch::update_gui(float delta_time)
{
if (gui)
{
gui->new_frame();
gui->show_top_window(get_name(), stats.get(), &get_debug_info());
draw_gui();
gui->update(delta_time);
gui->update_buffers();
}
}
void GpuDrawDispatch::create_and_init_resources(vkb::CommandBuffer& cmd_buf)
{
if (gui)
{
for (uint32_t frame_ndx = 0; frame_ndx < get_num_frames(); ++frame_ndx)
{
auto& frame = *get_render_context().get_render_frames()[frame_ndx].get();
auto& rt = frame.get_render_target();
auto image_view = rt.get_views().at(MRT_SWAPCHAIN).get_handle();
auto framebuffer_create_info = vkb::initializers::framebuffer_create_info();
framebuffer_create_info.renderPass = gui_render_pass;
framebuffer_create_info.attachmentCount = 1;
framebuffer_create_info.pAttachments = &image_view;
framebuffer_create_info.width = rt.get_extent().width;
framebuffer_create_info.height = rt.get_extent().height;
framebuffer_create_info.layers = 1;
vkDestroyFramebuffer(device->get_handle(), per_frame_gui_framebuffer[frame_ndx], nullptr);
VK_CHECK(vkCreateFramebuffer(device->get_handle(), &framebuffer_create_info, nullptr, &per_frame_gui_framebuffer[frame_ndx]));
}
}
example->create_and_init_resources(cmd_buf);
}
void GpuDrawDispatch::update(float delta_time)
{
get_render_context().begin_frame();
auto acquire_semaphore = get_render_context().consume_acquired_semaphore();
auto& frame = get_render_context().get_active_frame();
auto& graphics_queue = device->get_suitable_graphics_queue();
if (!resources_ready)
{
auto& cmd_buf = frame.request_command_buffer(graphics_queue);
cmd_buf.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
create_and_init_resources(cmd_buf);
cmd_buf.end();
auto submit_info = vkb::initializers::submit_info();
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &cmd_buf.get_handle();
auto fence = frame.request_fence();
VK_CHECK(graphics_queue.submit({submit_info}, fence));
vkWaitForFences(device->get_handle(), 1, &fence, VK_TRUE, ~0);
resources_ready = true;
}
update_gui(delta_time);
{
auto& cmd_buf = frame.request_command_buffer(graphics_queue);
cmd_buf.begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
record_frame_commands(cmd_buf, delta_time);
cmd_buf.end();
auto present_semaphore = frame.request_semaphore();
VkPipelineStageFlags stage_masks[] = { VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT };
auto submit_info = vkb::initializers::submit_info();
submit_info.waitSemaphoreCount = 1;
submit_info.pWaitDstStageMask = stage_masks;
submit_info.pWaitSemaphores = &acquire_semaphore;
submit_info.signalSemaphoreCount = 1;
submit_info.pSignalSemaphores = &present_semaphore;
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &cmd_buf.get_handle();
VK_CHECK(graphics_queue.submit({submit_info}, frame.request_fence()));
// The fence will be waited on implicitly the next time we acquire this frame again.
get_render_context().end_frame(present_semaphore);
}
if (frame_count == 1)
{
// If we're benchmarking, start the measurement after the resources have been loaded.
if (is_benchmarking)
{
platform->get_plugin_2<::plugins::BenchmarkMode>()->set_enabled(true);
}
if (is_stop_after)
{
platform->get_plugin_2<::plugins::StopAfter>()->set_enabled(true);
}
}
++frame_count;
platform->on_post_draw(get_render_context());
// Don't call VulkanSample::update(), it depends on the RenderPipeline and Scene which we don't use.
}
void GpuDrawDispatch::record_frame_commands(vkb::CommandBuffer& cmd_buf, float delta_time)
{
example->record_frame_commands(cmd_buf, delta_time);
if (gui)
{
auto& rt = get_render_context().get_active_frame().get_render_target();
auto render_pass_begin_info = vkb::initializers::render_pass_begin_info();
render_pass_begin_info.renderPass = gui_render_pass;
render_pass_begin_info.framebuffer = per_frame_gui_framebuffer[get_render_context().get_active_frame_index()];
render_pass_begin_info.renderArea.extent = rt.get_extent();
render_pass_begin_info.renderArea.offset = {0, 0};
vkCmdBeginRenderPass(cmd_buf.get_handle(), &render_pass_begin_info, VK_SUBPASS_CONTENTS_INLINE);
gui->draw(cmd_buf.get_handle());
vkCmdEndRenderPass(cmd_buf.get_handle());
}
}
VkPipelineShaderStageCreateInfo GpuDrawDispatch::load_shader(
const std::string& file,
VkShaderStageFlagBits stage)
{
VkPipelineShaderStageCreateInfo shader_stage_create_info = {};
shader_stage_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shader_stage_create_info.stage = stage;
shader_stage_create_info.pName = "main";
auto module_iter = shader_module_cache.find(file);
if (module_iter == shader_module_cache.end())
{
shader_stage_create_info.module = vkb::load_shader(file, device->get_handle(), stage);
assert(shader_stage_create_info.module != VK_NULL_HANDLE);
shader_module_cache.insert({file, shader_stage_create_info.module});
}
else
{
shader_stage_create_info.module = module_iter->second;
}
return shader_stage_create_info;
}
VkPipelineShaderStageCreateInfo GpuDrawDispatch::load_spv_shader(
const std::string& file,
VkShaderStageFlagBits stage)
{
VkPipelineShaderStageCreateInfo shader_stage_create_info = {};
shader_stage_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shader_stage_create_info.stage = stage;
shader_stage_create_info.pName = "main";
auto module_iter = shader_module_cache.find(file);
if (module_iter == shader_module_cache.end())
{
auto buffer = vkb::fs::read_shader_binary(file);
assert((buffer.size() % sizeof(uint32_t)) == 0);
VkShaderModuleCreateInfo module_create_info = {};
module_create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
module_create_info.codeSize = buffer.size();
module_create_info.pCode = reinterpret_cast<const uint32_t*>(buffer.data());
VK_CHECK(vkCreateShaderModule(device->get_handle(), &module_create_info, NULL, &shader_stage_create_info.module));
shader_module_cache.insert({file, shader_stage_create_info.module});
}
else
{
shader_stage_create_info.module = module_iter->second;
}
return shader_stage_create_info;
}
std::unique_ptr<vkb::sg::SubMesh> GpuDrawDispatch::load_model(const std::string &file, uint32_t index, bool use_indexed_draw, bool mesh_shader_buffer)
{
vkb::GLTFLoader loader{*device};
std::unique_ptr<vkb::sg::SubMesh> model = loader.read_model_from_file(file, index, !use_indexed_draw, mesh_shader_buffer);
if (!model)
{
LOGE("Cannot load model from file: {}", file.c_str());
throw std::runtime_error("Cannot load model from file: " + file);
}
return model;
}
}
std::unique_ptr<vkb::VulkanSample> create_gpu_draw_dispatch()
{
return std::make_unique<GpuDrawDispatch::GpuDrawDispatch>();
}