Skip to content

Commit

Permalink
reduced code bloat
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Oct 7, 2024
1 parent 38e5eee commit 6a2cfad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 102 deletions.
88 changes: 10 additions & 78 deletions fyrox-graphics/src/geometry_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f32>(),
AttributeKind::Float2 => size_of::<f32>() * 2,
AttributeKind::Float3 => size_of::<f32>() * 3,
AttributeKind::Float4 => size_of::<f32>() * 4,

AttributeKind::UnsignedByte => size_of::<u8>(),
AttributeKind::UnsignedByte2 => size_of::<u8>() * 2,
AttributeKind::UnsignedByte3 => size_of::<u8>() * 3,
AttributeKind::UnsignedByte4 => size_of::<u8>() * 4,

AttributeKind::UnsignedShort => size_of::<u16>(),
AttributeKind::UnsignedShort2 => size_of::<u16>() * 2,
AttributeKind::UnsignedShort3 => size_of::<u16>() * 3,
AttributeKind::UnsignedShort4 => size_of::<u16>() * 4,

AttributeKind::UnsignedInt => size_of::<u32>(),
AttributeKind::UnsignedInt2 => size_of::<u32>() * 2,
AttributeKind::UnsignedInt3 => size_of::<u32>() * 3,
AttributeKind::UnsignedInt4 => size_of::<u32>() * 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,
}
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions fyrox-impl/src/renderer/debug_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
24 changes: 6 additions & 18 deletions fyrox-impl/src/renderer/framework/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
12 changes: 8 additions & 4 deletions fyrox-impl/src/renderer/ui_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down

0 comments on commit 6a2cfad

Please sign in to comment.