Skip to content

Commit

Permalink
Increasing maximum vertex indices
Browse files Browse the repository at this point in the history
With the current dumb Gooey input implementation, the editor grows and
draws every glyph every frame. Pasting in the Advent of Code input for
day 1 caused too many indices to be queued, since the UI in Gooey is
gathered into a single IBO.
  • Loading branch information
ecton committed Dec 7, 2023
1 parent 138997a commit e0abd31
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1527,11 +1527,11 @@ impl BuildHasher for DefaultHasher {
#[derive(Default, Debug)]
struct VertexCollection<T> {
vertices: Vec<Vertex<T>>,
vertex_index_by_id: HashMap<VertexId, u16, DefaultHasher>,
vertex_index_by_id: HashMap<VertexId, u32, DefaultHasher>,
}

impl<T> VertexCollection<T> {
fn get_or_insert(&mut self, vertex: Vertex<T>) -> u16
fn get_or_insert(&mut self, vertex: Vertex<T>) -> u32
where
T: Copy,
Vertex<T>: Into<Vertex<i32>>,
Expand Down Expand Up @@ -1577,7 +1577,7 @@ where
&self.verticies
}

fn indices(&self) -> &[u16] {
fn indices(&self) -> &[u32] {
&[1, 0, 2, 1, 2, 3]
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub(crate) struct PushConstants {
#[derive(Debug)]
pub struct PreparedGraphic<Unit> {
pub(crate) vertices: Buffer<Vertex<Unit>>,
pub(crate) indices: Buffer<u16>,
pub(crate) indices: Buffer<u32>,
pub(crate) commands: SmallVec<[PreparedCommand; 2]>,
}

Expand Down Expand Up @@ -115,7 +115,7 @@ where
graphics.pass.set_vertex_buffer(0, self.vertices.as_slice());
graphics
.pass
.set_index_buffer(self.indices.as_slice(), wgpu::IndexFormat::Uint16);
.set_index_buffer(self.indices.as_slice(), wgpu::IndexFormat::Uint32);

for command in &self.commands {
if graphics.clip.current.size.is_zero() {
Expand Down
15 changes: 8 additions & 7 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'render, 'gfx> Renderer<'render, 'gfx> {
for &vertex_index in shape.source.indices() {
self.data
.indices
.push(vertex_map[usize::from(vertex_index)]);
.push(vertex_map[usize::try_from(vertex_index).assert("too many drawn indices")]);
}

let mut flags = Unit::flags();
Expand Down Expand Up @@ -252,6 +252,7 @@ mod text {

use figures::units::Px;
use figures::{Fraction, ScreenScale, ScreenUnit, UnscaledUnit};
use intentional::Assert;

use super::{
Angle, Color, Command, IntoSigned, Point, PushConstants, Renderer, Vertex, Zero,
Expand Down Expand Up @@ -449,14 +450,14 @@ mod text {
clip_index: u32,
dpi_scale: Fraction,
vertices: &mut VertexCollection<i32>,
indices: &mut Vec<u16>,
indices: &mut Vec<u32>,
textures: &mut HashMap<TextureId, Arc<wgpu::BindGroup>, DefaultHasher>,
commands: &mut Vec<Command>,
) where
Unit: ScreenUnit,
{
let translation = translation.into_px(dpi_scale).map(Px::into_unscaled);
let corners: [u16; 4] = array::from_fn(|index| {
let corners: [u32; 4] = array::from_fn(|index| {
let vertex = &blit.verticies[index];
vertices.get_or_insert(Vertex {
location: vertex.location.into_signed().map(Px::into_unscaled),
Expand All @@ -466,7 +467,7 @@ mod text {
});
let start_index = u32::try_from(indices.len()).expect("too many drawn indices");
for &index in blit.indices() {
indices.push(corners[usize::from(index)]);
indices.push(corners[usize::try_from(index).assert("too many drawn indices")]);
}
let mut flags = Px::flags() | FLAG_TEXTURED;
if let hash_map::Entry::Vacant(vacant) = textures.entry(cached.texture.id()) {
Expand Down Expand Up @@ -566,7 +567,7 @@ pub struct Drawing {
vertices: VertexCollection<i32>,
clips: Vec<Rect<UPx>>,
clip_lookup: HashMap<Rect<UPx>, u32, DefaultHasher>,
indices: Vec<u16>,
indices: Vec<u32>,
textures: HashMap<sealed::TextureId, Arc<wgpu::BindGroup>, DefaultHasher>,
commands: Vec<Command>,
#[cfg(feature = "cosmic-text")]
Expand All @@ -577,7 +578,7 @@ pub struct Drawing {
#[derive(Debug)]
struct RenderingBuffers {
vertex: DiffableBuffer<Vertex<i32>>,
index: DiffableBuffer<u16>,
index: DiffableBuffer<u32>,
}

impl Drawing {
Expand Down Expand Up @@ -626,7 +627,7 @@ impl Drawing {
.set_vertex_buffer(0, buffers.vertex.as_slice());
graphics
.pass
.set_index_buffer(buffers.index.as_slice(), wgpu::IndexFormat::Uint16);
.set_index_buffer(buffers.index.as_slice(), wgpu::IndexFormat::Uint32);

let mut current_clip_index = 0;
let mut current_clip = graphics.clip.current.0;
Expand Down
2 changes: 1 addition & 1 deletion src/sealed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub trait TextureSource {

pub trait ShapeSource<Unit> {
fn vertices(&self) -> &[Vertex<Unit>];
fn indices(&self) -> &[u16];
fn indices(&self) -> &[u32];
fn prepare(
&self,
texture: Option<&impl TextureSource>,
Expand Down
20 changes: 7 additions & 13 deletions src/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ use crate::{
#[derive(Debug, Clone, PartialEq)]
pub struct Shape<Unit, const TEXTURED: bool> {
pub(crate) vertices: SmallVec<[Vertex<Unit>; 6]>,
pub(crate) indices: SmallVec<[u16; 20]>,
pub(crate) indices: SmallVec<[u32; 20]>,
}

#[test]
fn shape_size() {
assert_eq!(std::mem::size_of::<Shape<i32, true>>(), 176);
assert_eq!(std::mem::size_of::<Shape<i32, true>>(), 216);
}

impl<Unit, const TEXTURED: bool> Default for Shape<Unit, TEXTURED> {
Expand Down Expand Up @@ -221,7 +221,7 @@ where
&self.vertices
}

fn indices(&self) -> &[u16] {
fn indices(&self) -> &[u32] {
&self.indices
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@ where
.map_err(|_| GeometryBuilderError::TooManyVertices)?,
);
self.shape.vertices.push(vertex);
if self.shape.vertices.len() > u16::MAX as usize {
if self.shape.vertices.len() > u32::MAX as usize {
return Err(GeometryBuilderError::TooManyVertices);
}

Expand Down Expand Up @@ -339,15 +339,9 @@ where
fn end_geometry(&mut self) {}

fn add_triangle(&mut self, a: VertexId, b: VertexId, c: VertexId) {
self.shape
.indices
.push(a.0.try_into().expect("checked in new_vertex"));
self.shape
.indices
.push(b.0.try_into().expect("checked in new_vertex"));
self.shape
.indices
.push(c.0.try_into().expect("checked in new_vertex"));
self.shape.indices.push(a.0);
self.shape.indices.push(b.0);
self.shape.indices.push(c.0);
}

fn abort_geometry(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ impl<'gfx> Graphics<'gfx> {
self.queue,
&mut glyphs,
|blit, cached, _glyph, _is_first_line, _baseline, _line_w| {
let corners: [u16; 4] =
let corners: [u32; 4] =
array::from_fn(|index| vertices.get_or_insert(blit.verticies[index]));
let start_index = u32::try_from(indices.len()).assert("too many drawn indices");
for &index in blit.indices() {
indices.push(corners[usize::from(index)]);
indices.push(corners[usize::try_from(index).assert("too many drawn indices")]);
}
let end_index = u32::try_from(indices.len()).assert("too many drawn indices");
match commands.last_mut() {
Expand Down

0 comments on commit e0abd31

Please sign in to comment.