-
Notifications
You must be signed in to change notification settings - Fork 149
/
spirv_reflect.c
5482 lines (4877 loc) · 214 KB
/
spirv_reflect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2017-2022 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "spirv_reflect.h"
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#if defined(WIN32)
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>
#else
#include <stdlib.h>
#endif
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 7) || defined(__APPLE_CC__)
#define FALLTHROUGH __attribute__((fallthrough))
#else
#define FALLTHROUGH
#endif
#if defined(SPIRV_REFLECT_ENABLE_ASSERTS)
#define SPV_REFLECT_ASSERT(COND) assert(COND);
#else
#define SPV_REFLECT_ASSERT(COND)
#endif
// clang-format off
enum {
SPIRV_STARTING_WORD_INDEX = 5,
SPIRV_WORD_SIZE = sizeof(uint32_t),
SPIRV_BYTE_WIDTH = 8,
SPIRV_MINIMUM_FILE_SIZE = SPIRV_STARTING_WORD_INDEX * SPIRV_WORD_SIZE,
SPIRV_DATA_ALIGNMENT = 4 * SPIRV_WORD_SIZE, // 16
SPIRV_ACCESS_CHAIN_INDEX_OFFSET = 4,
};
enum {
INVALID_VALUE = 0xFFFFFFFF,
};
enum {
MAX_NODE_NAME_LENGTH = 1024,
// Number of unique PhysicalStorageBuffer structs tracked to detect recursion
MAX_RECURSIVE_PHYSICAL_POINTER_CHECK = 128,
};
enum {
IMAGE_SAMPLED = 1,
IMAGE_STORAGE = 2,
};
typedef struct SpvReflectPrvArrayTraits {
uint32_t element_type_id;
uint32_t length_id;
} SpvReflectPrvArrayTraits;
typedef struct SpvReflectPrvImageTraits {
uint32_t sampled_type_id;
SpvDim dim;
uint32_t depth;
uint32_t arrayed;
uint32_t ms;
uint32_t sampled;
SpvImageFormat image_format;
} SpvReflectPrvImageTraits;
typedef struct SpvReflectPrvNumberDecoration {
uint32_t word_offset;
uint32_t value;
} SpvReflectPrvNumberDecoration;
typedef struct SpvReflectPrvStringDecoration {
uint32_t word_offset;
const char* value;
} SpvReflectPrvStringDecoration;
typedef struct SpvReflectPrvDecorations {
bool is_relaxed_precision;
bool is_block;
bool is_buffer_block;
bool is_row_major;
bool is_column_major;
bool is_built_in;
bool is_noperspective;
bool is_flat;
bool is_non_writable;
bool is_non_readable;
bool is_patch;
bool is_per_vertex;
bool is_per_task;
bool is_weight_texture;
bool is_block_match_texture;
SpvReflectUserType user_type;
SpvReflectPrvNumberDecoration set;
SpvReflectPrvNumberDecoration binding;
SpvReflectPrvNumberDecoration input_attachment_index;
SpvReflectPrvNumberDecoration location;
SpvReflectPrvNumberDecoration component;
SpvReflectPrvNumberDecoration offset;
SpvReflectPrvNumberDecoration uav_counter_buffer;
SpvReflectPrvStringDecoration semantic;
uint32_t array_stride;
uint32_t matrix_stride;
uint32_t spec_id;
SpvBuiltIn built_in;
} SpvReflectPrvDecorations;
typedef struct SpvReflectPrvNode {
uint32_t result_id;
SpvOp op;
uint32_t result_type_id;
uint32_t type_id;
SpvCapability capability;
SpvStorageClass storage_class;
uint32_t word_offset;
uint32_t word_count;
bool is_type;
SpvReflectPrvArrayTraits array_traits;
SpvReflectPrvImageTraits image_traits;
uint32_t image_type_id;
const char* name;
SpvReflectPrvDecorations decorations;
uint32_t member_count;
const char** member_names;
SpvReflectPrvDecorations* member_decorations;
} SpvReflectPrvNode;
typedef struct SpvReflectPrvString {
uint32_t result_id;
const char* string;
} SpvReflectPrvString;
// There are a limit set of instructions that can touch an OpVariable,
// these are represented here with how it was accessed
// Examples:
// OpImageRead -> OpLoad -> OpVariable
// OpImageWrite -> OpLoad -> OpVariable
// OpStore -> OpAccessChain -> OpAccessChain -> OpVariable
// OpAtomicIAdd -> OpAccessChain -> OpVariable
// OpAtomicLoad -> OpImageTexelPointer -> OpVariable
typedef struct SpvReflectPrvAccessedVariable {
SpvReflectPrvNode* p_node;
uint32_t result_id;
uint32_t variable_ptr;
uint32_t function_id;
uint32_t function_parameter_index;
} SpvReflectPrvAccessedVariable;
typedef struct SpvReflectPrvFunction {
uint32_t id;
uint32_t parameter_count;
uint32_t* parameters;
uint32_t callee_count;
uint32_t* callees;
struct SpvReflectPrvFunction** callee_ptrs;
uint32_t accessed_variable_count;
SpvReflectPrvAccessedVariable* accessed_variables;
} SpvReflectPrvFunction;
typedef struct SpvReflectPrvAccessChain {
uint32_t result_id;
uint32_t result_type_id;
//
// Pointing to the base of a composite object.
// Generally the id of descriptor block variable
uint32_t base_id;
//
// From spec:
// The first index in Indexes will select the
// top-level member/element/component/element
// of the base composite
uint32_t index_count;
uint32_t* indexes;
//
// Block variable ac is pointing to (for block references)
SpvReflectBlockVariable* block_var;
} SpvReflectPrvAccessChain;
// To prevent infinite recursion, we never walk down a
// PhysicalStorageBuffer struct twice, but incase a 2nd variable
// needs to use that struct, save a copy
typedef struct SpvReflectPrvPhysicalPointerStruct {
uint32_t struct_id;
// first variable to see the PhysicalStorageBuffer struct
SpvReflectBlockVariable* p_var;
} SpvReflectPrvPhysicalPointerStruct;
typedef struct SpvReflectPrvParser {
size_t spirv_word_count;
uint32_t* spirv_code;
uint32_t string_count;
SpvReflectPrvString* strings;
SpvSourceLanguage source_language;
uint32_t source_language_version;
uint32_t source_file_id;
const char* source_embedded;
size_t node_count;
SpvReflectPrvNode* nodes;
uint32_t entry_point_count;
uint32_t capability_count;
uint32_t function_count;
SpvReflectPrvFunction* functions;
uint32_t access_chain_count;
SpvReflectPrvAccessChain* access_chains;
uint32_t type_count;
uint32_t descriptor_count;
uint32_t push_constant_count;
SpvReflectTypeDescription* physical_pointer_check[MAX_RECURSIVE_PHYSICAL_POINTER_CHECK];
uint32_t physical_pointer_count;
SpvReflectPrvPhysicalPointerStruct* physical_pointer_structs;
uint32_t physical_pointer_struct_count;
} SpvReflectPrvParser;
// clang-format on
static uint32_t Max(uint32_t a, uint32_t b) { return a > b ? a : b; }
static uint32_t Min(uint32_t a, uint32_t b) { return a < b ? a : b; }
static uint32_t RoundUp(uint32_t value, uint32_t multiple) {
assert(multiple && ((multiple & (multiple - 1)) == 0));
return (value + multiple - 1) & ~(multiple - 1);
}
#define IsNull(ptr) (ptr == NULL)
#define IsNotNull(ptr) (ptr != NULL)
#define SafeFree(ptr) \
{ \
free((void*)ptr); \
ptr = NULL; \
}
static int SortCompareUint32(const void* a, const void* b) {
const uint32_t* p_a = (const uint32_t*)a;
const uint32_t* p_b = (const uint32_t*)b;
return (int)*p_a - (int)*p_b;
}
static int SortCompareAccessedVariable(const void* a, const void* b) {
const SpvReflectPrvAccessedVariable* p_a = (const SpvReflectPrvAccessedVariable*)a;
const SpvReflectPrvAccessedVariable* p_b = (const SpvReflectPrvAccessedVariable*)b;
return (int)p_a->variable_ptr - (int)p_b->variable_ptr;
}
//
// De-duplicates a sorted array and returns the new size.
//
// Note: The array doesn't actually need to be sorted, just
// arranged into "runs" so that all the entries with one
// value are adjacent.
//
static size_t DedupSortedUint32(uint32_t* arr, size_t size) {
if (size == 0) {
return 0;
}
size_t dedup_idx = 0;
for (size_t i = 0; i < size; ++i) {
if (arr[dedup_idx] != arr[i]) {
++dedup_idx;
arr[dedup_idx] = arr[i];
}
}
return dedup_idx + 1;
}
static bool SearchSortedUint32(const uint32_t* arr, size_t size, uint32_t target) {
size_t lo = 0;
size_t hi = size;
while (lo < hi) {
size_t mid = (hi - lo) / 2 + lo;
if (arr[mid] == target) {
return true;
} else if (arr[mid] < target) {
lo = mid + 1;
} else {
hi = mid;
}
}
return false;
}
static SpvReflectResult IntersectSortedAccessedVariable(const SpvReflectPrvAccessedVariable* p_arr0, size_t arr0_size,
const uint32_t* p_arr1, size_t arr1_size, uint32_t** pp_res,
size_t* res_size) {
*pp_res = NULL;
*res_size = 0;
if (IsNull(p_arr0) || IsNull(p_arr1)) {
return SPV_REFLECT_RESULT_SUCCESS;
}
const SpvReflectPrvAccessedVariable* p_arr0_end = p_arr0 + arr0_size;
const uint32_t* p_arr1_end = p_arr1 + arr1_size;
const SpvReflectPrvAccessedVariable* p_idx0 = p_arr0;
const uint32_t* p_idx1 = p_arr1;
while (p_idx0 != p_arr0_end && p_idx1 != p_arr1_end) {
if (p_idx0->variable_ptr < *p_idx1) {
++p_idx0;
} else if (p_idx0->variable_ptr > *p_idx1) {
++p_idx1;
} else {
++*res_size;
++p_idx0;
++p_idx1;
}
}
if (*res_size > 0) {
*pp_res = (uint32_t*)calloc(*res_size, sizeof(**pp_res));
if (IsNull(*pp_res)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
uint32_t* p_idxr = *pp_res;
p_idx0 = p_arr0;
p_idx1 = p_arr1;
while (p_idx0 != p_arr0_end && p_idx1 != p_arr1_end) {
if (p_idx0->variable_ptr < *p_idx1) {
++p_idx0;
} else if (p_idx0->variable_ptr > *p_idx1) {
++p_idx1;
} else {
*(p_idxr++) = p_idx0->variable_ptr;
++p_idx0;
++p_idx1;
}
}
}
return SPV_REFLECT_RESULT_SUCCESS;
}
static bool InRange(const SpvReflectPrvParser* p_parser, uint32_t index) {
bool in_range = false;
if (IsNotNull(p_parser)) {
in_range = (index < p_parser->spirv_word_count);
}
return in_range;
}
static SpvReflectResult ReadU32(SpvReflectPrvParser* p_parser, uint32_t word_offset, uint32_t* p_value) {
assert(IsNotNull(p_parser));
assert(IsNotNull(p_parser->spirv_code));
assert(InRange(p_parser, word_offset));
SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF;
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && InRange(p_parser, word_offset)) {
*p_value = *(p_parser->spirv_code + word_offset);
result = SPV_REFLECT_RESULT_SUCCESS;
}
return result;
}
#define UNCHECKED_READU32(parser, word_offset, value) \
{ (void)ReadU32(parser, word_offset, (uint32_t*)&(value)); }
#define CHECKED_READU32(parser, word_offset, value) \
{ \
SpvReflectResult checked_readu32_result = ReadU32(parser, word_offset, (uint32_t*)&(value)); \
if (checked_readu32_result != SPV_REFLECT_RESULT_SUCCESS) { \
return checked_readu32_result; \
} \
}
#define CHECKED_READU32_CAST(parser, word_offset, cast_to_type, value) \
{ \
uint32_t checked_readu32_cast_u32 = UINT32_MAX; \
SpvReflectResult checked_readu32_cast_result = ReadU32(parser, word_offset, (uint32_t*)&(checked_readu32_cast_u32)); \
if (checked_readu32_cast_result != SPV_REFLECT_RESULT_SUCCESS) { \
return checked_readu32_cast_result; \
} \
value = (cast_to_type)checked_readu32_cast_u32; \
}
#define IF_READU32(result, parser, word_offset, value) \
if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \
result = ReadU32(parser, word_offset, (uint32_t*)&(value)); \
}
#define IF_READU32_CAST(result, parser, word_offset, cast_to_type, value) \
if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \
uint32_t if_readu32_cast_u32 = UINT32_MAX; \
result = ReadU32(parser, word_offset, &if_readu32_cast_u32); \
if ((result) == SPV_REFLECT_RESULT_SUCCESS) { \
value = (cast_to_type)if_readu32_cast_u32; \
} \
}
static SpvReflectResult ReadStr(SpvReflectPrvParser* p_parser, uint32_t word_offset, uint32_t word_index, uint32_t word_count,
uint32_t* p_buf_size, char* p_buf) {
uint32_t limit = (word_offset + word_count);
assert(IsNotNull(p_parser));
assert(IsNotNull(p_parser->spirv_code));
assert(InRange(p_parser, limit));
SpvReflectResult result = SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF;
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && InRange(p_parser, limit)) {
const char* c_str = (const char*)(p_parser->spirv_code + word_offset + word_index);
uint32_t n = word_count * SPIRV_WORD_SIZE;
uint32_t length_with_terminator = 0;
for (uint32_t i = 0; i < n; ++i) {
char c = *(c_str + i);
if (c == 0) {
length_with_terminator = i + 1;
break;
}
}
if (length_with_terminator > 0) {
result = SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
if (IsNotNull(p_buf_size) && IsNotNull(p_buf)) {
result = SPV_REFLECT_RESULT_ERROR_RANGE_EXCEEDED;
if (length_with_terminator <= *p_buf_size) {
memset(p_buf, 0, *p_buf_size);
memcpy(p_buf, c_str, length_with_terminator);
result = SPV_REFLECT_RESULT_SUCCESS;
}
} else {
if (IsNotNull(p_buf_size)) {
*p_buf_size = length_with_terminator;
result = SPV_REFLECT_RESULT_SUCCESS;
}
}
}
}
return result;
}
static SpvReflectDecorationFlags ApplyDecorations(const SpvReflectPrvDecorations* p_decoration_fields) {
SpvReflectDecorationFlags decorations = SPV_REFLECT_DECORATION_NONE;
if (p_decoration_fields->is_relaxed_precision) {
decorations |= SPV_REFLECT_DECORATION_RELAXED_PRECISION;
}
if (p_decoration_fields->is_block) {
decorations |= SPV_REFLECT_DECORATION_BLOCK;
}
if (p_decoration_fields->is_buffer_block) {
decorations |= SPV_REFLECT_DECORATION_BUFFER_BLOCK;
}
if (p_decoration_fields->is_row_major) {
decorations |= SPV_REFLECT_DECORATION_ROW_MAJOR;
}
if (p_decoration_fields->is_column_major) {
decorations |= SPV_REFLECT_DECORATION_COLUMN_MAJOR;
}
if (p_decoration_fields->is_built_in) {
decorations |= SPV_REFLECT_DECORATION_BUILT_IN;
}
if (p_decoration_fields->is_noperspective) {
decorations |= SPV_REFLECT_DECORATION_NOPERSPECTIVE;
}
if (p_decoration_fields->is_flat) {
decorations |= SPV_REFLECT_DECORATION_FLAT;
}
if (p_decoration_fields->is_non_writable) {
decorations |= SPV_REFLECT_DECORATION_NON_WRITABLE;
}
if (p_decoration_fields->is_non_readable) {
decorations |= SPV_REFLECT_DECORATION_NON_READABLE;
}
if (p_decoration_fields->is_patch) {
decorations |= SPV_REFLECT_DECORATION_PATCH;
}
if (p_decoration_fields->is_per_vertex) {
decorations |= SPV_REFLECT_DECORATION_PER_VERTEX;
}
if (p_decoration_fields->is_per_task) {
decorations |= SPV_REFLECT_DECORATION_PER_TASK;
}
if (p_decoration_fields->is_weight_texture) {
decorations |= SPV_REFLECT_DECORATION_WEIGHT_TEXTURE;
}
if (p_decoration_fields->is_block_match_texture) {
decorations |= SPV_REFLECT_DECORATION_BLOCK_MATCH_TEXTURE;
}
return decorations;
}
static void ApplyNumericTraits(const SpvReflectTypeDescription* p_type, SpvReflectNumericTraits* p_numeric_traits) {
memcpy(p_numeric_traits, &p_type->traits.numeric, sizeof(p_type->traits.numeric));
}
static void ApplyArrayTraits(const SpvReflectTypeDescription* p_type, SpvReflectArrayTraits* p_array_traits) {
memcpy(p_array_traits, &p_type->traits.array, sizeof(p_type->traits.array));
}
static bool IsSpecConstant(const SpvReflectPrvNode* p_node) {
return (p_node->op == SpvOpSpecConstant || p_node->op == SpvOpSpecConstantOp || p_node->op == SpvOpSpecConstantTrue ||
p_node->op == SpvOpSpecConstantFalse);
}
static SpvReflectPrvNode* FindNode(SpvReflectPrvParser* p_parser, uint32_t result_id) {
SpvReflectPrvNode* p_node = NULL;
for (size_t i = 0; i < p_parser->node_count; ++i) {
SpvReflectPrvNode* p_elem = &(p_parser->nodes[i]);
if (p_elem->result_id == result_id) {
p_node = p_elem;
break;
}
}
return p_node;
}
static SpvReflectTypeDescription* FindType(SpvReflectShaderModule* p_module, uint32_t type_id) {
SpvReflectTypeDescription* p_type = NULL;
for (size_t i = 0; i < p_module->_internal->type_description_count; ++i) {
SpvReflectTypeDescription* p_elem = &(p_module->_internal->type_descriptions[i]);
if (p_elem->id == type_id) {
p_type = p_elem;
break;
}
}
return p_type;
}
static SpvReflectPrvAccessChain* FindAccessChain(SpvReflectPrvParser* p_parser, uint32_t id) {
const uint32_t ac_count = p_parser->access_chain_count;
for (uint32_t i = 0; i < ac_count; i++) {
if (p_parser->access_chains[i].result_id == id) {
return &p_parser->access_chains[i];
}
}
return 0;
}
// Access Chains mostly have their Base ID pointed directly to a OpVariable, but sometimes
// it will be through a load and this funciton handles the edge cases how to find that
static uint32_t FindAccessChainBaseVariable(SpvReflectPrvParser* p_parser, SpvReflectPrvAccessChain* p_access_chain) {
uint32_t base_id = p_access_chain->base_id;
SpvReflectPrvNode* base_node = FindNode(p_parser, base_id);
// TODO - This is just a band-aid to fix crashes.
// Need to understand why here and hopefully remove
// https://github.com/KhronosGroup/SPIRV-Reflect/pull/206
if (IsNull(base_node)) {
return 0;
}
while (base_node->op != SpvOpVariable) {
switch (base_node->op) {
case SpvOpLoad: {
UNCHECKED_READU32(p_parser, base_node->word_offset + 3, base_id);
} break;
case SpvOpFunctionParameter: {
UNCHECKED_READU32(p_parser, base_node->word_offset + 2, base_id);
} break;
case SpvOpBitcast:
// This can be caused by something like GL_EXT_buffer_reference_uvec2 trying to load a pointer.
// We currently call from a push constant, so no way to have a reference loop back into the PC block
return 0;
default: {
assert(false);
} break;
}
SpvReflectPrvAccessChain* base_ac = FindAccessChain(p_parser, base_id);
if (base_ac == 0) {
return 0;
}
base_id = base_ac->base_id;
base_node = FindNode(p_parser, base_id);
if (IsNull(base_node)) {
return 0;
}
}
return base_id;
}
static SpvReflectBlockVariable* GetRefBlkVar(SpvReflectPrvParser* p_parser, SpvReflectPrvAccessChain* p_access_chain) {
uint32_t base_id = p_access_chain->base_id;
SpvReflectPrvNode* base_node = FindNode(p_parser, base_id);
assert(base_node->op == SpvOpLoad);
UNCHECKED_READU32(p_parser, base_node->word_offset + 3, base_id);
SpvReflectPrvAccessChain* base_ac = FindAccessChain(p_parser, base_id);
assert(base_ac != 0);
SpvReflectBlockVariable* base_var = base_ac->block_var;
assert(base_var != 0);
return base_var;
}
bool IsPointerToPointer(SpvReflectPrvParser* p_parser, uint32_t type_id) {
SpvReflectPrvNode* ptr_node = FindNode(p_parser, type_id);
if (IsNull(ptr_node) || (ptr_node->op != SpvOpTypePointer)) {
return false;
}
uint32_t pte_id = 0;
UNCHECKED_READU32(p_parser, ptr_node->word_offset + 3, pte_id);
SpvReflectPrvNode* pte_node = FindNode(p_parser, pte_id);
if (IsNull(pte_node)) {
return false;
}
return pte_node->op == SpvOpTypePointer;
}
static SpvReflectResult CreateParser(size_t size, void* p_code, SpvReflectPrvParser* p_parser) {
if (p_code == NULL) {
return SPV_REFLECT_RESULT_ERROR_NULL_POINTER;
}
if (size < SPIRV_MINIMUM_FILE_SIZE) {
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_CODE_SIZE;
}
if ((size % 4) != 0) {
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_CODE_SIZE;
}
p_parser->spirv_word_count = size / SPIRV_WORD_SIZE;
p_parser->spirv_code = (uint32_t*)p_code;
if (p_parser->spirv_code[0] != SpvMagicNumber) {
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_MAGIC_NUMBER;
}
return SPV_REFLECT_RESULT_SUCCESS;
}
static void DestroyParser(SpvReflectPrvParser* p_parser) {
if (!IsNull(p_parser->nodes)) {
// Free nodes
for (size_t i = 0; i < p_parser->node_count; ++i) {
SpvReflectPrvNode* p_node = &(p_parser->nodes[i]);
if (IsNotNull(p_node->member_names)) {
SafeFree(p_node->member_names);
}
if (IsNotNull(p_node->member_decorations)) {
SafeFree(p_node->member_decorations);
}
}
// Free functions
for (size_t i = 0; i < p_parser->function_count; ++i) {
SafeFree(p_parser->functions[i].parameters);
SafeFree(p_parser->functions[i].callees);
SafeFree(p_parser->functions[i].callee_ptrs);
SafeFree(p_parser->functions[i].accessed_variables);
}
// Free access chains
for (uint32_t i = 0; i < p_parser->access_chain_count; ++i) {
SafeFree(p_parser->access_chains[i].indexes);
}
SafeFree(p_parser->nodes);
SafeFree(p_parser->strings);
SafeFree(p_parser->source_embedded);
SafeFree(p_parser->functions);
SafeFree(p_parser->access_chains);
if (IsNotNull(p_parser->physical_pointer_structs)) {
SafeFree(p_parser->physical_pointer_structs);
}
p_parser->node_count = 0;
}
}
static SpvReflectResult ParseNodes(SpvReflectPrvParser* p_parser) {
assert(IsNotNull(p_parser));
assert(IsNotNull(p_parser->spirv_code));
uint32_t* p_spirv = p_parser->spirv_code;
uint32_t spirv_word_index = SPIRV_STARTING_WORD_INDEX;
// Count nodes
uint32_t node_count = 0;
while (spirv_word_index < p_parser->spirv_word_count) {
uint32_t word = p_spirv[spirv_word_index];
SpvOp op = (SpvOp)(word & 0xFFFF);
uint32_t node_word_count = (word >> 16) & 0xFFFF;
if (node_word_count == 0) {
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_INSTRUCTION;
}
if (op == SpvOpAccessChain) {
++(p_parser->access_chain_count);
}
spirv_word_index += node_word_count;
++node_count;
}
if (node_count == 0) {
return SPV_REFLECT_RESULT_ERROR_SPIRV_UNEXPECTED_EOF;
}
// Allocate nodes
p_parser->node_count = node_count;
p_parser->nodes = (SpvReflectPrvNode*)calloc(p_parser->node_count, sizeof(*(p_parser->nodes)));
if (IsNull(p_parser->nodes)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
// Mark all nodes with an invalid state
for (uint32_t i = 0; i < node_count; ++i) {
p_parser->nodes[i].op = (SpvOp)INVALID_VALUE;
p_parser->nodes[i].storage_class = (SpvStorageClass)INVALID_VALUE;
p_parser->nodes[i].decorations.set.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.binding.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.location.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.component.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.offset.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.uav_counter_buffer.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.spec_id = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.built_in = (SpvBuiltIn)INVALID_VALUE;
}
// Mark source file id node
p_parser->source_file_id = (uint32_t)INVALID_VALUE;
p_parser->source_embedded = NULL;
// Function node
uint32_t function_node = (uint32_t)INVALID_VALUE;
// Allocate access chain
if (p_parser->access_chain_count > 0) {
p_parser->access_chains = (SpvReflectPrvAccessChain*)calloc(p_parser->access_chain_count, sizeof(*(p_parser->access_chains)));
if (IsNull(p_parser->access_chains)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
}
// Parse nodes
uint32_t node_index = 0;
uint32_t access_chain_index = 0;
spirv_word_index = SPIRV_STARTING_WORD_INDEX;
while (spirv_word_index < p_parser->spirv_word_count) {
uint32_t word = p_spirv[spirv_word_index];
SpvOp op = (SpvOp)(word & 0xFFFF);
uint32_t node_word_count = (word >> 16) & 0xFFFF;
SpvReflectPrvNode* p_node = &(p_parser->nodes[node_index]);
p_node->op = op;
p_node->word_offset = spirv_word_index;
p_node->word_count = node_word_count;
switch (p_node->op) {
default:
break;
case SpvOpString: {
++(p_parser->string_count);
} break;
case SpvOpSource: {
CHECKED_READU32_CAST(p_parser, p_node->word_offset + 1, SpvSourceLanguage, p_parser->source_language);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_parser->source_language_version);
if (p_node->word_count >= 4) {
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_parser->source_file_id);
}
if (p_node->word_count >= 5) {
const char* p_source = (const char*)(p_parser->spirv_code + p_node->word_offset + 4);
const size_t source_len = strlen(p_source);
char* p_source_temp = (char*)calloc(source_len + 1, sizeof(char));
if (IsNull(p_source_temp)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
#ifdef _WIN32
strcpy_s(p_source_temp, source_len + 1, p_source);
#else
strcpy(p_source_temp, p_source);
#endif
SafeFree(p_parser->source_embedded);
p_parser->source_embedded = p_source_temp;
}
} break;
case SpvOpSourceContinued: {
const char* p_source = (const char*)(p_parser->spirv_code + p_node->word_offset + 1);
const size_t source_len = strlen(p_source);
const size_t embedded_source_len = strlen(p_parser->source_embedded);
char* p_continued_source = (char*)calloc(source_len + embedded_source_len + 1, sizeof(char));
if (IsNull(p_continued_source)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
#ifdef _WIN32
strcpy_s(p_continued_source, embedded_source_len + 1, p_parser->source_embedded);
strcat_s(p_continued_source, embedded_source_len + source_len + 1, p_source);
#else
strcpy(p_continued_source, p_parser->source_embedded);
strcat(p_continued_source, p_source);
#endif
SafeFree(p_parser->source_embedded);
p_parser->source_embedded = p_continued_source;
} break;
case SpvOpEntryPoint: {
++(p_parser->entry_point_count);
} break;
case SpvOpCapability: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->capability);
++(p_parser->capability_count);
} break;
case SpvOpName:
case SpvOpMemberName: {
uint32_t member_offset = (p_node->op == SpvOpMemberName) ? 1 : 0;
uint32_t name_start = p_node->word_offset + member_offset + 2;
p_node->name = (const char*)(p_parser->spirv_code + name_start);
} break;
case SpvOpTypeStruct: {
p_node->member_count = p_node->word_count - 2;
FALLTHROUGH;
} // Fall through
// This is all the rest of OpType* that need to be tracked
// Possible new extensions might expose new type, will need to be added
// here
case SpvOpTypeVoid:
case SpvOpTypeBool:
case SpvOpTypeInt:
case SpvOpTypeFloat:
case SpvOpTypeVector:
case SpvOpTypeMatrix:
case SpvOpTypeSampler:
case SpvOpTypeOpaque:
case SpvOpTypeFunction:
case SpvOpTypeEvent:
case SpvOpTypeDeviceEvent:
case SpvOpTypeReserveId:
case SpvOpTypeQueue:
case SpvOpTypePipe:
case SpvOpTypeAccelerationStructureKHR:
case SpvOpTypeRayQueryKHR:
case SpvOpTypeHitObjectNV:
case SpvOpTypeCooperativeMatrixNV:
case SpvOpTypeCooperativeMatrixKHR: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
p_node->is_type = true;
} break;
case SpvOpTypeImage: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->image_traits.sampled_type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->image_traits.dim);
CHECKED_READU32(p_parser, p_node->word_offset + 4, p_node->image_traits.depth);
CHECKED_READU32(p_parser, p_node->word_offset + 5, p_node->image_traits.arrayed);
CHECKED_READU32(p_parser, p_node->word_offset + 6, p_node->image_traits.ms);
CHECKED_READU32(p_parser, p_node->word_offset + 7, p_node->image_traits.sampled);
CHECKED_READU32(p_parser, p_node->word_offset + 8, p_node->image_traits.image_format);
p_node->is_type = true;
} break;
case SpvOpTypeSampledImage: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->image_type_id);
p_node->is_type = true;
} break;
case SpvOpTypeArray: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->array_traits.element_type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->array_traits.length_id);
p_node->is_type = true;
} break;
case SpvOpTypeRuntimeArray: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->array_traits.element_type_id);
p_node->is_type = true;
} break;
case SpvOpTypePointer: {
uint32_t result_id;
CHECKED_READU32(p_parser, p_node->word_offset + 1, result_id);
// Look for forward pointer. Clear result id if found
SpvReflectPrvNode* p_fwd_node = FindNode(p_parser, result_id);
if (p_fwd_node) {
p_fwd_node->result_id = 0;
}
// Register pointer type
p_node->result_id = result_id;
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->storage_class);
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->type_id);
p_node->is_type = true;
} break;
case SpvOpTypeForwardPointer: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->storage_class);
p_node->is_type = true;
} break;
case SpvOpConstantTrue:
case SpvOpConstantFalse:
case SpvOpConstant:
case SpvOpConstantComposite:
case SpvOpConstantSampler:
case SpvOpConstantNull: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
} break;
case SpvOpSpecConstantTrue:
case SpvOpSpecConstantFalse:
case SpvOpSpecConstant:
case SpvOpSpecConstantComposite:
case SpvOpSpecConstantOp: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
} break;
case SpvOpVariable: {
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_node->storage_class);
} break;
case SpvOpLoad: {
// Only load enough so OpDecorate can reference the node, skip the remaining operands.
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_node->result_type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
} break;
case SpvOpAccessChain: {
SpvReflectPrvAccessChain* p_access_chain = &(p_parser->access_chains[access_chain_index]);
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_access_chain->result_type_id);
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_access_chain->result_id);
CHECKED_READU32(p_parser, p_node->word_offset + 3, p_access_chain->base_id);
//
// SPIRV_ACCESS_CHAIN_INDEX_OFFSET (4) is the number of words up until the first index:
// [Node, Result Type Id, Result Id, Base Id, <Indexes>]
//
p_access_chain->index_count = (node_word_count - SPIRV_ACCESS_CHAIN_INDEX_OFFSET);
if (p_access_chain->index_count > 0) {
p_access_chain->indexes = (uint32_t*)calloc(p_access_chain->index_count, sizeof(*(p_access_chain->indexes)));
if (IsNull(p_access_chain->indexes)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
// Parse any index values for access chain
for (uint32_t index_index = 0; index_index < p_access_chain->index_count; ++index_index) {
// Read index id
uint32_t index_id = 0;
CHECKED_READU32(p_parser, p_node->word_offset + SPIRV_ACCESS_CHAIN_INDEX_OFFSET + index_index, index_id);
// Find OpConstant node that contains index value
SpvReflectPrvNode* p_index_value_node = FindNode(p_parser, index_id);
if ((p_index_value_node != NULL) &&
(p_index_value_node->op == SpvOpConstant || p_index_value_node->op == SpvOpSpecConstant)) {
// Read index value
uint32_t index_value = UINT32_MAX;
CHECKED_READU32(p_parser, p_index_value_node->word_offset + 3, index_value);
assert(index_value != UINT32_MAX);
// Write index value to array
p_access_chain->indexes[index_index] = index_value;
}
}
}
++access_chain_index;
} break;
case SpvOpFunction: {
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
// Count function definitions, not function declarations. To determine
// the difference, set an in-function variable, and then if an OpLabel
// is reached before the end of the function increment the function
// count.
function_node = node_index;
} break;
case SpvOpLabel: {
if (function_node != (uint32_t)INVALID_VALUE) {
SpvReflectPrvNode* p_func_node = &(p_parser->nodes[function_node]);
CHECKED_READU32(p_parser, p_func_node->word_offset + 2, p_func_node->result_id);
++(p_parser->function_count);
}
FALLTHROUGH;
} // Fall through
case SpvOpFunctionEnd: {
function_node = (uint32_t)INVALID_VALUE;
} break;
case SpvOpFunctionParameter: {
CHECKED_READU32(p_parser, p_node->word_offset + 2, p_node->result_id);
} break;
case SpvOpBitcast:
case SpvOpShiftRightLogical:
case SpvOpIAdd:
case SpvOpISub:
case SpvOpIMul: