Skip to content

Commit

Permalink
Clean-up, dead code, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 committed Dec 23, 2024
1 parent 33f7713 commit b69d724
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 89 deletions.
94 changes: 17 additions & 77 deletions crates/vsvg-viewer/src/painters/line_painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,10 @@
//! compliant with the [WebGPU spec](https://gpuweb.github.io/gpuweb/#dictdef-gpuvertexbufferlayout).
//! As such, it is rejected by (recent versions of) Chrome and wgpu/metal since (23.0.0).
//!
//! To work around this limitation, we bind the point buffer four times, with an offset of one to
//! three vertices, respectively. Each of these bindings gets a stride of one vertex and exposes one
//! vertex to the shader, which is spec compliant.
//! Instead, we bind points as a read-only storage buffer and directly look up vertex data in that
//! buffer.
//!
//! ```text
//! first last
//! instance instance
//! │ │
//! ▼ ▼
//! ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬ ─ ─ ─ ─ ┬ ─ ─
//! p0 │ A0 │ A0 │ A1 │ A1 │ B0 │ B0 │ B1 │ B2 │ B3 │ B3 │ C2 │ C0 │ C1 │ C2 │ C0 C1 │
//! └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴ ─ ─ ─ ─ ┴ ─ ─
//! ┌ ─ ─┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬ ─ ─ ─ ─ ┐
//! p1 A0 │ A0 │ A1 │ A1 │ B0 │ B0 │ B1 │ B2 │ B3 │ B3 │ C2 │ C0 │ C1 │ C2 │ C0 │ C1 offset = 1
//! └ ─ ─└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴ ─ ─ ─ ─ ┘
//! ─ ─ ┬ ─ ─┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬ ─ ─
//! p2 │ A0 A0 │ A1 │ A1 │ B0 │ B0 │ B1 │ B2 │ B3 │ B3 │ C2 │ C0 │ C1 │ C2 │ C0 │ C1 │ offset = 2
//! ─ ─ ┴ ─ ─└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴ ─ ─
//! ┌ ─ ─ ─ ─ ┬ ─ ─┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
//! p3 A0 │ A0 A1 │ A1 │ B0 │ B0 │ B1 │ B2 │ B3 │ B3 │ C2 │ C0 │ C1 │ C2 │ C0 │ C1 │ offset = 3
//! └ ─ ─ ─ ─ ┴ ─ ─└────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
//! point buffer (bound four times)
//!
//!
//!
//! ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐
//! │ A0 │ │ │ │ B0 │ B1 │ B2 │ │ │ │ C0 │ C1 │ C2 │
//! └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘
//! attributes buffer
//! ```
//! Note: this is not compatible with WebGL.
use wgpu::{
include_wgsl, util::DeviceExt, vertex_attr_array, Buffer, ColorTargetState, PrimitiveTopology,
Expand Down Expand Up @@ -179,14 +153,14 @@ impl LinePainterData {

let (vertices, attribs) = Self::build_buffers(paths, display_options);

// prepare point buffer
// prepare point buffer – used as a storage binding!
let points_buffer =
render_objects
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Point instance buffer"),
contents: bytemuck::cast_slice(vertices.as_slice()),
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::STORAGE,
usage: wgpu::BufferUsages::STORAGE,
});

// prepare color buffer
Expand Down Expand Up @@ -288,38 +262,14 @@ pub(crate) struct LinePainter {

impl LinePainter {
pub(crate) fn new(render_objects: &EngineRenderObjects) -> Self {
// This is where we prepare the 4x binding of the same point buffer. Each binding has a
// stride of one vertex but a different starting offset.

let vertex_attributes = (0..4)
.map(|i| wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: i,
})
.collect::<Vec<_>>();

let mut buffer_layouts = vertex_attributes
.iter()
.map(|vertex_attrb| wgpu::VertexBufferLayout {
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: std::slice::from_ref(vertex_attrb),
})
.collect::<Vec<_>>();

// add the color and width attributes

let vertex_attrib_color_width = vertex_attr_array![
4 => Uint32,
5 => Float32,
];

buffer_layouts.push(wgpu::VertexBufferLayout {
let vertex_attrib_buffer_layout = wgpu::VertexBufferLayout {
array_stride: size_of::<Attribute>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &vertex_attrib_color_width,
});
attributes: &vertex_attr_array![
0 => Uint32,
1 => Float32,
],
};

let points_bind_group_layout =
render_objects
Expand Down Expand Up @@ -382,7 +332,7 @@ impl LinePainter {
module: &shader,
entry_point: Some("vs_main"),
compilation_options: Default::default(),
buffers: &buffer_layouts,
buffers: &[vertex_attrib_buffer_layout],
},
fragment: Some(wgpu::FragmentState {
module: &shader,
Expand Down Expand Up @@ -418,17 +368,11 @@ impl Painter for LinePainter {
render_objects: &EngineRenderObjects,
data: &LinePainterData,
) {
// `Buffer::slice(..)` panics for empty buffers in wgpu 23+
if data.points_buffer.size() == 0 {
// This also avoids the fact that `Buffer::slice(..)` panics for empty buffers in wgpu 23+
if data.instance_count == 0 {
return;
}

rpass.set_pipeline(&self.render_pipeline);
rpass.set_bind_group(0, &render_objects.camera_bind_group, &[]);

//////
//TODO: this could be done once

let points_bind_group =
render_objects
.device
Expand All @@ -441,15 +385,11 @@ impl Painter for LinePainter {
label: Some("point_bind_group"),
});

rpass.set_pipeline(&self.render_pipeline);
rpass.set_bind_group(0, &render_objects.camera_bind_group, &[]);
rpass.set_bind_group(1, &points_bind_group, &[]);
rpass.set_vertex_buffer(0, data.attributes_buffer.slice(..));

let offset = size_of::<Vertex>() as u64;
rpass.set_vertex_buffer(0, data.points_buffer.slice(..));
rpass.set_vertex_buffer(1, data.points_buffer.slice(offset..));
rpass.set_vertex_buffer(2, data.points_buffer.slice((2 * offset)..));
rpass.set_vertex_buffer(3, data.points_buffer.slice((3 * offset)..));

rpass.set_vertex_buffer(4, data.attributes_buffer.slice(..));
rpass.draw(0..4, 0..data.instance_count);
}
}
20 changes: 8 additions & 12 deletions crates/vsvg-viewer/src/shaders/line.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ struct CameraUniform {
};

@group(0) @binding(0) var<uniform> camera: CameraUniform;

// Vertex data.
//
// This buffer contains all the line vertices with the starting and ending vertices duplicated (or wrapped, for closed
// lines). The lenght of this array is `instance_count` + 4.
@group(1) @binding(0) var<storage, read> points: array<vec2<f32>>;

struct InstanceInput {
@location(0) p0: vec2<f32>,
@location(1) p1: vec2<f32>,
@location(2) p2: vec2<f32>,
@location(3) p3: vec2<f32>,
@location(4) color: u32,
@location(5) width: f32,
@location(0) color: u32,
@location(1) width: f32,
};

struct VertexOutput {
Expand Down Expand Up @@ -80,12 +81,7 @@ fn vs_main(

let w2 = instance.width/2. + (camera.anti_alias / camera.scale) / 2.;


// let p0 = instance.p0;
// let p1 = instance.p1;
// let p2 = instance.p2;
// let p3 = instance.p3;

// sliding window over the points buffer
let p0 = points[in_instance_index + 0];
let p1 = points[in_instance_index + 1];
let p2 = points[in_instance_index + 2];
Expand Down

0 comments on commit b69d724

Please sign in to comment.