-
Notifications
You must be signed in to change notification settings - Fork 570
/
spirv_cross_c.cpp
2962 lines (2574 loc) · 93.1 KB
/
spirv_cross_c.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 2019-2021 Hans-Kristian Arntzen
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
* 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>.
*/
#include "spirv_cross_c.h"
#if SPIRV_CROSS_C_API_CPP
#include "spirv_cpp.hpp"
#endif
#if SPIRV_CROSS_C_API_GLSL
#include "spirv_glsl.hpp"
#else
#include "spirv_cross.hpp"
#endif
#if SPIRV_CROSS_C_API_HLSL
#include "spirv_hlsl.hpp"
#endif
#if SPIRV_CROSS_C_API_MSL
#include "spirv_msl.hpp"
#endif
#if SPIRV_CROSS_C_API_REFLECT
#include "spirv_reflect.hpp"
#endif
#ifdef HAVE_SPIRV_CROSS_GIT_VERSION
#include "gitversion.h"
#endif
#include "spirv_parser.hpp"
#include <memory>
#include <new>
#include <string.h>
// clang-format off
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
#define SPVC_BEGIN_SAFE_SCOPE try
#else
#define SPVC_BEGIN_SAFE_SCOPE
#endif
#ifndef SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
#define SPVC_END_SAFE_SCOPE(context, error) \
catch (const std::exception &e) \
{ \
(context)->report_error(e.what()); \
return (error); \
}
#else
#define SPVC_END_SAFE_SCOPE(context, error)
#endif
using namespace std;
using namespace SPIRV_CROSS_NAMESPACE;
struct ScratchMemoryAllocation
{
virtual ~ScratchMemoryAllocation() = default;
};
struct StringAllocation : ScratchMemoryAllocation
{
explicit StringAllocation(const char *name)
: str(name)
{
}
explicit StringAllocation(std::string name)
: str(std::move(name))
{
}
std::string str;
};
template <typename T>
struct TemporaryBuffer : ScratchMemoryAllocation
{
SmallVector<T> buffer;
};
template <typename T, typename... Ts>
static inline std::unique_ptr<T> spvc_allocate(Ts &&... ts)
{
return std::unique_ptr<T>(new T(std::forward<Ts>(ts)...));
}
struct spvc_context_s
{
string last_error;
SmallVector<unique_ptr<ScratchMemoryAllocation>> allocations;
const char *allocate_name(const std::string &name);
spvc_error_callback callback = nullptr;
void *callback_userdata = nullptr;
void report_error(std::string msg);
};
void spvc_context_s::report_error(std::string msg)
{
last_error = std::move(msg);
if (callback)
callback(callback_userdata, last_error.c_str());
}
const char *spvc_context_s::allocate_name(const std::string &name)
{
SPVC_BEGIN_SAFE_SCOPE
{
auto alloc = spvc_allocate<StringAllocation>(name);
auto *ret = alloc->str.c_str();
allocations.emplace_back(std::move(alloc));
return ret;
}
SPVC_END_SAFE_SCOPE(this, nullptr)
}
struct spvc_parsed_ir_s : ScratchMemoryAllocation
{
spvc_context context = nullptr;
ParsedIR parsed;
};
struct spvc_compiler_s : ScratchMemoryAllocation
{
spvc_context context = nullptr;
unique_ptr<Compiler> compiler;
spvc_backend backend = SPVC_BACKEND_NONE;
};
struct spvc_compiler_options_s : ScratchMemoryAllocation
{
spvc_context context = nullptr;
uint32_t backend_flags = 0;
#if SPIRV_CROSS_C_API_GLSL
CompilerGLSL::Options glsl;
#endif
#if SPIRV_CROSS_C_API_MSL
CompilerMSL::Options msl;
#endif
#if SPIRV_CROSS_C_API_HLSL
CompilerHLSL::Options hlsl;
#endif
};
struct spvc_set_s : ScratchMemoryAllocation
{
std::unordered_set<VariableID> set;
};
// Dummy-inherit to we can keep our opaque type handle type safe in C-land as well,
// and avoid just throwing void * around.
struct spvc_type_s : SPIRType
{
};
struct spvc_constant_s : SPIRConstant
{
};
struct spvc_resources_s : ScratchMemoryAllocation
{
spvc_context context = nullptr;
SmallVector<spvc_reflected_resource> uniform_buffers;
SmallVector<spvc_reflected_resource> storage_buffers;
SmallVector<spvc_reflected_resource> stage_inputs;
SmallVector<spvc_reflected_resource> stage_outputs;
SmallVector<spvc_reflected_resource> subpass_inputs;
SmallVector<spvc_reflected_resource> storage_images;
SmallVector<spvc_reflected_resource> sampled_images;
SmallVector<spvc_reflected_resource> atomic_counters;
SmallVector<spvc_reflected_resource> push_constant_buffers;
SmallVector<spvc_reflected_resource> shader_record_buffers;
SmallVector<spvc_reflected_resource> separate_images;
SmallVector<spvc_reflected_resource> separate_samplers;
SmallVector<spvc_reflected_resource> acceleration_structures;
SmallVector<spvc_reflected_resource> gl_plain_uniforms;
SmallVector<spvc_reflected_builtin_resource> builtin_inputs;
SmallVector<spvc_reflected_builtin_resource> builtin_outputs;
bool copy_resources(SmallVector<spvc_reflected_resource> &outputs, const SmallVector<Resource> &inputs);
bool copy_resources(SmallVector<spvc_reflected_builtin_resource> &outputs, const SmallVector<BuiltInResource> &inputs);
bool copy_resources(const ShaderResources &resources);
};
spvc_result spvc_context_create(spvc_context *context)
{
auto *ctx = new (std::nothrow) spvc_context_s;
if (!ctx)
return SPVC_ERROR_OUT_OF_MEMORY;
*context = ctx;
return SPVC_SUCCESS;
}
void spvc_context_destroy(spvc_context context)
{
delete context;
}
void spvc_context_release_allocations(spvc_context context)
{
context->allocations.clear();
}
const char *spvc_context_get_last_error_string(spvc_context context)
{
return context->last_error.c_str();
}
SPVC_PUBLIC_API void spvc_context_set_error_callback(spvc_context context, spvc_error_callback cb, void *userdata)
{
context->callback = cb;
context->callback_userdata = userdata;
}
spvc_result spvc_context_parse_spirv(spvc_context context, const SpvId *spirv, size_t word_count,
spvc_parsed_ir *parsed_ir)
{
SPVC_BEGIN_SAFE_SCOPE
{
std::unique_ptr<spvc_parsed_ir_s> pir(new (std::nothrow) spvc_parsed_ir_s);
if (!pir)
{
context->report_error("Out of memory.");
return SPVC_ERROR_OUT_OF_MEMORY;
}
pir->context = context;
Parser parser(spirv, word_count);
parser.parse();
pir->parsed = std::move(parser.get_parsed_ir());
*parsed_ir = pir.get();
context->allocations.push_back(std::move(pir));
}
SPVC_END_SAFE_SCOPE(context, SPVC_ERROR_INVALID_SPIRV)
return SPVC_SUCCESS;
}
spvc_result spvc_context_create_compiler(spvc_context context, spvc_backend backend, spvc_parsed_ir parsed_ir,
spvc_capture_mode mode, spvc_compiler *compiler)
{
SPVC_BEGIN_SAFE_SCOPE
{
std::unique_ptr<spvc_compiler_s> comp(new (std::nothrow) spvc_compiler_s);
if (!comp)
{
context->report_error("Out of memory.");
return SPVC_ERROR_OUT_OF_MEMORY;
}
comp->backend = backend;
comp->context = context;
if (mode != SPVC_CAPTURE_MODE_COPY && mode != SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
{
context->report_error("Invalid argument for capture mode.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
switch (backend)
{
case SPVC_BACKEND_NONE:
if (mode == SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
comp->compiler.reset(new Compiler(std::move(parsed_ir->parsed)));
else if (mode == SPVC_CAPTURE_MODE_COPY)
comp->compiler.reset(new Compiler(parsed_ir->parsed));
break;
#if SPIRV_CROSS_C_API_GLSL
case SPVC_BACKEND_GLSL:
if (mode == SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
comp->compiler.reset(new CompilerGLSL(std::move(parsed_ir->parsed)));
else if (mode == SPVC_CAPTURE_MODE_COPY)
comp->compiler.reset(new CompilerGLSL(parsed_ir->parsed));
break;
#endif
#if SPIRV_CROSS_C_API_HLSL
case SPVC_BACKEND_HLSL:
if (mode == SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
comp->compiler.reset(new CompilerHLSL(std::move(parsed_ir->parsed)));
else if (mode == SPVC_CAPTURE_MODE_COPY)
comp->compiler.reset(new CompilerHLSL(parsed_ir->parsed));
break;
#endif
#if SPIRV_CROSS_C_API_MSL
case SPVC_BACKEND_MSL:
if (mode == SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
comp->compiler.reset(new CompilerMSL(std::move(parsed_ir->parsed)));
else if (mode == SPVC_CAPTURE_MODE_COPY)
comp->compiler.reset(new CompilerMSL(parsed_ir->parsed));
break;
#endif
#if SPIRV_CROSS_C_API_CPP
case SPVC_BACKEND_CPP:
if (mode == SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
comp->compiler.reset(new CompilerCPP(std::move(parsed_ir->parsed)));
else if (mode == SPVC_CAPTURE_MODE_COPY)
comp->compiler.reset(new CompilerCPP(parsed_ir->parsed));
break;
#endif
#if SPIRV_CROSS_C_API_REFLECT
case SPVC_BACKEND_JSON:
if (mode == SPVC_CAPTURE_MODE_TAKE_OWNERSHIP)
comp->compiler.reset(new CompilerReflection(std::move(parsed_ir->parsed)));
else if (mode == SPVC_CAPTURE_MODE_COPY)
comp->compiler.reset(new CompilerReflection(parsed_ir->parsed));
break;
#endif
default:
context->report_error("Invalid backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
*compiler = comp.get();
context->allocations.push_back(std::move(comp));
}
SPVC_END_SAFE_SCOPE(context, SPVC_ERROR_OUT_OF_MEMORY)
return SPVC_SUCCESS;
}
spvc_result spvc_compiler_create_compiler_options(spvc_compiler compiler, spvc_compiler_options *options)
{
SPVC_BEGIN_SAFE_SCOPE
{
std::unique_ptr<spvc_compiler_options_s> opt(new (std::nothrow) spvc_compiler_options_s);
if (!opt)
{
compiler->context->report_error("Out of memory.");
return SPVC_ERROR_OUT_OF_MEMORY;
}
opt->context = compiler->context;
opt->backend_flags = 0;
switch (compiler->backend)
{
#if SPIRV_CROSS_C_API_MSL
case SPVC_BACKEND_MSL:
opt->backend_flags |= SPVC_COMPILER_OPTION_MSL_BIT | SPVC_COMPILER_OPTION_COMMON_BIT;
opt->glsl = static_cast<CompilerMSL *>(compiler->compiler.get())->get_common_options();
opt->msl = static_cast<CompilerMSL *>(compiler->compiler.get())->get_msl_options();
break;
#endif
#if SPIRV_CROSS_C_API_HLSL
case SPVC_BACKEND_HLSL:
opt->backend_flags |= SPVC_COMPILER_OPTION_HLSL_BIT | SPVC_COMPILER_OPTION_COMMON_BIT;
opt->glsl = static_cast<CompilerHLSL *>(compiler->compiler.get())->get_common_options();
opt->hlsl = static_cast<CompilerHLSL *>(compiler->compiler.get())->get_hlsl_options();
break;
#endif
#if SPIRV_CROSS_C_API_GLSL
case SPVC_BACKEND_GLSL:
opt->backend_flags |= SPVC_COMPILER_OPTION_GLSL_BIT | SPVC_COMPILER_OPTION_COMMON_BIT;
opt->glsl = static_cast<CompilerGLSL *>(compiler->compiler.get())->get_common_options();
break;
#endif
default:
break;
}
*options = opt.get();
compiler->context->allocations.push_back(std::move(opt));
}
SPVC_END_SAFE_SCOPE(compiler->context, SPVC_ERROR_OUT_OF_MEMORY)
return SPVC_SUCCESS;
}
spvc_result spvc_compiler_options_set_bool(spvc_compiler_options options, spvc_compiler_option option,
spvc_bool value)
{
return spvc_compiler_options_set_uint(options, option, value ? 1 : 0);
}
spvc_result spvc_compiler_options_set_uint(spvc_compiler_options options, spvc_compiler_option option, unsigned value)
{
(void)value;
(void)option;
uint32_t supported_mask = options->backend_flags;
uint32_t required_mask = option & SPVC_COMPILER_OPTION_LANG_BITS;
if ((required_mask | supported_mask) != supported_mask)
{
options->context->report_error("Option is not supported by current backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
switch (option)
{
#if SPIRV_CROSS_C_API_GLSL
case SPVC_COMPILER_OPTION_FORCE_TEMPORARY:
options->glsl.force_temporary = value != 0;
break;
case SPVC_COMPILER_OPTION_FLATTEN_MULTIDIMENSIONAL_ARRAYS:
options->glsl.flatten_multidimensional_arrays = value != 0;
break;
case SPVC_COMPILER_OPTION_FIXUP_DEPTH_CONVENTION:
options->glsl.vertex.fixup_clipspace = value != 0;
break;
case SPVC_COMPILER_OPTION_FLIP_VERTEX_Y:
options->glsl.vertex.flip_vert_y = value != 0;
break;
case SPVC_COMPILER_OPTION_EMIT_LINE_DIRECTIVES:
options->glsl.emit_line_directives = value != 0;
break;
case SPVC_COMPILER_OPTION_ENABLE_STORAGE_IMAGE_QUALIFIER_DEDUCTION:
options->glsl.enable_storage_image_qualifier_deduction = value != 0;
break;
case SPVC_COMPILER_OPTION_FORCE_ZERO_INITIALIZED_VARIABLES:
options->glsl.force_zero_initialized_variables = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_SUPPORT_NONZERO_BASE_INSTANCE:
options->glsl.vertex.support_nonzero_base_instance = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_SEPARATE_SHADER_OBJECTS:
options->glsl.separate_shader_objects = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_ENABLE_420PACK_EXTENSION:
options->glsl.enable_420pack_extension = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_VERSION:
options->glsl.version = value;
break;
case SPVC_COMPILER_OPTION_GLSL_ES:
options->glsl.es = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_VULKAN_SEMANTICS:
options->glsl.vulkan_semantics = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_ES_DEFAULT_FLOAT_PRECISION_HIGHP:
options->glsl.fragment.default_float_precision =
value != 0 ? CompilerGLSL::Options::Precision::Highp : CompilerGLSL::Options::Precision::Mediump;
break;
case SPVC_COMPILER_OPTION_GLSL_ES_DEFAULT_INT_PRECISION_HIGHP:
options->glsl.fragment.default_int_precision =
value != 0 ? CompilerGLSL::Options::Precision::Highp : CompilerGLSL::Options::Precision::Mediump;
break;
case SPVC_COMPILER_OPTION_GLSL_EMIT_PUSH_CONSTANT_AS_UNIFORM_BUFFER:
options->glsl.emit_push_constant_as_uniform_buffer = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_EMIT_UNIFORM_BUFFER_AS_PLAIN_UNIFORMS:
options->glsl.emit_uniform_buffer_as_plain_uniforms = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_FORCE_FLATTENED_IO_BLOCKS:
options->glsl.force_flattened_io_blocks = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_OVR_MULTIVIEW_VIEW_COUNT:
options->glsl.ovr_multiview_view_count = value;
break;
case SPVC_COMPILER_OPTION_RELAX_NAN_CHECKS:
options->glsl.relax_nan_checks = value != 0;
break;
case SPVC_COMPILER_OPTION_GLSL_ENABLE_ROW_MAJOR_LOAD_WORKAROUND:
options->glsl.enable_row_major_load_workaround = value != 0;
break;
#endif
#if SPIRV_CROSS_C_API_HLSL
case SPVC_COMPILER_OPTION_HLSL_SHADER_MODEL:
options->hlsl.shader_model = value;
break;
case SPVC_COMPILER_OPTION_HLSL_POINT_SIZE_COMPAT:
options->hlsl.point_size_compat = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_POINT_COORD_COMPAT:
options->hlsl.point_coord_compat = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_SUPPORT_NONZERO_BASE_VERTEX_BASE_INSTANCE:
options->hlsl.support_nonzero_base_vertex_base_instance = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_FORCE_STORAGE_BUFFER_AS_UAV:
options->hlsl.force_storage_buffer_as_uav = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_NONWRITABLE_UAV_TEXTURE_AS_SRV:
options->hlsl.nonwritable_uav_texture_as_srv = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_ENABLE_16BIT_TYPES:
options->hlsl.enable_16bit_types = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_FLATTEN_MATRIX_VERTEX_INPUT_SEMANTICS:
options->hlsl.flatten_matrix_vertex_input_semantics = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_USE_ENTRY_POINT_NAME:
options->hlsl.use_entry_point_name = value != 0;
break;
case SPVC_COMPILER_OPTION_HLSL_PRESERVE_STRUCTURED_BUFFERS:
options->hlsl.preserve_structured_buffers = value != 0;
break;
#endif
#if SPIRV_CROSS_C_API_MSL
case SPVC_COMPILER_OPTION_MSL_VERSION:
options->msl.msl_version = value;
break;
case SPVC_COMPILER_OPTION_MSL_TEXEL_BUFFER_TEXTURE_WIDTH:
options->msl.texel_buffer_texture_width = value;
break;
case SPVC_COMPILER_OPTION_MSL_SWIZZLE_BUFFER_INDEX:
options->msl.swizzle_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_INDIRECT_PARAMS_BUFFER_INDEX:
options->msl.indirect_params_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_OUTPUT_BUFFER_INDEX:
options->msl.shader_output_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_PATCH_OUTPUT_BUFFER_INDEX:
options->msl.shader_patch_output_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_TESS_FACTOR_OUTPUT_BUFFER_INDEX:
options->msl.shader_tess_factor_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_INPUT_WORKGROUP_INDEX:
options->msl.shader_input_wg_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_POINT_SIZE_BUILTIN:
options->msl.enable_point_size_builtin = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_DISABLE_RASTERIZATION:
options->msl.disable_rasterization = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_CAPTURE_OUTPUT_TO_BUFFER:
options->msl.capture_output_to_buffer = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_SWIZZLE_TEXTURE_SAMPLES:
options->msl.swizzle_texture_samples = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_PAD_FRAGMENT_OUTPUT_COMPONENTS:
options->msl.pad_fragment_output_components = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_TESS_DOMAIN_ORIGIN_LOWER_LEFT:
options->msl.tess_domain_origin_lower_left = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_PLATFORM:
options->msl.platform = static_cast<CompilerMSL::Options::Platform>(value);
break;
case SPVC_COMPILER_OPTION_MSL_ARGUMENT_BUFFERS:
options->msl.argument_buffers = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_TEXTURE_BUFFER_NATIVE:
options->msl.texture_buffer_native = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_BUFFER_SIZE_BUFFER_INDEX:
options->msl.buffer_size_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_MULTIVIEW:
options->msl.multiview = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_VIEW_MASK_BUFFER_INDEX:
options->msl.view_mask_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_DEVICE_INDEX:
options->msl.device_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_VIEW_INDEX_FROM_DEVICE_INDEX:
options->msl.view_index_from_device_index = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_DISPATCH_BASE:
options->msl.dispatch_base = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_DYNAMIC_OFFSETS_BUFFER_INDEX:
options->msl.dynamic_offsets_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_TEXTURE_1D_AS_2D:
options->msl.texture_1D_as_2D = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_BASE_INDEX_ZERO:
options->msl.enable_base_index_zero = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_FRAMEBUFFER_FETCH_SUBPASS:
options->msl.use_framebuffer_fetch_subpasses = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_INVARIANT_FP_MATH:
options->msl.invariant_float_math = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_EMULATE_CUBEMAP_ARRAY:
options->msl.emulate_cube_array = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_DECORATION_BINDING:
options->msl.enable_decoration_binding = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_FORCE_ACTIVE_ARGUMENT_BUFFER_RESOURCES:
options->msl.force_active_argument_buffer_resources = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_FORCE_NATIVE_ARRAYS:
options->msl.force_native_arrays = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_FRAG_OUTPUT_MASK:
options->msl.enable_frag_output_mask = value;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_FRAG_DEPTH_BUILTIN:
options->msl.enable_frag_depth_builtin = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_FRAG_STENCIL_REF_BUILTIN:
options->msl.enable_frag_stencil_ref_builtin = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ENABLE_CLIP_DISTANCE_USER_VARYING:
options->msl.enable_clip_distance_user_varying = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_MULTI_PATCH_WORKGROUP:
options->msl.multi_patch_workgroup = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_INPUT_BUFFER_INDEX:
options->msl.shader_input_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_INDEX_BUFFER_INDEX:
options->msl.shader_index_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_VERTEX_FOR_TESSELLATION:
options->msl.vertex_for_tessellation = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_VERTEX_INDEX_TYPE:
options->msl.vertex_index_type = static_cast<CompilerMSL::Options::IndexType>(value);
break;
case SPVC_COMPILER_OPTION_MSL_MULTIVIEW_LAYERED_RENDERING:
options->msl.multiview_layered_rendering = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ARRAYED_SUBPASS_INPUT:
options->msl.arrayed_subpass_input = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_R32UI_LINEAR_TEXTURE_ALIGNMENT:
options->msl.r32ui_linear_texture_alignment = value;
break;
case SPVC_COMPILER_OPTION_MSL_R32UI_ALIGNMENT_CONSTANT_ID:
options->msl.r32ui_alignment_constant_id = value;
break;
case SPVC_COMPILER_OPTION_MSL_IOS_USE_SIMDGROUP_FUNCTIONS:
options->msl.ios_use_simdgroup_functions = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_EMULATE_SUBGROUPS:
options->msl.emulate_subgroups = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_FIXED_SUBGROUP_SIZE:
options->msl.fixed_subgroup_size = value;
break;
case SPVC_COMPILER_OPTION_MSL_FORCE_SAMPLE_RATE_SHADING:
options->msl.force_sample_rate_shading = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_IOS_SUPPORT_BASE_VERTEX_INSTANCE:
options->msl.ios_support_base_vertex_instance = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_RAW_BUFFER_TESE_INPUT:
options->msl.raw_buffer_tese_input = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_SHADER_PATCH_INPUT_BUFFER_INDEX:
options->msl.shader_patch_input_buffer_index = value;
break;
case SPVC_COMPILER_OPTION_MSL_MANUAL_HELPER_INVOCATION_UPDATES:
options->msl.manual_helper_invocation_updates = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_CHECK_DISCARDED_FRAG_STORES:
options->msl.check_discarded_frag_stores = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_ARGUMENT_BUFFERS_TIER:
options->msl.argument_buffers_tier = static_cast<CompilerMSL::Options::ArgumentBuffersTier>(value);
break;
case SPVC_COMPILER_OPTION_MSL_SAMPLE_DREF_LOD_ARRAY_AS_GRAD:
options->msl.sample_dref_lod_array_as_grad = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_READWRITE_TEXTURE_FENCES:
options->msl.readwrite_texture_fences = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_REPLACE_RECURSIVE_INPUTS:
options->msl.replace_recursive_inputs = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_AGX_MANUAL_CUBE_GRAD_FIXUP:
options->msl.agx_manual_cube_grad_fixup = value != 0;
break;
case SPVC_COMPILER_OPTION_MSL_FORCE_FRAGMENT_WITH_SIDE_EFFECTS_EXECUTION:
options->msl.force_fragment_with_side_effects_execution = value != 0;
break;
#endif
default:
options->context->report_error("Unknown option.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
return SPVC_SUCCESS;
}
spvc_result spvc_compiler_install_compiler_options(spvc_compiler compiler, spvc_compiler_options options)
{
(void)options;
switch (compiler->backend)
{
#if SPIRV_CROSS_C_API_GLSL
case SPVC_BACKEND_GLSL:
static_cast<CompilerGLSL &>(*compiler->compiler).set_common_options(options->glsl);
break;
#endif
#if SPIRV_CROSS_C_API_HLSL
case SPVC_BACKEND_HLSL:
static_cast<CompilerHLSL &>(*compiler->compiler).set_common_options(options->glsl);
static_cast<CompilerHLSL &>(*compiler->compiler).set_hlsl_options(options->hlsl);
break;
#endif
#if SPIRV_CROSS_C_API_MSL
case SPVC_BACKEND_MSL:
static_cast<CompilerMSL &>(*compiler->compiler).set_common_options(options->glsl);
static_cast<CompilerMSL &>(*compiler->compiler).set_msl_options(options->msl);
break;
#endif
default:
break;
}
return SPVC_SUCCESS;
}
spvc_result spvc_compiler_add_header_line(spvc_compiler compiler, const char *line)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend == SPVC_BACKEND_NONE)
{
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
static_cast<CompilerGLSL *>(compiler->compiler.get())->add_header_line(line);
return SPVC_SUCCESS;
#else
(void)line;
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_result spvc_compiler_require_extension(spvc_compiler compiler, const char *line)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend == SPVC_BACKEND_NONE)
{
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
static_cast<CompilerGLSL *>(compiler->compiler.get())->require_extension(line);
return SPVC_SUCCESS;
#else
(void)line;
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
size_t spvc_compiler_get_num_required_extensions(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend != SPVC_BACKEND_GLSL)
{
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
return static_cast<CompilerGLSL *>(compiler->compiler.get())->get_required_extensions().size();
#else
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return 0;
#endif
}
const char *spvc_compiler_get_required_extension(spvc_compiler compiler, size_t index)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend != SPVC_BACKEND_GLSL)
{
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return nullptr;
}
auto &exts = static_cast<CompilerGLSL *>(compiler->compiler.get())->get_required_extensions();
if (index < exts.size())
return exts[index].c_str();
else
return nullptr;
#else
(void)index;
compiler->context->report_error("Enabled extensions can only be queried on GLSL backend.");
return nullptr;
#endif
}
spvc_result spvc_compiler_flatten_buffer_block(spvc_compiler compiler, spvc_variable_id id)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend == SPVC_BACKEND_NONE)
{
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
static_cast<CompilerGLSL *>(compiler->compiler.get())->flatten_buffer_block(id);
return SPVC_SUCCESS;
#else
(void)id;
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_bool spvc_compiler_variable_is_depth_or_compare(spvc_compiler compiler, spvc_variable_id id)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend == SPVC_BACKEND_NONE)
{
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
return static_cast<CompilerGLSL *>(compiler->compiler.get())->variable_is_depth_or_compare(id) ? SPVC_TRUE : SPVC_FALSE;
#else
(void)id;
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_FALSE;
#endif
}
spvc_result spvc_compiler_mask_stage_output_by_location(spvc_compiler compiler,
unsigned location, unsigned component)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend == SPVC_BACKEND_NONE)
{
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
static_cast<CompilerGLSL *>(compiler->compiler.get())->mask_stage_output_by_location(location, component);
return SPVC_SUCCESS;
#else
(void)location;
(void)component;
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_result spvc_compiler_mask_stage_output_by_builtin(spvc_compiler compiler, SpvBuiltIn builtin)
{
#if SPIRV_CROSS_C_API_GLSL
if (compiler->backend == SPVC_BACKEND_NONE)
{
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
static_cast<CompilerGLSL *>(compiler->compiler.get())->mask_stage_output_by_builtin(spv::BuiltIn(builtin));
return SPVC_SUCCESS;
#else
(void)builtin;
compiler->context->report_error("Cross-compilation related option used on NONE backend which only supports reflection.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_result spvc_compiler_hlsl_set_root_constants_layout(spvc_compiler compiler,
const spvc_hlsl_root_constants *constant_info,
size_t count)
{
#if SPIRV_CROSS_C_API_HLSL
if (compiler->backend != SPVC_BACKEND_HLSL)
{
compiler->context->report_error("HLSL function used on a non-HLSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
auto &hlsl = *static_cast<CompilerHLSL *>(compiler->compiler.get());
vector<RootConstants> roots;
roots.reserve(count);
for (size_t i = 0; i < count; i++)
{
RootConstants root;
root.binding = constant_info[i].binding;
root.space = constant_info[i].space;
root.start = constant_info[i].start;
root.end = constant_info[i].end;
roots.push_back(root);
}
hlsl.set_root_constant_layouts(std::move(roots));
return SPVC_SUCCESS;
#else
(void)constant_info;
(void)count;
compiler->context->report_error("HLSL function used on a non-HLSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_result spvc_compiler_hlsl_add_vertex_attribute_remap(spvc_compiler compiler,
const spvc_hlsl_vertex_attribute_remap *remap,
size_t count)
{
#if SPIRV_CROSS_C_API_HLSL
if (compiler->backend != SPVC_BACKEND_HLSL)
{
compiler->context->report_error("HLSL function used on a non-HLSL backend.");