diff --git a/crates/bevy_render/src/draw.rs b/crates/bevy_render/src/draw.rs index 6faac65c32865..738bde1d96150 100644 --- a/crates/bevy_render/src/draw.rs +++ b/crates/bevy_render/src/draw.rs @@ -28,6 +28,9 @@ pub enum RenderCommand { buffer: BufferId, offset: u64, }, + SetScissorPass { + bounds: [u32; 4], + }, SetIndexBuffer { buffer: BufferId, offset: u64, @@ -112,6 +115,10 @@ impl Draw { }); } + pub fn set_scissor_rect(&mut self, bounds: [u32; 4]) { + self.render_command(RenderCommand::SetScissorPass { bounds }); + } + pub fn set_index_buffer(&mut self, buffer: BufferId, offset: u64, index_format: IndexFormat) { self.render_command(RenderCommand::SetIndexBuffer { buffer, diff --git a/crates/bevy_render/src/render_graph/nodes/pass_node.rs b/crates/bevy_render/src/render_graph/nodes/pass_node.rs index 5258f9fa3e0cc..c20ef3ff4b75c 100644 --- a/crates/bevy_render/src/render_graph/nodes/pass_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/pass_node.rs @@ -287,6 +287,9 @@ where render_pass.set_vertex_buffer(slot, buffer, offset); draw_state.set_vertex_buffer(slot, buffer, offset); } + RenderCommand::SetScissorPass {bounds} => { + render_pass.set_scissor_rect(bounds[0], bounds[1], bounds[2], bounds[3]); + } RenderCommand::SetIndexBuffer { buffer, offset, index_format } => { if draw_state.is_index_buffer_set(buffer, offset, index_format) { continue; diff --git a/crates/bevy_sprite/src/render/sprite_sheet.frag b/crates/bevy_sprite/src/render/sprite_sheet.frag index f7a430946cb37..8c8d9217f08d1 100644 --- a/crates/bevy_sprite/src/render/sprite_sheet.frag +++ b/crates/bevy_sprite/src/render/sprite_sheet.frag @@ -2,7 +2,6 @@ layout(location = 0) in vec2 v_Uv; layout(location = 1) in vec4 v_Color; -layout(location = 2) in vec4 v_Bounds; layout(location = 0) out vec4 o_Target; @@ -10,10 +9,6 @@ layout(set = 1, binding = 2) uniform texture2D TextureAtlas_texture; layout(set = 1, binding = 3) uniform sampler TextureAtlas_texture_sampler; void main() { - if(gl_FragCoord.x >= v_Bounds.x || gl_FragCoord.x <= v_Bounds.z || - gl_FragCoord.y >= v_Bounds.y || gl_FragCoord.y <= v_Bounds.w) { - discard; - } o_Target = v_Color * texture( sampler2D(TextureAtlas_texture, TextureAtlas_texture_sampler), v_Uv); diff --git a/crates/bevy_sprite/src/render/sprite_sheet.vert b/crates/bevy_sprite/src/render/sprite_sheet.vert index 1bc5a8514c06f..62ecf3323785a 100644 --- a/crates/bevy_sprite/src/render/sprite_sheet.vert +++ b/crates/bevy_sprite/src/render/sprite_sheet.vert @@ -6,7 +6,6 @@ layout(location = 2) in vec2 Vertex_Uv; layout(location = 0) out vec2 v_Uv; layout(location = 1) out vec4 v_Color; -layout(location = 2) out vec4 v_Bounds; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; @@ -35,7 +34,6 @@ layout(set = 2, binding = 1) uniform TextureAtlasSprite { vec4 color; uint index; uint flip; - vec4 bounds; }; void main() { @@ -84,5 +82,4 @@ void main() { v_Color = color; gl_Position = ViewProj * SpriteTransform * vec4(vertex_position, 1.0); - v_Bounds = bounds; } diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index d0632d0618e02..76df36000f6bb 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -1,7 +1,7 @@ use crate::Rect; use bevy_asset::Handle; use bevy_core::Bytes; -use bevy_math::{Vec2, Vec4}; +use bevy_math::Vec2; use bevy_reflect::TypeUuid; use bevy_render::{ color::Color, @@ -33,7 +33,6 @@ pub struct TextureAtlasSprite { pub index: u32, pub flip_x: bool, pub flip_y: bool, - pub bounds: Vec4, } impl RenderResource for TextureAtlasSprite { @@ -51,8 +50,7 @@ impl RenderResource for TextureAtlasSprite { self.color.write_bytes(color_buf); // Write the index buffer - let (index_buf, rest) = rest.split_at_mut(4); - let (flip_buf, bounds_buf) = rest.split_at_mut(4); + let (index_buf, flip_buf) = rest.split_at_mut(4); self.index.write_bytes(index_buf); // First bit means flip x, second bit means flip y @@ -60,8 +58,6 @@ impl RenderResource for TextureAtlasSprite { flip_buf[1] = 0; flip_buf[2] = 0; flip_buf[3] = 0; - - self.bounds.write_bytes(bounds_buf); } fn texture(&self) -> Option<&Handle> { @@ -76,7 +72,6 @@ impl Default for TextureAtlasSprite { color: Color::WHITE, flip_x: false, flip_y: false, - bounds: Default::default(), } } } diff --git a/crates/bevy_text/src/draw.rs b/crates/bevy_text/src/draw.rs index 749d738f6d6a1..9e26a456b5207 100644 --- a/crates/bevy_text/src/draw.rs +++ b/crates/bevy_text/src/draw.rs @@ -1,4 +1,4 @@ -use bevy_math::{Mat4, Vec3, Vec4}; +use bevy_math::{Mat4, Vec3}; use bevy_render::{ draw::{Draw, DrawContext, DrawError, Drawable}, mesh, @@ -21,7 +21,6 @@ pub struct DrawableText<'a> { pub text_glyphs: &'a Vec, pub msaa: &'a Msaa, pub font_quad_vertex_layout: &'a VertexBufferLayout, - pub bounds: Vec4, } impl<'a> Drawable for DrawableText<'a> { @@ -75,7 +74,6 @@ impl<'a> Drawable for DrawableText<'a> { color: self.sections[tv.section_index].style.color, flip_x: false, flip_y: false, - bounds: self.bounds, }; // To get the rendering right for non-one scaling factors, we need diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 1d51138826514..112ce1cac0c11 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -112,7 +112,6 @@ pub fn draw_text2d_system( font_quad_vertex_layout: &font_quad_vertex_layout, scale_factor, sections: &text.sections, - bounds: Default::default(), }; drawable_text.draw(&mut draw, &mut context).unwrap(); diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index ec3788c69fb4c..c941a5c8c0892 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -2,7 +2,6 @@ mod convert; use crate::{CalculatedSize, Node, Overflow, Style}; use bevy_app::EventReader; -use bevy_ecs::system::QuerySet; use bevy_ecs::{ entity::Entity, query::{Changed, FilterFetch, With, Without, WorldQuery}, @@ -10,6 +9,7 @@ use bevy_ecs::{ }; use bevy_log::warn; use bevy_math::{Vec2, Vec3, Vec4}; +use bevy_render::draw::Draw; use bevy_transform::prelude::{Children, GlobalTransform, Parent, Transform}; use bevy_utils::HashMap; use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows}; @@ -188,31 +188,32 @@ pub enum FlexError { unsafe impl Send for FlexSurface {} unsafe impl Sync for FlexSurface {} -pub fn bounds_node_system( +pub fn overflow_hidden_system( windows: Res, - mut query_set: QuerySet<( - Query<(Option<&Parent>, &Style, &Node, &GlobalTransform)>, - Query<&mut Node>, - )>, - query: Query>, + mut node_query: Query<(Entity, &mut Draw), With>, + parent_query: Query<(Option<&Parent>, &Style, &Node, &GlobalTransform)>, ) { // y gets flipped somewhere.. let window = windows.get_primary().unwrap(); - let window_height = window.height(); let get_bounds = |position: Vec3, size: Vec2| -> Vec4 { - Vec4::new( - position.x - size.x / 2.0, - window_height - (position.y + size.y / 2.0), - position.x + size.x / 2.0, - window_height - (position.y - size.y / 2.0), - ) + let mut bounds = Vec4::new( + f32::min(position.x - size.x / 2.0, 0.0), + f32::min(position.x + size.x / 2.0, 0.0), + size.x, + size.y, + ); + bounds.x = f32::max(bounds.x, window.width()); + bounds.y = f32::max(bounds.y, window.height()); + bounds.z = f32::max(bounds.z, window.width() - bounds.x); + bounds.w = f32::max(bounds.w, window.height() - bounds.y); + + bounds }; - for entity in query.iter() { + for (entity, mut draw) in node_query.iter_mut() { let mut current_entity = entity; let mut bounds = Default::default(); loop { - if let Ok((parent, style, node, global_transform)) = query_set.q0().get(current_entity) - { + if let Ok((parent, style, node, global_transform)) = parent_query.get(current_entity) { if current_entity == entity { bounds = get_bounds(global_transform.translation, node.size); } @@ -229,8 +230,9 @@ pub fn bounds_node_system( break; } } - if let Ok(mut node) = query_set.q1_mut().get_mut(entity) { - node.bounds = bounds; + if bounds.is_finite() && bounds.z != 0.0 && bounds.w != 0.0 { + println!("{:#?}", bounds); + draw.set_scissor_rect(*bounds.as_u32().as_ref()) } } } diff --git a/crates/bevy_ui/src/lib.rs b/crates/bevy_ui/src/lib.rs index 8b7f40c2f9e52..209cf7be9b5f6 100644 --- a/crates/bevy_ui/src/lib.rs +++ b/crates/bevy_ui/src/lib.rs @@ -81,7 +81,7 @@ impl Plugin for UiPlugin { ) .add_system_to_stage( CoreStage::PostUpdate, - bounds_node_system + overflow_hidden_system .system() .after(TransformSystem::TransformPropagate), ) diff --git a/crates/bevy_ui/src/render/ui.frag b/crates/bevy_ui/src/render/ui.frag index 456fc7f6ab5c1..d6f36c1e0f58f 100644 --- a/crates/bevy_ui/src/render/ui.frag +++ b/crates/bevy_ui/src/render/ui.frag @@ -1,7 +1,6 @@ #version 450 layout(location = 0) in vec2 v_Uv; -layout(location = 1) in vec4 v_Bounds; layout(location = 0) out vec4 o_Target; layout(set = 2, binding = 0) uniform ColorMaterial_color { @@ -14,11 +13,6 @@ layout(set = 2, binding = 2) uniform sampler ColorMaterial_texture_sampler; # endif void main() { - if(gl_FragCoord.x <= v_Bounds.x || gl_FragCoord.x >= v_Bounds.z || - gl_FragCoord.y <= v_Bounds.y || gl_FragCoord.y >= v_Bounds.w) - { - discard; - } vec4 color = Color; # ifdef COLORMATERIAL_TEXTURE color *= texture( diff --git a/crates/bevy_ui/src/render/ui.vert b/crates/bevy_ui/src/render/ui.vert index 021130100564a..b1f1aff935af6 100644 --- a/crates/bevy_ui/src/render/ui.vert +++ b/crates/bevy_ui/src/render/ui.vert @@ -5,7 +5,6 @@ layout(location = 1) in vec3 Vertex_Normal; layout(location = 2) in vec2 Vertex_Uv; layout(location = 0) out vec2 v_Uv; -layout(location = 1) out vec4 v_Bounds; layout(set = 0, binding = 0) uniform CameraViewProj { mat4 ViewProj; @@ -18,14 +17,8 @@ layout(set = 1, binding = 1) uniform Node_size { vec2 NodeSize; }; -layout(set = 1, binding = 2) uniform Node_bounds { - vec4 Bounds; -}; - - void main() { v_Uv = Vertex_Uv; - v_Bounds = Bounds; vec3 position = Vertex_Position * vec3(NodeSize, 0.0); gl_Position = ViewProj * Object * vec4(position, 1.0); } diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 537c0eefe4012..649b170d21fa5 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -1,5 +1,5 @@ use bevy_ecs::reflect::ReflectComponent; -use bevy_math::{Rect, Size, Vec2, Vec4}; +use bevy_math::{Rect, Size, Vec2}; use bevy_reflect::{Reflect, ReflectDeserialize}; use bevy_render::renderer::RenderResources; use serde::{Deserialize, Serialize}; @@ -9,7 +9,6 @@ use std::ops::{Add, AddAssign}; #[reflect(Component)] pub struct Node { pub size: Vec2, - pub bounds: Vec4, } #[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 22e2012aa643f..a8dce7ea1a191 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -177,7 +177,6 @@ pub fn draw_text_system( text_glyphs: &text_glyphs.glyphs, font_quad_vertex_layout: &vertex_buffer_layout, sections: &text.sections, - bounds: node.bounds, }; drawable_text.draw(&mut draw, &mut context).unwrap();