2021-01-30 05:11:23 +00:00
|
|
|
#include <metal_stdlib>
|
|
|
|
#include <simd/simd.h>
|
|
|
|
|
2021-03-26 04:21:17 +00:00
|
|
|
constexpr constant unsigned c_max_lights = 10u;
|
2021-01-30 05:11:23 +00:00
|
|
|
struct Globals {
|
2021-04-08 15:00:11 +00:00
|
|
|
metal::uint4 num_lights;
|
2021-01-30 05:11:23 +00:00
|
|
|
};
|
|
|
|
struct Light {
|
2021-04-08 15:00:11 +00:00
|
|
|
metal::float4x4 proj;
|
|
|
|
metal::float4 pos;
|
|
|
|
metal::float4 color;
|
2021-01-30 05:11:23 +00:00
|
|
|
};
|
|
|
|
typedef Light type3[1];
|
|
|
|
struct Lights {
|
|
|
|
type3 data;
|
|
|
|
};
|
2021-04-11 18:21:47 +00:00
|
|
|
constant metal::float3 c_ambient = {0.05, 0.05, 0.05};
|
2021-04-08 15:00:11 +00:00
|
|
|
|
|
|
|
float fetch_shadow(
|
|
|
|
metal::uint light_id,
|
|
|
|
metal::float4 homogeneous_coords,
|
|
|
|
metal::depth2d_array<float, metal::access::sample> t_shadow,
|
|
|
|
metal::sampler sampler_shadow
|
2021-01-30 05:11:23 +00:00
|
|
|
) {
|
2021-04-08 16:29:26 +00:00
|
|
|
if (homogeneous_coords.w <= 0.0) {
|
|
|
|
return 1.0;
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
2021-04-14 05:23:38 +00:00
|
|
|
float _e28 = t_shadow.sample_compare(sampler_shadow, ((metal::float2(homogeneous_coords.x, homogeneous_coords.y) * metal::float2(0.5, -0.5)) / homogeneous_coords.w) + metal::float2(0.5, 0.5), static_cast<int>(light_id), homogeneous_coords.z / homogeneous_coords.w);
|
2021-04-09 03:59:47 +00:00
|
|
|
return _e28;
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct fs_mainInput {
|
2021-04-19 05:11:00 +00:00
|
|
|
metal::float3 raw_normal [[user(loc0), center_perspective]];
|
|
|
|
metal::float4 position [[user(loc1), center_perspective]];
|
2021-01-30 05:11:23 +00:00
|
|
|
};
|
|
|
|
struct fs_mainOutput {
|
2021-04-08 15:00:11 +00:00
|
|
|
metal::float4 member [[color(0)]];
|
2021-01-30 05:11:23 +00:00
|
|
|
};
|
|
|
|
fragment fs_mainOutput fs_main(
|
2021-03-07 05:03:05 +00:00
|
|
|
fs_mainInput varyings [[stage_in]]
|
2021-04-20 14:01:32 +00:00
|
|
|
, constant Globals& u_globals [[user(fake0)]]
|
|
|
|
, constant Lights& s_lights [[user(fake0)]]
|
|
|
|
, metal::depth2d_array<float, metal::access::sample> t_shadow [[user(fake0)]]
|
|
|
|
, metal::sampler sampler_shadow [[user(fake0)]]
|
2021-01-30 05:11:23 +00:00
|
|
|
) {
|
2021-03-07 05:03:05 +00:00
|
|
|
const auto raw_normal = varyings.raw_normal;
|
|
|
|
const auto position = varyings.position;
|
2021-04-08 15:00:11 +00:00
|
|
|
metal::float3 color1 = c_ambient;
|
2021-04-08 16:29:26 +00:00
|
|
|
metal::uint i = 0u;
|
2021-01-30 05:11:23 +00:00
|
|
|
bool loop_init = true;
|
|
|
|
while(true) {
|
|
|
|
if (!loop_init) {
|
2021-04-08 16:29:26 +00:00
|
|
|
i = i + 1u;
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
|
|
|
loop_init = false;
|
2021-03-26 04:14:01 +00:00
|
|
|
if (i >= metal::min(u_globals.num_lights.x, c_max_lights)) {
|
2021-01-30 05:11:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-09 03:59:47 +00:00
|
|
|
Light _e21 = s_lights.data[i];
|
|
|
|
float _e25 = fetch_shadow(i, _e21.proj * position, t_shadow, sampler_shadow);
|
|
|
|
color1 = color1 + ((_e25 * metal::max(0.0, metal::dot(metal::normalize(raw_normal), metal::normalize(metal::float3(_e21.pos.x, _e21.pos.y, _e21.pos.z) - metal::float3(position.x, position.y, position.z))))) * metal::float3(_e21.color.x, _e21.color.y, _e21.color.z));
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
2021-04-08 16:29:26 +00:00
|
|
|
return fs_mainOutput { metal::float4(color1, 1.0) };
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|