-
Notifications
You must be signed in to change notification settings - Fork 326
/
Copy pathRmlUi_Renderer_VK.cpp
3044 lines (2400 loc) · 114 KB
/
RmlUi_Renderer_VK.cpp
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
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019-2023 The RmlUi Team, and contributors
*
* 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 "RmlUi_Renderer_VK.h"
#include "RmlUi_Vulkan/ShadersCompiledSPV.h"
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/FileInterface.h>
#include <RmlUi/Core/Log.h>
#include <RmlUi/Core/Math.h>
#include <RmlUi/Core/Platform.h>
#include <RmlUi/Core/Profiling.h>
#include <algorithm>
#include <string.h>
// AlignUp(314, 256) = 512
template <typename T>
static T AlignUp(T val, T alignment)
{
return (val + alignment - (T)1) & ~(alignment - (T)1);
}
VkValidationFeaturesEXT debug_validation_features_ext = {};
VkValidationFeatureEnableEXT debug_validation_features_ext_requested[] = {
VK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,
VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT,
VK_VALIDATION_FEATURE_ENABLE_BEST_PRACTICES_EXT,
};
#ifdef RMLUI_VK_DEBUG
static Rml::String FormatByteSize(VkDeviceSize size) noexcept
{
constexpr VkDeviceSize K = VkDeviceSize(1024);
if (size < K)
return Rml::CreateString("%zu B", size);
else if (size < K * K)
return Rml::CreateString("%g KB", double(size) / double(K));
return Rml::CreateString("%g MB", double(size) / double(K * K));
}
static VKAPI_ATTR VkBool32 VKAPI_CALL MyDebugReportCallback(VkDebugUtilsMessageSeverityFlagBitsEXT severityFlags,
VkDebugUtilsMessageTypeFlagsEXT /*messageTypeFlags*/, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* /*pUserData*/)
{
if (severityFlags & VkDebugUtilsMessageSeverityFlagBitsEXT::VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT)
{
return VK_FALSE;
}
#ifdef RMLUI_PLATFORM_WIN32
if (severityFlags & VkDebugUtilsMessageSeverityFlagBitsEXT::VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
{
// some logs are not passed to our UI, because of early calling for explicity I put native log output
OutputDebugString(TEXT("\n"));
OutputDebugStringA(pCallbackData->pMessage);
}
#endif
Rml::Log::Message(Rml::Log::LT_ERROR, "[Vulkan][VALIDATION] %s ", pCallbackData->pMessage);
return VK_FALSE;
}
#endif
RenderInterface_VK::RenderInterface_VK() :
m_is_transform_enabled{false}, m_is_apply_to_regular_geometry_stencil{false}, m_is_use_scissor_specified{false}, m_is_use_stencil_pipeline{false},
m_width{}, m_height{}, m_queue_index_present{}, m_queue_index_graphics{}, m_queue_index_compute{}, m_semaphore_index{},
m_semaphore_index_previous{}, m_image_index{}, m_p_instance{}, m_p_device{}, m_p_physical_device{}, m_p_surface{}, m_p_swapchain{},
m_p_allocator{}, m_p_current_command_buffer{}, m_p_descriptor_set_layout_vertex_transform{}, m_p_descriptor_set_layout_texture{},
m_p_pipeline_layout{}, m_p_pipeline_with_textures{}, m_p_pipeline_without_textures{},
m_p_pipeline_stencil_for_region_where_geometry_will_be_drawn{}, m_p_pipeline_stencil_for_regular_geometry_that_applied_to_region_with_textures{},
m_p_pipeline_stencil_for_regular_geometry_that_applied_to_region_without_textures{}, m_p_descriptor_set{}, m_p_render_pass{},
m_p_sampler_linear{}, m_scissor{}, m_scissor_original{}, m_viewport{}, m_p_queue_present{}, m_p_queue_graphics{}, m_p_queue_compute{},
#ifdef RMLUI_VK_DEBUG
m_debug_messenger{},
#endif
m_swapchain_format{}, m_texture_depthstencil{}, m_pending_for_deletion_textures_by_frames{}
{}
RenderInterface_VK::~RenderInterface_VK() {}
Rml::CompiledGeometryHandle RenderInterface_VK::CompileGeometry(Rml::Span<const Rml::Vertex> vertices, Rml::Span<const int> indices)
{
RMLUI_ZoneScopedN("Vulkan - CompileGeometry");
VkDescriptorSet p_current_descriptor_set = nullptr;
p_current_descriptor_set = m_p_descriptor_set;
RMLUI_VK_ASSERTMSG(p_current_descriptor_set,
"you can't have here an invalid pointer of VkDescriptorSet. Two reason might be. 1. - you didn't allocate it "
"at all or 2. - Somehing is wrong with allocation and somehow it was corrupted by something.");
auto* p_geometry_handle = new geometry_handle_t{};
uint32_t* pCopyDataToBuffer = nullptr;
const void* pData = reinterpret_cast<const void*>(vertices.data());
bool status = m_memory_pool.Alloc_VertexBuffer((uint32_t)vertices.size(), sizeof(Rml::Vertex), reinterpret_cast<void**>(&pCopyDataToBuffer),
&p_geometry_handle->m_p_vertex, &p_geometry_handle->m_p_vertex_allocation);
RMLUI_VK_ASSERTMSG(status, "failed to AllocVertexBuffer");
memcpy(pCopyDataToBuffer, pData, sizeof(Rml::Vertex) * vertices.size());
status = m_memory_pool.Alloc_IndexBuffer((uint32_t)indices.size(), sizeof(int), reinterpret_cast<void**>(&pCopyDataToBuffer),
&p_geometry_handle->m_p_index, &p_geometry_handle->m_p_index_allocation);
RMLUI_VK_ASSERTMSG(status, "failed to AllocIndexBuffer");
memcpy(pCopyDataToBuffer, indices.data(), sizeof(int) * indices.size());
p_geometry_handle->m_num_indices = (int)indices.size();
return Rml::CompiledGeometryHandle(p_geometry_handle);
}
void RenderInterface_VK::RenderGeometry(Rml::CompiledGeometryHandle geometry, Rml::Vector2f translation, Rml::TextureHandle texture)
{
RMLUI_ZoneScopedN("Vulkan - RenderCompiledGeometry");
if (m_p_current_command_buffer == nullptr)
return;
RMLUI_VK_ASSERTMSG(m_p_current_command_buffer, "must be valid otherwise you can't render now!!! (can't be)");
texture_data_t* p_texture = reinterpret_cast<texture_data_t*>(texture);
VkDescriptorImageInfo info_descriptor_image = {};
if (p_texture && p_texture->m_p_vk_descriptor_set == nullptr)
{
VkDescriptorSet p_texture_set = nullptr;
m_manager_descriptors.Alloc_Descriptor(m_p_device, &m_p_descriptor_set_layout_texture, &p_texture_set);
info_descriptor_image.imageView = p_texture->m_p_vk_image_view;
info_descriptor_image.sampler = p_texture->m_p_vk_sampler;
info_descriptor_image.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkWriteDescriptorSet info_write = {};
info_write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
info_write.dstSet = p_texture_set;
info_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
info_write.dstBinding = 2;
info_write.pImageInfo = &info_descriptor_image;
info_write.descriptorCount = 1;
vkUpdateDescriptorSets(m_p_device, 1, &info_write, 0, nullptr);
p_texture->m_p_vk_descriptor_set = p_texture_set;
}
geometry_handle_t* p_casted_compiled_geometry = reinterpret_cast<geometry_handle_t*>(geometry);
m_user_data_for_vertex_shader.m_translate = translation;
VkDescriptorSet p_current_descriptor_set = nullptr;
p_current_descriptor_set = m_p_descriptor_set;
RMLUI_VK_ASSERTMSG(p_current_descriptor_set,
"you can't have here an invalid pointer of VkDescriptorSet. Two reason might be. 1. - you didn't allocate it "
"at all or 2. - Somehing is wrong with allocation and somehow it was corrupted by something.");
shader_vertex_user_data_t* p_data = nullptr;
if (p_casted_compiled_geometry->m_p_shader_allocation == nullptr)
{
// it means it was freed in ReleaseCompiledGeometry method
bool status = m_memory_pool.Alloc_GeneralBuffer(sizeof(m_user_data_for_vertex_shader), reinterpret_cast<void**>(&p_data),
&p_casted_compiled_geometry->m_p_shader, &p_casted_compiled_geometry->m_p_shader_allocation);
RMLUI_VK_ASSERTMSG(status, "failed to allocate VkDescriptorBufferInfo for uniform data to shaders");
}
else
{
// it means our state is dirty and we need to update data, but it is not right in terms of architecture, for real better experience would
// be great to free all "compiled" geometries and "re-build" them in one general way, but here I got only three callings for
// font-face-layer textures (load_document example) and that shit. So better to think how to make it right, if it is fine okay, if it is
// not okay and like we really expect that ReleaseCompiledGeometry for all objects that needs to be rebuilt so better to implement that,
// but still it is a big architectural thing (or at least you need to do something big commits here to implement a such feature), so my
// implementation doesn't break anything what we had, but still it looks strange. If I get callings for releasing maybe I need to use it
// for all objects not separately????? Otherwise it is better to provide method for resizing (or some kind of "resizing" callback) for
// recalculating all geometry IDK, so it means you pass the existed geometry that wasn't pass to ReleaseCompiledGeometry, but from another
// hand you need to re-build compiled geometry again so we have two kinds of geometry one is compiled and never changes and one is dynamic
// and it goes through pipeline InitializationOfProgram...->Compile->Render->Release->Compile->Render->Release...
m_memory_pool.Free_GeometryHandle_ShaderDataOnly(p_casted_compiled_geometry);
bool status = m_memory_pool.Alloc_GeneralBuffer(sizeof(m_user_data_for_vertex_shader), reinterpret_cast<void**>(&p_data),
&p_casted_compiled_geometry->m_p_shader, &p_casted_compiled_geometry->m_p_shader_allocation);
RMLUI_VK_ASSERTMSG(status, "failed to allocate VkDescriptorBufferInfo for uniform data to shaders");
}
if (p_data)
{
p_data->m_transform = m_user_data_for_vertex_shader.m_transform;
p_data->m_translate = m_user_data_for_vertex_shader.m_translate;
}
else
{
RMLUI_VK_ASSERTMSG(p_data, "you can't reach this zone, it means something bad");
}
const uint32_t pDescriptorOffsets = static_cast<uint32_t>(p_casted_compiled_geometry->m_p_shader.offset);
VkDescriptorSet p_texture_descriptor_set = nullptr;
if (p_texture)
{
p_texture_descriptor_set = p_texture->m_p_vk_descriptor_set;
}
VkDescriptorSet p_sets[] = {p_current_descriptor_set, p_texture_descriptor_set};
int real_size_of_sets = 2;
if (p_texture == nullptr)
real_size_of_sets = 1;
vkCmdBindDescriptorSets(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_p_pipeline_layout, 0, real_size_of_sets, p_sets, 1,
&pDescriptorOffsets);
if (m_is_use_stencil_pipeline)
{
vkCmdBindPipeline(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_p_pipeline_stencil_for_region_where_geometry_will_be_drawn);
}
else
{
if (p_texture)
{
if (m_is_apply_to_regular_geometry_stencil)
{
vkCmdBindPipeline(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
m_p_pipeline_stencil_for_regular_geometry_that_applied_to_region_with_textures);
}
else
{
vkCmdBindPipeline(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_p_pipeline_with_textures);
}
}
else
{
if (m_is_apply_to_regular_geometry_stencil)
{
vkCmdBindPipeline(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
m_p_pipeline_stencil_for_regular_geometry_that_applied_to_region_without_textures);
}
else
{
vkCmdBindPipeline(m_p_current_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_p_pipeline_without_textures);
}
}
}
vkCmdBindVertexBuffers(m_p_current_command_buffer, 0, 1, &p_casted_compiled_geometry->m_p_vertex.buffer,
&p_casted_compiled_geometry->m_p_vertex.offset);
vkCmdBindIndexBuffer(m_p_current_command_buffer, p_casted_compiled_geometry->m_p_index.buffer, p_casted_compiled_geometry->m_p_index.offset,
VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(m_p_current_command_buffer, p_casted_compiled_geometry->m_num_indices, 1, 0, 0, 0);
}
void RenderInterface_VK::ReleaseGeometry(Rml::CompiledGeometryHandle geometry)
{
RMLUI_ZoneScopedN("Vulkan - ReleaseCompiledGeometry");
geometry_handle_t* p_casted_geometry = reinterpret_cast<geometry_handle_t*>(geometry);
m_pending_for_deletion_geometries.push_back(p_casted_geometry);
}
void RenderInterface_VK::EnableScissorRegion(bool enable)
{
if (m_p_current_command_buffer == nullptr)
return;
if (m_is_transform_enabled)
{
m_is_apply_to_regular_geometry_stencil = true;
}
m_is_use_scissor_specified = enable;
if (m_is_use_scissor_specified == false)
{
m_is_apply_to_regular_geometry_stencil = false;
vkCmdSetScissor(m_p_current_command_buffer, 0, 1, &m_scissor_original);
}
}
void RenderInterface_VK::SetScissorRegion(Rml::Rectanglei region)
{
if (m_is_use_scissor_specified)
{
if (m_is_transform_enabled)
{
Rml::Vertex vertices[4];
vertices[0].position = Rml::Vector2f(region.TopLeft());
vertices[1].position = Rml::Vector2f(region.TopRight());
vertices[2].position = Rml::Vector2f(region.BottomRight());
vertices[3].position = Rml::Vector2f(region.BottomLeft());
int indices[6] = {0, 2, 1, 0, 3, 2};
m_is_use_stencil_pipeline = true;
#ifdef RMLUI_DEBUG
VkDebugUtilsLabelEXT info{};
info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
info.color[0] = 1.0f;
info.color[1] = 1.0f;
info.color[2] = 0.0f;
info.color[3] = 1.0f;
info.pLabelName = "SetScissorRegion (generated region)";
vkCmdInsertDebugUtilsLabelEXT(m_p_current_command_buffer, &info);
#endif
VkClearDepthStencilValue info_clear_color{};
info_clear_color.depth = 1.0f;
info_clear_color.stencil = 0;
VkClearAttachment clear_attachment = {};
clear_attachment.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
clear_attachment.clearValue.depthStencil = info_clear_color;
clear_attachment.colorAttachment = 1;
VkClearRect clear_rect = {};
clear_rect.layerCount = 1;
clear_rect.rect.extent.width = m_width;
clear_rect.rect.extent.height = m_height;
vkCmdClearAttachments(m_p_current_command_buffer, 1, &clear_attachment, 1, &clear_rect);
if (Rml::CompiledGeometryHandle handle = CompileGeometry({vertices, 4}, {indices, 6}))
{
RenderGeometry(handle, {}, {});
ReleaseGeometry(handle);
}
m_is_use_stencil_pipeline = false;
m_is_apply_to_regular_geometry_stencil = true;
}
else
{
m_scissor.extent.width = region.Width();
m_scissor.extent.height = region.Height();
m_scissor.offset.x = Rml::Math::Clamp(region.Left(), 0, m_width);
m_scissor.offset.y = Rml::Math::Clamp(region.Top(), 0, m_height);
#ifdef RMLUI_DEBUG
VkDebugUtilsLabelEXT info{};
info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
info.color[0] = 1.0f;
info.color[1] = 0.0f;
info.color[2] = 0.0f;
info.color[3] = 1.0f;
info.pLabelName = "SetScissorRegion (offset)";
vkCmdInsertDebugUtilsLabelEXT(m_p_current_command_buffer, &info);
#endif
vkCmdSetScissor(m_p_current_command_buffer, 0, 1, &m_scissor);
}
}
}
// Set to byte packing, or the compiler will expand our struct, which means it won't read correctly from file
#pragma pack(1)
struct TGAHeader {
char idLength;
char colourMapType;
char dataType;
short int colourMapOrigin;
short int colourMapLength;
char colourMapDepth;
short int xOrigin;
short int yOrigin;
short int width;
short int height;
char bitsPerPixel;
char imageDescriptor;
};
// Restore packing
#pragma pack()
Rml::TextureHandle RenderInterface_VK::LoadTexture(Rml::Vector2i& texture_dimensions, const Rml::String& source)
{
Rml::FileInterface* file_interface = Rml::GetFileInterface();
Rml::FileHandle file_handle = file_interface->Open(source);
if (!file_handle)
{
return false;
}
file_interface->Seek(file_handle, 0, SEEK_END);
size_t buffer_size = file_interface->Tell(file_handle);
file_interface->Seek(file_handle, 0, SEEK_SET);
if (buffer_size <= sizeof(TGAHeader))
{
Rml::Log::Message(Rml::Log::LT_ERROR, "Texture file size is smaller than TGAHeader, file is not a valid TGA image.");
file_interface->Close(file_handle);
return false;
}
using Rml::byte;
Rml::UniquePtr<byte[]> buffer(new byte[buffer_size]);
file_interface->Read(buffer.get(), buffer_size, file_handle);
file_interface->Close(file_handle);
TGAHeader header;
memcpy(&header, buffer.get(), sizeof(TGAHeader));
int color_mode = header.bitsPerPixel / 8;
const size_t image_size = header.width * header.height * 4; // We always make 32bit textures
if (header.dataType != 2)
{
Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24/32bit uncompressed TGAs are supported.");
return false;
}
// Ensure we have at least 3 colors
if (color_mode < 3)
{
Rml::Log::Message(Rml::Log::LT_ERROR, "Only 24 and 32bit textures are supported.");
return false;
}
const byte* image_src = buffer.get() + sizeof(TGAHeader);
Rml::UniquePtr<byte[]> image_dest_buffer(new byte[image_size]);
byte* image_dest = image_dest_buffer.get();
// Targa is BGR, swap to RGB, flip Y axis, and convert to premultiplied alpha.
for (long y = 0; y < header.height; y++)
{
long read_index = y * header.width * color_mode;
long write_index = ((header.imageDescriptor & 32) != 0) ? read_index : (header.height - y - 1) * header.width * 4;
for (long x = 0; x < header.width; x++)
{
image_dest[write_index] = image_src[read_index + 2];
image_dest[write_index + 1] = image_src[read_index + 1];
image_dest[write_index + 2] = image_src[read_index];
if (color_mode == 4)
{
const byte alpha = image_src[read_index + 3];
for (size_t j = 0; j < 3; j++)
image_dest[write_index + j] = byte((image_dest[write_index + j] * alpha) / 255);
image_dest[write_index + 3] = alpha;
}
else
image_dest[write_index + 3] = 255;
write_index += 4;
read_index += color_mode;
}
}
texture_dimensions.x = header.width;
texture_dimensions.y = header.height;
return GenerateTexture({image_dest, image_size}, texture_dimensions);
}
Rml::TextureHandle RenderInterface_VK::GenerateTexture(Rml::Span<const Rml::byte> source_data, Rml::Vector2i source_dimensions)
{
RMLUI_ASSERT(source_data.data() && source_data.size() == size_t(source_dimensions.x * source_dimensions.y * 4));
Rml::String source_name = "generated-texture";
return CreateTexture(source_data, source_dimensions, source_name);
}
/*
How vulkan works with textures efficiently?
You need to create buffer that has CPU memory accessibility it means it uses your RAM memory for storing data and it has only CPU visibility (RAM)
After you create buffer that has GPU memory accessibility it means it uses by your video hardware and it has only VRAM (Video RAM) visibility
So you copy data to CPU_buffer and after you copy that thing to GPU_buffer, but delete CPU_buffer
So it means you "uploaded" data to GPU
Again, you need to "write" data into CPU buffer after you need to copy that data from buffer to GPU buffer and after that buffer go to GPU.
RAW_POINTER_DATA_BYTES_LITERALLY->COPY_TO->CPU->COPY_TO->GPU->Releasing_CPU <= that's how works uploading textures in Vulkan if you want to have
efficient handling otherwise it is cpu_to_gpu visibility and it means you create only ONE buffer that is accessible for CPU and for GPU, but it
will cause the worst performance...
*/
Rml::TextureHandle RenderInterface_VK::CreateTexture(Rml::Span<const Rml::byte> source, Rml::Vector2i dimensions, const Rml::String& name)
{
RMLUI_ZoneScopedN("Vulkan - GenerateTexture");
RMLUI_VK_ASSERTMSG(!source.empty(), "you pushed not valid data for copying to buffer");
RMLUI_VK_ASSERTMSG(m_p_allocator, "you have to initialize Vma Allocator for this method");
(void)name;
int width = dimensions.x;
int height = dimensions.y;
RMLUI_VK_ASSERTMSG(width, "invalid width");
RMLUI_VK_ASSERTMSG(height, "invalid height");
VkDeviceSize image_size = source.size();
VkFormat format = VkFormat::VK_FORMAT_R8G8B8A8_UNORM;
buffer_data_t cpu_buffer = CreateResource_StagingBuffer(image_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
void* data;
vmaMapMemory(m_p_allocator, cpu_buffer.m_p_vma_allocation, &data);
memcpy(data, source.data(), static_cast<size_t>(image_size));
vmaUnmapMemory(m_p_allocator, cpu_buffer.m_p_vma_allocation);
VkExtent3D extent_image = {};
extent_image.width = static_cast<uint32_t>(width);
extent_image.height = static_cast<uint32_t>(height);
extent_image.depth = 1;
auto* p_texture = new texture_data_t{};
VkImageCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
info.pNext = nullptr;
info.imageType = VK_IMAGE_TYPE_2D;
info.format = format;
info.extent = extent_image;
info.mipLevels = 1;
info.arrayLayers = 1;
info.samples = VK_SAMPLE_COUNT_1_BIT;
info.tiling = VK_IMAGE_TILING_OPTIMAL;
info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
VmaAllocationCreateInfo info_allocation = {};
info_allocation.usage = VMA_MEMORY_USAGE_GPU_ONLY;
VkImage p_image = nullptr;
VmaAllocation p_allocation = nullptr;
VmaAllocationInfo info_stats = {};
VkResult status = vmaCreateImage(m_p_allocator, &info, &info_allocation, &p_image, &p_allocation, &info_stats);
RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vmaCreateImage");
#ifdef RMLUI_VK_DEBUG
Rml::Log::Message(Rml::Log::LT_DEBUG, "Created texture '%s' [%dx%d, %s]", name.c_str(), dimensions.x, dimensions.y,
FormatByteSize(info_stats.size).c_str());
#endif
p_texture->m_p_vk_image = p_image;
p_texture->m_p_vma_allocation = p_allocation;
#ifdef RMLUI_VK_DEBUG
vmaSetAllocationName(m_p_allocator, p_allocation, name.c_str());
#endif
/*
* So Vulkan works only through VkCommandBuffer, it is for remembering API commands what you want to call from GPU
* So on CPU side you need to create a scope that consists of two things
* vkBeginCommandBuffer
* ... <= here your commands what you want to place into your command buffer and send it to GPU through vkQueueSubmit function
* vkEndCommandBuffer
*
* So commands start to work ONLY when you called the vkQueueSubmit otherwise you just "place" commands into your command buffer but you
* didn't issue any thing in order to start the work on GPU side. ALWAYS remember that just sumbit means execute async mode, so you have to wait
* operations before they exeecute fully otherwise you will get some errors or write/read concurrent state and all other stuff, vulkan validation
* will notify you :) (in most cases)
*
* BUT you need always sync what you have done when you called your vkQueueSubmit function, so it is wait method, but generally you can create
* another queue and isolate all stuff tbh
*
* So understing these principles you understand how to work with API and your GPU
*
* There's nothing hard, but it makes all stuff on programmer side if you remember OpenGL and how it was easy to load texture upload it and create
* buffers and it In OpenGL all stuff is handled by driver and other things, not a programmer definitely
*
* What we do here? We need to change the layout of our image. it means where we want to use it. So in our case we want to see that this image
* will be in shaders Because the initial state of create object is VK_IMAGE_LAYOUT_UNDEFINED means you can't just pass that VkImage handle to
* your functions and wait that it comes to shaders for exmaple No it doesn't work like that you have to have the explicit states of your resource
* and where it goes
*
* In our case we want to see in our pixel shader so we need to change transfer into this flag VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, because we
* want to copy so it means some transfer thing, but after we say it goes to pixel after our copying operation
*/
m_upload_manager.UploadToGPU([p_image, extent_image, cpu_buffer](VkCommandBuffer p_cmd) {
VkImageSubresourceRange range = {};
range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
range.baseMipLevel = 0;
range.baseArrayLayer = 0;
range.levelCount = 1;
range.layerCount = 1;
VkImageMemoryBarrier info_barrier = {};
info_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
info_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
info_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
info_barrier.image = p_image;
info_barrier.subresourceRange = range;
info_barrier.srcAccessMask = 0;
info_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
vkCmdPipelineBarrier(p_cmd, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &info_barrier);
VkBufferImageCopy region = {};
region.bufferOffset = 0;
region.bufferRowLength = 0;
region.bufferImageHeight = 0;
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageExtent = extent_image;
vkCmdCopyBufferToImage(p_cmd, cpu_buffer.m_p_vk_buffer, p_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
VkImageMemoryBarrier info_barrier_shader_read = {};
info_barrier_shader_read.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
info_barrier_shader_read.pNext = nullptr;
info_barrier_shader_read.image = p_image;
info_barrier_shader_read.subresourceRange = range;
info_barrier_shader_read.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
info_barrier_shader_read.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
info_barrier_shader_read.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
info_barrier_shader_read.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
vkCmdPipelineBarrier(p_cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1,
&info_barrier_shader_read);
});
DestroyResource_StagingBuffer(cpu_buffer);
VkImageViewCreateInfo info_image_view = {};
info_image_view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
info_image_view.pNext = nullptr;
info_image_view.image = p_texture->m_p_vk_image;
info_image_view.viewType = VK_IMAGE_VIEW_TYPE_2D;
info_image_view.format = format;
info_image_view.subresourceRange.baseMipLevel = 0;
info_image_view.subresourceRange.levelCount = 1;
info_image_view.subresourceRange.baseArrayLayer = 0;
info_image_view.subresourceRange.layerCount = 1;
info_image_view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
VkImageView p_image_view = nullptr;
status = vkCreateImageView(m_p_device, &info_image_view, nullptr, &p_image_view);
RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkCreateImageView");
p_texture->m_p_vk_image_view = p_image_view;
p_texture->m_p_vk_sampler = m_p_sampler_linear;
return reinterpret_cast<Rml::TextureHandle>(p_texture);
}
void RenderInterface_VK::ReleaseTexture(Rml::TextureHandle texture_handle)
{
texture_data_t* p_texture = reinterpret_cast<texture_data_t*>(texture_handle);
if (p_texture)
{
m_pending_for_deletion_textures_by_frames[m_semaphore_index_previous].push_back(p_texture);
}
}
void RenderInterface_VK::SetTransform(const Rml::Matrix4f* transform)
{
m_is_transform_enabled = !!(transform);
m_user_data_for_vertex_shader.m_transform = m_projection * (transform ? *transform : Rml::Matrix4f::Identity());
}
void RenderInterface_VK::BeginFrame()
{
Wait();
Update_PendingForDeletion_Textures_By_Frames();
Update_PendingForDeletion_Geometries();
m_command_buffer_ring.OnBeginFrame();
m_p_current_command_buffer = m_command_buffer_ring.GetCommandBufferForActiveFrame(CommandBufferName::Primary);
VkCommandBufferBeginInfo info = {};
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
info.pInheritanceInfo = nullptr;
info.pNext = nullptr;
info.flags = VkCommandBufferUsageFlagBits::VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
auto status = vkBeginCommandBuffer(m_p_current_command_buffer, &info);
RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkBeginCommandBuffer");
VkClearValue for_filling_back_buffer_color;
VkClearValue for_stencil_depth;
for_stencil_depth.depthStencil = {1.0f, 0};
for_filling_back_buffer_color.color = {{0.0f, 0.0f, 0.0f, 1.0f}};
const VkClearValue p_color_rt[] = {for_filling_back_buffer_color, for_stencil_depth};
VkRenderPassBeginInfo info_pass = {};
info_pass.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
info_pass.pNext = nullptr;
info_pass.renderPass = m_p_render_pass;
info_pass.framebuffer = m_swapchain_frame_buffers[m_image_index];
info_pass.pClearValues = p_color_rt;
info_pass.clearValueCount = 2;
info_pass.renderArea.offset.x = 0;
info_pass.renderArea.offset.y = 0;
info_pass.renderArea.extent.width = m_width;
info_pass.renderArea.extent.height = m_height;
vkCmdBeginRenderPass(m_p_current_command_buffer, &info_pass, VkSubpassContents::VK_SUBPASS_CONTENTS_INLINE);
vkCmdSetViewport(m_p_current_command_buffer, 0, 1, &m_viewport);
m_is_apply_to_regular_geometry_stencil = false;
}
void RenderInterface_VK::EndFrame()
{
if (m_p_current_command_buffer == nullptr)
return;
vkCmdEndRenderPass(m_p_current_command_buffer);
auto status = vkEndCommandBuffer(m_p_current_command_buffer);
RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkEndCommandBuffer");
Submit();
Present();
m_p_current_command_buffer = nullptr;
}
void RenderInterface_VK::SetViewport(int width, int height)
{
auto status = vkDeviceWaitIdle(m_p_device);
RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "failed to vkDeviceWaitIdle");
if (width > 0 && height > 0)
{
m_width = width;
m_height = height;
}
if (m_p_swapchain)
{
Destroy_Swapchain();
DestroyResourcesDependentOnSize();
m_p_swapchain = {};
}
VkExtent2D window_extent = GetValidSurfaceExtent();
if (window_extent.width == 0 || window_extent.height == 0)
return;
#ifdef RMLUI_VK_DEBUG
Rml::Log::Message(Rml::Log::Type::LT_DEBUG, "Rml width: %d height: %d | Vulkan width: %d height: %d", m_width, m_height, window_extent.width,
window_extent.height);
#endif
// we need to sync the data from Vulkan so we can't use native Rml's data about width and height so be careful otherwise we create framebuffer
// with Rml's width and height but they're different to what Vulkan determines for our window (e.g. device/swapchain)
m_width = window_extent.width;
m_height = window_extent.height;
Initialize_Swapchain(window_extent);
CreateResourcesDependentOnSize(window_extent);
}
bool RenderInterface_VK::IsSwapchainValid()
{
return m_p_swapchain != nullptr;
}
void RenderInterface_VK::RecreateSwapchain()
{
SetViewport(m_width, m_height);
}
bool RenderInterface_VK::Initialize(Rml::Vector<const char*> required_extensions, CreateSurfaceCallback create_surface_callback)
{
RMLUI_ZoneScopedN("Vulkan - Initialize");
int glad_result = 0;
glad_result = gladLoaderLoadVulkan(VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE);
RMLUI_VK_ASSERTMSG(glad_result != 0, "Vulkan loader failed - Global functions");
Initialize_Instance(std::move(required_extensions));
VkPhysicalDeviceProperties physical_device_properties = {};
Initialize_PhysicalDevice(physical_device_properties);
glad_result = gladLoaderLoadVulkan(m_p_instance, m_p_physical_device, VK_NULL_HANDLE);
RMLUI_VK_ASSERTMSG(glad_result != 0, "Vulkan loader failed - Instance functions");
Initialize_Surface(create_surface_callback);
Initialize_QueueIndecies();
Initialize_Device();
glad_result = gladLoaderLoadVulkan(m_p_instance, m_p_physical_device, m_p_device);
RMLUI_VK_ASSERTMSG(glad_result != 0, "Vulkan loader failed - Device functions");
Initialize_Queues();
Initialize_SyncPrimitives();
Initialize_Allocator();
Initialize_Resources(physical_device_properties);
return true;
}
void RenderInterface_VK::Shutdown()
{
RMLUI_ZoneScopedN("Vulkan - Shutdown");
auto status = vkDeviceWaitIdle(m_p_device);
RMLUI_VK_ASSERTMSG(status == VkResult::VK_SUCCESS, "you must have a valid status here");
DestroyResourcesDependentOnSize();
Destroy_Resources();
Destroy_Allocator();
Destroy_SyncPrimitives();
Destroy_Swapchain();
Destroy_Surface();
Destroy_Device();
Destroy_ReportDebugCallback();
Destroy_Instance();
gladLoaderUnloadVulkan();
}
void RenderInterface_VK::Initialize_Instance(Rml::Vector<const char*> required_extensions) noexcept
{
uint32_t required_version = GetRequiredVersionAndValidateMachine();
VkApplicationInfo info = {};
info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
info.pNext = nullptr;
info.pApplicationName = "RmlUi Shell";
info.applicationVersion = 50;
info.pEngineName = "RmlUi";
info.apiVersion = required_version;
Rml::Vector<const char*> instance_layer_names;
Rml::Vector<const char*> instance_extension_names = std::move(required_extensions);
CreatePropertiesFor_Instance(instance_layer_names, instance_extension_names);
VkInstanceCreateInfo info_instance = {};
info_instance.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
info_instance.pNext = &debug_validation_features_ext;
info_instance.flags = 0;
info_instance.pApplicationInfo = &info;
info_instance.enabledExtensionCount = static_cast<uint32_t>(instance_extension_names.size());
info_instance.ppEnabledExtensionNames = instance_extension_names.data();
info_instance.enabledLayerCount = static_cast<uint32_t>(instance_layer_names.size());
info_instance.ppEnabledLayerNames = instance_layer_names.data();
VkResult status = vkCreateInstance(&info_instance, nullptr, &m_p_instance);
RMLUI_VK_ASSERTMSG(status == VK_SUCCESS, "failed to vkCreateInstance");
CreateReportDebugCallback();
}
void RenderInterface_VK::Initialize_Device() noexcept
{
ExtensionPropertiesList device_extension_properties;
CreatePropertiesFor_Device(device_extension_properties);
Rml::Vector<const char*> device_extension_names;
AddExtensionToDevice(device_extension_names, device_extension_properties, VK_KHR_SWAPCHAIN_EXTENSION_NAME);
AddExtensionToDevice(device_extension_names, device_extension_properties, VK_EXT_SCALAR_BLOCK_LAYOUT_EXTENSION_NAME);
#ifdef RMLUI_DEBUG
AddExtensionToDevice(device_extension_names, device_extension_properties, VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#endif
float queue_priorities[1] = {0.0f};
VkDeviceQueueCreateInfo info_queue[2] = {};
info_queue[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
info_queue[0].pNext = nullptr;
info_queue[0].queueCount = 1;
info_queue[0].pQueuePriorities = queue_priorities;
info_queue[0].queueFamilyIndex = m_queue_index_graphics;
info_queue[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
info_queue[1].pNext = nullptr;
info_queue[1].queueCount = 1;
info_queue[1].pQueuePriorities = queue_priorities;
info_queue[1].queueFamilyIndex = m_queue_index_compute;
VkPhysicalDeviceFeatures features_physical_device = {};
features_physical_device.fillModeNonSolid = true;
features_physical_device.pipelineStatisticsQuery = true;
features_physical_device.fragmentStoresAndAtomics = true;
features_physical_device.vertexPipelineStoresAndAtomics = true;
features_physical_device.shaderImageGatherExtended = true;
features_physical_device.wideLines = true;
VkPhysicalDeviceShaderSubgroupExtendedTypesFeaturesKHR shader_subgroup_extended_type = {};
shader_subgroup_extended_type.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_SUBGROUP_EXTENDED_TYPES_FEATURES_KHR;
shader_subgroup_extended_type.pNext = nullptr;
shader_subgroup_extended_type.shaderSubgroupExtendedTypes = VK_TRUE;
VkPhysicalDeviceFeatures2 features_physical_device2 = {};
features_physical_device2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
features_physical_device2.features = features_physical_device;
features_physical_device2.pNext = &shader_subgroup_extended_type;
VkDeviceCreateInfo info_device = {};
info_device.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
info_device.pNext = &features_physical_device2;
info_device.queueCreateInfoCount = m_queue_index_compute != m_queue_index_graphics ? 2 : 1;
info_device.pQueueCreateInfos = info_queue;
info_device.enabledExtensionCount = static_cast<uint32_t>(device_extension_names.size());
info_device.ppEnabledExtensionNames = info_device.enabledExtensionCount ? device_extension_names.data() : nullptr;
info_device.pEnabledFeatures = nullptr;
VkResult status = vkCreateDevice(m_p_physical_device, &info_device, nullptr, &m_p_device);
RMLUI_VK_ASSERTMSG(status == VK_SUCCESS, "failed to vkCreateDevice");
}
void RenderInterface_VK::Initialize_PhysicalDevice(VkPhysicalDeviceProperties& out_physical_device_properties) noexcept
{
PhysicalDeviceWrapperList physical_devices;
CollectPhysicalDevices(physical_devices);
const PhysicalDeviceWrapper* selected_physical_device =
ChoosePhysicalDevice(physical_devices, VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU);
if (!selected_physical_device)
{
Rml::Log::Message(Rml::Log::LT_WARNING, "Failed to pick the discrete gpu, now trying to pick integrated GPU");
selected_physical_device = ChoosePhysicalDevice(physical_devices, VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU);
if (!selected_physical_device)
{
Rml::Log::Message(Rml::Log::LT_WARNING, "Failed to pick the integrated gpu, now trying to pick the CPU");
selected_physical_device = ChoosePhysicalDevice(physical_devices, VkPhysicalDeviceType::VK_PHYSICAL_DEVICE_TYPE_CPU);
}
}
RMLUI_VK_ASSERTMSG(selected_physical_device, "there's no suitable physical device for rendering, abort this application");
m_p_physical_device = selected_physical_device->m_p_physical_device;
vkGetPhysicalDeviceProperties(m_p_physical_device, &out_physical_device_properties);
#ifdef RMLUI_VK_DEBUG
const auto& properties = selected_physical_device->m_physical_device_properties;
Rml::Log::Message(Rml::Log::LT_DEBUG, "Picked physical device: %s", properties.deviceName);
#endif
}
void RenderInterface_VK::Initialize_Swapchain(VkExtent2D window_extent) noexcept
{
m_swapchain_format = ChooseSwapchainFormat();
VkSwapchainCreateInfoKHR info = {};
info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
info.pNext = nullptr;
info.surface = m_p_surface;
info.imageFormat = m_swapchain_format.format;
info.minImageCount = Choose_SwapchainImageCount();
info.imageColorSpace = m_swapchain_format.colorSpace;
info.imageExtent = window_extent;
info.preTransform = CreatePretransformSwapchain();
info.compositeAlpha = ChooseSwapchainCompositeAlpha();
info.imageArrayLayers = 1;
info.presentMode = GetPresentMode();
info.oldSwapchain = nullptr;
info.clipped = true;
info.imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
info.queueFamilyIndexCount = 0;
info.pQueueFamilyIndices = nullptr;