From 4138305491a4fcc7f287d283babc33d8acf5715a Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Wed, 30 Jun 2021 01:56:45 +0200 Subject: [PATCH] wgsl PBR fixes (#12) bevy_pbr2: Fix light uniforms --- pipelined/bevy_pbr2/src/render/light.rs | 3 ++- pipelined/bevy_pbr2/src/render/mod.rs | 1 - pipelined/bevy_pbr2/src/render/pbr.wgsl | 20 +++++++++++--------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pipelined/bevy_pbr2/src/render/light.rs b/pipelined/bevy_pbr2/src/render/light.rs index d9d785504b883..e9d678d4f9ac8 100644 --- a/pipelined/bevy_pbr2/src/render/light.rs +++ b/pipelined/bevy_pbr2/src/render/light.rs @@ -43,9 +43,10 @@ pub struct GpuLight { #[repr(C)] #[derive(Copy, Clone, Debug, AsStd140)] pub struct GpuLights { + // TODO: this comes first to work around a WGSL alignment issue. We need to solve this issue before releasing the renderer rework + lights: [GpuLight; MAX_OMNI_LIGHTS], ambient_color: Vec4, len: u32, - lights: [GpuLight; MAX_OMNI_LIGHTS], } // NOTE: this must be kept in sync MAX_OMNI_LIGHTS in pbr.frag diff --git a/pipelined/bevy_pbr2/src/render/mod.rs b/pipelined/bevy_pbr2/src/render/mod.rs index 9b7229981dc11..86066d94bed6b 100644 --- a/pipelined/bevy_pbr2/src/render/mod.rs +++ b/pipelined/bevy_pbr2/src/render/mod.rs @@ -42,7 +42,6 @@ impl FromWorld for PbrShaders { let render_device = world.get_resource::().unwrap(); let shader = Shader::from_wgsl(include_str!("pbr.wgsl")); let shader_module = render_device.create_shader_module(&shader); - println!("{}", GpuLights::std140_size_static()); // TODO: move this into ViewMeta? let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor { diff --git a/pipelined/bevy_pbr2/src/render/pbr.wgsl b/pipelined/bevy_pbr2/src/render/pbr.wgsl index 9eb3e9bf971c8..ac76008202837 100644 --- a/pipelined/bevy_pbr2/src/render/pbr.wgsl +++ b/pipelined/bevy_pbr2/src/render/pbr.wgsl @@ -22,9 +22,10 @@ struct Vertex { }; struct VertexOutput { - [[builtin(position)]] world_position: vec4; - [[location(0)]] world_normal: vec3; - [[location(1)]] uv: vec2; + [[builtin(position)]] clip_position: vec4; + [[location(0)]] world_position: vec4; + [[location(1)]] world_normal: vec3; + [[location(2)]] uv: vec2; }; [[stage(vertex)]] @@ -33,7 +34,8 @@ fn vertex(vertex: Vertex) -> VertexOutput { var out: VertexOutput; out.uv = vertex.uv; - out.world_position = view.view_proj * world_position; + out.world_position = world_position; + out.clip_position = view.view_proj * world_position; // FIXME: The inverse transpose of the model matrix should be used to correctly handle scaling // of normals out.world_normal = mat3x3(mesh.transform.x.xyz, mesh.transform.y.xyz, mesh.transform.z.xyz) * vertex.normal; @@ -95,11 +97,11 @@ struct OmniLight { [[block]] struct Lights { - ambient_color: vec4; - num_lights: u32; // NOTE: this array size must be kept in sync with the constants defined bevy_pbr2/src/render/light.rs // TODO: this can be removed if we move to storage buffers for light arrays omni_lights: array; + ambient_color: vec4; + num_lights: u32; }; let FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u; @@ -359,9 +361,9 @@ fn fetch_shadow(light_id: i32, homogeneous_coords: vec4) -> f32 { struct FragmentInput { [[builtin(front_facing)]] is_front: bool; - [[builtin(position)]] world_position: vec4; - [[location(0)]] world_normal: vec3; - [[location(1)]] uv: vec2; + [[location(0)]] world_position: vec4; + [[location(1)]] world_normal: vec3; + [[location(2)]] uv: vec2; }; [[stage(fragment)]]