Debian Codegraph @

third_party/spirv-cross/reference/opt/shaders-msl/frag/array-of-texture-swizzle.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 main0_out
9 {
10 float4 FragColor [[color(0)]];
11 };
12
13 struct main0_in
14 {
15 float2 vUV [[user(locn0)]];
16 };
17
18 template<typename T> struct spvRemoveReference { typedef T type; };
19 template<typename T> struct spvRemoveReference<thread T&> { typedef T type; };
20 template<typename T> struct spvRemoveReference<thread T&&> { typedef T type; };
21 template<typename T> inline constexpr thread T&& spvForward(thread typename spvRemoveReference<T>::type& x)
22 {
23 return static_cast<thread T&&>(x);
24 }
25 template<typename T> inline constexpr thread T&& spvForward(thread typename spvRemoveReference<T>::type&& x)
26 {
27 return static_cast<thread T&&>(x);
28 }
29
30 enum class spvSwizzle : uint
31 {
32 none = 0,
33 zero,
34 one,
35 red,
36 green,
37 blue,
38 alpha
39 };
40
41 template<typename T>
42 inline T spvGetSwizzle(vec<T, 4> x, T c, spvSwizzle s)
43 {
44 switch (s)
45 {
46 case spvSwizzle::none:
47 return c;
48 case spvSwizzle::zero:
49 return 0;
50 case spvSwizzle::one:
51 return 1;
52 case spvSwizzle::red:
53 return x.r;
54 case spvSwizzle::green:
55 return x.g;
56 case spvSwizzle::blue:
57 return x.b;
58 case spvSwizzle::alpha:
59 return x.a;
60 }
61 }
62
63 // Wrapper function that swizzles texture samples and fetches.
64 template<typename T>
65 inline vec<T, 4> spvTextureSwizzle(vec<T, 4> x, uint s)
66 {
67 if (!s)
68 return x;
69 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)));
70 }
71
72 template<typename T>
73 inline T spvTextureSwizzle(T x, uint s)
74 {
75 return spvTextureSwizzle(vec<T, 4>(x, 0, 0, 1), s).x;
76 }
77
78 fragment main0_out main0(main0_in in [[stage_in]], constant uint* spvSwizzleConstants [[buffer(30)]], array<texture2d<float>, 4> uSampler [[texture(0)]], array<sampler, 4> uSamplerSmplr [[sampler(0)]])
79 {
80 main0_out out = {};
81 constant uint* uSamplerSwzl = &spvSwizzleConstants[0];
82 out.FragColor = spvTextureSwizzle(uSampler[2].sample(uSamplerSmplr[2], in.vUV), uSamplerSwzl[2]);
83 out.FragColor += spvTextureSwizzle(uSampler[1].sample(uSamplerSmplr[1], in.vUV), uSamplerSwzl[1]);
84 return out;
85 }
86