forked from KhronosGroup/SPIRV-Cross
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spirv_cross_c.cpp
2131 lines (1860 loc) · 66.6 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 Hans-Kristian Arntzen
*
* 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_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<uint32_t> 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> separate_images;
SmallVector<spvc_reflected_resource> separate_samplers;
SmallVector<spvc_reflected_resource> acceleration_structures;
bool copy_resources(SmallVector<spvc_reflected_resource> &outputs, const SmallVector<Resource> &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 = 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(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(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(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(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(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(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_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;
#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;
#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;
#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
}
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_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.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
HLSLVertexAttributeRemap re;
auto &hlsl = *static_cast<CompilerHLSL *>(compiler->compiler.get());
for (size_t i = 0; i < count; i++)
{
re.location = remap[i].location;
re.semantic = remap[i].semantic;
hlsl.add_vertex_attribute_remap(re);
}
return SPVC_SUCCESS;
#else
(void)remap;
(void)count;
compiler->context->report_error("HLSL function used on a non-HLSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_variable_id spvc_compiler_hlsl_remap_num_workgroups_builtin(spvc_compiler compiler)
{
#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 0;
}
auto &hlsl = *static_cast<CompilerHLSL *>(compiler->compiler.get());
return hlsl.remap_num_workgroups_builtin();
#else
compiler->context->report_error("HLSL function used on a non-HLSL backend.");
return 0;
#endif
}
spvc_bool spvc_compiler_msl_is_rasterization_disabled(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.get_is_rasterization_disabled() ? SPVC_TRUE : SPVC_FALSE;
#else
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_bool spvc_compiler_msl_needs_swizzle_buffer(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.needs_swizzle_buffer() ? SPVC_TRUE : SPVC_FALSE;
#else
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_bool spvc_compiler_msl_needs_buffer_size_buffer(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.needs_buffer_size_buffer() ? SPVC_TRUE : SPVC_FALSE;
#else
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_bool spvc_compiler_msl_needs_aux_buffer(spvc_compiler compiler)
{
return spvc_compiler_msl_needs_swizzle_buffer(compiler);
}
spvc_bool spvc_compiler_msl_needs_output_buffer(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.needs_output_buffer() ? SPVC_TRUE : SPVC_FALSE;
#else
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_bool spvc_compiler_msl_needs_patch_output_buffer(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.needs_patch_output_buffer() ? SPVC_TRUE : SPVC_FALSE;
#else
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_bool spvc_compiler_msl_needs_input_threadgroup_mem(spvc_compiler compiler)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.needs_input_threadgroup_mem() ? SPVC_TRUE : SPVC_FALSE;
#else
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_result spvc_compiler_msl_add_vertex_attribute(spvc_compiler compiler, const spvc_msl_vertex_attribute *va)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
MSLVertexAttr attr;
attr.location = va->location;
attr.msl_buffer = va->msl_buffer;
attr.msl_offset = va->msl_offset;
attr.msl_stride = va->msl_stride;
attr.format = static_cast<MSLVertexFormat>(va->format);
attr.builtin = static_cast<spv::BuiltIn>(va->builtin);
attr.per_instance = va->per_instance != 0;
msl.add_msl_vertex_attribute(attr);
return SPVC_SUCCESS;
#else
(void)va;
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_result spvc_compiler_msl_add_resource_binding(spvc_compiler compiler,
const spvc_msl_resource_binding *binding)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
MSLResourceBinding bind;
bind.binding = binding->binding;
bind.desc_set = binding->desc_set;
bind.stage = static_cast<spv::ExecutionModel>(binding->stage);
bind.msl_buffer = binding->msl_buffer;
bind.msl_texture = binding->msl_texture;
bind.msl_sampler = binding->msl_sampler;
msl.add_msl_resource_binding(bind);
return SPVC_SUCCESS;
#else
(void)binding;
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_result spvc_compiler_msl_add_discrete_descriptor_set(spvc_compiler compiler, unsigned desc_set)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
msl.add_discrete_descriptor_set(desc_set);
return SPVC_SUCCESS;
#else
(void)desc_set;
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_ERROR_INVALID_ARGUMENT;
#endif
}
spvc_bool spvc_compiler_msl_is_vertex_attribute_used(spvc_compiler compiler, unsigned location)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.is_msl_vertex_attribute_used(location) ? SPVC_TRUE : SPVC_FALSE;
#else
(void)location;
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
spvc_bool spvc_compiler_msl_is_resource_used(spvc_compiler compiler, SpvExecutionModel model, unsigned set,
unsigned binding)
{
#if SPIRV_CROSS_C_API_MSL
if (compiler->backend != SPVC_BACKEND_MSL)
{
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
}
auto &msl = *static_cast<CompilerMSL *>(compiler->compiler.get());
return msl.is_msl_resource_binding_used(static_cast<spv::ExecutionModel>(model), set, binding) ? SPVC_TRUE :
SPVC_FALSE;
#else
(void)model;
(void)set;
(void)binding;
compiler->context->report_error("MSL function used on a non-MSL backend.");
return SPVC_FALSE;
#endif
}
#if SPIRV_CROSS_C_API_MSL
static void spvc_convert_msl_sampler(MSLConstexprSampler &samp, const spvc_msl_constexpr_sampler *sampler)
{
samp.s_address = static_cast<MSLSamplerAddress>(sampler->s_address);
samp.t_address = static_cast<MSLSamplerAddress>(sampler->t_address);
samp.r_address = static_cast<MSLSamplerAddress>(sampler->r_address);
samp.lod_clamp_min = sampler->lod_clamp_min;
samp.lod_clamp_max = sampler->lod_clamp_max;
samp.lod_clamp_enable = sampler->lod_clamp_enable != 0;
samp.min_filter = static_cast<MSLSamplerFilter>(sampler->min_filter);
samp.mag_filter = static_cast<MSLSamplerFilter>(sampler->mag_filter);
samp.mip_filter = static_cast<MSLSamplerMipFilter>(sampler->mip_filter);
samp.compare_enable = sampler->compare_enable != 0;
samp.anisotropy_enable = sampler->anisotropy_enable != 0;
samp.max_anisotropy = sampler->max_anisotropy;
samp.compare_func = static_cast<MSLSamplerCompareFunc>(sampler->compare_func);
samp.coord = static_cast<MSLSamplerCoord>(sampler->coord);
samp.border_color = static_cast<MSLSamplerBorderColor>(sampler->border_color);
}
static void spvc_convert_msl_sampler_ycbcr_conversion(MSLConstexprSampler &samp, const spvc_msl_sampler_ycbcr_conversion *conv)
{
samp.ycbcr_conversion_enable = conv != nullptr;
if (conv == nullptr) return;
samp.planes = conv->planes;
samp.resolution = static_cast<MSLFormatResolution>(conv->resolution);
samp.chroma_filter = static_cast<MSLSamplerFilter>(conv->chroma_filter);
samp.x_chroma_offset = static_cast<MSLChromaLocation>(conv->x_chroma_offset);
samp.y_chroma_offset = static_cast<MSLChromaLocation>(conv->y_chroma_offset);
for (int i = 0; i < 4; i++)
samp.swizzle[i] = static_cast<MSLComponentSwizzle>(conv->swizzle[i]);
samp.ycbcr_model = static_cast<MSLSamplerYCbCrModelConversion>(conv->ycbcr_model);
samp.ycbcr_range = static_cast<MSLSamplerYCbCrRange>(conv->ycbcr_range);
samp.bpc = conv->bpc;
}
#endif