mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-22 07:21:04 +01:00
Make the parser code more closely follow the spec's grammar around
`unary_expression`, `postfix_expression`, and `singular_expression`.
Change the handling of postfix expressions (indexing, member/component access,
and swizzling) to apply the indirection at the appropriate time, resulting in
code improvements on all output formats. For example, where we used to generate
the following MSL:
metal::float4 _e13 = bar.matrix[3];
float b = _e13.x;
we now generate, simply:
float b = bar.matrix[3].x;
Propagate WGSL reference types correctly, so that parenthesizing expressions no
longer causes the Load Rule to be applied.
Together with #1332 (already landed), this is a replacement for #1312, and
unblocks #1352.
Fixes #1351.
67 lines
2.4 KiB
HLSL
67 lines
2.4 KiB
HLSL
static const float3 c_ambient = float3(0.05000000074505806, 0.05000000074505806, 0.05000000074505806);
|
|
static const uint c_max_lights = 10;
|
|
|
|
struct Globals {
|
|
uint4 num_lights;
|
|
};
|
|
|
|
struct Light {
|
|
row_major float4x4 proj;
|
|
float4 pos;
|
|
float4 color;
|
|
};
|
|
|
|
cbuffer u_globals : register(b0) { Globals u_globals; }
|
|
ByteAddressBuffer s_lights : register(t1);
|
|
Texture2DArray<float> t_shadow : register(t2);
|
|
SamplerComparisonState sampler_shadow : register(s3);
|
|
|
|
struct FragmentInput_fs_main {
|
|
float3 raw_normal1 : LOC0;
|
|
float4 position1 : LOC1;
|
|
};
|
|
|
|
float fetch_shadow(uint light_id, float4 homogeneous_coords)
|
|
{
|
|
if ((homogeneous_coords.w <= 0.0)) {
|
|
return 1.0;
|
|
}
|
|
float2 flip_correction = float2(0.5, -0.5);
|
|
float2 light_local = (((homogeneous_coords.xy * flip_correction) / float2(homogeneous_coords.w.xx)) + float2(0.5, 0.5));
|
|
float _expr26 = t_shadow.SampleCmpLevelZero(sampler_shadow, float3(light_local, int(light_id)), (homogeneous_coords.z / homogeneous_coords.w));
|
|
return _expr26;
|
|
}
|
|
|
|
float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
|
|
{
|
|
float3 raw_normal = fragmentinput_fs_main.raw_normal1;
|
|
float4 position = fragmentinput_fs_main.position1;
|
|
float3 color = float3(0.05000000074505806, 0.05000000074505806, 0.05000000074505806);
|
|
uint i = 0u;
|
|
|
|
float3 normal = normalize(raw_normal);
|
|
bool loop_init = true;
|
|
while(true) {
|
|
if (!loop_init) {
|
|
uint _expr40 = i;
|
|
i = (_expr40 + 1u);
|
|
}
|
|
loop_init = false;
|
|
uint _expr12 = i;
|
|
uint _expr15 = u_globals.num_lights.x;
|
|
if ((_expr12 >= min(_expr15, c_max_lights))) {
|
|
break;
|
|
}
|
|
uint _expr19 = i;
|
|
Light light = {float4x4(asfloat(s_lights.Load4(_expr19*96+0+0+0)), asfloat(s_lights.Load4(_expr19*96+0+0+16)), asfloat(s_lights.Load4(_expr19*96+0+0+32)), asfloat(s_lights.Load4(_expr19*96+0+0+48))), asfloat(s_lights.Load4(_expr19*96+0+64)), asfloat(s_lights.Load4(_expr19*96+0+80))};
|
|
uint _expr22 = i;
|
|
const float _e25 = fetch_shadow(_expr22, mul(position, light.proj));
|
|
float3 light_dir = normalize((light.pos.xyz - position.xyz));
|
|
float diffuse = max(0.0, dot(normal, light_dir));
|
|
float3 _expr34 = color;
|
|
color = (_expr34 + ((_e25 * diffuse) * light.color.xyz));
|
|
}
|
|
float3 _expr43 = color;
|
|
return float4(_expr43, 1.0);
|
|
}
|