Skip to content
Open
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
7 changes: 7 additions & 0 deletions crates/bevy_render/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub enum RenderCommand {
buffer: BufferId,
offset: u64,
},
SetScissorPass {
bounds: [u32; 4],
},
SetIndexBuffer {
buffer: BufferId,
offset: u64,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_render/src/render_graph/nodes/pass_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions crates/bevy_sprite/src/render/sprite_sheet.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

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;

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);
Expand Down
3 changes: 0 additions & 3 deletions crates/bevy_sprite/src/render/sprite_sheet.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,7 +34,6 @@ layout(set = 2, binding = 1) uniform TextureAtlasSprite {
vec4 color;
uint index;
uint flip;
vec4 bounds;
};

void main() {
Expand Down Expand Up @@ -84,5 +82,4 @@ void main() {

v_Color = color;
gl_Position = ViewProj * SpriteTransform * vec4(vertex_position, 1.0);
v_Bounds = bounds;
}
9 changes: 2 additions & 7 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -51,17 +50,14 @@ 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
flip_buf[0] = if self.flip_x { 0b01 } else { 0 } | if self.flip_y { 0b10 } else { 0 };
flip_buf[1] = 0;
flip_buf[2] = 0;
flip_buf[3] = 0;

self.bounds.write_bytes(bounds_buf);
}

fn texture(&self) -> Option<&Handle<Texture>> {
Expand All @@ -76,7 +72,6 @@ impl Default for TextureAtlasSprite {
color: Color::WHITE,
flip_x: false,
flip_y: false,
bounds: Default::default(),
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_text/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_math::{Mat4, Vec3, Vec4};
use bevy_math::{Mat4, Vec3};
use bevy_render::{
draw::{Draw, DrawContext, DrawError, Drawable},
mesh,
Expand All @@ -21,7 +21,6 @@ pub struct DrawableText<'a> {
pub text_glyphs: &'a Vec<PositionedGlyph>,
pub msaa: &'a Msaa,
pub font_quad_vertex_layout: &'a VertexBufferLayout,
pub bounds: Vec4,
}

impl<'a> Drawable for DrawableText<'a> {
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
40 changes: 21 additions & 19 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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},
system::{Query, Res, ResMut},
};
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};
Expand Down Expand Up @@ -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<Windows>,
mut query_set: QuerySet<(
Query<(Option<&Parent>, &Style, &Node, &GlobalTransform)>,
Query<&mut Node>,
)>,
query: Query<Entity, With<Node>>,
mut node_query: Query<(Entity, &mut Draw), With<Node>>,
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);
}
Expand All @@ -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())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Plugin for UiPlugin {
)
.add_system_to_stage(
CoreStage::PostUpdate,
bounds_node_system
overflow_hidden_system
.system()
.after(TransformSystem::TransformPropagate),
)
Expand Down
6 changes: 0 additions & 6 deletions crates/bevy_ui/src/render/ui.frag
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
Expand Down
7 changes: 0 additions & 7 deletions crates/bevy_ui/src/render/ui.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
3 changes: 1 addition & 2 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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)]
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down