This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
nuklear-glfw-vulkan.h
1326 lines (1154 loc) · 49.2 KB
/
nuklear-glfw-vulkan.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
#ifndef NK_VULKAN_GLFW_H_
#define NK_VULKAN_GLFW_H_
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_VULKAN_IMPLEMENTATION
#include "nuklear.h"
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include "string.h"
enum nk_glfw_init_state { NK_GLFW3_DEFAULT = 0, NK_GLFW3_INSTALL_CALLBACKS };
NK_API struct nk_context *
nk_glfw3_init(GLFWwindow *win, VkDevice logical_device,
VkPhysicalDevice physical_device,
uint32_t graphics_queue_family_index, VkImageView *image_views,
uint32_t image_views_len, uint32_t width, uint32_t height,
VkFormat color_format, enum nk_glfw_init_state init_state);
NK_API void nk_glfw3_resize(uint32_t width, uint32_t height);
NK_API void nk_glfw3_shutdown();
NK_API void nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas);
NK_API void nk_glfw3_font_stash_end(VkQueue graphics_queue);
NK_API void nk_glfw3_new_frame();
NK_API VkSemaphore nk_glfw3_render(enum nk_anti_aliasing AA,
VkQueue graphics_queue,
uint32_t buffer_index,
VkSemaphore wait_semaphore);
NK_API void nk_glfw3_device_destroy(void);
NK_API void nk_glfw3_device_create(VkDevice logical_device,
VkPhysicalDevice physical_device,
uint32_t graphics_queue_family_index,
VkImageView *image_views,
uint32_t image_views_len, uint32_t width,
uint32_t height, VkFormat color_format);
NK_API void nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint);
NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff);
NK_API void nk_glfw3_mouse_button_callback(GLFWwindow *win, int button,
int action, int mods);
#endif
#ifdef NK_GLFW_VULKAN_IMPLEMENTATION
// ${NUKLEAR_SHADERS_START}
#include "nuklearshaders/nuklear.frag.h"
#include "nuklearshaders/nuklear.vert.h"
// ${NUKLEAR_SHADERS_END}
#ifndef NK_GLFW_TEXT_MAX
#define NK_GLFW_TEXT_MAX 256
#endif
#ifndef NK_GLFW_DOUBLE_CLICK_LO
#define NK_GLFW_DOUBLE_CLICK_LO 0.02
#endif
#ifndef NK_GLFW_DOUBLE_CLICK_HI
#define NK_GLFW_DOUBLE_CLICK_HI 0.2
#endif
#ifndef NK_GLFW_MAX_TEXTURES
#define NK_GLFW_MAX_TEXTURES 128
#endif
#define VK_COLOR_COMPONENT_MASK_RGBA \
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | \
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_INDEX_BUFFER 128 * 1024
struct Mat4f {
float m[16];
};
struct nk_vulkan_texture_descriptor_set {
VkImageView image_view;
VkDescriptorSet descriptor_set;
};
struct nk_vulkan_adapter {
struct nk_buffer cmds;
struct nk_draw_null_texture null;
VkDevice logical_device;
VkPhysicalDevice physical_device;
VkCommandPool command_pool;
VkImageView *image_views;
uint32_t image_views_len;
uint32_t width;
uint32_t height;
VkFormat color_format;
VkFramebuffer *framebuffers;
uint32_t framebuffers_len;
VkSemaphore render_completed;
VkSampler sampler;
VkImage font_image;
VkImageView font_image_view;
VkDeviceMemory font_memory;
VkPipelineLayout pipeline_layout;
VkRenderPass render_pass;
VkPipeline pipeline;
VkBuffer vertex_buffer;
VkDeviceMemory vertex_memory;
VkBuffer index_buffer;
VkDeviceMemory index_memory;
VkBuffer uniform_buffer;
VkDeviceMemory uniform_memory;
VkCommandBuffer *command_buffers;
uint32_t command_buffers_len;
VkDescriptorPool descriptor_pool;
VkDescriptorSetLayout uniform_descriptor_set_layout;
VkDescriptorSet uniform_descriptor_set;
VkDescriptorSetLayout texture_descriptor_set_layout;
struct nk_vulkan_texture_descriptor_set *texture_descriptor_sets;
uint32_t texture_descriptor_sets_len;
};
struct nk_glfw_vertex {
float position[2];
float uv[2];
nk_byte col[4];
};
static struct nk_glfw {
GLFWwindow *win;
int width, height;
int display_width, display_height;
struct nk_vulkan_adapter adapter;
struct nk_context ctx;
struct nk_font_atlas atlas;
struct nk_vec2 fb_scale;
unsigned int text[NK_GLFW_TEXT_MAX];
int text_len;
struct nk_vec2 scroll;
double last_button_click;
int is_double_click_down;
struct nk_vec2 double_click_pos;
} glfw;
VkPipelineShaderStageCreateInfo create_shader(struct nk_vulkan_adapter *adapter,
unsigned char *spv_shader,
uint32_t size,
VkShaderStageFlagBits stage_bit) {
VkShaderModuleCreateInfo create_info = {
VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
create_info.codeSize = size;
create_info.pCode = (const uint32_t *)spv_shader;
VkShaderModule module = VK_NULL_HANDLE;
VkResult result = vkCreateShaderModule(adapter->logical_device, &create_info,
NULL, &module);
NK_ASSERT(result == VK_SUCCESS);
VkPipelineShaderStageCreateInfo shader_info = {
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
shader_info.stage = stage_bit;
shader_info.module = module;
shader_info.pName = "main";
return shader_info;
}
void create_descriptor_pool(struct nk_vulkan_adapter *adapter) {
VkDescriptorPoolSize pool_sizes[2] = {
{
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1,
},
{
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
NK_GLFW_MAX_TEXTURES,
},
};
VkDescriptorPoolCreateInfo pool_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO};
pool_info.poolSizeCount = 2;
pool_info.pPoolSizes = pool_sizes;
pool_info.maxSets = 1 + NK_GLFW_MAX_TEXTURES;
VkResult result =
vkCreateDescriptorPool(adapter->logical_device, &pool_info,
VK_NULL_HANDLE, &adapter->descriptor_pool);
NK_ASSERT(result == VK_SUCCESS);
}
void create_uniform_descriptor_set_layout(struct nk_vulkan_adapter *adapter) {
VkDescriptorSetLayoutBinding binding = {
0,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
1,
VK_SHADER_STAGE_VERTEX_BIT,
};
VkDescriptorSetLayoutCreateInfo descriptor_set_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
descriptor_set_info.bindingCount = 1;
descriptor_set_info.pBindings = &binding;
VkResult result = vkCreateDescriptorSetLayout(
adapter->logical_device, &descriptor_set_info, NULL,
&adapter->uniform_descriptor_set_layout);
NK_ASSERT(result == VK_SUCCESS);
}
void create_texture_descriptor_set_layout(struct nk_vulkan_adapter *adapter) {
VkDescriptorSetLayoutBinding binding = {
0,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1,
VK_SHADER_STAGE_FRAGMENT_BIT,
};
VkDescriptorSetLayoutCreateInfo descriptor_set_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
descriptor_set_info.bindingCount = 1;
descriptor_set_info.pBindings = &binding;
VkResult result = vkCreateDescriptorSetLayout(
adapter->logical_device, &descriptor_set_info, NULL,
&adapter->texture_descriptor_set_layout);
NK_ASSERT(result == VK_SUCCESS);
}
void create_and_update_uniform_descriptor_set(
struct nk_vulkan_adapter *adapter) {
VkDescriptorSetAllocateInfo allocate_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
allocate_info.descriptorPool = adapter->descriptor_pool;
allocate_info.descriptorSetCount = 1;
allocate_info.pSetLayouts = &adapter->uniform_descriptor_set_layout;
VkResult result =
vkAllocateDescriptorSets(adapter->logical_device, &allocate_info,
&adapter->uniform_descriptor_set);
NK_ASSERT(result == VK_SUCCESS);
VkDescriptorBufferInfo buffer_info = {
adapter->uniform_buffer,
0,
sizeof(struct Mat4f),
};
VkWriteDescriptorSet descriptor_write = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
descriptor_write.dstSet = adapter->uniform_descriptor_set;
descriptor_write.dstBinding = 0;
descriptor_write.dstArrayElement = 0;
descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
descriptor_write.descriptorCount = 1;
descriptor_write.pBufferInfo = &buffer_info;
vkUpdateDescriptorSets(adapter->logical_device, 1, &descriptor_write, 0,
VK_NULL_HANDLE);
}
void create_texture_descriptor_sets(struct nk_vulkan_adapter *adapter) {
VkDescriptorSetLayout *descriptor_set_layouts =
(VkDescriptorSetLayout *)malloc(NK_GLFW_MAX_TEXTURES *
sizeof(VkDescriptorSetLayout));
VkDescriptorSet *descriptor_sets =
(VkDescriptorSet *)malloc(NK_GLFW_MAX_TEXTURES * sizeof(VkDescriptorSet));
adapter->texture_descriptor_sets =
(struct nk_vulkan_texture_descriptor_set *)malloc(
NK_GLFW_MAX_TEXTURES *
sizeof(struct nk_vulkan_texture_descriptor_set));
adapter->texture_descriptor_sets_len = 0;
uint32_t i;
for (i = 0; i < NK_GLFW_MAX_TEXTURES; i++) {
descriptor_set_layouts[i] = adapter->texture_descriptor_set_layout;
descriptor_sets[i] = adapter->texture_descriptor_sets[i].descriptor_set;
}
VkDescriptorSetAllocateInfo allocate_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
allocate_info.descriptorPool = adapter->descriptor_pool;
allocate_info.descriptorSetCount = NK_GLFW_MAX_TEXTURES;
allocate_info.pSetLayouts = descriptor_set_layouts;
VkResult result = vkAllocateDescriptorSets(adapter->logical_device,
&allocate_info, descriptor_sets);
NK_ASSERT(result == VK_SUCCESS);
for (i = 0; i < NK_GLFW_MAX_TEXTURES; i++) {
adapter->texture_descriptor_sets[i].descriptor_set = descriptor_sets[i];
}
}
void update_texture_descriptor_set(
struct nk_vulkan_adapter *adapter,
struct nk_vulkan_texture_descriptor_set *texture_descriptor_set,
VkImageView image_view) {
texture_descriptor_set->image_view = image_view;
VkDescriptorImageInfo descriptor_image_info = {
adapter->sampler,
texture_descriptor_set->image_view,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
};
VkWriteDescriptorSet descriptor_write = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
descriptor_write.dstSet = texture_descriptor_set->descriptor_set;
descriptor_write.dstBinding = 0;
descriptor_write.dstArrayElement = 0;
descriptor_write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptor_write.descriptorCount = 1;
descriptor_write.pImageInfo = &descriptor_image_info;
vkUpdateDescriptorSets(adapter->logical_device, 1, &descriptor_write, 0,
VK_NULL_HANDLE);
}
void create_pipeline_layout(struct nk_vulkan_adapter *adapter) {
VkDescriptorSetLayout descriptor_set_layouts[] = {
adapter->uniform_descriptor_set_layout,
adapter->texture_descriptor_set_layout};
VkPipelineLayoutCreateInfo pipeline_layout_info = {
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
pipeline_layout_info.setLayoutCount = 2;
pipeline_layout_info.pSetLayouts = descriptor_set_layouts;
VkResult result =
(vkCreatePipelineLayout(adapter->logical_device, &pipeline_layout_info,
NULL, &adapter->pipeline_layout));
NK_ASSERT(result == VK_SUCCESS);
}
void prepare_pipeline(struct nk_vulkan_adapter *adapter) {
VkPipelineInputAssemblyStateCreateInfo input_assembly_state = {
VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
input_assembly_state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
input_assembly_state.primitiveRestartEnable = VK_FALSE;
VkPipelineRasterizationStateCreateInfo rasterization_state = {
VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO};
rasterization_state.polygonMode = VK_POLYGON_MODE_FILL;
rasterization_state.cullMode = VK_CULL_MODE_NONE;
rasterization_state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
rasterization_state.lineWidth = 1.0f;
VkPipelineColorBlendAttachmentState attachment_state = {
VK_TRUE,
VK_BLEND_FACTOR_SRC_ALPHA,
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
VK_BLEND_OP_ADD,
VK_BLEND_FACTOR_SRC_ALPHA,
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
VK_BLEND_OP_ADD,
VK_COLOR_COMPONENT_MASK_RGBA,
};
VkPipelineColorBlendStateCreateInfo color_blend_state = {
VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO};
color_blend_state.attachmentCount = 1;
color_blend_state.pAttachments = &attachment_state;
VkPipelineViewportStateCreateInfo viewport_state = {
VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO};
viewport_state.viewportCount = 1;
viewport_state.scissorCount = 1;
VkPipelineMultisampleStateCreateInfo multisample_state = {
VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO};
multisample_state.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkDynamicState dynamic_states[2] = {VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR};
VkPipelineDynamicStateCreateInfo dynamic_state = {
VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
dynamic_state.pDynamicStates = dynamic_states;
dynamic_state.dynamicStateCount = 2;
VkPipelineShaderStageCreateInfo shader_stages[2] = {
create_shader(adapter, nuklearshaders_nuklear_vert_spv,
nuklearshaders_nuklear_vert_spv_len,
VK_SHADER_STAGE_VERTEX_BIT),
create_shader(adapter, nuklearshaders_nuklear_frag_spv,
nuklearshaders_nuklear_frag_spv_len,
VK_SHADER_STAGE_FRAGMENT_BIT)};
VkVertexInputBindingDescription vertex_input_info[1] = {
{
0,
sizeof(struct nk_glfw_vertex),
VK_VERTEX_INPUT_RATE_VERTEX,
},
};
VkVertexInputAttributeDescription vertex_attribute_description[3] = {
{
0,
0,
VK_FORMAT_R32G32_SFLOAT,
NK_OFFSETOF(struct nk_glfw_vertex, position),
},
{
1,
0,
VK_FORMAT_R32G32_SFLOAT,
NK_OFFSETOF(struct nk_glfw_vertex, uv),
},
{
2,
0,
VK_FORMAT_R8G8B8A8_UINT,
NK_OFFSETOF(struct nk_glfw_vertex, col),
},
};
VkPipelineVertexInputStateCreateInfo vertex_input = {
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO};
vertex_input.vertexBindingDescriptionCount = 1;
vertex_input.pVertexBindingDescriptions = vertex_input_info;
vertex_input.vertexAttributeDescriptionCount = 3;
vertex_input.pVertexAttributeDescriptions = vertex_attribute_description;
VkGraphicsPipelineCreateInfo pipeline_info = {
VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO};
pipeline_info.flags = 0;
pipeline_info.stageCount = 2;
pipeline_info.pStages = shader_stages;
pipeline_info.pVertexInputState = &vertex_input;
pipeline_info.pInputAssemblyState = &input_assembly_state;
pipeline_info.pViewportState = &viewport_state;
pipeline_info.pRasterizationState = &rasterization_state;
pipeline_info.pMultisampleState = &multisample_state;
pipeline_info.pColorBlendState = &color_blend_state;
pipeline_info.pDynamicState = &dynamic_state;
pipeline_info.layout = adapter->pipeline_layout;
pipeline_info.renderPass = adapter->render_pass;
pipeline_info.basePipelineIndex = -1;
pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
VkResult result = vkCreateGraphicsPipelines(
adapter->logical_device, VK_NULL_HANDLE, 1, &pipeline_info,
VK_NULL_HANDLE, &adapter->pipeline);
NK_ASSERT(result == VK_SUCCESS);
vkDestroyShaderModule(adapter->logical_device, shader_stages[0].module,
VK_NULL_HANDLE);
vkDestroyShaderModule(adapter->logical_device, shader_stages[1].module,
VK_NULL_HANDLE);
}
void prepare_render_pass(struct nk_vulkan_adapter *adapter) {
VkAttachmentDescription attachments[1] = {
{0, adapter->color_format, VK_SAMPLE_COUNT_1_BIT,
VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_STORE,
VK_ATTACHMENT_LOAD_OP_DONT_CARE, VK_ATTACHMENT_STORE_OP_DONT_CARE,
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL},
};
VkAttachmentReference color_reference = {
0,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
};
VkSubpassDependency subpass_dependencies[1] = {
{
VK_SUBPASS_EXTERNAL,
0,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
0,
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
},
};
VkSubpassDescription subpass_description = {
0, VK_PIPELINE_BIND_POINT_GRAPHICS,
0, NULL,
1, &color_reference,
NULL, VK_NULL_HANDLE,
0, NULL,
};
VkRenderPassCreateInfo render_pass_info = {
VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO};
render_pass_info.attachmentCount = 1;
render_pass_info.pAttachments = attachments;
render_pass_info.subpassCount = 1;
render_pass_info.pSubpasses = &subpass_description;
render_pass_info.dependencyCount = 1;
render_pass_info.pDependencies = subpass_dependencies;
VkResult result = vkCreateRenderPass(
adapter->logical_device, &render_pass_info, NULL, &adapter->render_pass);
NK_ASSERT(result == VK_SUCCESS);
}
void prepare_framebuffers(struct nk_vulkan_adapter *adapter) {
adapter->framebuffers =
(VkFramebuffer *)malloc(adapter->image_views_len * sizeof(VkFramebuffer));
size_t i;
for (i = 0; i < adapter->image_views_len; i++) {
VkFramebufferCreateInfo framebufferCreateInfo = {
VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO};
framebufferCreateInfo.renderPass = adapter->render_pass;
framebufferCreateInfo.attachmentCount = 1;
framebufferCreateInfo.pAttachments = &adapter->image_views[i];
framebufferCreateInfo.width = adapter->width;
framebufferCreateInfo.height = adapter->height;
framebufferCreateInfo.layers = 1;
VkResult result =
(vkCreateFramebuffer(adapter->logical_device, &framebufferCreateInfo,
NULL, &adapter->framebuffers[i]));
NK_ASSERT(result == VK_SUCCESS);
}
adapter->framebuffers_len = adapter->image_views_len;
}
void prepare_command_buffers(struct nk_vulkan_adapter *adapter) {
VkCommandBufferAllocateInfo allocate_info = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO};
allocate_info.commandPool = adapter->command_pool;
allocate_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocate_info.commandBufferCount = 1;
VkResult result = vkAllocateCommandBuffers(
adapter->logical_device, &allocate_info, adapter->command_buffers);
NK_ASSERT(result == VK_SUCCESS);
adapter->command_buffers_len = 1;
}
void prepare_semaphores(struct nk_vulkan_adapter *adapter) {
VkSemaphoreCreateInfo semaphore_info = {
VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
VkResult result =
(vkCreateSemaphore(adapter->logical_device, &semaphore_info,
VK_NULL_HANDLE, &adapter->render_completed));
NK_ASSERT(result == VK_SUCCESS);
}
uint32_t find_memory_index(VkPhysicalDevice physical_device,
uint32_t typeFilter,
VkMemoryPropertyFlags properties) {
VkPhysicalDeviceMemoryProperties mem_properties;
vkGetPhysicalDeviceMemoryProperties(physical_device, &mem_properties);
uint32_t i;
for (i = 0; i < mem_properties.memoryTypeCount; i++) {
if ((typeFilter & (1 << i)) &&
(mem_properties.memoryTypes[i].propertyFlags & properties) ==
properties) {
return i;
}
}
assert(0);
return 0;
}
void create_buffer_and_memory(struct nk_vulkan_adapter *adapter,
VkBuffer *buffer, VkBufferUsageFlags usage,
VkDeviceMemory *memory, VkDeviceSize size) {
VkBufferCreateInfo buffer_info = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
buffer_info.size = size;
buffer_info.usage = usage;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VkResult result;
result = vkCreateBuffer(adapter->logical_device, &buffer_info, VK_NULL_HANDLE,
buffer);
NK_ASSERT(result == VK_SUCCESS);
VkMemoryRequirements mem_reqs;
vkGetBufferMemoryRequirements(adapter->logical_device, *buffer, &mem_reqs);
VkMemoryAllocateInfo alloc_info = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
alloc_info.allocationSize = mem_reqs.size;
alloc_info.memoryTypeIndex =
find_memory_index(adapter->physical_device, mem_reqs.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
result = vkAllocateMemory(adapter->logical_device, &alloc_info,
VK_NULL_HANDLE, memory);
NK_ASSERT(result == VK_SUCCESS);
result = vkBindBufferMemory(adapter->logical_device, *buffer, *memory, 0);
NK_ASSERT(result == VK_SUCCESS);
}
void create_render_resources(struct nk_vulkan_adapter *adapter) {
prepare_render_pass(adapter);
prepare_framebuffers(adapter);
create_descriptor_pool(adapter);
create_uniform_descriptor_set_layout(adapter);
create_and_update_uniform_descriptor_set(adapter);
create_texture_descriptor_set_layout(adapter);
create_texture_descriptor_sets(adapter);
create_pipeline_layout(adapter);
prepare_pipeline(adapter);
}
void cleanup_render_resources(struct nk_vulkan_adapter *adapter) {
vkDestroyPipeline(adapter->logical_device, adapter->pipeline, VK_NULL_HANDLE);
vkDestroyPipelineLayout(adapter->logical_device, adapter->pipeline_layout,
VK_NULL_HANDLE);
vkDestroyDescriptorSetLayout(adapter->logical_device,
adapter->texture_descriptor_set_layout,
VK_NULL_HANDLE);
vkDestroyDescriptorSetLayout(adapter->logical_device,
adapter->uniform_descriptor_set_layout,
VK_NULL_HANDLE);
vkDestroyDescriptorPool(adapter->logical_device, adapter->descriptor_pool,
VK_NULL_HANDLE);
size_t i;
for (i = 0; i < adapter->framebuffers_len; i++) {
vkDestroyFramebuffer(adapter->logical_device, adapter->framebuffers[i],
VK_NULL_HANDLE);
}
free(adapter->framebuffers);
adapter->framebuffers_len = 0;
vkDestroyRenderPass(adapter->logical_device, adapter->render_pass,
VK_NULL_HANDLE);
}
void create_command_pool(struct nk_vulkan_adapter *adapter,
uint32_t graphics_queue_family_index) {
VkCommandPoolCreateInfo pool_info = {
VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO};
pool_info.queueFamilyIndex = graphics_queue_family_index;
pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
VkResult result =
(vkCreateCommandPool(adapter->logical_device, &pool_info, VK_NULL_HANDLE,
&adapter->command_pool));
NK_ASSERT(result == VK_SUCCESS);
}
NK_API void nk_glfw3_device_create(VkDevice logical_device,
VkPhysicalDevice physical_device,
uint32_t graphics_queue_family_index,
VkImageView *image_views,
uint32_t image_views_len, uint32_t width,
uint32_t height, VkFormat color_format) {
struct nk_vulkan_adapter *adapter = &glfw.adapter;
nk_buffer_init_default(&adapter->cmds);
adapter->logical_device = logical_device;
adapter->physical_device = physical_device;
adapter->image_views = image_views;
adapter->image_views_len = image_views_len;
adapter->width = width;
adapter->height = height;
adapter->color_format = color_format;
adapter->framebuffers = NULL;
adapter->framebuffers_len = 0;
adapter->command_buffers =
(VkCommandBuffer *)malloc(image_views_len * sizeof(VkCommandBuffer));
prepare_semaphores(adapter);
create_buffer_and_memory(adapter, &adapter->vertex_buffer,
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
&adapter->vertex_memory, MAX_VERTEX_BUFFER);
create_buffer_and_memory(adapter, &adapter->index_buffer,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
&adapter->index_memory, MAX_INDEX_BUFFER);
create_buffer_and_memory(adapter, &adapter->uniform_buffer,
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
&adapter->uniform_memory, sizeof(struct Mat4f));
create_render_resources(adapter);
create_command_pool(adapter, graphics_queue_family_index);
prepare_command_buffers(adapter);
}
NK_API void nk_glfw3_device_destroy(void) {
struct nk_vulkan_adapter *adapter = &glfw.adapter;
free(adapter->command_buffers);
nk_buffer_free(&adapter->cmds);
}
NK_API void nk_glfw3_char_callback(GLFWwindow *win, unsigned int codepoint) {
(void)win;
if (glfw.text_len < NK_GLFW_TEXT_MAX)
glfw.text[glfw.text_len++] = codepoint;
}
NK_API void nk_gflw3_scroll_callback(GLFWwindow *win, double xoff,
double yoff) {
(void)win;
(void)xoff;
glfw.scroll.x += (float)xoff;
glfw.scroll.y += (float)yoff;
}
NK_API void nk_glfw3_mouse_button_callback(GLFWwindow *window, int button,
int action, int mods) {
double x, y;
if (button != GLFW_MOUSE_BUTTON_LEFT)
return;
glfwGetCursorPos(window, &x, &y);
if (action == GLFW_PRESS) {
double dt = glfwGetTime() - glfw.last_button_click;
if (dt > NK_GLFW_DOUBLE_CLICK_LO && dt < NK_GLFW_DOUBLE_CLICK_HI) {
glfw.is_double_click_down = nk_true;
glfw.double_click_pos = nk_vec2((float)x, (float)y);
}
glfw.last_button_click = glfwGetTime();
} else
glfw.is_double_click_down = nk_false;
}
NK_INTERN void nk_glfw3_clipbard_paste(nk_handle usr,
struct nk_text_edit *edit) {
const char *text = glfwGetClipboardString(glfw.win);
if (text)
nk_textedit_paste(edit, text, nk_strlen(text));
(void)usr;
}
NK_INTERN void nk_glfw3_clipbard_copy(nk_handle usr, const char *text,
int len) {
char *str = 0;
(void)usr;
if (!len)
return;
str = (char *)malloc((size_t)len + 1);
if (!str)
return;
memcpy(str, text, (size_t)len);
str[len] = '\0';
glfwSetClipboardString(glfw.win, str);
free(str);
}
NK_API struct nk_context *
nk_glfw3_init(GLFWwindow *win, VkDevice logical_device,
VkPhysicalDevice physical_device,
uint32_t graphics_queue_family_index, VkImageView *image_views,
uint32_t image_views_len, uint32_t width, uint32_t height,
VkFormat color_format, enum nk_glfw_init_state init_state) {
glfw.win = win;
if (init_state == NK_GLFW3_INSTALL_CALLBACKS) {
glfwSetScrollCallback(win, nk_gflw3_scroll_callback);
glfwSetCharCallback(win, nk_glfw3_char_callback);
glfwSetMouseButtonCallback(win, nk_glfw3_mouse_button_callback);
}
nk_init_default(&glfw.ctx, 0);
glfw.ctx.clip.copy = nk_glfw3_clipbard_copy;
glfw.ctx.clip.paste = nk_glfw3_clipbard_paste;
glfw.ctx.clip.userdata = nk_handle_ptr(0);
glfw.last_button_click = 0;
nk_glfw3_device_create(logical_device, physical_device,
graphics_queue_family_index, image_views,
image_views_len, width, height, color_format);
glfw.is_double_click_down = nk_false;
glfw.double_click_pos = nk_vec2(0, 0);
return &glfw.ctx;
}
NK_API void nk_glfw3_resize(uint32_t width, uint32_t height) {
struct nk_vulkan_adapter *adapter = &glfw.adapter;
adapter->width = width;
adapter->height = height;
cleanup_render_resources(adapter);
create_render_resources(adapter);
}
NK_INTERN void nk_glfw3_device_upload_atlas(VkQueue graphics_queue,
const void *image, int width,
int height) {
struct nk_vulkan_adapter *adapter = &glfw.adapter;
VkImageCreateInfo image_info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
image_info.imageType = VK_IMAGE_TYPE_2D;
image_info.format = VK_FORMAT_R8G8B8A8_UNORM;
image_info.extent = (VkExtent3D){(uint32_t)width, (uint32_t)height, 1};
image_info.mipLevels = 1;
image_info.arrayLayers = 1;
image_info.samples = VK_SAMPLE_COUNT_1_BIT;
image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_info.usage =
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
VkResult result = vkCreateImage(adapter->logical_device, &image_info,
VK_NULL_HANDLE, &adapter->font_image);
assert(result == VK_SUCCESS);
VkMemoryRequirements mem_reqs;
vkGetImageMemoryRequirements(adapter->logical_device, adapter->font_image,
&mem_reqs);
VkMemoryAllocateInfo alloc_info = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
alloc_info.allocationSize = mem_reqs.size;
alloc_info.memoryTypeIndex =
find_memory_index(adapter->physical_device, mem_reqs.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
result = vkAllocateMemory(adapter->logical_device, &alloc_info,
VK_NULL_HANDLE, &adapter->font_memory);
NK_ASSERT(result == VK_SUCCESS);
result = vkBindImageMemory(adapter->logical_device, adapter->font_image,
adapter->font_memory, 0);
NK_ASSERT(result == VK_SUCCESS);
struct {
VkDeviceMemory memory;
VkBuffer buffer;
} staging_buffer;
VkBufferCreateInfo buffer_info = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
buffer_info.size = alloc_info.allocationSize;
buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
result = vkCreateBuffer(adapter->logical_device, &buffer_info, VK_NULL_HANDLE,
&staging_buffer.buffer);
NK_ASSERT(result == VK_SUCCESS);
vkGetBufferMemoryRequirements(adapter->logical_device, staging_buffer.buffer,
&mem_reqs);
alloc_info.allocationSize = mem_reqs.size;
alloc_info.memoryTypeIndex =
find_memory_index(adapter->physical_device, mem_reqs.memoryTypeBits,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
result = vkAllocateMemory(adapter->logical_device, &alloc_info,
VK_NULL_HANDLE, &staging_buffer.memory);
NK_ASSERT(result == VK_SUCCESS);
result = vkBindBufferMemory(adapter->logical_device, staging_buffer.buffer,
staging_buffer.memory, 0);
NK_ASSERT(result == VK_SUCCESS);
uint8_t *data = 0;
result = vkMapMemory(adapter->logical_device, staging_buffer.memory, 0,
alloc_info.allocationSize, 0, (void **)&data);
NK_ASSERT(result == VK_SUCCESS);
memcpy(data, image, width * height * 4);
vkUnmapMemory(adapter->logical_device, staging_buffer.memory);
// use the same command buffer as for render as we are regenerating the buffer
// during render anyway
VkCommandBufferBeginInfo begin_info = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
// TODO: kill array
VkCommandBuffer command_buffer = adapter->command_buffers[0];
result = vkBeginCommandBuffer(command_buffer, &begin_info);
NK_ASSERT(result == VK_SUCCESS);
VkImageMemoryBarrier image_memory_barrier = {
VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
image_memory_barrier.image = adapter->font_image;
image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
image_memory_barrier.subresourceRange =
(VkImageSubresourceRange){
VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1,
},
image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, VK_NULL_HANDLE, 0,
VK_NULL_HANDLE, 1, &image_memory_barrier);
VkBufferImageCopy buffer_copy_region = {
0,
0,
0,
{
VK_IMAGE_ASPECT_COLOR_BIT,
0,
0,
1,
},
{0, 0, 0},
{
(uint32_t)width,
(uint32_t)height,
1,
},
};
vkCmdCopyBufferToImage(
command_buffer, staging_buffer.buffer, adapter->font_image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &buffer_copy_region);
VkImageMemoryBarrier image_shader_memory_barrier = {
VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
image_shader_memory_barrier.image = adapter->font_image;
image_shader_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
image_shader_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
image_shader_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
image_shader_memory_barrier.newLayout =
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
image_shader_memory_barrier.subresourceRange =
(VkImageSubresourceRange){
VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1,
},
image_shader_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
image_shader_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0,
VK_NULL_HANDLE, 0, VK_NULL_HANDLE, 1,
&image_shader_memory_barrier);
result = vkEndCommandBuffer(command_buffer);
NK_ASSERT(result == VK_SUCCESS);
VkSubmitInfo submitInfo = {VK_STRUCTURE_TYPE_SUBMIT_INFO};
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &command_buffer;
result = vkQueueSubmit(graphics_queue, 1, &submitInfo, VK_NULL_HANDLE);
NK_ASSERT(result == VK_SUCCESS);
result = vkQueueWaitIdle(graphics_queue);
NK_ASSERT(result == VK_SUCCESS);
vkFreeMemory(adapter->logical_device, staging_buffer.memory, VK_NULL_HANDLE);
vkDestroyBuffer(adapter->logical_device, staging_buffer.buffer,
VK_NULL_HANDLE);
VkImageViewCreateInfo image_view_info = {
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
image_view_info.image = adapter->font_image;
image_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
image_view_info.format = image_info.format;
image_view_info.subresourceRange =
(VkImageSubresourceRange){VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
result = vkCreateImageView(adapter->logical_device, &image_view_info,
VK_NULL_HANDLE, &adapter->font_image_view);
NK_ASSERT(result == VK_SUCCESS);
VkSamplerCreateInfo sampler_info = {VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
sampler_info.maxAnisotropy = 1.0;
sampler_info.magFilter = VK_FILTER_LINEAR;
sampler_info.minFilter = VK_FILTER_LINEAR;
sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
sampler_info.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
sampler_info.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
sampler_info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
sampler_info.mipLodBias = 0.0f;
sampler_info.compareEnable = VK_FALSE;
sampler_info.compareOp = VK_COMPARE_OP_ALWAYS;
sampler_info.minLod = 0.0f;
sampler_info.maxLod = 0.0f;
sampler_info.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK;
result = vkCreateSampler(adapter->logical_device, &sampler_info,
VK_NULL_HANDLE, &adapter->sampler);
NK_ASSERT(result == VK_SUCCESS);
// write_font_descriptor_set(adapter);
}
NK_API void nk_glfw3_font_stash_begin(struct nk_font_atlas **atlas) {
nk_font_atlas_init_default(&glfw.atlas);
nk_font_atlas_begin(&glfw.atlas);
*atlas = &glfw.atlas;
}
NK_API void nk_glfw3_font_stash_end(VkQueue graphics_queue) {
struct nk_vulkan_adapter *dev = &glfw.adapter;
const void *image;
int w, h;
image = nk_font_atlas_bake(&glfw.atlas, &w, &h, NK_FONT_ATLAS_RGBA32);
nk_glfw3_device_upload_atlas(graphics_queue, image, w, h);
nk_font_atlas_end(&glfw.atlas, nk_handle_ptr(dev->font_image_view),
&glfw.adapter.null);
if (glfw.atlas.default_font) {
nk_style_set_font(&glfw.ctx, &glfw.atlas.default_font->handle);
}
}
NK_API void nk_glfw3_new_frame() {
int i;
double x, y;
struct nk_context *ctx = &glfw.ctx;
struct GLFWwindow *win = glfw.win;