Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the camera's view matrix to shaders via the Camera uniform #1203

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/shaders/hot.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ layout(location = 0) in vec3 Vertex_Position;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};

layout(set = 1, binding = 0) uniform Transform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ layout(location = 0) out vec4 o_Target;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};

layout(set = 1, binding = 0) uniform Lights {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ layout(location = 2) out vec2 v_Uv;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};

layout(set = 2, binding = 0) uniform Transform {
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_render/src/render_graph/nodes/camera_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn camera_node_system(
render_resource_context.map_buffer(staging_buffer);
staging_buffer
} else {
let size = std::mem::size_of::<[[f32; 4]; 4]>();
let size = std::mem::size_of::<[[[f32; 4]; 4]; 2]>();
let buffer = render_resource_context.create_buffer(BufferInfo {
size,
buffer_usage: BufferUsage::COPY_DST | BufferUsage::UNIFORM,
Expand All @@ -114,9 +114,12 @@ pub fn camera_node_system(
staging_buffer
};

let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();
let camera_matrix: [f32; 16] =
(camera.projection_matrix * global_transform.compute_matrix().inverse()).to_cols_array();
let matrix_size = std::mem::size_of::<[[[f32; 4]; 4]; 2]>();
let view_matrix = global_transform.compute_matrix().inverse();
let camera_matrix: [[f32; 16]; 2] = [
(camera.projection_matrix * view_matrix).to_cols_array(),
view_matrix.to_cols_array()
];

render_resource_context.write_mapped_buffer(
staging_buffer,
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_render/src/render_graph/nodes/pass_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ impl<Q: WorldQuery> PassNode<Q> {
index: 0,
bind_type: BindType::Uniform {
dynamic: false,
property: UniformProperty::Struct(vec![UniformProperty::Mat4]),
property: UniformProperty::Struct(vec![
UniformProperty::Mat4, // View Projection
UniformProperty::Mat4, // View
]),
},
shader_stage: BindingShaderStage::VERTEX | BindingShaderStage::FRAGMENT,
}],
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_render/src/shader/shader_reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ mod tests {
layout(location = 0) out vec4 v_Position;
layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};
layout(set = 1, binding = 0) uniform texture2D Texture;

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_sprite/src/render/sprite.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};

layout(set = 2, binding = 0) uniform Transform {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_sprite/src/render/sprite_sheet.vert
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ layout(location = 1) out vec4 v_Color;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};

// TODO: merge dimensions into "sprites" buffer when that is supported in the Uniforms derive abstraction
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_ui/src/render/ui.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ layout(location = 0) out vec2 v_Uv;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};

layout(set = 1, binding = 0) uniform Transform {
Expand Down
1 change: 1 addition & 0 deletions examples/shader/array_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ layout(location = 0) out vec4 v_Position;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};
layout(set = 1, binding = 0) uniform Transform {
mat4 Model;
Expand Down
1 change: 1 addition & 0 deletions examples/shader/mesh_custom_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ layout(location = 0) out vec3 v_color;

layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};
layout(set = 1, binding = 0) uniform Transform {
mat4 Model;
Expand Down
1 change: 1 addition & 0 deletions examples/shader/shader_custom_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const VERTEX_SHADER: &str = r#"
layout(location = 0) in vec3 Vertex_Position;
layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};
layout(set = 1, binding = 0) uniform Transform {
mat4 Model;
Expand Down
1 change: 1 addition & 0 deletions examples/shader/shader_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const VERTEX_SHADER: &str = r#"
layout(location = 0) in vec3 Vertex_Position;
layout(set = 0, binding = 0) uniform Camera {
mat4 ViewProj;
mat4 View;
};
layout(set = 1, binding = 0) uniform Transform {
mat4 Model;
Expand Down