Debian Codegraph @

third_party/spirv-cross/reference/shaders-msl/frag/array-of-texture-swizzle-nonconstant-uniform.msl2.swizzle.frag

raw
1 #pragma clang diagnostic ignored "-Wmissing-prototypes"
2
3 #include <metal_stdlib>
4 #include <simd/simd.h>
5
6 using namespace metal;
7
8 struct UBO
9 {
10 uint index;
11 };
12
13 struct UBO2
14 {
15 uint index2;
16 };
17
18 struct main0_out
19 {
20 float4 FragColor [[color(0)]];
21 };
22
23 struct main0_in
24 {
25 float2 vUV [[user(locn0)]];
26 };
27
28 template<typename T> struct spvRemoveReference { typedef T type; };
29 template<typename T> struct spvRemoveReference<thread T&> { typedef T type; };
30 template<typename T> struct spvRemoveReference<thread T&&> { typedef T type; };
31 template<typename T> inline constexpr thread T&& spvForward(thread typename spvRemoveReference<T>::type& x)
32 {
33 return static_cast<thread T&&>(x);
34 }
35 template<typename T> inline constexpr thread T&& spvForward(thread typename spvRemoveReference<T>::type&& x)
36 {
37 return static_cast<thread T&&>(x);
38 }
39
40 enum class spvSwizzle : uint
41 {
42 none = 0,
43 zero,
44 one,
45 red,
46 green,
47 blue,
48 alpha
49 };
50
51 template<typename T>
52 inline T spvGetSwizzle(vec<T, 4> x, T c, spvSwizzle s)
53 {
54 switch (s)
55 {
56 case spvSwizzle::none:
57 return c;
58 case spvSwizzle::zero:
59 return 0;
60 case spvSwizzle::one:
61 return 1;
62 case spvSwizzle::red:
63 return x.r;
64 case spvSwizzle::green:
65 return x.g;
66 case spvSwizzle::blue:
67 return x.b;
68 case spvSwizzle::alpha:
69 return x.a;
70 }
71 }
72
73 // Wrapper function that swizzles texture samples and fetches.
74 template<typename T>
75 inline vec<T, 4> spvTextureSwizzle(vec<T, 4> x, uint s)
76 {
77 if (!s)
78 return x;
79 return vec<T, 4>(spvGetSwizzle(x, x.r, spvSwizzle((s >> 0) & 0xFF)), spvGetSwizzle(x, x.g, spvSwizzle((s >> 8) & 0xFF)), spvGetSwizzle(x, x.b, spvSwizzle((s >> 16) & 0xFF)), spvGetSwizzle(x, x.a, spvSwizzle((s >> 24) & 0xFF)));
80 }
81
82 template<typename T>
83 inline T spvTextureSwizzle(T x, uint s)
84 {
85 return spvTextureSwizzle(vec<T, 4>(x, 0, 0, 1), s).x;
86 }
87
88 static inline __attribute__((always_inline))
89 float4 sample_in_func(thread const array<texture2d<float>, 4> uSampler, thread const array<sampler, 4> uSamplerSmplr, constant uint* uSamplerSwzl, constant UBO& uUBO, thread float2& vUV)
90 {
91 return spvTextureSwizzle(uSampler[uUBO.index].sample(uSamplerSmplr[uUBO.index], vUV), uSamplerSwzl[uUBO.index]);
92 }
93
94 static inline __attribute__((always_inline))
95 float4 sample_single_in_func(thread const texture2d<float> s, thread const sampler sSmplr, constant uint& sSwzl, thread float2& vUV)
96 {
97 return spvTextureSwizzle(s.sample(sSmplr, vUV), sSwzl);
98 }
99
100 fragment main0_out main0(main0_in in [[stage_in]], constant uint* spvSwizzleConstants [[buffer(30)]], constant UBO& uUBO [[buffer(0)]], constant UBO2& _50 [[buffer(1)]], array<texture2d<float>, 4> uSampler [[texture(0)]], array<sampler, 4> uSamplerSmplr [[sampler(0)]])
101 {
102 main0_out out = {};
103 constant uint* uSamplerSwzl = &spvSwizzleConstants[0];
104 out.FragColor = sample_in_func(uSampler, uSamplerSmplr, uSamplerSwzl, uUBO, in.vUV);
105 out.FragColor += sample_single_in_func(uSampler[_50.index2], uSamplerSmplr[_50.index2], uSamplerSwzl[_50.index2], in.vUV);
106 return out;
107 }
108