2021-07-17 03:56:15 +00:00
|
|
|
// language: metal1.1
|
2021-01-30 05:11:23 +00:00
|
|
|
#include <metal_stdlib>
|
|
|
|
#include <simd/simd.h>
|
|
|
|
|
2021-05-01 02:40:32 +00:00
|
|
|
struct _mslBufferSizes {
|
|
|
|
metal::uint size1;
|
|
|
|
};
|
|
|
|
|
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,
|
2021-05-13 05:26:08 +00:00
|
|
|
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-06-08 16:09:46 +00:00
|
|
|
metal::float2 flip_correction = metal::float2(0.5, -0.5);
|
|
|
|
metal::float2 light_local = ((homogeneous_coords.xy * flip_correction) / metal::float2(homogeneous_coords.w)) + metal::float2(0.5, 0.5);
|
|
|
|
float _e26 = t_shadow.sample_compare(sampler_shadow, light_local, static_cast<int>(light_id), homogeneous_coords.z / homogeneous_coords.w);
|
2021-04-21 02:03:54 +00:00
|
|
|
return _e26;
|
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-06-18 00:21:42 +00:00
|
|
|
metal::float3 color = c_ambient;
|
2021-04-08 16:29:26 +00:00
|
|
|
metal::uint i = 0u;
|
2021-06-08 16:09:46 +00:00
|
|
|
metal::float3 normal = metal::normalize(raw_normal);
|
2021-01-30 05:11:23 +00:00
|
|
|
bool loop_init = true;
|
|
|
|
while(true) {
|
|
|
|
if (!loop_init) {
|
2021-06-08 14:15:26 +00:00
|
|
|
metal::uint _e40 = i;
|
|
|
|
i = _e40 + 1u;
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
|
|
|
loop_init = false;
|
2021-06-08 14:15:26 +00:00
|
|
|
metal::uint _e12 = i;
|
|
|
|
metal::uint4 _e14 = u_globals.num_lights;
|
|
|
|
if (_e12 >= metal::min(_e14.x, c_max_lights)) {
|
2021-01-30 05:11:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-06-08 14:15:26 +00:00
|
|
|
metal::uint _e19 = i;
|
2021-06-08 16:09:46 +00:00
|
|
|
Light light = s_lights.data[_e19];
|
2021-06-08 14:15:26 +00:00
|
|
|
metal::uint _e22 = i;
|
2021-06-08 16:09:46 +00:00
|
|
|
float _e25 = fetch_shadow(_e22, light.proj * position, t_shadow, sampler_shadow);
|
|
|
|
metal::float3 light_dir = metal::normalize(light.pos.xyz - position.xyz);
|
|
|
|
float diffuse = metal::max(0.0, metal::dot(normal, light_dir));
|
2021-06-18 00:21:42 +00:00
|
|
|
metal::float3 _e34 = color;
|
|
|
|
color = _e34 + ((_e25 * diffuse) * light.color.xyz);
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|
2021-06-18 00:21:42 +00:00
|
|
|
metal::float3 _e43 = color;
|
2021-06-08 14:15:26 +00:00
|
|
|
return fs_mainOutput { metal::float4(_e43, 1.0) };
|
2021-01-30 05:11:23 +00:00
|
|
|
}
|