From 6a2cfadcca8aff4794639070913782e23f697014 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Mon, 7 Oct 2024 22:39:45 +0300 Subject: [PATCH] reduced code bloat --- fyrox-graphics/src/geometry_buffer.rs | 88 +++-------------------- fyrox-impl/src/renderer/debug_renderer.rs | 6 +- fyrox-impl/src/renderer/framework/mod.rs | 24 ++----- fyrox-impl/src/renderer/ui_renderer.rs | 12 ++-- 4 files changed, 28 insertions(+), 102 deletions(-) diff --git a/fyrox-graphics/src/geometry_buffer.rs b/fyrox-graphics/src/geometry_buffer.rs index 4d94ac6b5..61fc6e761 100644 --- a/fyrox-graphics/src/geometry_buffer.rs +++ b/fyrox-graphics/src/geometry_buffer.rs @@ -44,103 +44,35 @@ pub struct GeometryBuffer { #[allow(dead_code)] pub enum AttributeKind { Float, - Float2, - Float3, - Float4, - UnsignedByte, - UnsignedByte2, - UnsignedByte3, - UnsignedByte4, - UnsignedShort, - UnsignedShort2, - UnsignedShort3, - UnsignedShort4, - UnsignedInt, - UnsignedInt2, - UnsignedInt3, - UnsignedInt4, } pub struct AttributeDefinition { pub location: u32, pub kind: AttributeKind, + pub component_count: usize, pub normalized: bool, pub divisor: u32, } impl AttributeKind { - pub fn size_bytes(self) -> usize { + pub fn size(self) -> usize { match self { AttributeKind::Float => size_of::(), - AttributeKind::Float2 => size_of::() * 2, - AttributeKind::Float3 => size_of::() * 3, - AttributeKind::Float4 => size_of::() * 4, - AttributeKind::UnsignedByte => size_of::(), - AttributeKind::UnsignedByte2 => size_of::() * 2, - AttributeKind::UnsignedByte3 => size_of::() * 3, - AttributeKind::UnsignedByte4 => size_of::() * 4, - AttributeKind::UnsignedShort => size_of::(), - AttributeKind::UnsignedShort2 => size_of::() * 2, - AttributeKind::UnsignedShort3 => size_of::() * 3, - AttributeKind::UnsignedShort4 => size_of::() * 4, - AttributeKind::UnsignedInt => size_of::(), - AttributeKind::UnsignedInt2 => size_of::() * 2, - AttributeKind::UnsignedInt3 => size_of::() * 3, - AttributeKind::UnsignedInt4 => size_of::() * 4, - } - } - - fn get_type(self) -> u32 { - match self { - AttributeKind::Float - | AttributeKind::Float2 - | AttributeKind::Float3 - | AttributeKind::Float4 => glow::FLOAT, - - AttributeKind::UnsignedByte - | AttributeKind::UnsignedByte2 - | AttributeKind::UnsignedByte3 - | AttributeKind::UnsignedByte4 => glow::UNSIGNED_BYTE, - - AttributeKind::UnsignedShort - | AttributeKind::UnsignedShort2 - | AttributeKind::UnsignedShort3 - | AttributeKind::UnsignedShort4 => glow::UNSIGNED_SHORT, - - AttributeKind::UnsignedInt - | AttributeKind::UnsignedInt2 - | AttributeKind::UnsignedInt3 - | AttributeKind::UnsignedInt4 => glow::UNSIGNED_INT, } } - fn length(self) -> usize { + fn gl_type(self) -> u32 { match self { - AttributeKind::Float - | AttributeKind::UnsignedByte - | AttributeKind::UnsignedShort - | AttributeKind::UnsignedInt => 1, - - AttributeKind::Float2 - | AttributeKind::UnsignedByte2 - | AttributeKind::UnsignedShort2 - | AttributeKind::UnsignedInt2 => 2, - - AttributeKind::Float3 - | AttributeKind::UnsignedByte3 - | AttributeKind::UnsignedShort3 - | AttributeKind::UnsignedInt3 => 3, - - AttributeKind::Float4 - | AttributeKind::UnsignedByte4 - | AttributeKind::UnsignedShort4 - | AttributeKind::UnsignedInt4 => 4, + AttributeKind::Float => glow::FLOAT, + AttributeKind::UnsignedByte => glow::UNSIGNED_BYTE, + AttributeKind::UnsignedShort => glow::UNSIGNED_SHORT, + AttributeKind::UnsignedInt => glow::UNSIGNED_INT, } } } @@ -205,8 +137,8 @@ impl GeometryBuffer { for definition in buffer.attributes { server.gl.vertex_attrib_pointer_f32( definition.location, - definition.kind.length() as i32, - definition.kind.get_type(), + definition.component_count as i32, + definition.kind.gl_type(), definition.normalized, buffer.data.element_size as i32, offset as i32, @@ -216,7 +148,7 @@ impl GeometryBuffer { .vertex_attrib_divisor(definition.location, definition.divisor); server.gl.enable_vertex_attrib_array(definition.location); - offset += definition.kind.size_bytes(); + offset += definition.kind.size() * definition.component_count; if offset > buffer.data.element_size { return Err(FrameworkError::InvalidAttributeDescriptor); diff --git a/fyrox-impl/src/renderer/debug_renderer.rs b/fyrox-impl/src/renderer/debug_renderer.rs index 098c6e475..c2c4a17f3 100644 --- a/fyrox-impl/src/renderer/debug_renderer.rs +++ b/fyrox-impl/src/renderer/debug_renderer.rs @@ -111,12 +111,14 @@ impl DebugRenderer { AttributeDefinition { location: 0, divisor: 0, - kind: AttributeKind::Float3, + kind: AttributeKind::Float, + component_count: 3, normalized: false, }, AttributeDefinition { location: 1, - kind: AttributeKind::UnsignedByte4, + kind: AttributeKind::UnsignedByte, + component_count: 4, normalized: true, divisor: 0, }, diff --git a/fyrox-impl/src/renderer/framework/mod.rs b/fyrox-impl/src/renderer/framework/mod.rs index 8d4c3e5db..9e9360859 100644 --- a/fyrox-impl/src/renderer/framework/mod.rs +++ b/fyrox-impl/src/renderer/framework/mod.rs @@ -156,25 +156,13 @@ impl GeometryBufferExt for GeometryBuffer { .iter() .map(|a| AttributeDefinition { location: a.shader_location as u32, - kind: match (a.data_type, a.size) { - (VertexAttributeDataType::F32, 1) => AttributeKind::Float, - (VertexAttributeDataType::F32, 2) => AttributeKind::Float2, - (VertexAttributeDataType::F32, 3) => AttributeKind::Float3, - (VertexAttributeDataType::F32, 4) => AttributeKind::Float4, - (VertexAttributeDataType::U32, 1) => AttributeKind::UnsignedInt, - (VertexAttributeDataType::U32, 2) => AttributeKind::UnsignedInt2, - (VertexAttributeDataType::U32, 3) => AttributeKind::UnsignedInt3, - (VertexAttributeDataType::U32, 4) => AttributeKind::UnsignedInt4, - (VertexAttributeDataType::U16, 1) => AttributeKind::UnsignedShort, - (VertexAttributeDataType::U16, 2) => AttributeKind::UnsignedShort2, - (VertexAttributeDataType::U16, 3) => AttributeKind::UnsignedShort3, - (VertexAttributeDataType::U16, 4) => AttributeKind::UnsignedShort4, - (VertexAttributeDataType::U8, 1) => AttributeKind::UnsignedByte, - (VertexAttributeDataType::U8, 2) => AttributeKind::UnsignedByte2, - (VertexAttributeDataType::U8, 3) => AttributeKind::UnsignedByte3, - (VertexAttributeDataType::U8, 4) => AttributeKind::UnsignedByte4, - _ => unreachable!(), + kind: match a.data_type { + VertexAttributeDataType::F32 => AttributeKind::Float, + VertexAttributeDataType::U32 => AttributeKind::UnsignedInt, + VertexAttributeDataType::U16 => AttributeKind::UnsignedShort, + VertexAttributeDataType::U8 => AttributeKind::UnsignedByte, }, + component_count: a.size as usize, normalized: a.normalized, divisor: a.divisor as u32, }) diff --git a/fyrox-impl/src/renderer/ui_renderer.rs b/fyrox-impl/src/renderer/ui_renderer.rs index 80df79b10..d55bd173b 100644 --- a/fyrox-impl/src/renderer/ui_renderer.rs +++ b/fyrox-impl/src/renderer/ui_renderer.rs @@ -116,19 +116,22 @@ impl UiRenderer { attributes: &[ AttributeDefinition { location: 0, - kind: AttributeKind::Float2, + kind: AttributeKind::Float, + component_count: 2, normalized: false, divisor: 0, }, AttributeDefinition { location: 1, - kind: AttributeKind::Float2, + kind: AttributeKind::Float, + component_count: 2, normalized: false, divisor: 0, }, AttributeDefinition { location: 2, - kind: AttributeKind::UnsignedByte4, + kind: AttributeKind::UnsignedByte, + component_count: 4, normalized: true, // Make sure [0; 255] -> [0; 1] divisor: 0, }, @@ -145,7 +148,8 @@ impl UiRenderer { // We're interested only in position. Fragment shader won't run for clipping geometry anyway. AttributeDefinition { location: 0, - kind: AttributeKind::Float2, + kind: AttributeKind::Float, + component_count: 2, normalized: false, divisor: 0, },