Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to instance #539

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions sugarloaf/src/components/rich_text/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ struct Batch {
image: Option<TextureId>,
mask: Option<TextureId>,
vertices: Vec<Vertex>,
indices: Vec<u32>,
subpix: bool,
}

Expand All @@ -62,7 +61,6 @@ impl Batch {
self.image = None;
self.mask = None;
self.vertices.clear();
self.indices.clear();
self.subpix = false;
}

Expand Down Expand Up @@ -151,33 +149,18 @@ impl Batch {
uv: [r, t],
},
];
let base = self.vertices.len() as u32;
self.vertices.extend_from_slice(&verts);
self.indices.extend_from_slice(&[
base,
base + 1,
base + 2,
base + 2,
base,
base + 3,
]);
}

#[inline]
fn build_display_list(&self, list: &mut DisplayList) {
let first_vertex = list.vertices.len() as u32;
let first_index = list.indices.len() as u32;
list.vertices.extend_from_slice(&self.vertices);
list.indices
.extend(self.indices.iter().map(|i| *i + first_vertex));
if let Some(tex) = self.mask {
list.commands.push(Command::BindTexture(0, tex));
}
if let Some(tex) = self.image {
list.commands.push(Command::BindTexture(1, tex));
}
list.indices_to_draw
.push((first_index, first_index + self.indices.len() as u32));
}
}

Expand Down Expand Up @@ -336,8 +319,6 @@ impl BatchManager {
#[derive(Default, Debug, Clone)]
pub struct DisplayList {
vertices: Vec<Vertex>,
indices: Vec<u32>,
indices_to_draw: Vec<(u32, u32)>,
commands: Vec<Command>,
}

Expand All @@ -354,18 +335,6 @@ impl DisplayList {
&self.vertices
}

/// Returns the buffered indices to draw.
#[inline]
pub fn indices_to_draw(&self) -> &[(u32, u32)] {
&self.indices_to_draw
}

/// Returns the buffered indices for the display list.
#[inline]
pub fn indices(&self) -> &[u32] {
&self.indices
}

/// Returns the sequence of display commands.
#[inline]
pub fn commands(&self) -> &[Command] {
Expand All @@ -376,9 +345,7 @@ impl DisplayList {
#[inline]
pub fn clear(&mut self) {
self.vertices.clear();
self.indices.clear();
self.commands.clear();
self.indices_to_draw.clear();
}
}

Expand Down
74 changes: 14 additions & 60 deletions sugarloaf/src/components/rich_text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,22 @@ pub struct RichTextBrush {
bind_group_layout: wgpu::BindGroupLayout,
pipeline: wgpu::RenderPipeline,
textures: FxHashMap<TextureId, Texture>,
index_buffer: wgpu::Buffer,
index_buffer_size: u64,
current_transform: [f32; 16],
comp: Compositor,
draw_layout_cache: DrawLayoutCache,
dlist: DisplayList,
bind_group_needs_update: bool,
first_run: bool,
supported_vertex_buffer: usize,
images: ImageCache,
glyphs: GlyphCache,
vertices_quantity: usize,
}

impl RichTextBrush {
pub fn new(context: &Context) -> Self {
let device = &context.device;
let dlist = DisplayList::new();
let supported_vertex_buffer = 2_000;
let vertices_quantity = 1;

let current_transform =
orthographic_projection(context.size.width, context.size.height);
Expand Down Expand Up @@ -228,9 +226,9 @@ impl RichTextBrush {
module: &shader,
entry_point: "vs_main",
buffers: &[wgpu::VertexBufferLayout {
array_stride: mem::size_of::<Vertex>() as u64,
array_stride: mem::size_of::<Vertex>() as wgpu::BufferAddress,
// https://docs.rs/wgpu/latest/wgpu/enum.VertexStepMode.html
step_mode: wgpu::VertexStepMode::Vertex,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &wgpu::vertex_attr_array!(
0 => Float32x4,
1 => Float32x4,
Expand All @@ -248,32 +246,24 @@ impl RichTextBrush {
write_mask: wgpu::ColorWrites::ALL,
})],
}),
primitive: wgpu::PrimitiveState::default(),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
..Default::default()
},
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
});

let vertex_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("rich_text::Instances Buffer"),
size: mem::size_of::<Vertex>() as u64 * supported_vertex_buffer as u64,
size: mem::size_of::<Vertex>() as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});

let index_buffer_size: &[u32] = bytemuck::cast_slice(dlist.indices());
let index_buffer_size = index_buffer_size.len() as u64;
let index_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("rich_text::Indices Buffer"),
size: index_buffer_size,
usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});

RichTextBrush {
bind_group_layout,
index_buffer_size,
index_buffer,
color_texture_view,
mask_texture_view,
sampler,
Expand All @@ -289,7 +279,7 @@ impl RichTextBrush {
vertex_buffer,
first_run: true,
bind_group_needs_update: true,
supported_vertex_buffer,
vertices_quantity,
current_transform,
}
}
Expand Down Expand Up @@ -373,7 +363,6 @@ impl RichTextBrush {
) {
// let start = std::time::Instant::now();
let vertices: &[Vertex] = self.dlist.vertices();
let indices: &[u32] = self.dlist.indices();

// There's nothing to render
if vertices.is_empty() {
Expand All @@ -393,14 +382,14 @@ impl RichTextBrush {
self.current_transform = transform;
}

if vertices.len() > self.supported_vertex_buffer {
if vertices.len() > self.vertices_quantity {
self.vertex_buffer.destroy();

self.supported_vertex_buffer = vertices.len();
self.vertices_quantity = vertices.len();
self.vertex_buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("sugarloaf::rich_text::Pipeline instances"),
size: mem::size_of::<Vertex>() as u64
* self.supported_vertex_buffer as u64,
* self.vertices_quantity as u64,
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
Expand All @@ -411,29 +400,6 @@ impl RichTextBrush {
queue.write_buffer(&self.vertex_buffer, 0, vertices_bytes);
}

let indices_raw: &[u8] = bytemuck::cast_slice(indices);
let indices_raw_size = indices_raw.len() as u64;

if self.index_buffer_size >= indices_raw_size {
queue.write_buffer(&self.index_buffer, 0, indices_raw);
} else {
self.index_buffer.destroy();

let size = next_copy_buffer_size(indices_raw_size);
let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("rich_text::Indices"),
size,
usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: true,
});
buffer.slice(..).get_mapped_range_mut()[..indices_raw.len()]
.copy_from_slice(indices_raw);
buffer.unmap();

self.index_buffer = buffer;
self.index_buffer_size = size;
}

let mut color_texture_updated: Option<&TextureId> = None;
let mut mask_texture_updated: Option<&TextureId> = None;

Expand Down Expand Up @@ -531,12 +497,7 @@ impl RichTextBrush {
rpass.set_pipeline(&self.pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_vertex_buffer(0, self.vertex_buffer.slice(..));
rpass.set_index_buffer(self.index_buffer.slice(..), wgpu::IndexFormat::Uint32);

// Draw the specified range of indexed triangles.
for items in self.dlist.indices_to_draw() {
rpass.draw_indexed(items.0..items.1, 0, 0..1);
}
rpass.draw(0..4, 0..vertices.len() as u32);

self.bind_group_needs_update = false;
self.first_run = false;
Expand Down Expand Up @@ -925,10 +886,3 @@ fn fetch_dimensions(

dimension
}

#[inline]
fn next_copy_buffer_size(size: u64) -> u64 {
let align_mask = wgpu::COPY_BUFFER_ALIGNMENT - 1;
((size.next_power_of_two() + align_mask) & !align_mask)
.max(wgpu::COPY_BUFFER_ALIGNMENT)
}
3 changes: 2 additions & 1 deletion sugarloaf/src/components/rich_text/rich_text.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Globals {
@group(0) @binding(3) var font_sampler: sampler;

struct VertexInput {
@builtin(vertex_index) vertex_index: u32,
@builtin(vertex_index) v_index: u32,
@location(0) v_pos: vec4<f32>,
@location(1) v_color: vec4<f32>,
@location(2) v_uv: vec2<f32>,
Expand All @@ -28,6 +28,7 @@ fn vs_main(input: VertexInput) -> VertexOutput {
out.f_color = input.v_color;
out.f_uv = input.v_uv;

let v = input.v_index;
var use_tex: i32 = 0;
var use_mask: i32 = 0;

Expand Down
Loading