From 36e107ddccba9581ab63ac5cf451b8bfa244fa42 Mon Sep 17 00:00:00 2001 From: Torstein Grindvik Date: Mon, 10 Oct 2022 19:23:43 +0000 Subject: [PATCH] Add globals struct to mesh2d (#6222) See commit message. I noticed I couldn't use `globals.time` when using `Material2d`. I copied the solution from 807336203903e516a8705a13316c2c2c20c0f5bb , and now `Material2d` works for me. Perhaps some of these struct definitions could be shared in the future, but for now I've just copy pasted it (it looked like the `View` struct was done that way). Ping @IceSentry , I saw a comment on the linked commit that you intended to do this work at some point in the future. --- crates/bevy_sprite/src/mesh2d/mesh.rs | 31 ++++++++++++++++--- .../src/mesh2d/mesh2d_view_bindings.wgsl | 3 ++ .../src/mesh2d/mesh2d_view_types.wgsl | 11 +++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/crates/bevy_sprite/src/mesh2d/mesh.rs b/crates/bevy_sprite/src/mesh2d/mesh.rs index fe2c21544ee44..6aa9ac5c51373 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh.rs +++ b/crates/bevy_sprite/src/mesh2d/mesh.rs @@ -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}, @@ -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"), }); @@ -429,14 +440,24 @@ pub fn queue_mesh2d_view_bind_groups( mesh2d_pipeline: Res, view_uniforms: Res, views: Query>, + globals_buffer: Res, ) { - 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, }); diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl index cc138d28facb7..9f16794faf813 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_view_bindings.wgsl @@ -4,3 +4,6 @@ @group(0) @binding(0) var view: View; + +@group(0) @binding(1) +var globals: Globals; diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl index b4b1e8d98c178..1759fa499fd07 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl @@ -11,3 +11,14 @@ struct View { // viewport(x_origin, y_origin, width, height) viewport: vec4, }; + +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, +}