-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathvulkan_resources.h
1848 lines (1589 loc) · 86.5 KB
/
vulkan_resources.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 HALIDE_RUNTIME_VULKAN_RESOURCES_H
#define HALIDE_RUNTIME_VULKAN_RESOURCES_H
#include "vulkan_internal.h"
#include "vulkan_memory.h"
// --------------------------------------------------------------------------
namespace Halide {
namespace Runtime {
namespace Internal {
namespace Vulkan {
// Defines the specialization constants used for dynamically overiding the dispatch size
struct VulkanWorkgroupSizeBinding {
uint32_t constant_id[3] = {0}; // zero if unused
};
// Data used to override specialization constants for dynamic dispatching
struct VulkanDispatchData {
uint32_t global_size[3] = {0}; // aka blocks
uint32_t local_size[3] = {0}; // aka threads
uint32_t shared_mem_bytes = 0;
VulkanWorkgroupSizeBinding local_size_binding = {};
};
// Specialization constant binding information
struct VulkanSpecializationConstant {
uint32_t constant_id = 0;
uint32_t type_size = 0;
const char *constant_name = nullptr;
};
// Shared memory allocation variable information
struct VulkanSharedMemoryAllocation {
uint32_t constant_id = 0; // specialization constant to override allocation array size (or zero if unused)
uint32_t type_size = 0;
uint32_t array_size = 0;
const char *variable_name = nullptr;
};
// Entry point metadata for shader modules
struct VulkanShaderBinding {
char *entry_point_name = nullptr;
VulkanDispatchData dispatch_data = {};
VkDescriptorPool descriptor_pool = VK_NULL_HANDLE;
VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
VkPipeline compute_pipeline = VK_NULL_HANDLE;
uint32_t uniform_buffer_count = 0;
uint32_t storage_buffer_count = 0;
uint32_t specialization_constants_count = 0;
uint32_t shared_memory_allocations_count = 0;
VulkanSpecializationConstant *specialization_constants = nullptr;
VulkanSharedMemoryAllocation *shared_memory_allocations = nullptr;
uint32_t bindings_count = 0;
MemoryRegion *args_region = nullptr;
};
// Compiled shader module and associated bindings
struct VulkanCompiledShaderModule {
VkShaderModule shader_module = VK_NULL_HANDLE;
VkDescriptorSetLayout *descriptor_set_layouts = nullptr;
VkPipelineLayout pipeline_layout = VK_NULL_HANDLE;
uint32_t shader_count = 0;
VulkanShaderBinding *shader_bindings = nullptr;
};
// Compilation cache for compiled shader modules
struct VulkanCompilationCacheEntry {
VulkanMemoryAllocator *allocator = nullptr;
VulkanCompiledShaderModule **compiled_modules = nullptr;
uint32_t module_count = 0;
};
WEAK Halide::Internal::GPUCompilationCache<VkDevice, VulkanCompilationCacheEntry *> compilation_cache;
// --------------------------------------------------------------------------
namespace { // internalize
// --------------------------------------------------------------------------
int vk_create_command_pool(void *user_context, VulkanMemoryAllocator *allocator, uint32_t queue_index, VkCommandPool *command_pool) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_command_pool (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "queue_index: " << queue_index << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create command pool ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
VkCommandPoolCreateInfo command_pool_info =
{
VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // struct type
nullptr, // pointer to struct extending this
VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, // flags. Assume transient short-lived single-use command buffers
queue_index // queue family index corresponding to the compute command queue
};
VkResult result = vkCreateCommandPool(allocator->current_device(), &command_pool_info, allocator->callbacks(), command_pool);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: Failed to create command pool!\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_destroy_command_pool(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_destroy_command_pool (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "command_pool: " << (void *)command_pool << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to destroy command pool ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
vkResetCommandPool(allocator->current_device(), command_pool, VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT);
vkDestroyCommandPool(allocator->current_device(), command_pool, allocator->callbacks());
return halide_error_code_success;
}
// --
int vk_create_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool, VkCommandBuffer *command_buffer) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_command_buffer (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "command_pool: " << (void *)command_pool << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create command buffer ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
VkCommandBufferAllocateInfo command_buffer_info =
{
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // struct type
nullptr, // pointer to struct extending this
command_pool, // command pool for allocation
VK_COMMAND_BUFFER_LEVEL_PRIMARY, // command buffer level
1 // number to allocate
};
VkResult result = vkAllocateCommandBuffers(allocator->current_device(), &command_buffer_info, command_buffer);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: Failed to allocate command buffers!\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_destroy_command_buffer(void *user_context, VulkanMemoryAllocator *allocator, VkCommandPool command_pool, VkCommandBuffer command_buffer) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_destroy_command_buffer (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "command_pool: " << (void *)command_pool << ", "
<< "command_buffer: " << (void *)command_buffer << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to destroy command buffer ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
vkFreeCommandBuffers(allocator->current_device(), command_pool, 1, &command_buffer);
return halide_error_code_success;
}
// Struct for handling destruction of a transient command buffer ... gets destroyed when object goes out of scope
struct ScopedVulkanCommandBufferAndPool {
void *user_context = nullptr;
VulkanMemoryAllocator *allocator = nullptr;
VkCommandPool command_pool = VK_NULL_HANDLE;
VkCommandBuffer command_buffer = VK_NULL_HANDLE;
int error_code = halide_error_code_success;
ScopedVulkanCommandBufferAndPool(void *uc, VulkanMemoryAllocator *vma, uint32_t queue_family_index)
: user_context(uc), allocator(vma) {
error_code = vk_create_command_pool(user_context, allocator, queue_family_index, &command_pool);
if (error_code == halide_error_code_success) {
error_code = vk_create_command_buffer(user_context, allocator, command_pool, &command_buffer);
}
}
~ScopedVulkanCommandBufferAndPool() {
if ((allocator != nullptr) && (command_pool != VK_NULL_HANDLE)) {
if (command_buffer != VK_NULL_HANDLE) {
vk_destroy_command_buffer(user_context, allocator, command_pool, command_buffer);
}
vk_destroy_command_pool(user_context, allocator, command_pool);
}
user_context = nullptr;
allocator = nullptr;
command_pool = VK_NULL_HANDLE;
command_buffer = VK_NULL_HANDLE;
}
};
int vk_fill_command_buffer_with_dispatch_call(void *user_context,
VkDevice device,
VkCommandBuffer command_buffer,
VkPipeline compute_pipeline,
VkPipelineLayout pipeline_layout,
VkDescriptorSet descriptor_set,
uint32_t descriptor_set_index,
int blocksX, int blocksY, int blocksZ) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_fill_command_buffer_with_dispatch_call (user_context: " << user_context << ", "
<< "device: " << (void *)device << ", "
<< "command_buffer: " << (void *)command_buffer << ", "
<< "pipeline_layout: " << (void *)pipeline_layout << ", "
<< "descriptor_set: " << (void *)descriptor_set << ", "
<< "descriptor_set_index: " << descriptor_set_index << ", "
<< "blocks: " << blocksX << ", " << blocksY << ", " << blocksZ << ")\n";
#endif
VkCommandBufferBeginInfo command_buffer_begin_info = {
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, // struct type
nullptr, // pointer to struct extending this
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT, // flags
nullptr // pointer to parent command buffer
};
VkResult result = vkBeginCommandBuffer(command_buffer, &command_buffer_begin_info);
if (result != VK_SUCCESS) {
error(user_context) << "vkBeginCommandBuffer returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, compute_pipeline);
vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout,
descriptor_set_index, 1, &descriptor_set, 0, nullptr);
vkCmdDispatch(command_buffer, blocksX, blocksY, blocksZ);
result = vkEndCommandBuffer(command_buffer);
if (result != VK_SUCCESS) {
error(user_context) << "vkEndCommandBuffer returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_submit_command_buffer(void *user_context, VkQueue queue, VkCommandBuffer command_buffer) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_submit_command_buffer (user_context: " << user_context << ", "
<< "queue: " << (void *)queue << ", "
<< "command_buffer: " << (void *)command_buffer << ")\n";
#endif
VkSubmitInfo submit_info =
{
VK_STRUCTURE_TYPE_SUBMIT_INFO, // struct type
nullptr, // pointer to struct extending this
0, // wait semaphore count
nullptr, // semaphores
nullptr, // pipeline stages where semaphore waits occur
1, // how many command buffers to execute
&command_buffer, // the command buffers
0, // number of semaphores to signal
nullptr // the semaphores to signal
};
VkResult result = vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkQueueSubmit returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
// --
bool vk_needs_scalar_uniform_buffer(void *user_context,
size_t arg_sizes[],
void *args[],
int8_t arg_is_buffer[]) {
int i = 0;
while (arg_sizes[i] > 0) {
if (!arg_is_buffer[i]) {
return true;
}
i++;
}
return false;
}
uint32_t vk_count_bindings_for_descriptor_set(void *user_context,
size_t arg_sizes[],
void *args[],
int8_t arg_is_buffer[]) {
// first binding is for passing scalar parameters in a buffer (if necessary)
uint32_t bindings_count = vk_needs_scalar_uniform_buffer(user_context, arg_sizes, args, arg_is_buffer);
int i = 0;
while (arg_sizes[i] > 0) {
if (arg_is_buffer[i]) {
bindings_count++;
}
i++;
}
return bindings_count;
}
// --
int vk_create_descriptor_pool(void *user_context,
VulkanMemoryAllocator *allocator,
uint32_t uniform_buffer_count,
uint32_t storage_buffer_count,
VkDescriptorPool *descriptor_pool) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_descriptor_pool (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "uniform_buffer_count: " << (uint32_t)uniform_buffer_count << ", "
<< "storage_buffer_count: " << (uint32_t)storage_buffer_count << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create descriptor pool ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
BlockStorage::Config pool_config;
pool_config.entry_size = sizeof(VkDescriptorPoolSize);
pool_config.minimum_capacity = (uniform_buffer_count ? 1 : 0) + (storage_buffer_count ? 1 : 0);
BlockStorage pool_sizes(user_context, pool_config);
// First binding is reserved for passing scalar parameters as a uniform buffer
if (uniform_buffer_count > 0) {
VkDescriptorPoolSize uniform_buffer_size = {
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // descriptor type
uniform_buffer_count // all kernel args are packed into uniform buffers
};
pool_sizes.append(user_context, &uniform_buffer_size);
}
if (storage_buffer_count > 0) {
VkDescriptorPoolSize storage_buffer_size = {
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // descriptor type
storage_buffer_count // all halide buffers are passed as storage buffers
};
pool_sizes.append(user_context, &storage_buffer_size);
}
VkDescriptorPoolCreateInfo descriptor_pool_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, // struct type
nullptr, // point to struct extending this
0, // flags
1, // this pool will only be used for creating one descriptor set!
(uint32_t)pool_sizes.size(), // pool size count
(const VkDescriptorPoolSize *)pool_sizes.data() // ptr to descriptr pool sizes
};
VkResult result = vkCreateDescriptorPool(allocator->current_device(), &descriptor_pool_info, allocator->callbacks(), descriptor_pool);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: Failed to create descriptor pool! vkCreateDescriptorPool returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_destroy_descriptor_pool(void *user_context,
VulkanMemoryAllocator *allocator,
VkDescriptorPool descriptor_pool) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_destroy_descriptor_pool (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "descriptor_pool: " << (void *)descriptor_pool << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to destroy descriptor pool ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
vkDestroyDescriptorPool(allocator->current_device(), descriptor_pool, allocator->callbacks());
return halide_error_code_success;
}
// --
int vk_create_descriptor_set_layout(void *user_context,
VulkanMemoryAllocator *allocator,
uint32_t uniform_buffer_count,
uint32_t storage_buffer_count,
VkDescriptorSetLayout *layout) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_descriptor_set_layout (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "uniform_buffer_count: " << uniform_buffer_count << ", "
<< "storage_buffer_count: " << storage_buffer_count << ", "
<< "layout: " << (void *)layout << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create descriptor set layout ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
BlockStorage::Config layout_config;
layout_config.entry_size = sizeof(VkDescriptorSetLayoutBinding);
layout_config.minimum_capacity = uniform_buffer_count + storage_buffer_count;
BlockStorage layout_bindings(user_context, layout_config);
// add all uniform buffers first
for (uint32_t n = 0; n < uniform_buffer_count; ++n) {
VkDescriptorSetLayoutBinding uniform_buffer_layout = {
(uint32_t)layout_bindings.size(), // binding index
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // descriptor type
1, // descriptor count
VK_SHADER_STAGE_COMPUTE_BIT, // stage flags
nullptr // immutable samplers
};
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " [" << (uint32_t)layout_bindings.size() << "] : UNIFORM_BUFFER\n";
#endif
layout_bindings.append(user_context, &uniform_buffer_layout);
}
// Add all other storage buffers
for (uint32_t n = 0; n < storage_buffer_count; ++n) {
// halide buffers will be passed as STORAGE_BUFFERS
VkDescriptorSetLayoutBinding storage_buffer_layout = {
(uint32_t)layout_bindings.size(), // binding index
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // descriptor type
1, // descriptor count
VK_SHADER_STAGE_COMPUTE_BIT, // stage flags
nullptr // immutable samplers
};
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " [" << (uint32_t)layout_bindings.size() << "] : STORAGE_BUFFER\n";
#endif
layout_bindings.append(user_context, &storage_buffer_layout);
}
// Create the LayoutInfo struct
VkDescriptorSetLayoutCreateInfo layout_info = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, // structure type
nullptr, // pointer to a struct extending this info
0, // flags
(uint32_t)layout_bindings.size(), // binding count
(VkDescriptorSetLayoutBinding *)layout_bindings.data() // pointer to layout bindings array
};
// Create the descriptor set layout
VkResult result = vkCreateDescriptorSetLayout(allocator->current_device(), &layout_info, allocator->callbacks(), layout);
if (result != VK_SUCCESS) {
error(user_context) << "vkCreateDescriptorSetLayout returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_destroy_descriptor_set_layout(void *user_context,
VulkanMemoryAllocator *allocator,
VkDescriptorSetLayout descriptor_set_layout) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_destroy_descriptor_set_layout (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "layout: " << (void *)descriptor_set_layout << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to destroy descriptor set layout ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
vkDestroyDescriptorSetLayout(allocator->current_device(), descriptor_set_layout, allocator->callbacks());
return halide_error_code_success;
}
// --
int vk_create_descriptor_set(void *user_context,
VulkanMemoryAllocator *allocator,
VkDescriptorSetLayout descriptor_set_layout,
VkDescriptorPool descriptor_pool,
VkDescriptorSet *descriptor_set) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_descriptor_set (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "descriptor_set_layout: " << (void *)descriptor_set_layout << ", "
<< "descriptor_pool: " << (void *)descriptor_pool << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create descriptor set ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
VkDescriptorSetAllocateInfo descriptor_set_info =
{
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, // struct type
nullptr, // pointer to struct extending this
descriptor_pool, // pool from which to allocate sets
1, // number of descriptor sets
&descriptor_set_layout // pointer to array of descriptor set layouts
};
VkResult result = vkAllocateDescriptorSets(allocator->current_device(), &descriptor_set_info, descriptor_set);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkAllocateDescriptorSets returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_update_descriptor_set(void *user_context,
VulkanMemoryAllocator *allocator,
VkBuffer *scalar_args_buffer,
size_t uniform_buffer_count,
size_t storage_buffer_count,
size_t arg_sizes[],
void *args[],
int8_t arg_is_buffer[],
VkDescriptorSet descriptor_set) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_update_descriptor_set (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "scalar_args_buffer: " << (void *)scalar_args_buffer << ", "
<< "uniform_buffer_count: " << (uint32_t)uniform_buffer_count << ", "
<< "storage_buffer_count: " << (uint32_t)storage_buffer_count << ", "
<< "descriptor_set: " << (void *)descriptor_set << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create descriptor set ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
BlockStorage::Config dbi_config;
dbi_config.minimum_capacity = storage_buffer_count + uniform_buffer_count;
dbi_config.entry_size = sizeof(VkDescriptorBufferInfo);
BlockStorage descriptor_buffer_info(user_context, dbi_config);
BlockStorage::Config wds_config;
wds_config.minimum_capacity = storage_buffer_count + uniform_buffer_count;
wds_config.entry_size = sizeof(VkWriteDescriptorSet);
BlockStorage write_descriptor_set(user_context, wds_config);
// First binding will be the scalar args buffer (if needed) passed as a UNIFORM BUFFER
VkDescriptorBufferInfo *scalar_args_entry = nullptr;
if (scalar_args_buffer != nullptr) {
VkDescriptorBufferInfo scalar_args_descriptor_buffer_info = {
*scalar_args_buffer, // the buffer
0, // offset
VK_WHOLE_SIZE // range
};
descriptor_buffer_info.append(user_context, &scalar_args_descriptor_buffer_info);
scalar_args_entry = (VkDescriptorBufferInfo *)descriptor_buffer_info.back();
#ifdef DEBUG_RUNTIME
debug(user_context) << " [" << (uint32_t)write_descriptor_set.size() << "] UNIFORM_BUFFER : "
<< "buffer=" << (void *)scalar_args_buffer << " "
<< "offset=" << (uint32_t)(0) << " "
<< "size=VK_WHOLE_SIZE\n";
#endif
VkWriteDescriptorSet uniform_buffer_write_descriptor_set = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, // struct type
nullptr, // pointer to struct extending this
descriptor_set, // descriptor set to update
0, // binding slot
0, // array elem
1, // num to update
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, // descriptor type
nullptr, // for images
scalar_args_entry, // info for buffer
nullptr // for texel buffers
};
write_descriptor_set.append(user_context, &uniform_buffer_write_descriptor_set);
}
// Add all the other device buffers as STORAGE BUFFERs
for (size_t i = 0; arg_sizes[i] > 0; i++) {
if (arg_is_buffer[i]) {
// get the allocated region for the buffer
MemoryRegion *device_region = reinterpret_cast<MemoryRegion *>(((halide_buffer_t *)args[i])->device);
MemoryRegion *owner = allocator->owner_of(user_context, device_region);
// retrieve the buffer from the region
VkBuffer *device_buffer = reinterpret_cast<VkBuffer *>(owner->handle);
if (device_buffer == nullptr) {
error(user_context) << "Vulkan: Failed to retrieve buffer for device memory!\n";
return halide_error_code_internal_error;
}
VkDeviceSize range_offset = device_region->range.head_offset;
VkDeviceSize range_size = device_region->size - device_region->range.head_offset - device_region->range.tail_offset;
halide_abort_if_false(user_context, (device_region->size - device_region->range.head_offset - device_region->range.tail_offset) > 0);
VkDescriptorBufferInfo device_buffer_info = {
*device_buffer, // the buffer
range_offset, // range offset
range_size // range size
};
descriptor_buffer_info.append(user_context, &device_buffer_info);
VkDescriptorBufferInfo *device_buffer_entry = (VkDescriptorBufferInfo *)descriptor_buffer_info.back();
#ifdef DEBUG_RUNTIME
debug(user_context) << " [" << (uint32_t)write_descriptor_set.size() << "] STORAGE_BUFFER : "
<< "region=" << (void *)device_region << " "
<< "buffer=" << (void *)device_buffer << " "
<< "offset=" << (uint32_t)(range_offset) << " "
<< "size=" << (uint32_t)(range_size) << "\n";
#endif
VkWriteDescriptorSet storage_buffer_write_descriptor_set = {
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, // struct type
nullptr, // pointer to struct extending this
descriptor_set, // descriptor set to update
(uint32_t)write_descriptor_set.size(), // binding slot
0, // array elem
1, // num to update
VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, // descriptor type
nullptr, // for images
device_buffer_entry, // info for buffer
nullptr // for texel buffers
};
write_descriptor_set.append(user_context, &storage_buffer_write_descriptor_set);
}
}
// issue the update call to populate the descriptor set
vkUpdateDescriptorSets(allocator->current_device(), (uint32_t)write_descriptor_set.size(), (const VkWriteDescriptorSet *)write_descriptor_set.data(), 0, nullptr);
return halide_error_code_success;
}
// --
size_t vk_estimate_scalar_uniform_buffer_size(void *user_context,
size_t arg_sizes[],
void *args[],
int8_t arg_is_buffer[]) {
int i = 0;
int scalar_uniform_buffer_size = 0;
while (arg_sizes[i] > 0) {
if (!arg_is_buffer[i]) {
scalar_uniform_buffer_size += arg_sizes[i];
}
i++;
}
return scalar_uniform_buffer_size;
}
MemoryRegion *vk_create_scalar_uniform_buffer(void *user_context,
VulkanMemoryAllocator *allocator,
size_t scalar_buffer_size) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_scalar_uniform_buffer (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "scalar_buffer_size: " << (uint32_t)scalar_buffer_size << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create scalar uniform buffer ... invalid allocator pointer!\n";
return nullptr;
}
MemoryRequest request = {0};
request.size = scalar_buffer_size;
request.properties.usage = MemoryUsage::UniformStorage;
request.properties.caching = MemoryCaching::UncachedCoherent;
request.properties.visibility = MemoryVisibility::HostToDevice;
// allocate a new region
MemoryRegion *region = allocator->reserve(user_context, request);
if ((region == nullptr) || (region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to create scalar uniform buffer ... unable to allocate device memory!\n";
return nullptr;
}
// return the allocated region for the uniform buffer
return region;
}
int vk_update_scalar_uniform_buffer(void *user_context,
VulkanMemoryAllocator *allocator,
MemoryRegion *region,
size_t arg_sizes[],
void *args[],
int8_t arg_is_buffer[]) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_update_scalar_uniform_buffer (user_context: " << user_context << ", "
<< "region: " << (void *)region << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to update scalar uniform buffer ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
if ((region == nullptr) || (region->handle == nullptr)) {
error(user_context) << "Vulkan: Failed to update scalar uniform buffer ... invalid memory region!\n";
return halide_error_code_internal_error;
}
// map the region to a host ptr
uint8_t *host_ptr = (uint8_t *)allocator->map(user_context, region);
if (host_ptr == nullptr) {
error(user_context) << "Vulkan: Failed to update scalar uniform buffer ... unable to map host pointer to device memory!\n";
return halide_error_code_internal_error;
}
// copy to the (host-visible/coherent) scalar uniform buffer
size_t arg_offset = 0;
for (size_t i = 0; arg_sizes[i] > 0; i++) {
if (!arg_is_buffer[i]) {
memcpy(host_ptr + arg_offset, args[i], arg_sizes[i]);
arg_offset += arg_sizes[i];
}
}
// unmap the pointer to the buffer for the region
allocator->unmap(user_context, region);
return halide_error_code_success;
}
int vk_destroy_scalar_uniform_buffer(void *user_context, VulkanMemoryAllocator *allocator,
MemoryRegion *scalar_args_region) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_destroy_scalar_uniform_buffer (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "scalar_args_region: " << (void *)scalar_args_region << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to destroy scalar uniform buffer ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
if (!scalar_args_region) {
return halide_error_code_success;
}
int error_code = halide_error_code_success;
if (halide_can_reuse_device_allocations(user_context)) {
error_code = allocator->release(user_context, scalar_args_region);
} else {
error_code = allocator->reclaim(user_context, scalar_args_region);
}
return error_code;
}
// --
int vk_create_pipeline_layout(void *user_context,
VulkanMemoryAllocator *allocator,
uint32_t descriptor_set_count,
VkDescriptorSetLayout *descriptor_set_layouts,
VkPipelineLayout *pipeline_layout) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_pipeline_layout (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "descriptor_set_count: " << descriptor_set_count << ", "
<< "descriptor_set_layouts: " << (void *)descriptor_set_layouts << ", "
<< "pipeline_layout: " << (void *)pipeline_layout << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create pipeline layout ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
if (allocator->current_physical_device_limits().maxBoundDescriptorSets > 0) {
uint64_t max_bound_descriptor_sets = allocator->current_physical_device_limits().maxBoundDescriptorSets;
if (descriptor_set_count > max_bound_descriptor_sets) {
error(user_context) << "Vulkan: Number of descriptor sets for pipeline layout exceeds the number that can be bound by device!\n"
<< " requested: " << descriptor_set_count << ","
<< " available: " << max_bound_descriptor_sets << "\n";
return halide_error_code_incompatible_device_interface;
}
}
VkPipelineLayoutCreateInfo pipeline_layout_info = {
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, // structure type
nullptr, // pointer to a structure extending this
0, // flags
descriptor_set_count, // number of descriptor sets
descriptor_set_layouts, // pointer to the descriptor sets
0, // number of push constant ranges
nullptr // pointer to push constant range structs
};
VkResult result = vkCreatePipelineLayout(allocator->current_device(), &pipeline_layout_info, allocator->callbacks(), pipeline_layout);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: vkCreatePipelineLayout returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_destroy_pipeline_layout(void *user_context,
VulkanMemoryAllocator *allocator,
VkPipelineLayout pipeline_layout) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_destroy_pipeline_layout (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "pipeline_layout: " << (void *)pipeline_layout << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to destroy pipeline layout ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
vkDestroyPipelineLayout(allocator->current_device(), pipeline_layout, allocator->callbacks());
return halide_error_code_success;
}
// --
int vk_create_compute_pipeline(void *user_context,
VulkanMemoryAllocator *allocator,
const char *pipeline_name,
VkShaderModule shader_module,
VkPipelineLayout pipeline_layout,
VkSpecializationInfo *specialization_info,
VkPipeline *compute_pipeline) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_create_compute_pipeline (user_context: " << user_context << ", "
<< "allocator: " << (void *)allocator << ", "
<< "shader_module: " << (void *)shader_module << ", "
<< "pipeline_layout: " << (void *)pipeline_layout << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to create compute pipeline ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
VkComputePipelineCreateInfo compute_pipeline_info =
{
VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, // structure type
nullptr, // pointer to a structure extending this
0, // flags
// VkPipelineShaderStageCreatInfo
{
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // structure type
nullptr, // pointer to a structure extending this
0, // flags
VK_SHADER_STAGE_COMPUTE_BIT, // compute stage shader
shader_module, // shader module
pipeline_name, // entry point name
specialization_info, // pointer to VkSpecializationInfo struct
},
pipeline_layout, // pipeline layout
VK_NULL_HANDLE, // base pipeline handle for derived pipeline
0 // base pipeline index for derived pipeline
};
VkResult result = vkCreateComputePipelines(allocator->current_device(), VK_NULL_HANDLE, 1, &compute_pipeline_info, allocator->callbacks(), compute_pipeline);
if (result != VK_SUCCESS) {
error(user_context) << "Vulkan: Failed to create compute pipeline! vkCreateComputePipelines returned " << vk_get_error_name(result) << "\n";
return halide_error_code_generic_error;
}
return halide_error_code_success;
}
int vk_setup_compute_pipeline(void *user_context,
VulkanMemoryAllocator *allocator,
VulkanShaderBinding *shader_bindings,
VulkanDispatchData *dispatch_data,
VkShaderModule shader_module,
VkPipelineLayout pipeline_layout,
VkPipeline *compute_pipeline) {
#ifdef DEBUG_RUNTIME
debug(user_context)
<< " vk_setup_compute_pipeline (user_context: " << user_context << ", "
<< "entry_point_name: '" << shader_bindings->entry_point_name << "', "
<< "allocator: " << (void *)allocator << ", "
<< "shader_bindings: " << (void *)shader_bindings << ", "
<< "dispatch_data: " << (void *)dispatch_data << ", "
<< "shader_module: " << (void *)shader_module << ", "
<< "pipeline_layout: " << (void *)pipeline_layout << ")\n";
#endif
if (allocator == nullptr) {
error(user_context) << "Vulkan: Failed to setup compute pipeline ... invalid allocator pointer!\n";
return halide_error_code_generic_error;
}
if (shader_bindings == nullptr) {
error(user_context) << "Vulkan: Failed to setup compute pipeline ... invalid shader bindings!\n";
return halide_error_code_generic_error;
}
if (shader_bindings == nullptr) {
error(user_context) << "Vulkan: Failed to setup compute pipeline ... invalid dispatch data!\n";
return halide_error_code_generic_error;
}
VkResult result = VK_SUCCESS;
const char *entry_point_name = shader_bindings->entry_point_name;
if (entry_point_name == nullptr) {
error(user_context) << "Vulkan: Failed to setup compute pipeline ... missing entry point name!\n";
return halide_error_code_generic_error;
}
uint32_t dispatch_constant_index = 0;
uint32_t dispatch_constant_ids[4] = {0, 0, 0, 0};
uint32_t dispatch_constant_values[4] = {0, 0, 0, 0};
// locate the mapping for overriding any dynamic shared memory allocation sizes
if (shader_bindings->shared_memory_allocations_count && dispatch_data->shared_mem_bytes) {
uint32_t shared_mem_constant_id = 0;
uint32_t static_shared_mem_bytes = 0;
uint32_t shared_mem_type_size = 0;
for (uint32_t sm = 0; sm < shader_bindings->shared_memory_allocations_count; sm++) {
VulkanSharedMemoryAllocation *allocation = &(shader_bindings->shared_memory_allocations[sm]);
if (allocation->constant_id == 0) {
// static fixed-size allocation
static_shared_mem_bytes += allocation->type_size * allocation->array_size;
} else {
// dynamic allocation
if (shared_mem_constant_id > 0) {
error(user_context) << "Vulkan: Multiple dynamic shared memory allocations found! Only one is suported!!\n";
result = VK_ERROR_TOO_MANY_OBJECTS;
break;
}
shared_mem_constant_id = allocation->constant_id;
shared_mem_type_size = allocation->type_size;
}
}
uint32_t shared_mem_bytes_avail = (dispatch_data->shared_mem_bytes - static_shared_mem_bytes);
#ifdef DEBUG_RUNTIME
debug(user_context) << " pipeline uses " << static_shared_mem_bytes << " bytes of static shared memory\n";
debug(user_context) << " dispatch requests " << dispatch_data->shared_mem_bytes << " bytes of shared memory\n";
debug(user_context) << " dynamic shared memory " << shared_mem_bytes_avail << " bytes available\n";
#endif
// setup the dynamic array size
if ((shared_mem_constant_id > 0) && (shared_mem_bytes_avail > 0)) {
uint32_t dynamic_array_size = (uint32_t)shared_mem_bytes_avail / shared_mem_type_size;
#ifdef DEBUG_RUNTIME
debug(user_context) << " setting shared memory to " << (uint32_t)dynamic_array_size << " elements "
<< "(or " << (uint32_t)shared_mem_bytes_avail << " bytes)\n";
#endif
// save the shared mem specialization constant in the first slot
dispatch_constant_ids[dispatch_constant_index] = shared_mem_constant_id;
dispatch_constant_values[dispatch_constant_index] = dynamic_array_size;
dispatch_constant_index++;
}
// verify the device can actually support the necessary amount of shared memory requested
if (allocator->current_physical_device_limits().maxComputeSharedMemorySize > 0) {
uint64_t device_shared_mem_size = allocator->current_physical_device_limits().maxComputeSharedMemorySize;
if (static_shared_mem_bytes > device_shared_mem_size) {
error(user_context) << "Vulkan: Amount of static shared memory used exceeds device limit!\n"
<< " requested: " << static_shared_mem_bytes << " bytes,"
<< " available: " << device_shared_mem_size << " bytes\n";
return halide_error_code_incompatible_device_interface;
}
if (dispatch_data->shared_mem_bytes > device_shared_mem_size) {
error(user_context) << "Vulkan: Amount of dynamic shared memory used exceeds device limit!\n"
<< " requested: " << dispatch_data->shared_mem_bytes << " bytes,"
<< " available: " << device_shared_mem_size << " bytes\n";
return halide_error_code_incompatible_device_interface;
}
}
}
// locate the mapping for overriding any dynamic workgroup local sizes
if (shader_bindings->dispatch_data.local_size_binding.constant_id[0] != 0) {
for (uint32_t dim = 0; dim < 3; dim++) {
dispatch_constant_ids[dispatch_constant_index] = shader_bindings->dispatch_data.local_size_binding.constant_id[dim];
dispatch_constant_values[dispatch_constant_index] = dispatch_data->local_size[dim];
dispatch_constant_index++;
}
}