Skip to content

Commit 723ccf9

Browse files
committed
More cleanup.
1 parent 3f73945 commit 723ccf9

File tree

6 files changed

+80
-99
lines changed

6 files changed

+80
-99
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/processing_render/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ x11 = ["bevy/x11"]
1313

1414
[dependencies]
1515
bevy = { workspace = true }
16-
bitflags = "2"
1716
lyon = "1.0"
1817
raw-window-handle = "0.6"
1918
thiserror = "2"

crates/processing_render/src/geometry/attribute.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn get_mesh_mut<'a>(
4040
.ok_or(ProcessingError::GeometryNotFound)
4141
}
4242

43-
macro_rules! impl_bulk_getter {
43+
macro_rules! impl_getter {
4444
($name:ident, $attr:expr, $variant:ident, $type:ty) => {
4545
pub fn $name(
4646
In((entity, range)): In<(Entity, Range<usize>)>,
@@ -61,10 +61,10 @@ macro_rules! impl_bulk_getter {
6161
};
6262
}
6363

64-
impl_bulk_getter!(get_positions, Mesh::ATTRIBUTE_POSITION, Float32x3, [f32; 3]);
65-
impl_bulk_getter!(get_normals, Mesh::ATTRIBUTE_NORMAL, Float32x3, [f32; 3]);
66-
impl_bulk_getter!(get_colors, Mesh::ATTRIBUTE_COLOR, Float32x4, [f32; 4]);
67-
impl_bulk_getter!(get_uvs, Mesh::ATTRIBUTE_UV_0, Float32x2, [f32; 2]);
64+
impl_getter!(get_positions, Mesh::ATTRIBUTE_POSITION, Float32x3, [f32; 3]);
65+
impl_getter!(get_normals, Mesh::ATTRIBUTE_NORMAL, Float32x3, [f32; 3]);
66+
impl_getter!(get_colors, Mesh::ATTRIBUTE_COLOR, Float32x4, [f32; 4]);
67+
impl_getter!(get_uvs, Mesh::ATTRIBUTE_UV_0, Float32x2, [f32; 2]);
6868

6969
pub fn get_indices(
7070
In((entity, range)): In<(Entity, Range<usize>)>,
@@ -165,6 +165,9 @@ pub struct Attribute {
165165

166166
impl Attribute {
167167
pub fn new(name: impl Into<String>, format: AttributeFormat) -> Self {
168+
// we leak here to get a 'static str for the attribute name, but this is okay because
169+
// we never expect to unload attributes during the lifetime of the application
170+
// and attribute names are generally small in number
168171
let name: &'static str = Box::leak(name.into().into_boxed_str());
169172
let id = hash_attr_name(name);
170173
let inner = MeshVertexAttribute::new(name, id, format.to_vertex_format());
@@ -372,26 +375,3 @@ pub fn set_attribute(
372375
)),
373376
}
374377
}
375-
376-
pub fn to_attribute_values(values: &[AttributeValue]) -> Option<VertexAttributeValues> {
377-
macro_rules! convert {
378-
($variant:ident, $bevy:ident, $default:expr) => {
379-
Some(VertexAttributeValues::$bevy(
380-
values
381-
.iter()
382-
.map(|v| match v {
383-
AttributeValue::$variant(x) => *x,
384-
_ => $default,
385-
})
386-
.collect(),
387-
))
388-
};
389-
}
390-
391-
match values.first()? {
392-
AttributeValue::Float(_) => convert!(Float, Float32, 0.0),
393-
AttributeValue::Float2(_) => convert!(Float2, Float32x2, [0.0; 2]),
394-
AttributeValue::Float3(_) => convert!(Float3, Float32x3, [0.0; 3]),
395-
AttributeValue::Float4(_) => convert!(Float4, Float32x4, [0.0; 4]),
396-
}
397-
}

crates/processing_render/src/geometry/layout.rs

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use bevy::prelude::*;
22

33
use super::BuiltinAttributes;
4+
use crate::error::{ProcessingError, Result};
45

56
// bevy requires an attribute id for each unique vertex attribute. we don't really want to
67
// expose this to users, so we hash the attribute name to generate a unique id. in theory
@@ -39,7 +40,9 @@ impl VertexLayout {
3940
}
4041

4142
pub fn push(&mut self, attr: Entity) {
42-
self.attributes.push(attr);
43+
if !self.attributes.contains(&attr) {
44+
self.attributes.push(attr);
45+
}
4346
}
4447

4548
pub fn has_attribute(&self, attr_entity: Entity) -> bool {
@@ -53,42 +56,57 @@ pub fn create(In(()): In<()>, mut commands: Commands) -> Entity {
5356

5457
pub fn create_default(world: &mut World) -> Entity {
5558
let builtins = world.resource::<BuiltinAttributes>();
56-
let attrs = vec![builtins.position, builtins.normal, builtins.color, builtins.uv];
59+
let attrs = vec![
60+
builtins.position,
61+
builtins.normal,
62+
builtins.color,
63+
builtins.uv,
64+
];
5765
world.spawn(VertexLayout::with_attributes(attrs)).id()
5866
}
5967

60-
pub fn add_position(world: &mut World, entity: Entity) {
68+
pub fn add_position(world: &mut World, entity: Entity) -> Result<()> {
6169
let position = world.resource::<BuiltinAttributes>().position;
62-
if let Some(mut layout) = world.get_mut::<VertexLayout>(entity) {
63-
layout.push(position);
64-
}
70+
let mut layout = world
71+
.get_mut::<VertexLayout>(entity)
72+
.ok_or(ProcessingError::LayoutNotFound)?;
73+
layout.push(position);
74+
Ok(())
6575
}
6676

67-
pub fn add_normal(world: &mut World, entity: Entity) {
77+
pub fn add_normal(world: &mut World, entity: Entity) -> Result<()> {
6878
let normal = world.resource::<BuiltinAttributes>().normal;
69-
if let Some(mut layout) = world.get_mut::<VertexLayout>(entity) {
70-
layout.push(normal);
71-
}
79+
let mut layout = world
80+
.get_mut::<VertexLayout>(entity)
81+
.ok_or(ProcessingError::LayoutNotFound)?;
82+
layout.push(normal);
83+
Ok(())
7284
}
7385

74-
pub fn add_color(world: &mut World, entity: Entity) {
86+
pub fn add_color(world: &mut World, entity: Entity) -> Result<()> {
7587
let color = world.resource::<BuiltinAttributes>().color;
76-
if let Some(mut layout) = world.get_mut::<VertexLayout>(entity) {
77-
layout.push(color);
78-
}
88+
let mut layout = world
89+
.get_mut::<VertexLayout>(entity)
90+
.ok_or(ProcessingError::LayoutNotFound)?;
91+
layout.push(color);
92+
Ok(())
7993
}
8094

81-
pub fn add_uv(world: &mut World, entity: Entity) {
95+
pub fn add_uv(world: &mut World, entity: Entity) -> Result<()> {
8296
let uv = world.resource::<BuiltinAttributes>().uv;
83-
if let Some(mut layout) = world.get_mut::<VertexLayout>(entity) {
84-
layout.push(uv);
85-
}
97+
let mut layout = world
98+
.get_mut::<VertexLayout>(entity)
99+
.ok_or(ProcessingError::LayoutNotFound)?;
100+
layout.push(uv);
101+
Ok(())
86102
}
87103

88-
pub fn add_attribute(world: &mut World, layout_entity: Entity, attr_entity: Entity) {
89-
if let Some(mut layout) = world.get_mut::<VertexLayout>(layout_entity) {
90-
layout.push(attr_entity);
91-
}
104+
pub fn add_attribute(world: &mut World, layout_entity: Entity, attr_entity: Entity) -> Result<()> {
105+
let mut layout = world
106+
.get_mut::<VertexLayout>(layout_entity)
107+
.ok_or(ProcessingError::LayoutNotFound)?;
108+
layout.push(attr_entity);
109+
Ok(())
92110
}
93111

94112
pub fn destroy(In(entity): In<Entity>, mut commands: Commands) {

crates/processing_render/src/geometry/mod.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,19 @@ pub fn create_with_layout(
117117
mut meshes: ResMut<Assets<Mesh>>,
118118
layouts: Query<&VertexLayout>,
119119
attrs: Query<&Attribute>,
120-
) -> Entity {
121-
let layout = layouts.get(layout_entity).unwrap();
120+
) -> Result<Entity> {
121+
let layout = layouts
122+
.get(layout_entity)
123+
.map_err(|_| ProcessingError::LayoutNotFound)?;
122124
let mut mesh = Mesh::new(
123125
topology.to_primitive_topology(),
124126
RenderAssetUsages::default(),
125127
);
126128

127129
for &attr_entity in layout.attributes() {
128-
let attr = attrs.get(attr_entity).unwrap();
130+
let attr = attrs
131+
.get(attr_entity)
132+
.map_err(|_| ProcessingError::InvalidEntity)?;
129133
let empty_values = match attr.inner.format {
130134
bevy::render::render_resource::VertexFormat::Float32 => {
131135
VertexAttributeValues::Float32(Vec::new())
@@ -141,11 +145,11 @@ pub fn create_with_layout(
141145
}
142146
_ => continue,
143147
};
144-
mesh.insert_attribute(attr.inner.clone(), empty_values);
148+
mesh.insert_attribute(attr.inner, empty_values);
145149
}
146150

147151
let handle = meshes.add(mesh);
148-
commands.spawn(Geometry::new(handle, layout_entity)).id()
152+
Ok(commands.spawn(Geometry::new(handle, layout_entity)).id())
149153
}
150154

151155
pub fn create_box(
@@ -237,28 +241,25 @@ pub fn vertex(
237241
positions.push([x, y, z]);
238242
}
239243

240-
if layout.has_attribute(builtins.normal) {
241-
if let Some(VertexAttributeValues::Float32x3(normals)) =
244+
if layout.has_attribute(builtins.normal)
245+
&& let Some(VertexAttributeValues::Float32x3(normals)) =
242246
mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL)
243-
{
244-
normals.push(geometry.current_normal);
245-
}
247+
{
248+
normals.push(geometry.current_normal);
246249
}
247250

248-
if layout.has_attribute(builtins.color) {
249-
if let Some(VertexAttributeValues::Float32x4(colors)) =
251+
if layout.has_attribute(builtins.color)
252+
&& let Some(VertexAttributeValues::Float32x4(colors)) =
250253
mesh.attribute_mut(Mesh::ATTRIBUTE_COLOR)
251-
{
252-
colors.push(geometry.current_color);
253-
}
254+
{
255+
colors.push(geometry.current_color);
254256
}
255257

256-
if layout.has_attribute(builtins.uv) {
257-
if let Some(VertexAttributeValues::Float32x2(uvs)) =
258+
if layout.has_attribute(builtins.uv)
259+
&& let Some(VertexAttributeValues::Float32x2(uvs)) =
258260
mesh.attribute_mut(Mesh::ATTRIBUTE_UV_0)
259-
{
260-
uvs.push(geometry.current_uv);
261-
}
261+
{
262+
uvs.push(geometry.current_uv);
262263
}
263264

264265
for &attr_entity in layout.attributes() {
@@ -273,7 +274,7 @@ pub fn vertex(
273274
.get(attr_entity)
274275
.map_err(|_| ProcessingError::InvalidEntity)?;
275276
if let Some(current) = geometry.custom_current.get(&attr.inner.id) {
276-
match (mesh.attribute_mut(attr.inner.clone()), current) {
277+
match (mesh.attribute_mut(attr.inner), current) {
277278
(Some(VertexAttributeValues::Float32(values)), AttributeValue::Float(v)) => {
278279
values.push(*v);
279280
}

crates/processing_render/src/lib.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -700,41 +700,26 @@ pub fn geometry_layout_create() -> error::Result<Entity> {
700700
}
701701

702702
pub fn geometry_layout_add_position(entity: Entity) -> error::Result<()> {
703-
app_mut(|app| {
704-
geometry::layout::add_position(app.world_mut(), entity);
705-
Ok(())
706-
})
703+
app_mut(|app| geometry::layout::add_position(app.world_mut(), entity))
707704
}
708705

709706
pub fn geometry_layout_add_normal(entity: Entity) -> error::Result<()> {
710-
app_mut(|app| {
711-
geometry::layout::add_normal(app.world_mut(), entity);
712-
Ok(())
713-
})
707+
app_mut(|app| geometry::layout::add_normal(app.world_mut(), entity))
714708
}
715709

716710
pub fn geometry_layout_add_color(entity: Entity) -> error::Result<()> {
717-
app_mut(|app| {
718-
geometry::layout::add_color(app.world_mut(), entity);
719-
Ok(())
720-
})
711+
app_mut(|app| geometry::layout::add_color(app.world_mut(), entity))
721712
}
722713

723714
pub fn geometry_layout_add_uv(entity: Entity) -> error::Result<()> {
724-
app_mut(|app| {
725-
geometry::layout::add_uv(app.world_mut(), entity);
726-
Ok(())
727-
})
715+
app_mut(|app| geometry::layout::add_uv(app.world_mut(), entity))
728716
}
729717

730718
pub fn geometry_layout_add_attribute(
731719
layout_entity: Entity,
732720
attr_entity: Entity,
733721
) -> error::Result<()> {
734-
app_mut(|app| {
735-
geometry::layout::add_attribute(app.world_mut(), layout_entity, attr_entity);
736-
Ok(())
737-
})
722+
app_mut(|app| geometry::layout::add_attribute(app.world_mut(), layout_entity, attr_entity))
738723
}
739724

740725
pub fn geometry_layout_destroy(entity: Entity) -> error::Result<()> {
@@ -802,10 +787,9 @@ pub fn geometry_create_with_layout(
802787
topology: geometry::Topology,
803788
) -> error::Result<Entity> {
804789
app_mut(|app| {
805-
Ok(app
806-
.world_mut()
790+
app.world_mut()
807791
.run_system_cached_with(geometry::create_with_layout, (layout_entity, topology))
808-
.unwrap())
792+
.unwrap()
809793
})
810794
}
811795

@@ -1024,7 +1008,7 @@ pub fn geometry_get_attribute(
10241008
.world()
10251009
.get::<geometry::Attribute>(attr_entity)
10261010
.ok_or(error::ProcessingError::InvalidEntity)?;
1027-
let inner = attr.inner.clone();
1011+
let inner = attr.inner;
10281012
app.world_mut()
10291013
.run_system_cached_with(geometry::get_attribute, (geo_entity, inner, index))
10301014
.unwrap()
@@ -1042,7 +1026,7 @@ pub fn geometry_get_attributes(
10421026
.world()
10431027
.get::<geometry::Attribute>(attr_entity)
10441028
.ok_or(error::ProcessingError::InvalidEntity)?;
1045-
let inner = attr.inner.clone();
1029+
let inner = attr.inner;
10461030
app.world_mut()
10471031
.run_system_cached_with(geometry::get_attributes, (geo_entity, inner, start..end))
10481032
.unwrap()
@@ -1060,7 +1044,7 @@ pub fn geometry_set_attribute(
10601044
.world()
10611045
.get::<geometry::Attribute>(attr_entity)
10621046
.ok_or(error::ProcessingError::InvalidEntity)?;
1063-
let inner = attr.inner.clone();
1047+
let inner = attr.inner;
10641048
app.world_mut()
10651049
.run_system_cached_with(geometry::set_attribute, (geo_entity, inner, index, value))
10661050
.unwrap()

0 commit comments

Comments
 (0)