mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 22:11:01 +01:00
81 lines
2.6 KiB
Plaintext
81 lines
2.6 KiB
Plaintext
#include <metal_stdlib>
|
|
#include <simd/simd.h>
|
|
|
|
struct _mslBufferSizes {
|
|
metal::uint size1;
|
|
};
|
|
|
|
constexpr constant unsigned c_max_lights = 10u;
|
|
struct Globals {
|
|
metal::uint4 num_lights;
|
|
};
|
|
struct Light {
|
|
metal::float4x4 proj;
|
|
metal::float4 pos;
|
|
metal::float4 color;
|
|
};
|
|
typedef Light type3[1];
|
|
struct Lights {
|
|
type3 data;
|
|
};
|
|
constant metal::float3 c_ambient = {0.05, 0.05, 0.05};
|
|
|
|
float fetch_shadow(
|
|
metal::uint light_id,
|
|
metal::float4 homogeneous_coords,
|
|
metal::depth2d_array<float, metal::access::sample> t_shadow,
|
|
metal::sampler sampler_shadow
|
|
) {
|
|
if (homogeneous_coords.w <= 0.0) {
|
|
return 1.0;
|
|
}
|
|
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);
|
|
return _e26;
|
|
}
|
|
|
|
struct fs_mainInput {
|
|
metal::float3 raw_normal [[user(loc0), center_perspective]];
|
|
metal::float4 position [[user(loc1), center_perspective]];
|
|
};
|
|
struct fs_mainOutput {
|
|
metal::float4 member [[color(0)]];
|
|
};
|
|
fragment fs_mainOutput fs_main(
|
|
fs_mainInput varyings [[stage_in]]
|
|
, 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)]]
|
|
) {
|
|
const auto raw_normal = varyings.raw_normal;
|
|
const auto position = varyings.position;
|
|
metal::float3 color = c_ambient;
|
|
metal::uint i = 0u;
|
|
metal::float3 normal = metal::normalize(raw_normal);
|
|
bool loop_init = true;
|
|
while(true) {
|
|
if (!loop_init) {
|
|
metal::uint _e40 = i;
|
|
i = _e40 + 1u;
|
|
}
|
|
loop_init = false;
|
|
metal::uint _e12 = i;
|
|
metal::uint4 _e14 = u_globals.num_lights;
|
|
if (_e12 >= metal::min(_e14.x, c_max_lights)) {
|
|
break;
|
|
}
|
|
metal::uint _e19 = i;
|
|
Light light = s_lights.data[_e19];
|
|
metal::uint _e22 = i;
|
|
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));
|
|
metal::float3 _e34 = color;
|
|
color = _e34 + ((_e25 * diffuse) * light.color.xyz);
|
|
}
|
|
metal::float3 _e43 = color;
|
|
return fs_mainOutput { metal::float4(_e43, 1.0) };
|
|
}
|