forked from KhronosGroup/SPIRV-Cross
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spirv_cross_parsed_ir.cpp
1064 lines (895 loc) · 24.2 KB
/
spirv_cross_parsed_ir.cpp
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 2018-2021 Arm Limited
*
* 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.
*/
/*
* At your option, you may choose to accept this material under either:
* 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
* 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
* SPDX-License-Identifier: Apache-2.0 OR MIT.
*/
#include "spirv_cross_parsed_ir.hpp"
#include <algorithm>
#include <assert.h>
using namespace std;
using namespace spv;
namespace SPIRV_CROSS_NAMESPACE
{
ParsedIR::ParsedIR()
{
// If we move ParsedIR, we need to make sure the pointer stays fixed since the child Variant objects consume a pointer to this group,
// so need an extra pointer here.
pool_group.reset(new ObjectPoolGroup);
pool_group->pools[TypeType].reset(new ObjectPool<SPIRType>);
pool_group->pools[TypeVariable].reset(new ObjectPool<SPIRVariable>);
pool_group->pools[TypeConstant].reset(new ObjectPool<SPIRConstant>);
pool_group->pools[TypeFunction].reset(new ObjectPool<SPIRFunction>);
pool_group->pools[TypeFunctionPrototype].reset(new ObjectPool<SPIRFunctionPrototype>);
pool_group->pools[TypeBlock].reset(new ObjectPool<SPIRBlock>);
pool_group->pools[TypeExtension].reset(new ObjectPool<SPIRExtension>);
pool_group->pools[TypeExpression].reset(new ObjectPool<SPIRExpression>);
pool_group->pools[TypeConstantOp].reset(new ObjectPool<SPIRConstantOp>);
pool_group->pools[TypeCombinedImageSampler].reset(new ObjectPool<SPIRCombinedImageSampler>);
pool_group->pools[TypeAccessChain].reset(new ObjectPool<SPIRAccessChain>);
pool_group->pools[TypeUndef].reset(new ObjectPool<SPIRUndef>);
pool_group->pools[TypeString].reset(new ObjectPool<SPIRString>);
}
// Should have been default-implemented, but need this on MSVC 2013.
ParsedIR::ParsedIR(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT
{
*this = move(other);
}
ParsedIR &ParsedIR::operator=(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT
{
if (this != &other)
{
pool_group = move(other.pool_group);
spirv = move(other.spirv);
meta = move(other.meta);
for (int i = 0; i < TypeCount; i++)
ids_for_type[i] = move(other.ids_for_type[i]);
ids_for_constant_or_type = move(other.ids_for_constant_or_type);
ids_for_constant_or_variable = move(other.ids_for_constant_or_variable);
declared_capabilities = move(other.declared_capabilities);
declared_extensions = move(other.declared_extensions);
block_meta = move(other.block_meta);
continue_block_to_loop_header = move(other.continue_block_to_loop_header);
entry_points = move(other.entry_points);
ids = move(other.ids);
addressing_model = other.addressing_model;
memory_model = other.memory_model;
default_entry_point = other.default_entry_point;
source = other.source;
loop_iteration_depth_hard = other.loop_iteration_depth_hard;
loop_iteration_depth_soft = other.loop_iteration_depth_soft;
meta_needing_name_fixup = std::move(other.meta_needing_name_fixup);
}
return *this;
}
ParsedIR::ParsedIR(const ParsedIR &other)
: ParsedIR()
{
*this = other;
}
ParsedIR &ParsedIR::operator=(const ParsedIR &other)
{
if (this != &other)
{
spirv = other.spirv;
meta = other.meta;
for (int i = 0; i < TypeCount; i++)
ids_for_type[i] = other.ids_for_type[i];
ids_for_constant_or_type = other.ids_for_constant_or_type;
ids_for_constant_or_variable = other.ids_for_constant_or_variable;
declared_capabilities = other.declared_capabilities;
declared_extensions = other.declared_extensions;
block_meta = other.block_meta;
continue_block_to_loop_header = other.continue_block_to_loop_header;
entry_points = other.entry_points;
default_entry_point = other.default_entry_point;
source = other.source;
loop_iteration_depth_hard = other.loop_iteration_depth_hard;
loop_iteration_depth_soft = other.loop_iteration_depth_soft;
addressing_model = other.addressing_model;
memory_model = other.memory_model;
meta_needing_name_fixup = other.meta_needing_name_fixup;
// Very deliberate copying of IDs. There is no default copy constructor, nor a simple default constructor.
// Construct object first so we have the correct allocator set-up, then we can copy object into our new pool group.
ids.clear();
ids.reserve(other.ids.size());
for (size_t i = 0; i < other.ids.size(); i++)
{
ids.emplace_back(pool_group.get());
ids.back() = other.ids[i];
}
}
return *this;
}
void ParsedIR::set_id_bounds(uint32_t bounds)
{
ids.reserve(bounds);
while (ids.size() < bounds)
ids.emplace_back(pool_group.get());
block_meta.resize(bounds);
}
// Roll our own versions of these functions to avoid potential locale shenanigans.
static bool is_alpha(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static bool is_numeric(char c)
{
return c >= '0' && c <= '9';
}
static bool is_alphanumeric(char c)
{
return is_alpha(c) || is_numeric(c);
}
static bool is_valid_identifier(const string &name)
{
if (name.empty())
return true;
if (is_numeric(name[0]))
return false;
for (auto c : name)
if (!is_alphanumeric(c) && c != '_')
return false;
bool saw_underscore = false;
// Two underscores in a row is not a valid identifier either.
// Technically reserved, but it's easier to treat it as invalid.
for (auto c : name)
{
bool is_underscore = c == '_';
if (is_underscore && saw_underscore)
return false;
saw_underscore = is_underscore;
}
return true;
}
static bool is_reserved_prefix(const string &name)
{
// Generic reserved identifiers used by the implementation.
return name.compare(0, 3, "gl_", 3) == 0 ||
// Ignore this case for now, might rewrite internal code to always use spv prefix.
//name.compare(0, 11, "SPIRV_Cross", 11) == 0 ||
name.compare(0, 3, "spv", 3) == 0;
}
static bool is_reserved_identifier(const string &name, bool member, bool allow_reserved_prefixes)
{
if (!allow_reserved_prefixes && is_reserved_prefix(name))
return true;
if (member)
{
// Reserved member identifiers come in one form:
// _m[0-9]+$.
if (name.size() < 3)
return false;
if (name.compare(0, 2, "_m", 2) != 0)
return false;
size_t index = 2;
while (index < name.size() && is_numeric(name[index]))
index++;
return index == name.size();
}
else
{
// Reserved non-member identifiers come in two forms:
// _[0-9]+$, used for temporaries which map directly to a SPIR-V ID.
// _[0-9]+_, used for auxillary temporaries which derived from a SPIR-V ID.
if (name.size() < 2)
return false;
if (name[0] != '_' || !is_numeric(name[1]))
return false;
size_t index = 2;
while (index < name.size() && is_numeric(name[index]))
index++;
return index == name.size() || (index < name.size() && name[index] == '_');
}
}
bool ParsedIR::is_globally_reserved_identifier(std::string &str, bool allow_reserved_prefixes)
{
return is_reserved_identifier(str, false, allow_reserved_prefixes);
}
uint32_t ParsedIR::get_spirv_version() const
{
return spirv[1];
}
static string make_unreserved_identifier(const string &name)
{
if (is_reserved_prefix(name))
return "_RESERVED_IDENTIFIER_FIXUP_" + name;
else
return "_RESERVED_IDENTIFIER_FIXUP" + name;
}
void ParsedIR::sanitize_underscores(std::string &str)
{
// Compact adjacent underscores to make it valid.
auto dst = str.begin();
auto src = dst;
bool saw_underscore = false;
while (src != str.end())
{
bool is_underscore = *src == '_';
if (saw_underscore && is_underscore)
{
src++;
}
else
{
if (dst != src)
*dst = *src;
dst++;
src++;
saw_underscore = is_underscore;
}
}
str.erase(dst, str.end());
}
static string ensure_valid_identifier(const string &name)
{
// Functions in glslangValidator are mangled with name(<mangled> stuff.
// Normally, we would never see '(' in any legal identifiers, so just strip them out.
auto str = name.substr(0, name.find('('));
if (str.empty())
return str;
if (is_numeric(str[0]))
str[0] = '_';
for (auto &c : str)
if (!is_alphanumeric(c) && c != '_')
c = '_';
ParsedIR::sanitize_underscores(str);
return str;
}
const string &ParsedIR::get_name(ID id) const
{
auto *m = find_meta(id);
if (m)
return m->decoration.alias;
else
return empty_string;
}
const string &ParsedIR::get_member_name(TypeID id, uint32_t index) const
{
auto *m = find_meta(id);
if (m)
{
if (index >= m->members.size())
return empty_string;
return m->members[index].alias;
}
else
return empty_string;
}
void ParsedIR::sanitize_identifier(std::string &name, bool member, bool allow_reserved_prefixes)
{
if (!is_valid_identifier(name))
name = ensure_valid_identifier(name);
if (is_reserved_identifier(name, member, allow_reserved_prefixes))
name = make_unreserved_identifier(name);
}
void ParsedIR::fixup_reserved_names()
{
for (uint32_t id : meta_needing_name_fixup)
{
auto &m = meta[id];
sanitize_identifier(m.decoration.alias, false, false);
for (auto &memb : m.members)
sanitize_identifier(memb.alias, true, false);
}
meta_needing_name_fixup.clear();
}
void ParsedIR::set_name(ID id, const string &name)
{
auto &m = meta[id];
m.decoration.alias = name;
if (!is_valid_identifier(name) || is_reserved_identifier(name, false, false))
meta_needing_name_fixup.insert(id);
}
void ParsedIR::set_member_name(TypeID id, uint32_t index, const string &name)
{
auto &m = meta[id];
m.members.resize(max(meta[id].members.size(), size_t(index) + 1));
m.members[index].alias = name;
if (!is_valid_identifier(name) || is_reserved_identifier(name, true, false))
meta_needing_name_fixup.insert(id);
}
void ParsedIR::set_decoration_string(ID id, Decoration decoration, const string &argument)
{
auto &dec = meta[id].decoration;
dec.decoration_flags.set(decoration);
switch (decoration)
{
case DecorationHlslSemanticGOOGLE:
dec.hlsl_semantic = argument;
break;
default:
break;
}
}
void ParsedIR::set_decoration(ID id, Decoration decoration, uint32_t argument)
{
auto &dec = meta[id].decoration;
dec.decoration_flags.set(decoration);
switch (decoration)
{
case DecorationBuiltIn:
dec.builtin = true;
dec.builtin_type = static_cast<BuiltIn>(argument);
break;
case DecorationLocation:
dec.location = argument;
break;
case DecorationComponent:
dec.component = argument;
break;
case DecorationOffset:
dec.offset = argument;
break;
case DecorationXfbBuffer:
dec.xfb_buffer = argument;
break;
case DecorationXfbStride:
dec.xfb_stride = argument;
break;
case DecorationStream:
dec.stream = argument;
break;
case DecorationArrayStride:
dec.array_stride = argument;
break;
case DecorationMatrixStride:
dec.matrix_stride = argument;
break;
case DecorationBinding:
dec.binding = argument;
break;
case DecorationDescriptorSet:
dec.set = argument;
break;
case DecorationInputAttachmentIndex:
dec.input_attachment = argument;
break;
case DecorationSpecId:
dec.spec_id = argument;
break;
case DecorationIndex:
dec.index = argument;
break;
case DecorationHlslCounterBufferGOOGLE:
meta[id].hlsl_magic_counter_buffer = argument;
meta[argument].hlsl_is_magic_counter_buffer = true;
break;
case DecorationFPRoundingMode:
dec.fp_rounding_mode = static_cast<FPRoundingMode>(argument);
break;
default:
break;
}
}
void ParsedIR::set_member_decoration(TypeID id, uint32_t index, Decoration decoration, uint32_t argument)
{
meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1));
auto &dec = meta[id].members[index];
dec.decoration_flags.set(decoration);
switch (decoration)
{
case DecorationBuiltIn:
dec.builtin = true;
dec.builtin_type = static_cast<BuiltIn>(argument);
break;
case DecorationLocation:
dec.location = argument;
break;
case DecorationComponent:
dec.component = argument;
break;
case DecorationBinding:
dec.binding = argument;
break;
case DecorationOffset:
dec.offset = argument;
break;
case DecorationXfbBuffer:
dec.xfb_buffer = argument;
break;
case DecorationXfbStride:
dec.xfb_stride = argument;
break;
case DecorationStream:
dec.stream = argument;
break;
case DecorationSpecId:
dec.spec_id = argument;
break;
case DecorationMatrixStride:
dec.matrix_stride = argument;
break;
case DecorationIndex:
dec.index = argument;
break;
default:
break;
}
}
// Recursively marks any constants referenced by the specified constant instruction as being used
// as an array length. The id must be a constant instruction (SPIRConstant or SPIRConstantOp).
void ParsedIR::mark_used_as_array_length(ID id)
{
switch (ids[id].get_type())
{
case TypeConstant:
get<SPIRConstant>(id).is_used_as_array_length = true;
break;
case TypeConstantOp:
{
auto &cop = get<SPIRConstantOp>(id);
if (cop.opcode == OpCompositeExtract)
mark_used_as_array_length(cop.arguments[0]);
else if (cop.opcode == OpCompositeInsert)
{
mark_used_as_array_length(cop.arguments[0]);
mark_used_as_array_length(cop.arguments[1]);
}
else
for (uint32_t arg_id : cop.arguments)
mark_used_as_array_length(arg_id);
break;
}
case TypeUndef:
break;
default:
assert(0);
}
}
Bitset ParsedIR::get_buffer_block_type_flags(const SPIRType &type) const
{
if (type.member_types.empty())
return {};
Bitset all_members_flags = get_member_decoration_bitset(type.self, 0);
for (uint32_t i = 1; i < uint32_t(type.member_types.size()); i++)
all_members_flags.merge_and(get_member_decoration_bitset(type.self, i));
return all_members_flags;
}
Bitset ParsedIR::get_buffer_block_flags(const SPIRVariable &var) const
{
auto &type = get<SPIRType>(var.basetype);
assert(type.basetype == SPIRType::Struct);
// Some flags like non-writable, non-readable are actually found
// as member decorations. If all members have a decoration set, propagate
// the decoration up as a regular variable decoration.
Bitset base_flags;
auto *m = find_meta(var.self);
if (m)
base_flags = m->decoration.decoration_flags;
if (type.member_types.empty())
return base_flags;
auto all_members_flags = get_buffer_block_type_flags(type);
base_flags.merge_or(all_members_flags);
return base_flags;
}
const Bitset &ParsedIR::get_member_decoration_bitset(TypeID id, uint32_t index) const
{
auto *m = find_meta(id);
if (m)
{
if (index >= m->members.size())
return cleared_bitset;
return m->members[index].decoration_flags;
}
else
return cleared_bitset;
}
bool ParsedIR::has_decoration(ID id, Decoration decoration) const
{
return get_decoration_bitset(id).get(decoration);
}
uint32_t ParsedIR::get_decoration(ID id, Decoration decoration) const
{
auto *m = find_meta(id);
if (!m)
return 0;
auto &dec = m->decoration;
if (!dec.decoration_flags.get(decoration))
return 0;
switch (decoration)
{
case DecorationBuiltIn:
return dec.builtin_type;
case DecorationLocation:
return dec.location;
case DecorationComponent:
return dec.component;
case DecorationOffset:
return dec.offset;
case DecorationXfbBuffer:
return dec.xfb_buffer;
case DecorationXfbStride:
return dec.xfb_stride;
case DecorationStream:
return dec.stream;
case DecorationBinding:
return dec.binding;
case DecorationDescriptorSet:
return dec.set;
case DecorationInputAttachmentIndex:
return dec.input_attachment;
case DecorationSpecId:
return dec.spec_id;
case DecorationArrayStride:
return dec.array_stride;
case DecorationMatrixStride:
return dec.matrix_stride;
case DecorationIndex:
return dec.index;
case DecorationFPRoundingMode:
return dec.fp_rounding_mode;
default:
return 1;
}
}
const string &ParsedIR::get_decoration_string(ID id, Decoration decoration) const
{
auto *m = find_meta(id);
if (!m)
return empty_string;
auto &dec = m->decoration;
if (!dec.decoration_flags.get(decoration))
return empty_string;
switch (decoration)
{
case DecorationHlslSemanticGOOGLE:
return dec.hlsl_semantic;
default:
return empty_string;
}
}
void ParsedIR::unset_decoration(ID id, Decoration decoration)
{
auto &dec = meta[id].decoration;
dec.decoration_flags.clear(decoration);
switch (decoration)
{
case DecorationBuiltIn:
dec.builtin = false;
break;
case DecorationLocation:
dec.location = 0;
break;
case DecorationComponent:
dec.component = 0;
break;
case DecorationOffset:
dec.offset = 0;
break;
case DecorationXfbBuffer:
dec.xfb_buffer = 0;
break;
case DecorationXfbStride:
dec.xfb_stride = 0;
break;
case DecorationStream:
dec.stream = 0;
break;
case DecorationBinding:
dec.binding = 0;
break;
case DecorationDescriptorSet:
dec.set = 0;
break;
case DecorationInputAttachmentIndex:
dec.input_attachment = 0;
break;
case DecorationSpecId:
dec.spec_id = 0;
break;
case DecorationHlslSemanticGOOGLE:
dec.hlsl_semantic.clear();
break;
case DecorationFPRoundingMode:
dec.fp_rounding_mode = FPRoundingModeMax;
break;
case DecorationHlslCounterBufferGOOGLE:
{
auto &counter = meta[id].hlsl_magic_counter_buffer;
if (counter)
{
meta[counter].hlsl_is_magic_counter_buffer = false;
counter = 0;
}
break;
}
default:
break;
}
}
bool ParsedIR::has_member_decoration(TypeID id, uint32_t index, Decoration decoration) const
{
return get_member_decoration_bitset(id, index).get(decoration);
}
uint32_t ParsedIR::get_member_decoration(TypeID id, uint32_t index, Decoration decoration) const
{
auto *m = find_meta(id);
if (!m)
return 0;
if (index >= m->members.size())
return 0;
auto &dec = m->members[index];
if (!dec.decoration_flags.get(decoration))
return 0;
switch (decoration)
{
case DecorationBuiltIn:
return dec.builtin_type;
case DecorationLocation:
return dec.location;
case DecorationComponent:
return dec.component;
case DecorationBinding:
return dec.binding;
case DecorationOffset:
return dec.offset;
case DecorationXfbBuffer:
return dec.xfb_buffer;
case DecorationXfbStride:
return dec.xfb_stride;
case DecorationStream:
return dec.stream;
case DecorationSpecId:
return dec.spec_id;
case DecorationIndex:
return dec.index;
default:
return 1;
}
}
const Bitset &ParsedIR::get_decoration_bitset(ID id) const
{
auto *m = find_meta(id);
if (m)
{
auto &dec = m->decoration;
return dec.decoration_flags;
}
else
return cleared_bitset;
}
void ParsedIR::set_member_decoration_string(TypeID id, uint32_t index, Decoration decoration, const string &argument)
{
meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1));
auto &dec = meta[id].members[index];
dec.decoration_flags.set(decoration);
switch (decoration)
{
case DecorationHlslSemanticGOOGLE:
dec.hlsl_semantic = argument;
break;
default:
break;
}
}
const string &ParsedIR::get_member_decoration_string(TypeID id, uint32_t index, Decoration decoration) const
{
auto *m = find_meta(id);
if (m)
{
if (!has_member_decoration(id, index, decoration))
return empty_string;
auto &dec = m->members[index];
switch (decoration)
{
case DecorationHlslSemanticGOOGLE:
return dec.hlsl_semantic;
default:
return empty_string;
}
}
else
return empty_string;
}
void ParsedIR::unset_member_decoration(TypeID id, uint32_t index, Decoration decoration)
{
auto &m = meta[id];
if (index >= m.members.size())
return;
auto &dec = m.members[index];
dec.decoration_flags.clear(decoration);
switch (decoration)
{
case DecorationBuiltIn:
dec.builtin = false;
break;
case DecorationLocation:
dec.location = 0;
break;
case DecorationComponent:
dec.component = 0;
break;
case DecorationOffset:
dec.offset = 0;
break;
case DecorationXfbBuffer:
dec.xfb_buffer = 0;
break;
case DecorationXfbStride:
dec.xfb_stride = 0;
break;
case DecorationStream:
dec.stream = 0;
break;
case DecorationSpecId:
dec.spec_id = 0;
break;
case DecorationHlslSemanticGOOGLE:
dec.hlsl_semantic.clear();
break;
default:
break;
}
}
uint32_t ParsedIR::increase_bound_by(uint32_t incr_amount)
{
auto curr_bound = ids.size();
auto new_bound = curr_bound + incr_amount;
ids.reserve(ids.size() + incr_amount);
for (uint32_t i = 0; i < incr_amount; i++)
ids.emplace_back(pool_group.get());
block_meta.resize(new_bound);
return uint32_t(curr_bound);
}
void ParsedIR::remove_typed_id(Types type, ID id)
{
auto &type_ids = ids_for_type[type];
type_ids.erase(remove(begin(type_ids), end(type_ids), id), end(type_ids));
}
void ParsedIR::reset_all_of_type(Types type)
{
for (auto &id : ids_for_type[type])
if (ids[id].get_type() == type)
ids[id].reset();
ids_for_type[type].clear();
}
void ParsedIR::add_typed_id(Types type, ID id)
{
if (loop_iteration_depth_hard != 0)
SPIRV_CROSS_THROW("Cannot add typed ID while looping over it.");
if (loop_iteration_depth_soft != 0)
{
if (!ids[id].empty())
SPIRV_CROSS_THROW("Cannot override IDs when loop is soft locked.");
return;
}
if (ids[id].empty() || ids[id].get_type() != type)
{
switch (type)
{
case TypeConstant:
ids_for_constant_or_variable.push_back(id);
ids_for_constant_or_type.push_back(id);
break;
case TypeVariable:
ids_for_constant_or_variable.push_back(id);
break;
case TypeType:
case TypeConstantOp:
ids_for_constant_or_type.push_back(id);
break;
default:
break;
}
}
if (ids[id].empty())
{
ids_for_type[type].push_back(id);
}
else if (ids[id].get_type() != type)
{
remove_typed_id(ids[id].get_type(), id);
ids_for_type[type].push_back(id);
}
}
const Meta *ParsedIR::find_meta(ID id) const
{
auto itr = meta.find(id);
if (itr != end(meta))
return &itr->second;
else
return nullptr;
}
Meta *ParsedIR::find_meta(ID id)
{
auto itr = meta.find(id);
if (itr != end(meta))
return &itr->second;
else
return nullptr;
}
ParsedIR::LoopLock ParsedIR::create_loop_hard_lock() const
{
return ParsedIR::LoopLock(&loop_iteration_depth_hard);
}
ParsedIR::LoopLock ParsedIR::create_loop_soft_lock() const
{
return ParsedIR::LoopLock(&loop_iteration_depth_soft);
}
ParsedIR::LoopLock::~LoopLock()
{
if (lock)
(*lock)--;
}
ParsedIR::LoopLock::LoopLock(uint32_t *lock_)
: lock(lock_)
{
if (lock)
(*lock)++;
}
ParsedIR::LoopLock::LoopLock(LoopLock &&other) SPIRV_CROSS_NOEXCEPT
{
*this = move(other);
}