Skip to content

Commit

Permalink
Provide GlobalsUniform in UiMaterial shaders (bevyengine#10739)
Browse files Browse the repository at this point in the history
`GlobalsUniform` provides the current time to shaders, which is useful
for animations. `UiMaterial` is an abstraction that makes it easier to
write custom shaders for UI elements.
This PR makes it possible to use the `GlobalsUniform` in `UiMaterial`
shaders.

The `GlobalsUniform` is bound to `@group(0) @binding(1)`. It is
accessible in shaders with:
```wgsl

@group(0) @binding(1)
var<uniform> globals: Globals;
```

---

Added `GlobalsUniform` in `UiMaterial` shaders

Should I modify the existing ui_material example to showcase this?
  • Loading branch information
Davier committed Nov 29, 2023
1 parent 48abb48 commit 12068fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
7 changes: 6 additions & 1 deletion crates/bevy_ui/src/render/ui_material.wgsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#import bevy_render::view::View
#import bevy_render::{
view::View,
globals::Globals,
}
#import bevy_ui::ui_vertex_output::UiVertexOutput

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

@vertex
fn vertex(
Expand Down
39 changes: 28 additions & 11 deletions crates/bevy_ui/src/render/ui_material_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use bevy_ecs::{
use bevy_math::{Mat4, Rect, Vec2, Vec4Swizzles};
use bevy_render::{
extract_component::ExtractComponentPlugin,
globals::{GlobalsBuffer, GlobalsUniform},
render_asset::RenderAssets,
render_phase::*,
render_resource::*,
Expand Down Expand Up @@ -222,16 +223,28 @@ impl<M: UiMaterial> FromWorld for UiMaterialPipeline<M> {
let ui_layout = M::bind_group_layout(render_device);

let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
entries: &[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: Some(ViewUniform::min_size()),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: Some(ViewUniform::min_size()),
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX | ShaderStages::FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(GlobalsUniform::min_size()),
},
count: None,
},
count: None,
}],
],
label: Some("ui_view_layout"),
});
UiMaterialPipeline {
Expand Down Expand Up @@ -432,18 +445,22 @@ pub fn prepare_uimaterial_nodes<M: UiMaterial>(
mut ui_meta: ResMut<UiMaterialMeta>,
mut extracted_uinodes: ResMut<ExtractedUiMaterialNodes<M>>,
view_uniforms: Res<ViewUniforms>,
globals_buffer: Res<GlobalsBuffer>,
ui_material_pipeline: Res<UiMaterialPipeline<M>>,
mut phases: Query<&mut RenderPhase<TransparentUi>>,
mut previous_len: Local<usize>,
) {
if let Some(view_binding) = view_uniforms.uniforms.binding() {
if let (Some(view_binding), Some(globals_binding)) = (
view_uniforms.uniforms.binding(),
globals_buffer.buffer.binding(),
) {
let mut batches: Vec<(Entity, UiMaterialBatch<M>)> = Vec::with_capacity(*previous_len);

ui_meta.vertices.clear();
ui_meta.view_bind_group = Some(render_device.create_bind_group(
"ui_material_view_bind_group",
&ui_material_pipeline.view_layout,
&BindGroupEntries::single(view_binding),
&BindGroupEntries::sequential((view_binding, globals_binding)),
));
let mut index = 0;

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ui/src/ui_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ use bevy_render::render_resource::{AsBindGroup, RenderPipelineDescriptor, Shader
///
/// If you only use the fragment shader make sure to import `UiVertexOutput` from
/// `bevy_ui::ui_vertex_output` in your wgsl shader.
/// Also note that bind group 0 is always bound to the [`View Uniform`](bevy_render::view::ViewUniform).
/// Also note that bind group 0 is always bound to the [`View Uniform`](bevy_render::view::ViewUniform)
/// and the [`Globals Uniform`](bevy_render::globals::GlobalsUniform).
///
/// ```wgsl
/// #import bevy_ui::ui_vertex_output UiVertexOutput
Expand Down

0 comments on commit 12068fc

Please sign in to comment.