Use u16 for indices

This commit is contained in:
Maximilian Ammann 2022-01-08 11:15:43 +01:00
parent ebc7476266
commit 59ebdc4f93
4 changed files with 53 additions and 43 deletions

View File

@ -39,7 +39,11 @@ impl Default for SceneParams {
}
}
const INDEX_FORMAT: wgpu::IndexFormat = wgpu::IndexFormat::Uint32;
const INDEX_FORMAT: wgpu::IndexFormat = wgpu::IndexFormat::Uint16; // Must match IndexDataType
const VERTEX_BUFFER_SIZE: BufferAddress = 1024 * 1024 * 16;
const INDICES_BUFFER_SIZE: BufferAddress = 1024 * 1024 * 16;
const TILE_META_COUNT: BufferAddress = 512;
const TILE_MASK_INSTANCE_COUNT: BufferAddress = 512;
pub struct State {
instance: wgpu::Instance,
@ -86,11 +90,6 @@ impl SceneParams {
}
}
const VERTEX_BUFFER_SIZE: BufferAddress = 1024 * 1024 * 32;
const INDICES_BUFFER_SIZE: BufferAddress = 1024 * 1024 * 32;
const TILE_META_COUNT: BufferAddress = 512;
const TILE_MASK_INSTANCE_COUNT: BufferAddress = 512;
impl State {
pub async fn new(window: &Window) -> Self {
let mut measure = Measure::time();

View File

@ -14,7 +14,7 @@ use vector_tile::geometry::{Command, Geometry};
use vector_tile::tile::Tile;
use crate::render::shader_ffi::GpuVertexUniform;
use crate::tesselation::{Tesselated, WithId, DEFAULT_TOLERANCE};
use crate::tesselation::{IndexDataType, Tesselated, VertexConstructor, DEFAULT_TOLERANCE};
pub struct RustLogo();
@ -25,8 +25,7 @@ impl<
fn tesselate_stroke(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
prim_id: u32,
) -> Range<u32> {
) -> Range<IndexDataType> {
let mut stroke_tess = StrokeTessellator::new();
let initial_indices_count = buffer.indices.len();
@ -40,18 +39,17 @@ impl<
.tessellate_path(
&rust_logo,
&StrokeOptions::tolerance(DEFAULT_TOLERANCE),
&mut BuffersBuilder::new(buffer, WithId(prim_id)),
&mut BuffersBuilder::new(buffer, VertexConstructor()),
)
.unwrap();
initial_indices_count as u32..buffer.indices.len() as u32
initial_indices_count as IndexDataType..buffer.indices.len() as IndexDataType
}
fn tesselate_fill(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
prim_id: u32,
) -> Range<u32> {
) -> Range<IndexDataType> {
let mut fill_tess = FillTessellator::new();
let initial_indices_count = buffer.indices.len();
@ -66,10 +64,10 @@ impl<
&rust_logo,
&FillOptions::tolerance(DEFAULT_TOLERANCE)
.with_fill_rule(lyon_path::FillRule::NonZero),
&mut BuffersBuilder::new(buffer, WithId(prim_id as u32)),
&mut BuffersBuilder::new(buffer, VertexConstructor()),
)
.unwrap();
initial_indices_count as u32..buffer.indices.len() as u32
initial_indices_count as IndexDataType..buffer.indices.len() as IndexDataType
}
}

View File

@ -9,46 +9,55 @@ use std::ops::Range;
const DEFAULT_TOLERANCE: f32 = 0.02;
pub type IndexDataType = u32; // Must match INDEX_FORMAT
pub type IndexDataType = u16; // Must match INDEX_FORMAT
pub trait Tesselated<OutputIndex: std::ops::Add> {
fn tesselate_stroke(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
prim_id: u32,
) -> Range<u32>;
) -> Range<IndexDataType>;
fn tesselate_fill(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
prim_id: u32,
) -> Range<u32>;
) -> Range<IndexDataType>;
fn empty_range(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
_prim_id: u32,
) -> Range<u32> {
let initial_indices_count = buffer.indices.len() as u32;
) -> Range<IndexDataType> {
let initial_indices_count = buffer.indices.len() as IndexDataType;
initial_indices_count..initial_indices_count
}
}
/// This vertex constructor forwards the positions and normals provided by the
/// tessellators and add a shape id.
pub struct WithId(pub u32);
pub struct VertexConstructor();
impl FillVertexConstructor<GpuVertexUniform> for WithId {
impl FillVertexConstructor<GpuVertexUniform> for VertexConstructor {
fn new_vertex(&mut self, vertex: FillVertex) -> GpuVertexUniform {
GpuVertexUniform::new(vertex.position().to_array(), [0.0, 0.0], self.0)
GpuVertexUniform::new(vertex.position().to_array(), [0.0, 0.0])
}
}
impl StrokeVertexConstructor<GpuVertexUniform> for WithId {
impl StrokeVertexConstructor<GpuVertexUniform> for VertexConstructor {
fn new_vertex(&mut self, vertex: StrokeVertex) -> GpuVertexUniform {
GpuVertexUniform::new(
vertex.position_on_path().to_array(),
vertex.normal().to_array(),
self.0,
)
}
}
trait Align<V: bytemuck::Pod, I: bytemuck::Pod> {
fn align_indices(&mut self);
}
impl<V: bytemuck::Pod, I: bytemuck::Pod> Align<V, I> for VertexBuffers<V, I> {
fn align_indices(&mut self) {
let alignment = wgpu::COPY_BUFFER_ALIGNMENT as usize / std::mem::size_of::<I>();
let padding = self.indices.len() % alignment;
if padding > 0 {
self.indices
.extend(std::iter::repeat(I::zeroed()).take(alignment - padding));
}
}
}

View File

@ -1,3 +1,5 @@
use std::ops::Range;
use lyon::lyon_tessellation::LineJoin;
use lyon::tessellation;
use lyon::tessellation::geometry_builder::MaxIndex;
@ -7,13 +9,12 @@ use lyon::tessellation::{
};
use lyon_path::builder::SvgPathBuilder;
use lyon_path::Path;
use std::ops::Range;
use vector_tile::geometry::{Command, Geometry};
use vector_tile::tile::Tile;
use crate::render::shader_ffi::GpuVertexUniform;
use crate::tesselation::{Tesselated, WithId, DEFAULT_TOLERANCE};
use crate::tesselation::{Align, IndexDataType, Tesselated, VertexConstructor, DEFAULT_TOLERANCE};
fn build_path(tile: &Tile, fill: bool) -> Path {
let mut tile_builder = Path::builder().with_svg();
@ -85,14 +86,16 @@ fn build_path(tile: &Tile, fill: bool) -> Path {
}
impl<
OutputIndex: std::ops::Add + std::convert::From<lyon::lyon_tessellation::VertexId> + MaxIndex,
OutputIndex: std::ops::Add
+ std::convert::From<lyon::lyon_tessellation::VertexId>
+ MaxIndex
+ bytemuck::Pod,
> Tesselated<OutputIndex> for Tile
{
fn tesselate_stroke(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
prim_id: u32,
) -> Range<u32> {
) -> Range<IndexDataType> {
let mut tesselator = StrokeTessellator::new();
let initial_indices_count = buffer.indices.len();
@ -102,19 +105,20 @@ impl<
tesselator
.tessellate_path(
&tile_path,
&StrokeOptions::default(),
&mut BuffersBuilder::new(buffer, WithId(prim_id)),
&StrokeOptions::tolerance(DEFAULT_TOLERANCE),
&mut BuffersBuilder::new(buffer, VertexConstructor()),
)
.unwrap();
initial_indices_count as u32..buffer.indices.len() as u32
buffer.align_indices();
initial_indices_count as IndexDataType..buffer.indices.len() as IndexDataType
}
fn tesselate_fill(
&self,
buffer: &mut VertexBuffers<GpuVertexUniform, OutputIndex>,
prim_id: u32,
) -> Range<u32> {
) -> Range<IndexDataType> {
let mut tesselator = FillTessellator::new();
let initial_indices_count = buffer.indices.len();
@ -124,11 +128,11 @@ impl<
tesselator
.tessellate_path(
&tile_path,
&FillOptions::default(),
&mut BuffersBuilder::new(buffer, WithId(prim_id)),
&FillOptions::tolerance(DEFAULT_TOLERANCE),
&mut BuffersBuilder::new(buffer, VertexConstructor()),
)
.unwrap();
initial_indices_count as u32..buffer.indices.len() as u32
initial_indices_count as IndexDataType..buffer.indices.len() as IndexDataType
}
}