2019-08-14 15:09:39 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wmissing-braces"
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-variable"
|
|
|
|
|
2017-02-04 09:07:20 +00:00
|
|
|
#include <metal_stdlib>
|
|
|
|
#include <simd/simd.h>
|
2019-08-14 15:09:39 +00:00
|
|
|
|
|
|
|
template <typename T, size_t Num>
|
|
|
|
struct unsafe_array
|
|
|
|
{
|
|
|
|
T __Elements[Num ? Num : 1];
|
|
|
|
|
|
|
|
constexpr size_t size() const thread { return Num; }
|
|
|
|
constexpr size_t max_size() const thread { return Num; }
|
|
|
|
constexpr bool empty() const thread { return Num == 0; }
|
|
|
|
|
|
|
|
constexpr size_t size() const device { return Num; }
|
|
|
|
constexpr size_t max_size() const device { return Num; }
|
|
|
|
constexpr bool empty() const device { return Num == 0; }
|
|
|
|
|
|
|
|
constexpr size_t size() const constant { return Num; }
|
|
|
|
constexpr size_t max_size() const constant { return Num; }
|
|
|
|
constexpr bool empty() const constant { return Num == 0; }
|
|
|
|
|
|
|
|
constexpr size_t size() const threadgroup { return Num; }
|
|
|
|
constexpr size_t max_size() const threadgroup { return Num; }
|
|
|
|
constexpr bool empty() const threadgroup { return Num == 0; }
|
|
|
|
|
|
|
|
thread T &operator[](size_t pos) thread
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
constexpr const thread T &operator[](size_t pos) const thread
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
device T &operator[](size_t pos) device
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
constexpr const device T &operator[](size_t pos) const device
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr const constant T &operator[](size_t pos) const constant
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
threadgroup T &operator[](size_t pos) threadgroup
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
constexpr const threadgroup T &operator[](size_t pos) const threadgroup
|
|
|
|
{
|
|
|
|
return __Elements[pos];
|
|
|
|
}
|
|
|
|
};
|
2017-02-04 09:07:20 +00:00
|
|
|
|
|
|
|
using namespace metal;
|
|
|
|
|
|
|
|
struct Light
|
|
|
|
{
|
2017-11-06 02:34:42 +00:00
|
|
|
packed_float3 Position;
|
2017-02-04 09:07:20 +00:00
|
|
|
float Radius;
|
|
|
|
float4 Color;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UBO
|
|
|
|
{
|
|
|
|
float4x4 uMVP;
|
2019-08-14 15:09:39 +00:00
|
|
|
unsafe_array<Light,4> lights;
|
2017-02-04 09:07:20 +00:00
|
|
|
};
|
|
|
|
|
2018-03-05 15:34:42 +00:00
|
|
|
struct Light_1
|
|
|
|
{
|
|
|
|
float3 Position;
|
|
|
|
float Radius;
|
|
|
|
float4 Color;
|
|
|
|
};
|
|
|
|
|
2017-02-04 09:07:20 +00:00
|
|
|
struct main0_out
|
|
|
|
{
|
|
|
|
float4 vColor [[user(locn0)]];
|
|
|
|
float4 gl_Position [[position]];
|
|
|
|
};
|
|
|
|
|
2018-06-12 15:41:35 +00:00
|
|
|
struct main0_in
|
|
|
|
{
|
|
|
|
float4 aVertex [[attribute(0)]];
|
|
|
|
float3 aNormal [[attribute(1)]];
|
|
|
|
};
|
|
|
|
|
2017-02-04 09:07:20 +00:00
|
|
|
vertex main0_out main0(main0_in in [[stage_in]], constant UBO& _21 [[buffer(0)]])
|
|
|
|
{
|
|
|
|
main0_out out = {};
|
|
|
|
out.gl_Position = _21.uMVP * in.aVertex;
|
|
|
|
out.vColor = float4(0.0);
|
2019-06-06 09:10:39 +00:00
|
|
|
Light_1 light;
|
2017-02-04 09:07:20 +00:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2019-01-17 09:06:23 +00:00
|
|
|
light.Position = float3(_21.lights[i].Position);
|
2017-02-04 09:07:20 +00:00
|
|
|
light.Radius = _21.lights[i].Radius;
|
|
|
|
light.Color = _21.lights[i].Color;
|
|
|
|
float3 L = in.aVertex.xyz - light.Position;
|
MSL: Emit F{Min,Max,Clamp} as fast:: and N{Min,Max,Clamp} as precise::.
This roughly matches their semantics in SPIR-V and MSL. For `FMin`,
`FMax`, and `FClamp`, and the Metal functions `fast::min()`,
`fast::max()`, and `fast::clamp()`, the result is undefined if any
operand is NaN. For the 'N' operations and their corresponding MSL
`precise::` functions, the result is consistent with IEEE 754 (first
non-NaN wins; result is NaN if all operands are NaN).
We can only do this with 32-bit floats, though, because Metal only
provides these variants for `float`. `half` only has one variant of
these functions that is presumably consistent with IEEE 754. I guess
that's OK; the SPIR-V spec only says that `F{Min,Max,Clamp}` are
undefined for NaNs. Performance might suffer, though.
2018-08-31 23:15:07 +00:00
|
|
|
out.vColor += ((_21.lights[i].Color * fast::clamp(1.0 - (length(L) / light.Radius), 0.0, 1.0)) * dot(in.aNormal, normalize(L)));
|
2017-02-04 09:07:20 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|