Remove padding in INDICES

pull/304/head
Hiroaki Yutani 2 years ago
parent e760f9f50c
commit 8c6c491b11

@ -58,7 +58,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
struct State {
surface: wgpu::Surface,

@ -58,7 +58,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
struct State {
surface: wgpu::Surface,

@ -61,7 +61,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
struct State {
surface: wgpu::Surface,

@ -61,7 +61,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
struct State {
surface: wgpu::Surface,

@ -61,7 +61,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(

@ -61,7 +61,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(

@ -62,7 +62,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(

@ -69,7 +69,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(

@ -62,7 +62,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
const DEPTH_VERTICES: &[Vertex] = &[
Vertex {

@ -62,7 +62,7 @@ const VERTICES: &[Vertex] = &[
}, // E
];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4, /* padding */ 0];
const INDICES: &[u16] = &[0, 1, 4, 1, 2, 4, 2, 3, 4];
#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(

@ -335,11 +335,10 @@ const INDICES: &[u16] = &[
0, 1, 4,
1, 2, 4,
2, 3, 4,
/* padding */ 0,
];
```
Now with this setup our `VERTICES` take up about 120 bytes and `INDICES` is just 18 bytes given that `u16` is 2 bytes wide. We add 2 bytes padding as wgpu requires buffers to be aligned to 4 bytes. All together our pentagon is 134 bytes in total. That means we saved 82 bytes! It may not seem like much, but when dealing with tri counts in the hundreds of thousands, indexing saves a lot of memory.
Now with this setup our `VERTICES` take up about 120 bytes and `INDICES` is just 18 bytes given that `u16` is 2 bytes wide. All together our pentagon is 134 bytes in total. That means we saved 82 bytes! It may not seem like much, but when dealing with tri counts in the hundreds of thousands, indexing saves a lot of memory.
There's a couple of things we need to change in order to use indexing. The first is we need to create a buffer to store the indices. In `State`'s `new()` method create the `index_buffer` after you create the `vertex_buffer`. Also change `num_vertices` to `num_indices` and set it equal to `INDICES.len()`.

Loading…
Cancel
Save