Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mtl_shader_defines.msl « metal « shaders « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3b32783620d0d0b69a7c4f66737ec8205f88bd1d (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** Special header for mapping commonly defined tokens to API-specific variations.
 * Where possible, this will adhere closely to base GLSL, where semantics are the same.
 * However, host code shader code may need modifying to support types where necessary variations
 * exist between APIs but are not expressed through the source. (e.g. distinctio between depth2d
 * and texture2d types in metal).
 */

/* Base instance with offsets. */
#define gpu_BaseInstance gl_BaseInstanceARB
#define gpu_InstanceIndex (gl_InstanceID + gpu_BaseInstance)

/* derivative signs. */
#define DFDX_SIGN 1.0
#define DFDY_SIGN 1.0

/* Type definitions. */
#define vec2 float2
#define vec3 float3
#define vec4 float4
#define mat2 float2x2
#define mat2x2 float2x2
#define mat3 float3x3
#define mat4 float4x4
#define ivec2 int2
#define ivec3 int3
#define ivec4 int4
#define uvec2 uint2
#define uvec3 uint3
#define uvec4 uint4
/* MTLBOOL is used for native boolean's generated by the Metal backend, to avoid type-emulation
 * for GLSL bools, which are treated as integers. */
#define MTLBOOL bool
#define bool int
#define bvec2 bool2
#define bvec3 bool3
#define bvec4 bool4
#define vec3_1010102_Unorm uint
#define vec3_1010102_Inorm int

/* Strip GLSL Decorators. */
#define in
#define flat
#define smooth
#define noperspective
#define layout(std140) struct
#define uniform

/* Used to replace 'out' in function parameters with threadlocal reference
 * shortened to avoid expanding the glsl source string. */
#define THD thread

/* Generate wrapper structs for combined texture and sampler type. */
#ifdef USE_ARGUMENT_BUFFER_FOR_SAMPLERS
#  define COMBINED_SAMPLER_TYPE(STRUCT_NAME, TEX_TYPE) \
    template<typename T, access A = access::sample> struct STRUCT_NAME { \
      thread TEX_TYPE<T, A> *texture; \
      constant sampler *samp; \
    }
#else
#  define COMBINED_SAMPLER_TYPE(STRUCT_NAME, TEX_TYPE) \
    template<typename T, access A = access::sample> struct STRUCT_NAME { \
      thread TEX_TYPE<T, A> *texture; \
      thread sampler *samp; \
    }
#endif

/* Add any types as needed. */
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_1d, texture1d);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_1d_array, texture1d_array);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_2d, texture2d);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_depth_2d, depth2d);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_2d_array, texture2d_array);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_depth_2d_array, depth2d_array);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_3d, texture3d);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_buffer, texture_buffer);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_cube, texturecube);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_cube_array, texturecube_array);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_depth_cube, texturecube_array);
COMBINED_SAMPLER_TYPE(_mtl_combined_image_sampler_depth_cube_array, texturecube_array);

/* Sampler struct for argument buffer. */
#ifdef USE_ARGUMENT_BUFFER_FOR_SAMPLERS
struct SStruct {
  array<sampler, ARGUMENT_BUFFER_NUM_SAMPLERS> sampler_args [[id(0)]];
};
#endif

/* Samplers as function parameters. */
#define sampler1D thread _mtl_combined_image_sampler_1d<float>
#define sampler1DArray thread _mtl_combined_image_sampler_1d_array<float>
#define sampler2D thread _mtl_combined_image_sampler_2d<float>
#define depth2D thread _mtl_combined_image_sampler_depth_2d<float>
#define sampler2DArray thread _mtl_combined_image_sampler_2d_array<float>
#define sampler2DArrayShadow thread _mtl_combined_image_sampler_depth_2d_array<float>
#define depth2DArrayShadow thread _mtl_combined_image_sampler_depth_2d_array<float>
#define sampler3D thread _mtl_combined_image_sampler_3d<float>
#define samplerBuffer thread _mtl_combined_image_sampler_buffer<float, access::read>
#define samplerCube thread _mtl_combined_image_sampler_cube<float>
#define samplerCubeArray thread _mtl_combined_image_sampler_cube_array<float>

#define usampler1D thread _mtl_combined_image_sampler_1d<uint>
#define usampler1DArray thread _mtl_combined_image_sampler_1d_array<uint>
#define usampler2D thread _mtl_combined_image_sampler_2d<uint>
#define udepth2D thread _mtl_combined_image_sampler_depth_2d<uint>
#define usampler2DArray thread _mtl_combined_image_sampler_2d_array<uint>
#define usampler2DArrayShadow thread _mtl_combined_image_sampler_depth_2d_array<uint>
#define udepth2DArrayShadow thread _mtl_combined_image_sampler_depth_2d_array<uint>
#define usampler3D thread _mtl_combined_image_sampler_3d<uint>
#define usamplerBuffer thread _mtl_combined_image_sampler_buffer<uint, access::read>
#define usamplerCube thread _mtl_combined_image_sampler_cube<uint>
#define usamplerCubeArray thread _mtl_combined_image_sampler_cube_array<uint>

#define isampler1D thread _mtl_combined_image_sampler_1d<int>
#define isampler1DArray thread _mtl_combined_image_sampler_1d_array<int>
#define isampler2D thread _mtl_combined_image_sampler_2d<int>
#define idepth2D thread _mtl_combined_image_sampler_depth_2d<int>
#define isampler2DArray thread _mtl_combined_image_sampler_2d_array<int>
#define isampler2DArrayShadow thread _mtl_combined_image_sampler_depth_2d_array<int>
#define idepth2DArrayShadow thread _mtl_combined_image_sampler_depth_2d_array<int>
#define isampler3D thread _mtl_combined_image_sampler_3d<int>
#define isamplerBuffer thread _mtl_combined_image_sampler_buffer<int, access::read>
#define isamplerCube thread _mtl_combined_image_sampler_cube<int>
#define isamplerCubeArray thread _mtl_combined_image_sampler_cube_array<int>

/* Vector accessor aliases. */
#define st xy

/* Texture functions. */
#define texelFetch _texelFetch_internal
#define texelFetchOffset(__tex, __texel, __lod, __offset) \
  _texelFetch_internal(__tex, __texel, __lod, __offset)
#define texture2(__tex, __uv) _texture_internal_samp(__tex, __uv)
#define texture3(__tex, __uv, _bias) _texture_internal_bias(__tex, __uv, bias(float(_bias)))
#define textureLod(__tex, __uv, __lod) _texture_internal_level(__tex, __uv, level(float(__lod)))
#define textureLodOffset(__tex, __uv, __lod, __offset) \
  _texture_internal_level(__tex, __uv, level(float(__lod)), __offset)
#define textureGather2(__tex, __uv) _texture_gather_internal(__tex, __uv, 0)
#define textureGather3(__tex, __uv, __comp) _texture_gather_internal(__tex, __uv, __comp)
#define textureGatherOffset(__tex, __offset, __uv, __comp) \
  _texture_gather_internal(__tex, __uv, __comp, __offset)

#define TEXURE_MACRO(_1, _2, _3, TEXNAME, ...) TEXNAME
#define texture(...) TEXURE_MACRO(__VA_ARGS__, texture3, texture2)(__VA_ARGS__)
#define textureGather(...) TEXURE_MACRO(__VA_ARGS__, textureGather3, textureGather2)(__VA_ARGS__)

/* Texture-write functions. */
#define imageStore(_tex, _coord, _value) _texture_write_internal(_tex, _coord, _value)

/* Singular return values from texture functions of type DEPTH are often indexed with either .r or
 * .x. This is a lightweight wrapper type for handling this syntax. */
union _msl_return_float {
  float r;
  float x;
  inline operator float() const
  {
    return r;
  }
};

/* Add custom texture sampling/reading routines for each type to account for special return cases,
 * e.g. returning a float with an r parameter Note: Cannot use template specialization for input
 * type, as return types are specific to the signature of 'tex'. */
/* Texture Read. */
template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_1d<S, A> tex, T texel)
{
  float w = tex.texture->get_width();
  if (texel >= 0 && texel < w) {
    return tex.texture->read(uint(texel));
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T>
inline vec<S, 4> _texelFetch_internal(
    const thread _mtl_combined_image_sampler_buffer<S, access::read> tex, T texel)
{
  float w = tex.texture->get_width();
  if (texel >= 0 && texel < w) {
    return tex.texture->read(uint(texel));
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_1d<S, A> tex,
                                      T texel,
                                      uint lod,
                                      T offset = 0)
{
  float w = tex.texture->get_width();
  if ((texel + offset) >= 0 && (texel + offset) < w) {
    /* LODs not supported for 1d textures. This must be zero. */
    return tex.texture->read(uint(texel + offset), 0);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_1d<S, A> tex,
                                      vec<T, 1> texel,
                                      uint lod,
                                      vec<T, 1> offset = 0)
{
  float w = tex.texture->get_width();
  if ((texel + offset) >= 0 && (texel + offset) < w) {
    /* LODs not supported for 1d textures. This must be zero. */
    return tex.texture->read(uint(texel + offset), 0);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, int n, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_1d<S, A> tex,
                                      vec<T, n> texel,
                                      uint lod,
                                      vec<T, n> offset = vec<T, n>(0))
{
  float w = tex.texture->get_width();
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w) {
    /* LODs not supported for 1d textures. This must be zero. */
    return tex.texture->read(uint(texel.x + offset.x), 0);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_1d_array<S, A> tex,
                                      vec<T, 2> texel,
                                      uint lod,
                                      vec<T, 2> offset = vec<T, 2>(0, 0))
{

  float w = tex.texture->get_width();
  float h = tex.texture->get_array_size();
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w && (texel.y + offset.y) >= 0 &&
      (texel.y + offset.y) < h) {
    /* LODs not supported for 1d textures. This must be zero. */
    return tex.texture->read(uint(texel.x + offset.x), uint(texel.y + offset.y), 0);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_2d<S, A> tex,
                                      vec<T, 2> texel,
                                      uint lod,
                                      vec<T, 2> offset = vec<T, 2>(0))
{

  float w = tex.texture->get_width() >> lod;
  float h = tex.texture->get_height() >> lod;
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w && (texel.y + offset.y) >= 0 &&
      (texel.y + offset.y) < h) {
    return tex.texture->read(uint2(texel + offset), lod);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_2d_array<S, A> tex,
                                      vec<T, 3> texel,
                                      uint lod,
                                      vec<T, 3> offset = vec<T, 3>(0))
{
  float w = tex.texture->get_width() >> lod;
  float h = tex.texture->get_height() >> lod;
  float d = tex.texture->get_array_size();
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w && (texel.y + offset.y) >= 0 &&
      (texel.y + offset.y) < h && (texel.z + offset.z) >= 0 && (texel.z + offset.z) < d) {
    return tex.texture->read(uint2(texel.xy + offset.xy), uint(texel.z + offset.z), lod);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texelFetch_internal(thread _mtl_combined_image_sampler_3d<S, A> tex,
                                      vec<T, 3> texel,
                                      uint lod,
                                      vec<T, 3> offset = vec<T, 3>(0))
{

  float w = tex.texture->get_width() >> lod;
  float h = tex.texture->get_height() >> lod;
  float d = tex.texture->get_depth() >> lod;
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w && (texel.y + offset.y) >= 0 &&
      (texel.y + offset.y) < h && (texel.z + offset.z) >= 0 && (texel.z + offset.z) < d) {
    return tex.texture->read(uint3(texel + offset), lod);
  }
  else {
    return vec<S, 4>(0);
  }
}

template<typename T, access A>
inline _msl_return_float _texelFetch_internal(
    thread _mtl_combined_image_sampler_depth_2d<float, A> tex,
    vec<T, 2> texel,
    uint lod,
    vec<T, 2> offset = vec<T, 2>(0))
{

  float w = tex.texture->get_width() >> lod;
  float h = tex.texture->get_height() >> lod;
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w && (texel.y + offset.y) >= 0 &&
      (texel.y + offset.y) < h) {
    _msl_return_float fl = {tex.texture->read(uint2(texel + offset), lod)};
    return fl;
  }
  else {
    _msl_return_float fl = {0};
    return fl;
  }
}

template<typename S, typename T, access A>
inline vec<S, 4> _texture_internal_samp(thread _mtl_combined_image_sampler_2d_array<S, A> tex,
                                        vec<T, 3> texel,
                                        uint lod,
                                        vec<T, 3> offset = vec<T, 3>(0))
{

  float w = tex.texture->get_width() >> lod;
  float h = tex.texture->get_height() >> lod;
  float d = tex.texture->get_array_size();
  if ((texel.x + offset.x) >= 0 && (texel.x + offset.x) < w && (texel.y + offset.y) >= 0 &&
      (texel.y + offset.y) < h && (texel.z + offset.z) >= 0 && (texel.z + offset.z) < d) {
    return tex.texture->read(uint2(texel.xy + offset.xy), uint(texel.z + offset.z), lod);
  }
  else {
    return vec<S, 4>(0);
  }
}

/* Sample. */
template<typename T>
inline vec<T, 4> _texture_internal_samp(
    thread _mtl_combined_image_sampler_1d<T, access::sample> tex, float u)
{
  return tex.texture->sample(*tex.samp, u);
}

inline float4 _texture_internal_samp(
    thread _mtl_combined_image_sampler_1d_array<float, access::sample> tex, float2 ua)
{
  return tex.texture->sample(*tex.samp, ua.x, uint(ua.y));
}

inline int4 _texture_internal_samp(thread _mtl_combined_image_sampler_2d<int, access::sample> tex,
                                   float2 uv)
{
  return tex.texture->sample(*tex.samp, uv);
}

inline uint4 _texture_internal_samp(
    thread _mtl_combined_image_sampler_2d<uint, access::sample> tex, float2 uv)
{
  return tex.texture->sample(*tex.samp, uv);
}

inline float4 _texture_internal_samp(
    thread _mtl_combined_image_sampler_2d<float, access::sample> tex, float2 uv)
{
  return tex.texture->sample(*tex.samp, uv);
}

inline _msl_return_float _texture_internal_samp(
    thread _mtl_combined_image_sampler_depth_2d<float, access::sample> tex, float2 uv)
{
  _msl_return_float fl = {tex.texture->sample(*tex.samp, uv)};
  return fl;
}

template<typename T>
inline vec<T, 4> _texture_internal_samp(
    thread _mtl_combined_image_sampler_3d<T, access::sample> tex, float3 uvw)
{
  return tex.texture->sample(*tex.samp, uvw);
}

template<typename T>
inline vec<T, 4> _texture_internal_samp(
    thread _mtl_combined_image_sampler_2d_array<T, access::sample> tex, float3 uva)
{
  return tex.texture->sample(*tex.samp, uva.xy, uint(uva.z));
}

inline _msl_return_float _texture_internal_samp(
    thread _mtl_combined_image_sampler_depth_2d_array<float, access::sample> tex, float3 uva)
{
  _msl_return_float fl = {tex.texture->sample(*tex.samp, uva.xy, uint(uva.z))};
  return fl;
}

inline _msl_return_float _texture_internal_samp(
    thread _mtl_combined_image_sampler_depth_2d_array<float, access::sample> tex, float4 uvac)
{
  _msl_return_float fl = {
      tex.texture->sample_compare(*tex.samp, uvac.xy, uint(uvac.z), uvac.w, level(0))};
  return fl;
}

template<typename T>
inline vec<T, 4> _texture_internal_samp(
    thread _mtl_combined_image_sampler_cube<T, access::sample> tex, float3 uvs)
{
  return tex.texture->sample(*tex.samp, uvs.xyz);
}

template<typename T>
inline vec<T, 4> _texture_internal_samp(
    thread _mtl_combined_image_sampler_cube_array<T, access::sample> tex, float4 coord_a)
{
  return tex.texture->sample(*tex.samp, coord_a.xyz, uint(coord_a.w));
}

/* Sample Level. */
template<typename T>
inline vec<T, 4> _texture_internal_level(
    thread _mtl_combined_image_sampler_1d<T, access::sample> tex,
    float u,
    level options,
    int offset = 0)
{
  /* LODs not supported for 1d textures. This must be zero. */
  return tex.texture->sample(*tex.samp, u);
}

inline float4 _texture_internal_level(
    thread _mtl_combined_image_sampler_1d_array<float, access::sample> tex,
    float2 ua,
    level options,
    int offset = 0)
{
  /* LODs not supported for 1d textures. This must be zero. */
  return tex.texture->sample(*tex.samp, ua.x, uint(ua.y));
}

inline int4 _texture_internal_level(thread _mtl_combined_image_sampler_2d<int, access::sample> tex,
                                    float2 uv,
                                    level options,
                                    int2 offset = int2(0))
{
  return tex.texture->sample(*tex.samp, uv, options, offset);
}

inline uint4 _texture_internal_level(
    thread _mtl_combined_image_sampler_2d<uint, access::sample> tex,
    float2 uv,
    level options,
    int2 offset = int2(0))
{
  return tex.texture->sample(*tex.samp, uv, options, offset);
}

inline float4 _texture_internal_level(
    thread _mtl_combined_image_sampler_2d<float, access::sample> tex,
    float2 uv,
    level options,
    int2 offset = int2(0))
{
  return tex.texture->sample(*tex.samp, uv, options, offset);
}

inline _msl_return_float _texture_internal_level(
    thread _mtl_combined_image_sampler_depth_2d<float, access::sample> tex,
    float2 uv,
    level options,
    int2 offset = int2(0))
{
  _msl_return_float fl = {tex.texture->sample(*tex.samp, uv, options, offset)};
  return fl;
}

template<typename T>
inline vec<T, 4> _texture_internal_level(
    thread _mtl_combined_image_sampler_3d<T, access::sample> tex,
    float3 uvw,
    level options = level(0),
    int3 offset = int3(0))
{
  return tex.texture->sample(*tex.samp, uvw, options, offset);
}

template<typename T>
inline vec<T, 4> _texture_internal_level(
    thread _mtl_combined_image_sampler_2d_array<T, access::sample> tex,
    float3 uva,
    level options = level(0),
    int2 offset = int2(0))
{
  return tex.texture->sample(*tex.samp, uva.xy, uint(uva.z), options, offset);
}

inline _msl_return_float _texture_internal_level(
    thread _mtl_combined_image_sampler_depth_2d_array<float, access::sample> tex,
    float3 uva,
    level options = level(0),
    int2 offset = int2(0))
{
  _msl_return_float fl = {tex.texture->sample(*tex.samp, uva.xy, uint(uva.z), options, offset)};
  return fl;
}

inline _msl_return_float _texture_internal_level(
    thread _mtl_combined_image_sampler_depth_2d_array<float, access::sample> tex,
    float4 uvac,
    level options = level(0),
    int2 offset = int2(0))
{
  _msl_return_float fl = {
      tex.texture->sample_compare(*tex.samp, uvac.xy, uint(uvac.z), uvac.w, level(0), offset)};
  return fl;
}

template<typename T>
inline vec<T, 4> _texture_internal_level(
    thread _mtl_combined_image_sampler_cube<T, access::sample> tex,
    float3 uvs,
    level options = level(0),
    int2 offset = int2(0))
{
  return tex.texture->sample(*tex.samp, uvs.xyz, options);
}

template<typename T>
inline vec<T, 4> _texture_internal_level(
    thread _mtl_combined_image_sampler_cube_array<T, access::sample> tex,
    float4 coord_a,
    level options = level(0),
    int3 offset = int3(0))
{
  return tex.texture->sample(*tex.samp, coord_a.xyz, uint(coord_a.w), options);
}

/* Sample Bias. */
template<typename T>
inline vec<T, 4> _texture_internal_bias(
    thread _mtl_combined_image_sampler_1d<T, access::sample> tex,
    float u,
    bias options = bias(0.0),
    int offset = 0)
{
  return tex.texture->sample(*tex.samp, u);
}

inline float4 _texture_internal_bias(
    thread _mtl_combined_image_sampler_2d<float, access::sample> tex,
    float2 uv,
    bias options = bias(0.0),
    int2 offset = int2(0))
{
  return tex.texture->sample(*tex.samp, uv, options, offset);
}

inline _msl_return_float _texture_internal_bias(
    thread _mtl_combined_image_sampler_depth_2d<float, access::sample> tex,
    float2 uv,
    bias options = bias(0),
    int2 offset = int2(0))
{
  _msl_return_float fl = {tex.texture->sample(*tex.samp, uv, options, offset)};
  return fl;
}

/* Texture Gather. */
component int_to_component(const int comp)
{
  switch (comp) {
    default:
    case 0:
      return component::x;
    case 1:
      return component::y;
    case 2:
      return component::z;
    case 3:
      return component::w;
  }
  return component::x;
}

inline float4 _texture_gather_internal(
    thread _mtl_combined_image_sampler_depth_2d<float, access::sample> tex,
    float2 uv,
    const int comp = 0,
    int2 offset = int2(0))
{
  return tex.texture->gather(*tex.samp, uv, offset);
}

template<typename T>
inline vec<T, 4> _texture_gather_internal(
    thread _mtl_combined_image_sampler_2d<T, access::sample> tex,
    float2 uv,
    const int comp = 0,
    int2 offset = int2(0))
{
  return tex.texture->gather(*tex.samp, uv, offset);
}

template<typename T>
inline vec<T, 4> _texture_gather_internal(
    thread _mtl_combined_image_sampler_2d_array<T, access::sample> tex,
    float2 uv,
    const int comp = 0,
    int2 offset = int2(0))
{
  return tex.texture->gather(*tex.samp, uv, offset);
}

/* Texture write support. */
template<typename S, typename T, access A>
inline void _texture_write_internal(thread _mtl_combined_image_sampler_2d<S, A> tex,
                                    T _coord,
                                    vec<S, 4> value)
{
  float w = tex.texture->get_width();
  float h = tex.texture->get_height();
  if (_coord.x >= 0 && _coord.x < w && _coord.y >= 0 && _coord.y < h) {
    tex.texture->write(value, uint2(_coord.xy));
  }
}

template<typename S, typename T, access A>
inline void _texture_write_internal(thread _mtl_combined_image_sampler_3d<S, A> tex,
                                    T _coord,
                                    vec<S, 4> value)
{
  float w = tex.texture->get_width();
  float h = tex.texture->get_height();
  float d = tex.texture->get_depth();
  if (_coord.x >= 0 && _coord.x < w && _coord.y >= 0 && _coord.y < h && _coord.z >= 0 &&
      _coord.z < d) {
    tex.texture->write(value, uint3(_coord.xyz));
  }
}

/* SSBO Vertex Fetch Mode. */
#ifdef MTL_SSBO_VERTEX_FETCH
/* Enabled when geometry is passed via raw buffer bindings, rather than using
 * vertex assembly in the vertex-descriptor.
 *
 *  To describe the layout of input attribute data, we will generate uniforms (defaulting to 0)
 * with the names per unique input attribute with name `attr`:
 *
 * - uniform_ssbo_stride_##attr  -- Representing the stride between element.
 * - uniform_ssbo_offset_##attr  -- Representing the base offset within the vertex.
 * - uniform_ssbo_fetchmode_##attr - Whether using per-vertex (=0) or per-instance fetch (=1).
 * - uniform_ssbo_vbo_id_##attr - buffer binding index for VBO with data for this attribute.
 * - uniform_ssbo_type_##attr - The type of data in the currently bound buffer.
 *
 * If the uniform_ssbo_type_* does not match with the desired type, then it is the responsibility
 * of the shader to perform the conversion. Types should always be read as the raw attribute type,
 * and then converted. e.g. If the uniform_ssbo_type_* is `int`, but we want to read it to be
 * normalized to a float.
 * The implementation should query the attribute type using vertex_fetch_get_attr_type(attr_name):
 *
 * float fweight = 0.0;
 * if(vertex_fetch_get_attr_type(in_weight) == GPU_SHADER_ATTR_TYPE_INT) {
 *   int iweight = vertex_fetch_attribute(gl_VertexID, in_weight, int);
 *   fweight = (float)iweight/(float)INT32_MAX;
 * } else {
 *   fweight = = vertex_fetch_attribute(gl_VertexID, in_weight, float);
 * }
 *
 * Note: These uniforms are generated as part of the same data block used for regular uniforms
 * and attribute data is written prior to each draw call, depending on the configuration of
 * the vertex descriptor for an MTLBatch or MTLImmedaite call. */
#  define PPCAT_NX(A, B) A##B
#  define PPCAT(A, B) PPCAT_NX(A, B)

#  define RESOLVE_VERTEX(v_id) \
    ((UNIFORM_SSBO_USES_INDEXED_RENDERING_STR > 0) ? \
         ((UNIFORM_SSBO_INDEX_MODE_U16_STR > 0) ? MTL_INDEX_DATA_U16[v_id] : \
                                                  MTL_INDEX_DATA_U32[v_id]) : \
         v_id)
#  define ATTR_TYPE(attr) PPCAT(SSBO_ATTR_TYPE_, attr)
#  define vertex_fetch_attribute_raw(n, attr, type) \
    (reinterpret_cast<constant type *>( \
        &MTL_VERTEX_DATA[PPCAT(UNIFORM_SSBO_VBO_ID_STR, attr)] \
                        [(PPCAT(UNIFORM_SSBO_STRIDE_STR, attr) * \
                          ((PPCAT(UNIFORM_SSBO_FETCHMODE_STR, attr)) ? gl_InstanceID : n)) + \
                         PPCAT(UNIFORM_SSBO_OFFSET_STR, attr)]))[0]
#  define vertex_fetch_attribute(n, attr, type) \
    vertex_fetch_attribute_raw(RESOLVE_VERTEX(n), attr, type)
#  define vertex_id_from_index_id(n) RESOLVE_VERTEX(n)
#  define vertex_fetch_get_input_prim_type() UNIFORM_SSBO_INPUT_PRIM_TYPE_STR
#  define vertex_fetch_get_input_vert_count() UNIFORM_SSBO_INPUT_VERT_COUNT_STR
#  define vertex_fetch_get_attr_type(attr) PPCAT(UNIFORM_SSBO_TYPE_STR, attr)

/* Must mirror GPU_primitive.h. */
#  define GPU_PRIM_POINTS 0
#  define GPU_PRIM_LINES 1
#  define GPU_PRIM_TRIS 2
#  define GPU_PRIM_LINE_STRIP 3
#  define GPU_PRIM_LINE_LOOP 4
#  define GPU_PRIM_TRI_STRIP 5
#  define GPU_PRIM_TRI_FAN 6
#  define GPU_PRIM_LINES_ADJ 7
#  define GPU_PRIM_TRIS_ADJ 8
#  define GPU_PRIM_LINE_STRIP_ADJ 9
#endif

/* Common Functions. */
#define dFdx(x) dfdx(x)
#define dFdy(x) dfdy(x)
#define mod(x, y) _mtlmod(x, y)
#define discard discard_fragment()
#define inversesqrt rsqrt

inline float radians(float deg)
{
  /* Constant factor: M_PI_F/180.0. */
  return deg * 0.01745329251f;
}

inline float degrees(float rad)
{
  /* Constant factor: 180.0/M_PI_F. */
  return rad * 57.2957795131;
}

#define select(A, B, C) mix(A, B, C)

/* Type conversions and type truncations. */
inline float4 to_float4(float3 val)
{
  return float4(val, 1.0);
}

/* Type conversions and type truncations (Utility Functions). */
inline float3x3 mat4_to_mat3(float4x4 matrix)
{
  return float3x3(matrix[0].xyz, matrix[1].xyz, matrix[2].xyz);
}

inline int floatBitsToInt(float f)
{
  return as_type<int>(f);
}

inline int2 floatBitsToInt(float2 f)
{
  return as_type<int2>(f);
}

inline int3 floatBitsToInt(float3 f)
{
  return as_type<int3>(f);
}

inline int4 floatBitsToInt(float4 f)
{
  return as_type<int4>(f);
}

inline uint floatBitsToUint(float f)
{
  return as_type<uint>(f);
}

inline uint2 floatBitsToUint(float2 f)
{
  return as_type<uint2>(f);
}

inline uint3 floatBitsToUint(float3 f)
{
  return as_type<uint3>(f);
}

inline uint4 floatBitsToUint(float4 f)
{
  return as_type<uint4>(f);
}

inline float intBitsToFloat(int f)
{
  return as_type<float>(f);
}

inline float2 intBitsToFloat(int2 f)
{
  return as_type<float2>(f);
}

inline float3 intBitsToFloat(int3 f)
{
  return as_type<float3>(f);
}

inline float4 intBitsToFloat(int4 f)
{
  return as_type<float4>(f);
}

/* Texture size functions. Add texture types as needed. */
template<typename T, access A>
int textureSize(thread _mtl_combined_image_sampler_1d<T, A> image, uint lod)
{
  return int(image.texture->get_width());
}

template<typename T, access A>
int2 textureSize(thread _mtl_combined_image_sampler_1d_array<T, A> image, uint lod)
{
  return int2(image.texture->get_width(), image.texture->get_array_size());
}

template<typename T, access A>
int2 textureSize(thread _mtl_combined_image_sampler_2d<T, A> image, uint lod)
{
  return int2(image.texture->get_width(lod), image.texture->get_height(lod));
}

template<typename T, access A>
int2 textureSize(thread _mtl_combined_image_sampler_depth_2d<T, A> image, uint lod)
{
  return int2(image.texture->get_width(lod), image.texture->get_height(lod));
}

template<typename T, access A>
int3 textureSize(thread _mtl_combined_image_sampler_2d_array<T, A> image, uint lod)
{
  return int3(image.texture->get_width(lod),
              image.texture->get_height(lod),
              image.texture->get_array_size());
}

template<typename T, access A>
int3 textureSize(thread _mtl_combined_image_sampler_depth_2d_array<T, A> image, uint lod)
{
  return int3(image.texture->get_width(lod),
              image.texture->get_height(lod),
              image.texture->get_array_size());
}

template<typename T, access A>
int2 textureSize(thread _mtl_combined_image_sampler_cube<T, A> image, uint lod)
{
  return int2(image.texture->get_width(lod), image.texture->get_height(lod));
}

template<typename T, access A>
int3 textureSize(thread _mtl_combined_image_sampler_3d<T, A> image, uint lod)
{
  return int3(image.texture->get_width(lod),
              image.texture->get_height(lod),
              image.texture->get_depth(lod));
}

/* Equality and comparison functions. */
#define lessThan(a, b) ((a) < (b))
#define lessThanEqual(a, b) ((a) <= (b))
#define greaterThan(a, b) ((a) > (b))
#define greaterThanEqual(a, b) ((a) >= (b))
#define equal(a, b) ((a) == (b))
#define notEqual(a, b) ((a) != (b))

template<typename T, int n> bool all(vec<T, n> x)
{
  bool _all = true;
  for (int i = 0; i < n; i++) {
    _all = _all && (x[i] > 0);
  }
  return _all;
}

template<typename T, int n> bool any(vec<T, n> x)
{
  bool _any = false;
  for (int i = 0; i < n; i++) {
    _any = _any || (x[i] > 0);
  }
  return _any;
}

/* Modulo functionality. */
int _mtlmod(int a, int b)
{
  return a - b * (a / b);
}

template<typename T, int n> vec<T, n> _mtlmod(vec<T, n> x, vec<T, n> y)
{
  return x - y * floor(x / y);
}

template<typename T, int n, typename U> vec<T, n> _mtlmod(vec<T, n> x, U y)
{
  return x - vec<T, n>(y) * floor(x / vec<T, n>(y));
}

template<typename T, typename U, int n> vec<U, n> _mtlmod(T x, vec<U, n> y)
{
  return vec<U, n>(x) - y * floor(vec<U, n>(x) / y);
}

/* Mathematical functions. */
template<typename T> T atan(T y, T x)
{
  return atan2(y, x);
}

/* Matrix Inverse. */
float4x4 inverse(float4x4 a)
{
  float b00 = a[0][0] * a[1][1] - a[0][1] * a[1][0];
  float b01 = a[0][0] * a[1][2] - a[0][2] * a[1][0];
  float b02 = a[0][0] * a[1][3] - a[0][3] * a[1][0];
  float b03 = a[0][1] * a[1][2] - a[0][2] * a[1][1];
  float b04 = a[0][1] * a[1][3] - a[0][3] * a[1][1];
  float b05 = a[0][2] * a[1][3] - a[0][3] * a[1][2];
  float b06 = a[2][0] * a[3][1] - a[2][1] * a[3][0];
  float b07 = a[2][0] * a[3][2] - a[2][2] * a[3][0];
  float b08 = a[2][0] * a[3][3] - a[2][3] * a[3][0];
  float b09 = a[2][1] * a[3][2] - a[2][2] * a[3][1];
  float b10 = a[2][1] * a[3][3] - a[2][3] * a[3][1];
  float b11 = a[2][2] * a[3][3] - a[2][3] * a[3][2];

  float invdet = 1.0 / (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06);

  return float4x4(a[1][1] * b11 - a[1][2] * b10 + a[1][3] * b09,
                  a[0][2] * b10 - a[0][1] * b11 - a[0][3] * b09,
                  a[3][1] * b05 - a[3][2] * b04 + a[3][3] * b03,
                  a[2][2] * b04 - a[2][1] * b05 - a[2][3] * b03,
                  a[1][2] * b08 - a[1][0] * b11 - a[1][3] * b07,
                  a[0][0] * b11 - a[0][2] * b08 + a[0][3] * b07,
                  a[3][2] * b02 - a[3][0] * b05 - a[3][3] * b01,
                  a[2][0] * b05 - a[2][2] * b02 + a[2][3] * b01,
                  a[1][0] * b10 - a[1][1] * b08 + a[1][3] * b06,
                  a[0][1] * b08 - a[0][0] * b10 - a[0][3] * b06,
                  a[3][0] * b04 - a[3][1] * b02 + a[3][3] * b00,
                  a[2][1] * b02 - a[2][0] * b04 - a[2][3] * b00,
                  a[1][1] * b07 - a[1][0] * b09 - a[1][2] * b06,
                  a[0][0] * b09 - a[0][1] * b07 + a[0][2] * b06,
                  a[3][1] * b01 - a[3][0] * b03 - a[3][2] * b00,
                  a[2][0] * b03 - a[2][1] * b01 + a[2][2] * b00) *
         invdet;
}

float3x3 inverse(float3x3 m)
{

  float invdet = 1.0 / (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) -
                        m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2]) +
                        m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]));

  float3x3 inverse(0);
  inverse[0][0] = +(m[1][1] * m[2][2] - m[2][1] * m[1][2]);
  inverse[1][0] = -(m[1][0] * m[2][2] - m[2][0] * m[1][2]);
  inverse[2][0] = +(m[1][0] * m[2][1] - m[2][0] * m[1][1]);
  inverse[0][1] = -(m[0][1] * m[2][2] - m[2][1] * m[0][2]);
  inverse[1][1] = +(m[0][0] * m[2][2] - m[2][0] * m[0][2]);
  inverse[2][1] = -(m[0][0] * m[2][1] - m[2][0] * m[0][1]);
  inverse[0][2] = +(m[0][1] * m[1][2] - m[1][1] * m[0][2]);
  inverse[1][2] = -(m[0][0] * m[1][2] - m[1][0] * m[0][2]);
  inverse[2][2] = +(m[0][0] * m[1][1] - m[1][0] * m[0][1]);
  inverse = inverse * invdet;

  return inverse;
}

/* Additional overloads for builtin functions. */
float distance(float x, float y)
{
  return abs(y - x);
}

/* Overload for mix(A, B, float ratio). */
template<typename T, int Size> vec<T, Size> mix(vec<T, Size> a, vec<T, Size> b, float val)
{
  return mix(a, b, vec<T, Size>(val));
}

/* Overload for mix(A, B, bvec<N>). */
template<typename T, int Size>
vec<T, Size> mix(vec<T, Size> a, vec<T, Size> b, vec<int, Size> mask)
{
  vec<T, Size> result;
  for (int i = 0; i < Size; i++) {
    result[i] = mask[i] ? b[i] : a[i];
  }
  return result;
}

/* Using vec<bool, S> does not appear to work, splitting cases. */
/* Overload for mix(A, B, bvec<N>). */
template<typename T> vec<T, 4> mix(vec<T, 4> a, vec<T, 4> b, bvec4 mask)
{
  vec<T, 4> result;
  for (int i = 0; i < 4; i++) {
    result[i] = mask[i] ? b[i] : a[i];
  }
  return result;
}

/* Overload for mix(A, B, bvec<N>). */
template<typename T> vec<T, 3> mix(vec<T, 3> a, vec<T, 3> b, bvec3 mask)
{
  vec<T, 3> result;
  for (int i = 0; i < 3; i++) {
    result[i] = mask[i] ? b[i] : a[i];
  }
  return result;
}

/* Overload for mix(A, B, bvec<N>). */
template<typename T> vec<T, 2> mix(vec<T, 2> a, vec<T, 2> b, bvec2 mask)
{
  vec<T, 2> result;
  for (int i = 0; i < 2; i++) {
    result[i] = mask[i] ? b[i] : a[i];
  }
  return result;
}

/* Overload for mix(A, B, bvec<N>). */
template<typename T> T mix(T a, T b, MTLBOOL mask)
{
  return (mask) ? b : a;
}

template<typename T, unsigned int Size> bool is_zero(vec<T, Size> a)
{
  for (int i = 0; i < Size; i++) {
    if (a[i] != T(0)) {
      return false;
    }
  }
  return true;
}

/* Matrix conversion fallback. */
mat3 MAT3(vec3 a, vec3 b, vec3 c)
{
  return mat3(a, b, c);
}
mat3 MAT3(float f)
{
  return mat3(f);
}
mat3 MAT3(mat4 m)
{
  return mat4_to_mat3(m);
}