wgpu/tests/out/msl/shadow.msl

82 lines
2.6 KiB
Plaintext
Raw Normal View History

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>
struct _mslBufferSizes {
metal::uint size1;
};
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;
};
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
}
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 {
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(
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)]]
2021-01-30 05:11:23 +00:00
) {
const auto raw_normal = varyings.raw_normal;
const auto position = varyings.position;
metal::float3 color = c_ambient;
2021-04-08 16:29:26 +00:00
metal::uint i = 0u;
metal::float3 normal = metal::normalize(raw_normal);
2021-01-30 05:11:23 +00:00
bool loop_init = true;
while(true) {
if (!loop_init) {
metal::uint _e40 = i;
i = _e40 + 1u;
2021-01-30 05:11:23 +00:00
}
loop_init = false;
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;
}
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);
2021-01-30 05:11:23 +00:00
}
metal::float3 _e43 = color;
return fs_mainOutput { metal::float4(_e43, 1.0) };
2021-01-30 05:11:23 +00:00
}