forked from KhronosGroup/SPIRV-Reflect
-
Notifications
You must be signed in to change notification settings - Fork 16
/
spirv_reflect.c
4797 lines (4304 loc) · 162 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
#if defined(SPIRV_REFLECT_ENABLE_ASSERTS)
#define SPV_REFLECT_ASSERT(COND) \
assert(COND);
#else
#define SPV_REFLECT_ASSERT(COND)
#endif
// Temporary enums until these make it into SPIR-V/Vulkan
// clang-format off
enum {
SpvReflectOpDecorateId = 332,
SpvReflectOpDecorateStringGOOGLE = 5632,
SpvReflectOpMemberDecorateStringGOOGLE = 5633,
SpvReflectDecorationHlslCounterBufferGOOGLE = 5634,
SpvReflectDecorationHlslSemanticGOOGLE = 5635
};
// clang-format on
// 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,
};
// clang-format on
// clang-format off
enum {
INVALID_VALUE = 0xFFFFFFFF,
};
// clang-format on
// clang-format off
enum {
MAX_NODE_NAME_LENGTH = 1024,
};
// clang-format on
// clang-format off
enum {
IMAGE_SAMPLED = 1,
IMAGE_STORAGE = 2
};
// clang-format on
// clang-format off
typedef struct ArrayTraits {
uint32_t element_type_id;
uint32_t length_id;
} ArrayTraits;
// clang-format on
// clang-format off
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;
// clang-format on
// clang-format off
typedef struct NumberDecoration {
uint32_t word_offset;
uint32_t value;
} NumberDecoration;
// clang-format on
// clang-format off
typedef struct StringDecoration {
uint32_t word_offset;
const char* value;
} StringDecoration;
// clang-format on
// clang-format off
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;
// clang-format on
// clang-format off
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;
// clang-format on
// clang-format off
typedef struct String {
uint32_t result_id;
const char* string;
} String;
// clang-format on
// clang-format off
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;
// clang-format on
// clang-format off
typedef struct AccessChain {
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;
} AccessChain;
// clang-format on
// clang-format off
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;
const char* source_embedded;
size_t node_count;
Node* nodes;
uint32_t entry_point_count;
uint32_t function_count;
Function* functions;
uint32_t access_chain_count;
AccessChain* access_chains;
uint32_t type_count;
uint32_t descriptor_count;
uint32_t push_constant_count;
} Parser;
// clang-format on
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);
}
#define IsNull(ptr) \
(ptr == 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* p_arr0,
size_t arr0_size,
const uint32_t* p_arr1,
size_t arr1_size,
uint32_t** pp_res,
size_t* res_size
)
{
*res_size = 0;
const uint32_t* arr0_end = p_arr0 + arr0_size;
const uint32_t* arr1_end = p_arr1 + arr1_size;
const uint32_t* idx0 = p_arr0;
const uint32_t* idx1 = p_arr1;
while (idx0 != arr0_end && idx1 != arr1_end) {
if (*idx0 < *idx1) {
++idx0;
} else if (*idx0 > *idx1) {
++idx1;
} else {
++*res_size;
++idx0;
++idx1;
}
}
*pp_res = NULL;
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* idxr = *pp_res;
idx0 = p_arr0;
idx1 = p_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);
}
// 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);
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];
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 = (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;
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 = (AccessChain*)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;
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);
}
if (p_node->word_count >= 5) {
const char* p_source = (const char*)(p_parser->spirv_code + p_node->word_offset + 4);
char* p_source_temp = (char*)calloc(strlen(p_source) + 1, sizeof(char*));
if (IsNull(p_source_temp)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
strcpy(p_source_temp, p_source);
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);
char* p_continued_source = (char*)calloc(strlen(p_source) + strlen(p_parser->source_embedded) + 1, sizeof(char*));
if (IsNull(p_continued_source)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
strcpy(p_continued_source, p_parser->source_embedded);
strcat(p_continued_source, p_source);
SafeFree(p_parser->source_embedded);
p_parser->source_embedded = p_continued_source;
}
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:
case SpvOpTypeAccelerationStructureKHR:
case SpvOpTypeRayQueryKHR:
{
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 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:
{
AccessChain* 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
Node* p_index_value_node = FindNode(p_parser, index_id);
if ((p_index_value_node != NULL) && (p_index_value_node->op == SpvOpConstant)) {
// 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) {
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;
}
}
}
//Source code
if (IsNotNull(p_parser->source_embedded))
{
char* p_source = (char*)calloc(strlen(p_parser->source_embedded) + 1, sizeof(char*));
if (IsNull(p_source)) {
return SPV_REFLECT_RESULT_ERROR_ALLOC_FAILED;
}
strcpy(p_source, p_parser->source_embedded);
p_module->source_source = p_source;
}
}
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;
}