| 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 |
static inline __attribute__((always_inline)) |
| 79 |
float4 sample_in_func(thread const array<texture2d<float>, 4> uSampler, thread const array<sampler, 4> uSamplerSmplr, constant uint* uSamplerSwzl, thread float2& vUV) |
| 80 |
{ |
| 81 |
return spvTextureSwizzle(uSampler[2].sample(uSamplerSmplr[2], vUV), uSamplerSwzl[2]); |
| 82 |
} |
| 83 |
|
| 84 |
static inline __attribute__((always_inline)) |
| 85 |
float4 sample_single_in_func(thread const texture2d<float> s, thread const sampler sSmplr, constant uint& sSwzl, thread float2& vUV) |
| 86 |
{ |
| 87 |
return spvTextureSwizzle(s.sample(sSmplr, vUV), sSwzl); |
| 88 |
} |
| 89 |
|
| 90 |
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)]]) |
| 91 |
{ |
| 92 |
main0_out out = {}; |
| 93 |
constant uint* uSamplerSwzl = &spvSwizzleConstants[0]; |
| 94 |
out.FragColor = sample_in_func(uSampler, uSamplerSmplr, uSamplerSwzl, in.vUV); |
| 95 |
out.FragColor += sample_single_in_func(uSampler[1], uSamplerSmplr[1], uSamplerSwzl[1], in.vUV); |
| 96 |
return out; |
| 97 |
} |
| 98 |
|