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

[Merged by Bors] - Add globals struct to mesh2d #6222

Closed
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
31 changes: 26 additions & 5 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bevy_math::{Mat4, Vec2};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::{
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
globals::{GlobalsBuffer, GlobalsUniform},
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
render_asset::RenderAssets,
render_phase::{EntityRenderCommand, RenderCommandResult, TrackedRenderPass},
Expand Down Expand Up @@ -176,6 +177,16 @@ impl FromWorld for Mesh2dPipeline {
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(GlobalsUniform::min_size()),
},
count: None,
},
],
label: Some("mesh2d_view_layout"),
});
Expand Down Expand Up @@ -418,14 +429,24 @@ pub fn queue_mesh2d_view_bind_groups(
mesh2d_pipeline: Res<Mesh2dPipeline>,
view_uniforms: Res<ViewUniforms>,
views: Query<Entity, With<ExtractedView>>,
globals_buffer: Res<GlobalsBuffer>,
) {
if let Some(view_binding) = view_uniforms.uniforms.binding() {
if let (Some(view_binding), Some(globals)) = (
view_uniforms.uniforms.binding(),
globals_buffer.buffer.binding(),
) {
for entity in &views {
let view_bind_group = render_device.create_bind_group(&BindGroupDescriptor {
entries: &[BindGroupEntry {
binding: 0,
resource: view_binding.clone(),
}],
entries: &[
BindGroupEntry {
binding: 0,
resource: view_binding.clone(),
},
BindGroupEntry {
binding: 1,
resource: globals.clone(),
},
],
label: Some("mesh2d_view_bind_group"),
layout: &mesh2d_pipeline.view_layout,
});
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

@group(0) @binding(0)
var<uniform> view: View;

@group(0) @binding(1)
var<uniform> globals: Globals;
11 changes: 11 additions & 0 deletions crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ struct View {
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
};

struct Globals {
// The time since startup in seconds
// Wraps to 0 after 1 hour.
time: f32,
// The delta time since the previous frame in seconds
delta_time: f32,
// Frame count since the start of the app.
// It wraps to zero when it reaches the maximum value of a u32.
frame_count: u32,
}