From 0e17b8a8fe153bf7bcc8ce9731803879e8b2ef44 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Wed, 21 Jun 2023 09:55:32 +0200 Subject: [PATCH 1/2] Workaround --- crates/bevy_gizmos/src/lib.rs | 112 +++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 30 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index b288d72f747dc..1a19dd9fb9249 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -452,12 +452,22 @@ impl RenderCommand

for DrawLineGizmo { return RenderCommandResult::Failure; }; - pass.set_vertex_buffer(0, line_gizmo.position_buffer.slice(..)); - pass.set_vertex_buffer(1, line_gizmo.color_buffer.slice(..)); - let instances = if line_gizmo.strip { + let item_size = VertexFormat::Float32x3.size(); + let buffer_size = line_gizmo.position_buffer.size() - item_size; + pass.set_vertex_buffer(0, line_gizmo.position_buffer.slice(..buffer_size)); + pass.set_vertex_buffer(1, line_gizmo.position_buffer.slice(item_size..)); + + let item_size = VertexFormat::Float32x4.size(); + let buffer_size = line_gizmo.color_buffer.size() - item_size; + pass.set_vertex_buffer(2, line_gizmo.color_buffer.slice(..buffer_size)); + pass.set_vertex_buffer(3, line_gizmo.color_buffer.slice(item_size..)); + u32::max(line_gizmo.vertex_count, 1) - 1 } else { + pass.set_vertex_buffer(0, line_gizmo.position_buffer.slice(..)); + pass.set_vertex_buffer(1, line_gizmo.color_buffer.slice(..)); + line_gizmo.vertex_count / 2 }; @@ -468,42 +478,84 @@ impl RenderCommand

for DrawLineGizmo { } fn line_gizmo_vertex_buffer_layouts(strip: bool) -> Vec { - let stride_multiplier = if strip { 1 } else { 2 }; use VertexFormat::*; - vec![ - // Positions - VertexBufferLayout { - array_stride: Float32x3.size() * stride_multiplier, - step_mode: VertexStepMode::Instance, - attributes: vec![ - VertexAttribute { + if strip { + vec![ + // Positions + VertexBufferLayout { + array_stride: Float32x3.size(), + step_mode: VertexStepMode::Instance, + attributes: vec![VertexAttribute { format: Float32x3, offset: 0, shader_location: 0, - }, - VertexAttribute { + }], + }, + VertexBufferLayout { + array_stride: Float32x3.size(), + step_mode: VertexStepMode::Instance, + attributes: vec![VertexAttribute { format: Float32x3, - offset: Float32x3.size(), + offset: 0, shader_location: 1, - }, - ], - }, - // Colors - VertexBufferLayout { - array_stride: Float32x4.size() * stride_multiplier, - step_mode: VertexStepMode::Instance, - attributes: vec![ - VertexAttribute { + }], + }, + // Colors + VertexBufferLayout { + array_stride: Float32x4.size(), + step_mode: VertexStepMode::Instance, + attributes: vec![VertexAttribute { format: Float32x4, offset: 0, shader_location: 2, - }, - VertexAttribute { + }], + }, + VertexBufferLayout { + array_stride: Float32x4.size(), + step_mode: VertexStepMode::Instance, + attributes: vec![VertexAttribute { format: Float32x4, - offset: Float32x4.size(), + offset: 0, shader_location: 3, - }, - ], - }, - ] + }], + }, + ] + } else { + vec![ + // Positions + VertexBufferLayout { + array_stride: Float32x3.size() * 2, + step_mode: VertexStepMode::Instance, + attributes: vec![ + VertexAttribute { + format: Float32x3, + offset: 0, + shader_location: 0, + }, + VertexAttribute { + format: Float32x3, + offset: Float32x3.size(), + shader_location: 1, + }, + ], + }, + // Colors + VertexBufferLayout { + array_stride: Float32x4.size() * 2, + step_mode: VertexStepMode::Instance, + attributes: vec![ + VertexAttribute { + format: Float32x4, + offset: 0, + shader_location: 2, + }, + VertexAttribute { + format: Float32x4, + offset: Float32x4.size(), + shader_location: 3, + }, + ], + }, + ] + } } From 7fec0f30a73ab4bc5ac94854881389e3e65ddabb Mon Sep 17 00:00:00 2001 From: devil-ira Date: Wed, 21 Jun 2023 19:39:20 +0200 Subject: [PATCH 2/2] dry --- crates/bevy_gizmos/src/lib.rs | 115 +++++++++++++--------------------- 1 file changed, 43 insertions(+), 72 deletions(-) diff --git a/crates/bevy_gizmos/src/lib.rs b/crates/bevy_gizmos/src/lib.rs index 1a19dd9fb9249..69f5e685d03d5 100644 --- a/crates/bevy_gizmos/src/lib.rs +++ b/crates/bevy_gizmos/src/lib.rs @@ -479,83 +479,54 @@ impl RenderCommand

for DrawLineGizmo { fn line_gizmo_vertex_buffer_layouts(strip: bool) -> Vec { use VertexFormat::*; + let mut position_layout = VertexBufferLayout { + array_stride: Float32x3.size(), + step_mode: VertexStepMode::Instance, + attributes: vec![VertexAttribute { + format: Float32x3, + offset: 0, + shader_location: 0, + }], + }; + + let mut color_layout = VertexBufferLayout { + array_stride: Float32x4.size(), + step_mode: VertexStepMode::Instance, + attributes: vec![VertexAttribute { + format: Float32x4, + offset: 0, + shader_location: 2, + }], + }; + if strip { vec![ - // Positions - VertexBufferLayout { - array_stride: Float32x3.size(), - step_mode: VertexStepMode::Instance, - attributes: vec![VertexAttribute { - format: Float32x3, - offset: 0, - shader_location: 0, - }], - }, - VertexBufferLayout { - array_stride: Float32x3.size(), - step_mode: VertexStepMode::Instance, - attributes: vec![VertexAttribute { - format: Float32x3, - offset: 0, - shader_location: 1, - }], + position_layout.clone(), + { + position_layout.attributes[0].shader_location = 1; + position_layout }, - // Colors - VertexBufferLayout { - array_stride: Float32x4.size(), - step_mode: VertexStepMode::Instance, - attributes: vec![VertexAttribute { - format: Float32x4, - offset: 0, - shader_location: 2, - }], - }, - VertexBufferLayout { - array_stride: Float32x4.size(), - step_mode: VertexStepMode::Instance, - attributes: vec![VertexAttribute { - format: Float32x4, - offset: 0, - shader_location: 3, - }], + color_layout.clone(), + { + color_layout.attributes[0].shader_location = 3; + color_layout }, ] } else { - vec![ - // Positions - VertexBufferLayout { - array_stride: Float32x3.size() * 2, - step_mode: VertexStepMode::Instance, - attributes: vec![ - VertexAttribute { - format: Float32x3, - offset: 0, - shader_location: 0, - }, - VertexAttribute { - format: Float32x3, - offset: Float32x3.size(), - shader_location: 1, - }, - ], - }, - // Colors - VertexBufferLayout { - array_stride: Float32x4.size() * 2, - step_mode: VertexStepMode::Instance, - attributes: vec![ - VertexAttribute { - format: Float32x4, - offset: 0, - shader_location: 2, - }, - VertexAttribute { - format: Float32x4, - offset: Float32x4.size(), - shader_location: 3, - }, - ], - }, - ] + position_layout.array_stride *= 2; + position_layout.attributes.push(VertexAttribute { + format: Float32x3, + offset: Float32x3.size(), + shader_location: 1, + }); + + color_layout.array_stride *= 2; + color_layout.attributes.push(VertexAttribute { + format: Float32x4, + offset: Float32x4.size(), + shader_location: 3, + }); + + vec![position_layout, color_layout] } }