forked from nsubtil/SPIRV-Reflect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spirv_reflect.c
4117 lines (3673 loc) · 144 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-2018 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 <stdlib.h>
#include <crtdbg.h>
#else
#include <stdlib.h>
#endif
// Temporary enums until these make it into SPIR-V/Vulkan
enum {
SpvReflectOpDecorateId = 332,
SpvReflectOpDecorateStringGOOGLE = 5632,
SpvReflectOpMemberDecorateStringGOOGLE = 5633,
SpvReflectDecorationHlslCounterBufferGOOGLE = 5634,
SpvReflectDecorationHlslSemanticGOOGLE = 5635
};
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
};
enum {
INVALID_VALUE = 0xFFFFFFFF,
};
enum {
MAX_NODE_NAME_LENGTH = 1024,
};
enum {
IMAGE_SAMPLED = 1,
IMAGE_STORAGE = 2
};
typedef struct ArrayTraits {
uint32_t element_type_id;
uint32_t length_id;
} ArrayTraits;
typedef struct ImageTraits {
uint32_t sampled_type_id;
SpvDim dim;
uint32_t depth;
uint32_t arrayed;
uint32_t ms;
uint32_t sampled;
SpvImageFormat image_format;
} ImageTraits;
typedef struct NumberDecoration {
uint32_t word_offset;
uint32_t value;
} NumberDecoration;
typedef struct StringDecoration {
uint32_t word_offset;
const char* value;
} StringDecoration;
typedef struct Decorations {
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;
NumberDecoration set;
NumberDecoration binding;
NumberDecoration input_attachment_index;
NumberDecoration location;
NumberDecoration offset;
NumberDecoration uav_counter_buffer;
StringDecoration semantic;
uint32_t array_stride;
uint32_t matrix_stride;
SpvBuiltIn built_in;
} Decorations;
typedef struct Node {
uint32_t result_id;
SpvOp op;
uint32_t result_type_id;
uint32_t type_id;
SpvStorageClass storage_class;
uint32_t word_offset;
uint32_t word_count;
bool is_type;
ArrayTraits array_traits;
ImageTraits image_traits;
uint32_t image_type_id;
const char* name;
Decorations decorations;
uint32_t member_count;
const char** member_names;
Decorations* member_decorations;
} Node;
typedef struct String {
uint32_t result_id;
const char* string;
} String;
typedef struct Function {
uint32_t id;
uint32_t callee_count;
uint32_t* callees;
struct Function** callee_ptrs;
uint32_t accessed_ptr_count;
uint32_t* accessed_ptrs;
} Function;
typedef struct Parser {
size_t spirv_word_count;
uint32_t* spirv_code;
uint32_t string_count;
String* strings;
SpvSourceLanguage source_language;
uint32_t source_language_version;
uint32_t source_file_id;
String source_embedded;
size_t node_count;
Node* nodes;
uint32_t entry_point_count;
uint32_t function_count;
Function* functions;
uint32_t type_count;
uint32_t descriptor_count;
uint32_t push_constant_count;
} Parser;
//static uint32_t Min(uint32_t a, uint32_t b)
//{
// return a < b ? a : b;
//}
static uint32_t Max(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);
}
//static bool IsNull(const void* p)
//{
// bool is_null = (p == NULL);
// return is_null;
//}
#define IsNull(ptr) \
(ptr == NULL)
//static bool IsNotNull(const void* p)
//{
// bool is_not_null = (p != NULL);
// return is_not_null;
//}
#define IsNotNull(ptr) \
(ptr != NULL)
#define SafeFree(ptr) \
{ \
if (ptr != NULL) { \
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;
}
//
// 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 IntersectSortedUint32(const uint32_t* arr0, size_t arr0_size,
const uint32_t* arr1, size_t arr1_size,
uint32_t** res,
size_t* res_size) {
*res_size = 0;
const uint32_t* arr0_end = arr0 + arr0_size;
const uint32_t* arr1_end = arr1 + arr1_size;
const uint32_t* idx0 = arr0;
const uint32_t* idx1 = arr1;
while (idx0 != arr0_end && idx1 != arr1_end) {
if (*idx0 < *idx1) {
++idx0;
} else if (*idx0 > *idx1) {
++idx1;
} else {
++*res_size;
++idx0;
++idx1;
}
}
*res = NULL;
if (*res_size > 0) {
*res = (uint32_t*)calloc(*res_size, sizeof(**res));
if (IsNull(*res)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
uint32_t* idxr = *res;
idx0 = arr0;
idx1 = arr1;
while (idx0 != arr0_end && idx1 != arr1_end) {
if (*idx0 < *idx1) {
++idx0;
} else if (*idx0 > *idx1) {
++idx1;
} else {
*(idxr++) = *idx0;
++idx0;
++idx1;
}
}
}
return SPV_REFLECT_RESULT_SUCCESS;
}
static bool InRange(const Parser* 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(Parser* 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 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(Parser* 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 Decorations* p_decoration_fields)
{
SpvReflectDecorationFlags decorations = SPV_REFLECT_DECORATION_NONE;
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;
}
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 Node* FindNode(Parser* p_parser, uint32_t result_id)
{
Node* p_node = NULL;
for (size_t i = 0; i < p_parser->node_count; ++i) {
Node* 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 SpvReflectResult CreateParser(size_t size, void* p_code, Parser* 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(Parser* p_parser)
{
if (!IsNull(p_parser->nodes)) {
// Free nodes
for (size_t i = 0; i < p_parser->node_count; ++i) {
Node* 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].callees);
SafeFree(p_parser->functions[i].callee_ptrs);
SafeFree(p_parser->functions[i].accessed_ptrs);
}
SafeFree(p_parser->nodes);
SafeFree(p_parser->strings);
SafeFree(p_parser->functions);
p_parser->node_count = 0;
}
}
static SpvReflectResult ParseNodes(Parser* 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];
uint32_t node_word_count = (word >> 16) & 0xFFFF;
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 = (Node*)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.offset.value = (uint32_t)INVALID_VALUE;
p_parser->nodes[i].decorations.uav_counter_buffer.value = (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;
uint32_t function_node = (uint32_t)INVALID_VALUE;
// Parse nodes
uint32_t node_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;
Node* 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);
}
}
break;
case SpvOpEntryPoint: {
++(p_parser->entry_point_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;
} // Fall through
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:
{
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: {
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);
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 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 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) {
Node* 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);
}
} // Fall through
case SpvOpFunctionEnd:
{
function_node = (uint32_t)INVALID_VALUE;
}
break;
}
if (p_node->is_type) {
++(p_parser->type_count);
}
spirv_word_index += node_word_count;
++node_index;
}
return SPV_REFLECT_RESULT_SUCCESS;
}
static SpvReflectResult ParseStrings(Parser* p_parser)
{
assert(IsNotNull(p_parser));
assert(IsNotNull(p_parser->spirv_code));
assert(IsNotNull(p_parser->nodes));
// Early out
if (p_parser->string_count == 0) {
return SPV_REFLECT_RESULT_SUCCESS;
}
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
// Allocate string storage
p_parser->strings = (String*)calloc(p_parser->string_count, sizeof(*(p_parser->strings)));
uint32_t string_index = 0;
for (size_t i = 0; i < p_parser->node_count; ++i) {
Node* p_node = &(p_parser->nodes[i]);
if (p_node->op != SpvOpString) {
continue;
}
// Paranoid check against string count
assert(string_index < p_parser->string_count);
if (string_index >= p_parser->string_count) {
return SPV_REFLECT_RESULT_ERROR_COUNT_MISMATCH;
}
// Result id
String* p_string = &(p_parser->strings[string_index]);
CHECKED_READU32(p_parser, p_node->word_offset + 1, p_string->result_id);
// String
uint32_t string_start = p_node->word_offset + 2;
p_string->string = (const char*)(p_parser->spirv_code + string_start);
// Increment string index
++string_index;
}
}
return SPV_REFLECT_RESULT_SUCCESS;
}
static SpvReflectResult ParseSource(Parser* p_parser, SpvReflectShaderModule* p_module)
{
assert(IsNotNull(p_parser));
assert(IsNotNull(p_parser->spirv_code));
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code)) {
// Source file
if (IsNotNull(p_parser->strings)) {
for (uint32_t i = 0; i < p_parser->string_count; ++i) {
String* p_string = &(p_parser->strings[i]);
if (p_string->result_id == p_parser->source_file_id) {
p_module->source_file = p_string->string;
break;
}
}
}
}
return SPV_REFLECT_RESULT_SUCCESS;
}
static SpvReflectResult ParseFunction(Parser* p_parser, Node* p_func_node, Function* p_func, size_t first_label_index)
{
p_func->id = p_func_node->result_id;
p_func->callee_count = 0;
p_func->accessed_ptr_count = 0;
for (size_t i = first_label_index; i < p_parser->node_count; ++i) {
Node* p_node = &(p_parser->nodes[i]);
if (p_node->op == SpvOpFunctionEnd) {
break;
}
switch (p_node->op) {
case SpvOpFunctionCall: {
++(p_func->callee_count);
}
break;
case SpvOpLoad:
case SpvOpAccessChain:
case SpvOpInBoundsAccessChain:
case SpvOpPtrAccessChain:
case SpvOpArrayLength:
case SpvOpGenericPtrMemSemantics:
case SpvOpInBoundsPtrAccessChain:
case SpvOpStore:
{
++(p_func->accessed_ptr_count);
}
break;
case SpvOpCopyMemory:
case SpvOpCopyMemorySized:
{
p_func->accessed_ptr_count += 2;
}
break;
default: break;
}
}
if (p_func->callee_count > 0) {
p_func->callees = (uint32_t*)calloc(p_func->callee_count,
sizeof(*(p_func->callees)));
if (IsNull(p_func->callees)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
}
if (p_func->accessed_ptr_count > 0) {
p_func->accessed_ptrs = (uint32_t*)calloc(p_func->accessed_ptr_count,
sizeof(*(p_func->accessed_ptrs)));
if (IsNull(p_func->accessed_ptrs)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
}
p_func->callee_count = 0;
p_func->accessed_ptr_count = 0;
for (size_t i = first_label_index; i < p_parser->node_count; ++i) {
Node* p_node = &(p_parser->nodes[i]);
if (p_node->op == SpvOpFunctionEnd) {
break;
}
switch (p_node->op) {
case SpvOpFunctionCall: {
CHECKED_READU32(p_parser, p_node->word_offset + 3,
p_func->callees[p_func->callee_count]);
(++p_func->callee_count);
}
break;
case SpvOpLoad:
case SpvOpAccessChain:
case SpvOpInBoundsAccessChain:
case SpvOpPtrAccessChain:
case SpvOpArrayLength:
case SpvOpGenericPtrMemSemantics:
case SpvOpInBoundsPtrAccessChain:
{
CHECKED_READU32(p_parser, p_node->word_offset + 3,
p_func->accessed_ptrs[p_func->accessed_ptr_count]);
(++p_func->accessed_ptr_count);
}
break;
case SpvOpStore:
{
CHECKED_READU32(p_parser, p_node->word_offset + 2,
p_func->accessed_ptrs[p_func->accessed_ptr_count]);
(++p_func->accessed_ptr_count);
}
break;
case SpvOpCopyMemory:
case SpvOpCopyMemorySized:
{
CHECKED_READU32(p_parser, p_node->word_offset + 2,
p_func->accessed_ptrs[p_func->accessed_ptr_count]);
(++p_func->accessed_ptr_count);
CHECKED_READU32(p_parser, p_node->word_offset + 3,
p_func->accessed_ptrs[p_func->accessed_ptr_count]);
(++p_func->accessed_ptr_count);
}
break;
default: break;
}
}
if (p_func->callee_count > 0) {
qsort(p_func->callees, p_func->callee_count,
sizeof(*(p_func->callees)), SortCompareUint32);
}
p_func->callee_count = (uint32_t)DedupSortedUint32(p_func->callees,
p_func->callee_count);
if (p_func->accessed_ptr_count > 0) {
qsort(p_func->accessed_ptrs, p_func->accessed_ptr_count,
sizeof(*(p_func->accessed_ptrs)), SortCompareUint32);
}
p_func->accessed_ptr_count = (uint32_t)DedupSortedUint32(p_func->accessed_ptrs,
p_func->accessed_ptr_count);
return SPV_REFLECT_RESULT_SUCCESS;
}
static int SortCompareFunctions(const void* a, const void* b)
{
const Function* af = (const Function*)a;
const Function* bf = (const Function*)b;
return (int)af->id - (int)bf->id;
}
static SpvReflectResult ParseFunctions(Parser* p_parser)
{
assert(IsNotNull(p_parser));
assert(IsNotNull(p_parser->spirv_code));
assert(IsNotNull(p_parser->nodes));
if (IsNotNull(p_parser) && IsNotNull(p_parser->spirv_code) && IsNotNull(p_parser->nodes)) {
if (p_parser->function_count == 0) {
return SPV_REFLECT_RESULT_SUCCESS;
}
p_parser->functions = (Function*)calloc(p_parser->function_count,
sizeof(*(p_parser->functions)));
if (IsNull(p_parser->functions)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
size_t function_index = 0;
for (size_t i = 0; i < p_parser->node_count; ++i) {
Node* p_node = &(p_parser->nodes[i]);
if (p_node->op != SpvOpFunction) {
continue;
}
// Skip over function declarations that aren't definitions
bool func_definition = false;
// Intentionally reuse i to avoid iterating over these nodes more than
// once
for (; i < p_parser->node_count; ++i) {
if (p_parser->nodes[i].op == SpvOpLabel) {
func_definition = true;
break;
}
if (p_parser->nodes[i].op == SpvOpFunctionEnd) {
break;
}
}
if (!func_definition) {
continue;
}
Function* p_function = &(p_parser->functions[function_index]);
SpvReflectResult result = ParseFunction(p_parser, p_node, p_function, i);
if (result != SPV_REFLECT_RESULT_SUCCESS) {
return result;
}
++function_index;
}
qsort(p_parser->functions, p_parser->function_count,
sizeof(*(p_parser->functions)), SortCompareFunctions);
// Once they're sorted, link the functions with pointers to improve graph
// traversal efficiency
for (size_t i = 0; i < p_parser->function_count; ++i) {
Function* p_func = &(p_parser->functions[i]);
if (p_func->callee_count == 0) {
continue;
}
p_func->callee_ptrs = (Function**)calloc(p_func->callee_count,
sizeof(*(p_func->callee_ptrs)));
for (size_t j = 0, k = 0; j < p_func->callee_count; ++j) {
while (p_parser->functions[k].id != p_func->callees[j]) {
++k;
if (k >= p_parser->function_count) {
// Invalid called function ID somewhere
return SPV_REFLECT_RESULT_ERROR_SPIRV_INVALID_ID_REFERENCE;
}