forked from KhronosGroup/Vulkan-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvulkan_sample.h
1372 lines (1180 loc) · 43.7 KB
/
vulkan_sample.h
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2019-2024, Arm Limited and Contributors
* Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/hpp_utils.h"
#include "hpp_gltf_loader.h"
#include "hpp_gui.h"
#include "platform/application.h"
#include "rendering/hpp_render_pipeline.h"
#include "scene_graph/components/camera.h"
#include "scene_graph/scripts/animation.h"
#if defined(PLATFORM__MACOS)
#include <TargetConditionals.h>
#endif
namespace vkb
{
/**
* @mainpage Overview of the framework
*
* @section initialization Initialization
*
* @subsection platform_init Platform initialization
* The lifecycle of a Vulkan sample starts by instantiating the correct Platform
* (e.g. WindowsPlatform) and then calling initialize() on it, which sets up
* the windowing system and logging. Then it calls the parent Platform::initialize(),
* which takes ownership of the active application. It's the platforms responsibility
* to then call VulkanSample::prepare() to prepare the vulkan sample when it is ready.
*
* @subsection sample_init Sample initialization
* The preparation step is divided in two steps, one in VulkanSample and the other in the
* specific sample, such as SurfaceRotation.
* VulkanSample::prepare() contains functions that do not require customization,
* including creating a Vulkan instance, the surface and getting physical devices.
* The prepare() function for the specific sample completes the initialization, including:
* - setting enabled Stats
* - creating the Device
* - creating the Swapchain
* - creating the RenderContext (or child class)
* - preparing the RenderContext
* - loading the sg::Scene
* - creating the RenderPipeline with ShaderModule (s)
* - creating the sg::Camera
* - creating the Gui
*
* @section frame_rendering Frame rendering
*
* @subsection update Update function
* Rendering happens in the update() function. Each sample can override it, e.g.
* to recreate the Swapchain in SwapchainImages when required by user input.
* Typically a sample will then call VulkanSample::update().
*
* @subsection rendering Rendering
* A series of steps are performed, some of which can be customized (it will be
* highlighted when that's the case):
*
* - calling sg::Script::update() for all sg::Script (s)
* - beginning a frame in RenderContext (does the necessary waiting on fences and
* acquires an core::Image)
* - requesting a CommandBuffer
* - updating Stats and Gui
* - getting an active RenderTarget constructed by the factory function of the RenderFrame
* - setting up barriers for color and depth, note that these are only for the default RenderTarget
* - calling VulkanSample::draw_swapchain_renderpass (see below)
* - setting up a barrier for the Swapchain transition to present
* - submitting the CommandBuffer and end the Frame (present)
*
* @subsection draw_swapchain Draw swapchain renderpass
* The function starts and ends a RenderPass which includes setting up viewport, scissors,
* blend state (etc.) and calling draw_scene.
* Note that RenderPipeline::draw is not virtual in RenderPipeline, but internally it calls
* Subpass::draw for each Subpass, which is virtual and can be customized.
*
* @section framework_classes Main framework classes
*
* - RenderContext
* - RenderFrame
* - RenderTarget
* - RenderPipeline
* - ShaderModule
* - ResourceCache
* - BufferPool
* - Core classes: Classes in vkb::core wrap Vulkan objects for indexing and hashing.
*/
class Gui;
class RenderPipeline;
namespace core
{
class HPPCommandBuffer;
class HPPDebugUtils;
class HPPDevice;
class HPPInstance;
class HPPPhysicalDevice;
} // namespace core
namespace rendering
{
class HPPRenderContext;
class HPPRenderTarget;
} // namespace rendering
namespace stats
{
class HPPStats;
}
template <vkb::BindingType bindingType>
class VulkanSample : public vkb::Application
{
using Parent = vkb::Application;
/// <summary>
/// PUBLIC INTERFACE
/// </summary>
public:
VulkanSample() = default;
~VulkanSample() override;
using CommandBufferType = typename std::conditional<bindingType == BindingType::Cpp, vkb::core::HPPCommandBuffer, vkb::CommandBuffer>::type;
using DeviceType = typename std::conditional<bindingType == BindingType::Cpp, vkb::core::HPPDevice, vkb::Device>::type;
using GuiType = typename std::conditional<bindingType == BindingType::Cpp, vkb::HPPGui, vkb::Gui>::type;
using InstanceType = typename std::conditional<bindingType == BindingType::Cpp, vkb::core::HPPInstance, vkb::Instance>::type;
using PhysicalDeviceType = typename std::conditional<bindingType == BindingType::Cpp, vkb::core::HPPPhysicalDevice, vkb::PhysicalDevice>::type;
using RenderContextType = typename std::conditional<bindingType == BindingType::Cpp, vkb::rendering::HPPRenderContext, vkb::RenderContext>::type;
using RenderPipelineType = typename std::conditional<bindingType == BindingType::Cpp, vkb::rendering::HPPRenderPipeline, vkb::RenderPipeline>::type;
using RenderTargetType = typename std::conditional<bindingType == BindingType::Cpp, vkb::rendering::HPPRenderTarget, vkb::RenderTarget>::type;
using StatsType = typename std::conditional<bindingType == BindingType::Cpp, vkb::stats::HPPStats, vkb::Stats>::type;
using Extent2DType = typename std::conditional<bindingType == BindingType::Cpp, vk::Extent2D, VkExtent2D>::type;
using SurfaceFormatType = typename std::conditional<bindingType == BindingType::Cpp, vk::SurfaceFormatKHR, VkSurfaceFormatKHR>::type;
using SurfaceType = typename std::conditional<bindingType == BindingType::Cpp, vk::SurfaceKHR, VkSurfaceKHR>::type;
Configuration &get_configuration();
RenderContextType &get_render_context();
RenderContextType const &get_render_context() const;
bool has_render_context() const;
/// <summary>
/// PROTECTED VIRTUAL INTERFACE
/// </summary>
protected:
// from Application
void input_event(const InputEvent &input_event) override;
void finish() override;
bool resize(uint32_t width, uint32_t height) override;
/**
* @brief Create the Vulkan device used by this sample
* @note Can be overridden to implement custom device creation
*/
virtual std::unique_ptr<DeviceType> create_device(PhysicalDeviceType &gpu);
/**
* @brief Create the Vulkan instance used by this sample
* @note Can be overridden to implement custom instance creation
*/
virtual std::unique_ptr<InstanceType> create_instance(bool headless);
/**
* @brief Override this to customise the creation of the render_context
*/
virtual void create_render_context();
/**
* @brief Prepares the render target and draws to it, calling draw_renderpass
* @param command_buffer The command buffer to record the commands to
* @param render_target The render target that is being drawn to
*/
virtual void draw(CommandBufferType &command_buffer, RenderTargetType &render_target);
/**
* @brief Samples should override this function to draw their interface
*/
virtual void draw_gui();
/**
* @brief Starts the render pass, executes the render pipeline, and then ends the render pass
* @param command_buffer The command buffer to record the commands to
* @param render_target The render target that is being drawn to
*/
virtual void draw_renderpass(CommandBufferType &command_buffer, RenderTargetType &render_target);
/**
* @brief Get additional sample-specific instance layers.
*
* @return Vector of additional instance layers. Default is empty vector.
*/
virtual const std::vector<const char *> get_validation_layers();
/**
* @brief Override this to customise the creation of the swapchain and render_context
*/
virtual void prepare_render_context();
/**
* @brief Triggers the render pipeline, it can be overridden by samples to specialize their rendering logic
* @param command_buffer The command buffer to record the commands to
*/
virtual void render(CommandBufferType &command_buffer);
/**
* @brief Request features from the gpu based on what is supported
*/
virtual void request_gpu_features(PhysicalDeviceType &gpu);
/**
* @brief Resets the stats view max values for high demanding configs
* Should be overridden by the samples since they
* know which configuration is resource demanding
*/
virtual void reset_stats_view();
/**
* @brief Updates the debug window, samples can override this to insert their own data elements
*/
virtual void update_debug_window();
/// <summary>
/// PROTECTED INTERFACE
/// </summary>
/**
* @brief Add a sample-specific device extension
* @param extension The extension name
* @param optional (Optional) Whether the extension is optional
*/
void add_device_extension(const char *extension, bool optional = false);
/**
* @brief Add a sample-specific instance extension
* @param extension The extension name
* @param optional (Optional) Whether the extension is optional
*/
void add_instance_extension(const char *extension, bool optional = false);
void create_gui(const Window &window, StatsType const *stats = nullptr, const float font_size = 21.0f, bool explicit_update = false);
/**
* @brief A helper to create a render context
*/
void create_render_context(const std::vector<SurfaceFormatType> &surface_priority_list);
DeviceType &get_device();
DeviceType const &get_device() const;
GuiType &get_gui();
GuiType const &get_gui() const;
InstanceType &get_instance();
InstanceType const &get_instance() const;
RenderPipelineType &get_render_pipeline();
RenderPipelineType const &get_render_pipeline() const;
sg::Scene &get_scene();
StatsType &get_stats();
SurfaceType get_surface() const;
std::vector<SurfaceFormatType> &get_surface_priority_list();
std::vector<SurfaceFormatType> const &get_surface_priority_list() const;
bool has_device() const;
bool has_instance() const;
bool has_gui() const;
bool has_render_pipeline() const;
bool has_scene();
/**
* @brief Loads the scene
*
* @param path The path of the glTF file
*/
void load_scene(const std::string &path);
/**
* @brief Additional sample initialization
*/
bool prepare(const ApplicationOptions &options) override;
/**
* @brief Set the Vulkan API version to request at instance creation time
*/
void set_api_version(uint32_t requested_api_version);
/**
* @brief Sets whether or not the first graphics queue should have higher priority than other queues.
* Very specific feature which is used by async compute samples.
* Needs to be called before prepare().
* @param enable If true, present queue will have prio 1.0 and other queues have prio 0.5.
* Default state is false, where all queues have 0.5 priority.
*/
void set_high_priority_graphics_queue_enable(bool enable);
void set_render_context(std::unique_ptr<RenderContextType> &&render_context);
void set_render_pipeline(std::unique_ptr<RenderPipelineType> &&render_pipeline);
/**
* @brief Main loop sample events
*/
void update(float delta_time) override;
/**
* @brief Update GUI
* @param delta_time
*/
void update_gui(float delta_time);
/**
* @brief Update scene
* @param delta_time
*/
void update_scene(float delta_time);
/**
* @brief Update counter values
* @param delta_time
*/
void update_stats(float delta_time);
/**
* @brief Set viewport and scissor state in command buffer for a given extent
*/
static void set_viewport_and_scissor(CommandBufferType const &command_buffer, Extent2DType const &extent);
/// <summary>
/// PRIVATE INTERFACE
/// </summary>
private:
void create_render_context_impl(const std::vector<vk::SurfaceFormatKHR> &surface_priority_list);
void draw_impl(vkb::core::HPPCommandBuffer &command_buffer, vkb::rendering::HPPRenderTarget &render_target);
void draw_renderpass_impl(vkb::core::HPPCommandBuffer &command_buffer, vkb::rendering::HPPRenderTarget &render_target);
void render_impl(vkb::core::HPPCommandBuffer &command_buffer);
static void set_viewport_and_scissor_impl(vkb::core::HPPCommandBuffer const &command_buffer, vk::Extent2D const &extent);
/**
* @brief Get sample-specific device extensions.
*
* @return Map of device extensions and whether or not they are optional. Default is empty map.
*/
std::unordered_map<const char *, bool> const &get_device_extensions() const;
/**
* @brief Get sample-specific instance extensions.
*
* @return Map of instance extensions and whether or not they are optional. Default is empty map.
*/
std::unordered_map<const char *, bool> const &get_instance_extensions() const;
/// <summary>
/// PRIVATE MEMBERS
/// </summary>
private:
/**
* @brief The Vulkan instance
*/
std::unique_ptr<vkb::core::HPPInstance> instance;
/**
* @brief The Vulkan device
*/
std::unique_ptr<vkb::core::HPPDevice> device;
/**
* @brief Context used for rendering, it is responsible for managing the frames and their underlying images
*/
std::unique_ptr<vkb::rendering::HPPRenderContext> render_context;
/**
* @brief Pipeline used for rendering, it should be set up by the concrete sample
*/
std::unique_ptr<vkb::rendering::HPPRenderPipeline> render_pipeline;
/**
* @brief Holds all scene information
*/
std::unique_ptr<sg::Scene> scene;
std::unique_ptr<vkb::HPPGui> gui;
std::unique_ptr<vkb::stats::HPPStats> stats;
static constexpr float STATS_VIEW_RESET_TIME{10.0f}; // 10 seconds
/**
* @brief The Vulkan surface
*/
vk::SurfaceKHR surface;
/**
* @brief A list of surface formats in order of priority (vector[0] has high priority, vector[size-1] has low priority)
*/
std::vector<vk::SurfaceFormatKHR> surface_priority_list = {
{vk::Format::eR8G8B8A8Srgb, vk::ColorSpaceKHR::eSrgbNonlinear},
{vk::Format::eB8G8R8A8Srgb, vk::ColorSpaceKHR::eSrgbNonlinear}};
/**
* @brief The configuration of the sample
*/
Configuration configuration{};
/** @brief Set of device extensions to be enabled for this example and whether they are optional (must be set in the derived constructor) */
std::unordered_map<const char *, bool> device_extensions;
/** @brief Set of instance extensions to be enabled for this example and whether they are optional (must be set in the derived constructor) */
std::unordered_map<const char *, bool> instance_extensions;
/** @brief The Vulkan API version to request for this sample at instance creation time */
uint32_t api_version = VK_API_VERSION_1_0;
/** @brief Whether or not we want a high priority graphics queue. */
bool high_priority_graphics_queue{false};
std::unique_ptr<vkb::core::HPPDebugUtils> debug_utils;
};
template <vkb::BindingType bindingType>
inline VulkanSample<bindingType>::~VulkanSample()
{
if (device)
{
device->get_handle().waitIdle();
}
scene.reset();
stats.reset();
gui.reset();
render_context.reset();
device.reset();
if (surface)
{
instance->get_handle().destroySurfaceKHR(surface);
}
instance.reset();
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::add_device_extension(const char *extension, bool optional)
{
device_extensions[extension] = optional;
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::add_instance_extension(const char *extension, bool optional)
{
instance_extensions[extension] = optional;
}
template <vkb::BindingType bindingType>
inline std::unique_ptr<typename VulkanSample<bindingType>::DeviceType> VulkanSample<bindingType>::create_device(PhysicalDeviceType &gpu)
{
if constexpr (bindingType == BindingType::Cpp)
{
return std::make_unique<vkb::core::HPPDevice>(gpu, surface, std::move(debug_utils), get_device_extensions());
}
else
{
return std::make_unique<vkb::Device>(gpu,
static_cast<VkSurfaceKHR>(surface),
std::unique_ptr<vkb::DebugUtils>(reinterpret_cast<vkb::DebugUtils *>(debug_utils.release())),
get_device_extensions());
}
}
template <vkb::BindingType bindingType>
inline std::unique_ptr<typename VulkanSample<bindingType>::InstanceType> VulkanSample<bindingType>::create_instance(bool headless)
{
return std::make_unique<InstanceType>(get_name(), get_instance_extensions(), get_validation_layers(), headless, api_version);
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::create_render_context()
{
create_render_context_impl(surface_priority_list);
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::create_render_context(const std::vector<SurfaceFormatType> &surface_priority_list)
{
if constexpr (bindingType == BindingType::Cpp)
{
create_render_context_impl(surface_priority_list);
}
else
{
create_render_context_impl(reinterpret_cast<std::vector<vk::SurfaceFormatKHR> const &>(surface_priority_list));
}
}
template <vkb::BindingType bindingType>
void VulkanSample<bindingType>::create_render_context_impl(const std::vector<vk::SurfaceFormatKHR> &surface_priority_list)
{
#ifdef VK_USE_PLATFORM_ANDROID_KHR
vk::PresentModeKHR present_mode = (window->get_properties().vsync == Window::Vsync::OFF) ? vk::PresentModeKHR::eMailbox : vk::PresentModeKHR::eFifo;
std::vector<vk::PresentModeKHR> present_mode_priority_list{vk::PresentModeKHR::eFifo, vk::PresentModeKHR::eMailbox, vk::PresentModeKHR::eImmediate};
#else
vk::PresentModeKHR present_mode = (window->get_properties().vsync == Window::Vsync::ON) ? vk::PresentModeKHR::eFifo : vk::PresentModeKHR::eMailbox;
std::vector<vk::PresentModeKHR> present_mode_priority_list{vk::PresentModeKHR::eMailbox, vk::PresentModeKHR::eFifo, vk::PresentModeKHR::eImmediate};
#endif
render_context =
std::make_unique<vkb::rendering::HPPRenderContext>(*device, surface, *window, present_mode, present_mode_priority_list, surface_priority_list);
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::draw(CommandBufferType &command_buffer, RenderTargetType &render_target)
{
if constexpr (bindingType == BindingType::Cpp)
{
draw_impl(command_buffer, render_target);
}
else
{
draw_impl(reinterpret_cast<vkb::core::HPPCommandBuffer &>(command_buffer), reinterpret_cast<vkb::rendering::HPPRenderTarget &>(render_target));
}
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::draw_impl(vkb::core::HPPCommandBuffer &command_buffer, vkb::rendering::HPPRenderTarget &render_target)
{
auto &views = render_target.get_views();
{
// Image 0 is the swapchain
vkb::common::HPPImageMemoryBarrier memory_barrier{};
memory_barrier.old_layout = vk::ImageLayout::eUndefined;
memory_barrier.new_layout = vk::ImageLayout::eColorAttachmentOptimal;
memory_barrier.src_access_mask = {};
memory_barrier.dst_access_mask = vk::AccessFlagBits::eColorAttachmentWrite;
memory_barrier.src_stage_mask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
memory_barrier.dst_stage_mask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
command_buffer.image_memory_barrier(views[0], memory_barrier);
render_target.set_layout(0, memory_barrier.new_layout);
// Skip 1 as it is handled later as a depth-stencil attachment
for (size_t i = 2; i < views.size(); ++i)
{
command_buffer.image_memory_barrier(views[i], memory_barrier);
render_target.set_layout(static_cast<uint32_t>(i), memory_barrier.new_layout);
}
}
{
vkb::common::HPPImageMemoryBarrier memory_barrier{};
memory_barrier.old_layout = vk::ImageLayout::eUndefined;
memory_barrier.new_layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
memory_barrier.src_access_mask = {};
memory_barrier.dst_access_mask = vk::AccessFlagBits::eDepthStencilAttachmentRead | vk::AccessFlagBits::eDepthStencilAttachmentWrite;
memory_barrier.src_stage_mask = vk::PipelineStageFlagBits::eTopOfPipe;
memory_barrier.dst_stage_mask = vk::PipelineStageFlagBits::eEarlyFragmentTests | vk::PipelineStageFlagBits::eLateFragmentTests;
command_buffer.image_memory_barrier(views[1], memory_barrier);
render_target.set_layout(1, memory_barrier.new_layout);
}
if constexpr (bindingType == BindingType::Cpp)
{
draw_renderpass(command_buffer, render_target);
}
else
{
draw_renderpass(reinterpret_cast<vkb::CommandBuffer &>(command_buffer), reinterpret_cast<vkb::RenderTarget &>(render_target));
}
{
vkb::common::HPPImageMemoryBarrier memory_barrier{};
memory_barrier.old_layout = vk::ImageLayout::eColorAttachmentOptimal;
memory_barrier.new_layout = vk::ImageLayout::ePresentSrcKHR;
memory_barrier.src_access_mask = vk::AccessFlagBits::eColorAttachmentWrite;
memory_barrier.src_stage_mask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
memory_barrier.dst_stage_mask = vk::PipelineStageFlagBits::eBottomOfPipe;
command_buffer.image_memory_barrier(views[0], memory_barrier);
render_target.set_layout(0, memory_barrier.new_layout);
}
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::draw_gui()
{
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::draw_renderpass(CommandBufferType &command_buffer, RenderTargetType &render_target)
{
if constexpr (bindingType == BindingType::Cpp)
{
draw_renderpass_impl(command_buffer, render_target);
}
else
{
draw_renderpass_impl(reinterpret_cast<vkb::core::HPPCommandBuffer &>(command_buffer),
reinterpret_cast<vkb::rendering::HPPRenderTarget &>(render_target));
}
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::draw_renderpass_impl(vkb::core::HPPCommandBuffer &command_buffer, vkb::rendering::HPPRenderTarget &render_target)
{
if constexpr (bindingType == BindingType::Cpp)
{
set_viewport_and_scissor(command_buffer, render_target.get_extent());
render(command_buffer);
}
else
{
set_viewport_and_scissor(reinterpret_cast<vkb::CommandBuffer const &>(command_buffer),
reinterpret_cast<VkExtent2D const &>(render_target.get_extent()));
render(reinterpret_cast<vkb::CommandBuffer &>(command_buffer));
}
if (gui)
{
gui->draw(command_buffer);
}
command_buffer.get_handle().endRenderPass();
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::finish()
{
Parent::finish();
if (device)
{
device->get_handle().waitIdle();
}
}
template <vkb::BindingType bindingType>
inline Configuration &VulkanSample<bindingType>::get_configuration()
{
return configuration;
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::DeviceType const &VulkanSample<bindingType>::get_device() const
{
if constexpr (bindingType == BindingType::Cpp)
{
return reinterpret_cast<vkb::core::HPPDevice const &>(*device);
}
else
{
return *device;
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::DeviceType &VulkanSample<bindingType>::get_device()
{
if constexpr (bindingType == BindingType::Cpp)
{
return *device;
}
else
{
return reinterpret_cast<vkb::Device &>(*device);
}
}
template <vkb::BindingType bindingType>
inline std::unordered_map<const char *, bool> const &VulkanSample<bindingType>::get_device_extensions() const
{
return device_extensions;
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::GuiType &VulkanSample<bindingType>::get_gui()
{
if constexpr (bindingType == BindingType::Cpp)
{
return *gui;
}
else
{
return reinterpret_cast<vkb::Gui &>(*gui);
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::GuiType const &VulkanSample<bindingType>::get_gui() const
{
if constexpr (bindingType == BindingType::Cpp)
{
return *gui;
}
else
{
return reinterpret_cast<vkb::Gui const &>(*gui);
}
}
template <vkb::BindingType bindingType>
inline std::vector<typename VulkanSample<bindingType>::SurfaceFormatType> &VulkanSample<bindingType>::get_surface_priority_list()
{
if constexpr (bindingType == BindingType::Cpp)
{
return surface_priority_list;
}
else
{
return reinterpret_cast<std::vector<VkSurfaceFormatKHR> &>(surface_priority_list);
}
}
template <vkb::BindingType bindingType>
inline std::vector<typename VulkanSample<bindingType>::SurfaceFormatType> const &VulkanSample<bindingType>::get_surface_priority_list() const
{
if constexpr (bindingType == BindingType::Cpp)
{
return surface_priority_list;
}
else
{
return reinterpret_cast<std::vector<VkSurfaceFormatKHR> const &>(surface_priority_list);
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::InstanceType &VulkanSample<bindingType>::get_instance()
{
if constexpr (bindingType == BindingType::Cpp)
{
return *instance;
}
else
{
return reinterpret_cast<vkb::Instance &>(*instance);
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::InstanceType const &VulkanSample<bindingType>::get_instance() const
{
if constexpr (bindingType == BindingType::Cpp)
{
return *instance;
}
else
{
return reinterpret_cast<vkb::Instance const &>(*instance);
}
}
template <vkb::BindingType bindingType>
inline std::unordered_map<const char *, bool> const &VulkanSample<bindingType>::get_instance_extensions() const
{
return instance_extensions;
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::RenderContextType const &VulkanSample<bindingType>::get_render_context() const
{
assert(render_context && "Render context is not valid");
if constexpr (bindingType == BindingType::Cpp)
{
return reinterpret_cast<vkb::rendering::HPPRenderContext const &>(*render_context);
}
else
{
return *render_context;
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::RenderContextType &VulkanSample<bindingType>::get_render_context()
{
assert(render_context && "Render context is not valid");
if constexpr (bindingType == BindingType::Cpp)
{
return *render_context;
}
else
{
return reinterpret_cast<vkb::RenderContext &>(*render_context);
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::RenderPipelineType const &VulkanSample<bindingType>::get_render_pipeline() const
{
assert(render_pipeline && "Render pipeline was not created");
if constexpr (bindingType == BindingType::Cpp)
{
return *render_pipeline;
}
else
{
return reinterpret_cast<vkb::RenderPipeline const &>(*render_pipeline);
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::RenderPipelineType &VulkanSample<bindingType>::get_render_pipeline()
{
assert(render_pipeline && "Render pipeline was not created");
if constexpr (bindingType == BindingType::Cpp)
{
return *render_pipeline;
}
else
{
return reinterpret_cast<vkb::RenderPipeline &>(*render_pipeline);
}
}
template <vkb::BindingType bindingType>
inline sg::Scene &VulkanSample<bindingType>::get_scene()
{
assert(scene && "Scene not loaded");
return *scene;
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::StatsType &VulkanSample<bindingType>::get_stats()
{
if constexpr (bindingType == BindingType::Cpp)
{
return *stats;
}
else
{
return reinterpret_cast<vkb::Stats &>(*stats);
}
}
template <vkb::BindingType bindingType>
inline typename VulkanSample<bindingType>::SurfaceType VulkanSample<bindingType>::get_surface() const
{
if constexpr (bindingType == BindingType::Cpp)
{
return surface;
}
else
{
return static_cast<VkSurfaceKHR>(surface);
}
}
template <vkb::BindingType bindingType>
inline const std::vector<const char *> VulkanSample<bindingType>::get_validation_layers()
{
return {};
}
template <vkb::BindingType bindingType>
inline bool VulkanSample<bindingType>::has_device() const
{
return device != nullptr;
}
template <vkb::BindingType bindingType>
inline bool VulkanSample<bindingType>::has_instance() const
{
return instance != nullptr;
}
template <vkb::BindingType bindingType>
inline bool VulkanSample<bindingType>::has_gui() const
{
return gui != nullptr;
}
template <vkb::BindingType bindingType>
inline bool VulkanSample<bindingType>::has_render_context() const
{
return render_context != nullptr;
}
template <vkb::BindingType bindingType>
inline bool VulkanSample<bindingType>::has_render_pipeline() const
{
return render_pipeline != nullptr;
}
template <vkb::BindingType bindingType>
bool VulkanSample<bindingType>::has_scene()
{
return scene != nullptr;
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::input_event(const InputEvent &input_event)
{
Parent::input_event(input_event);
bool gui_captures_event = false;
if (gui)
{
gui_captures_event = gui->input_event(input_event);
}
if (!gui_captures_event)
{
if (scene && scene->has_component<sg::Script>())
{
auto scripts = scene->get_components<sg::Script>();
for (auto script : scripts)
{
script->input_event(input_event);
}
}
}
if (input_event.get_source() == EventSource::Keyboard)
{
const auto &key_event = static_cast<const KeyInputEvent &>(input_event);
if (key_event.get_action() == KeyAction::Down &&
(key_event.get_code() == KeyCode::PrintScreen || key_event.get_code() == KeyCode::F12))
{
vkb::common::screenshot(*render_context, "screenshot-" + get_name());
}
}
}
template <vkb::BindingType bindingType>
inline void VulkanSample<bindingType>::load_scene(const std::string &path)
{
vkb::HPPGLTFLoader loader(*device);
scene = loader.read_scene_from_file(path);
if (!scene)
{
LOGE("Cannot load scene: {}", path.c_str());
throw std::runtime_error("Cannot load scene: " + path);
}
}
template <vkb::BindingType bindingType>
inline bool VulkanSample<bindingType>::prepare(const ApplicationOptions &options)
{
if (!Parent::prepare(options))
{
return false;
}
LOGI("Initializing Vulkan sample");
// initialize C++-Bindings default dispatcher, first step
#if TARGET_OS_IPHONE
static vk::DynamicLoader dl("vulkan.framework/vulkan");
#else
static vk::DynamicLoader dl;
#endif
VULKAN_HPP_DEFAULT_DISPATCHER.init(dl.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr"));
bool headless = window->get_window_mode() == Window::Mode::Headless;
// for a while we're running on mixed C- and C++-bindings, needing volk for the C-bindings!
VkResult result = volkInitialize();
if (result)
{
throw VulkanException(result, "Failed to initialize volk.");
}
// Creating the vulkan instance
for (const char *extension_name : window->get_required_surface_extensions())
{
add_instance_extension(extension_name);
}
#ifdef VKB_VULKAN_DEBUG
{
std::vector<vk::ExtensionProperties> available_instance_extensions = vk::enumerateInstanceExtensionProperties();
auto debugExtensionIt =
std::find_if(available_instance_extensions.begin(),
available_instance_extensions.end(),
[](vk::ExtensionProperties const &ep) { return strcmp(ep.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0; });
if (debugExtensionIt != available_instance_extensions.end())
{
LOGI("Vulkan debug utils enabled ({})", VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
debug_utils = std::make_unique<vkb::core::HPPDebugUtilsExtDebugUtils>();
add_instance_extension(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}
}
#endif
if constexpr (bindingType == BindingType::Cpp)
{
instance = create_instance(headless);