Skip to content

Commit

Permalink
wgsl PBR fixes (#12)
Browse files Browse the repository at this point in the history
bevy_pbr2: Fix light uniforms
  • Loading branch information
superdump authored and cart committed Jul 24, 2021
1 parent 7792b29 commit 3f70f92
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pipelined/bevy_pbr2/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pipelined/bevy_pbr2/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl FromWorld for PbrShaders {
let render_device = world.get_resource::<RenderDevice>().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 {
Expand Down
20 changes: 11 additions & 9 deletions pipelined/bevy_pbr2/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ struct Vertex {
};

struct VertexOutput {
[[builtin(position)]] world_position: vec4<f32>;
[[location(0)]] world_normal: vec3<f32>;
[[location(1)]] uv: vec2<f32>;
[[builtin(position)]] clip_position: vec4<f32>;
[[location(0)]] world_position: vec4<f32>;
[[location(1)]] world_normal: vec3<f32>;
[[location(2)]] uv: vec2<f32>;
};

[[stage(vertex)]]
Expand All @@ -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<f32>(mesh.transform.x.xyz, mesh.transform.y.xyz, mesh.transform.z.xyz) * vertex.normal;
Expand Down Expand Up @@ -95,11 +97,11 @@ struct OmniLight {

[[block]]
struct Lights {
ambient_color: vec4<f32>;
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<OmniLight, 10>;
ambient_color: vec4<f32>;
num_lights: u32;
};

let FLAGS_BASE_COLOR_TEXTURE_BIT: u32 = 1u;
Expand Down Expand Up @@ -359,9 +361,9 @@ fn fetch_shadow(light_id: i32, homogeneous_coords: vec4<f32>) -> f32 {

struct FragmentInput {
[[builtin(front_facing)]] is_front: bool;
[[builtin(position)]] world_position: vec4<f32>;
[[location(0)]] world_normal: vec3<f32>;
[[location(1)]] uv: vec2<f32>;
[[location(0)]] world_position: vec4<f32>;
[[location(1)]] world_normal: vec3<f32>;
[[location(2)]] uv: vec2<f32>;
};

[[stage(fragment)]]
Expand Down

0 comments on commit 3f70f92

Please sign in to comment.