Skip to content

Commit b4f71ab

Browse files
Release 0.6.2
This is a hotfix for some dodgy vertex offset code. It still needs improving, but this fixes the root cause of the issue.
1 parent 2d2daa5 commit b4f71ab

File tree

5 files changed

+45
-35
lines changed

5 files changed

+45
-35
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ From 0.4.0 onwards, all breaking changes will be explicitly labelled, to make it
66

77
This project adheres to Semantic Versioning.
88

9+
## [0.6.2] - 2021-03-15
10+
11+
### Fixed
12+
13+
* `VertexBuffer::set_data` was mistakenly measuring its offset in individual floats, rather than vertices. This was inconsistent with `IndexBuffer`, and could potentially lead to corrupted data.
14+
* I do not believe this was a memory safety issue, as all writes were still valid and aligned - they were just in the wrong place!
15+
916
## [0.6.1] - 2021-03-15
1017

1118
### Added
@@ -737,7 +744,8 @@ for. This can be useful when implementing more complex animation behaviors. ([@V
737744

738745
* Initial release!
739746

740-
[Upcoming]: https://github.com/17cupsofcoffee/tetra/compare/0.6.1..HEAD
747+
[Upcoming]: https://github.com/17cupsofcoffee/tetra/compare/0.6.2..HEAD
748+
[0.6.2]: https://github.com/17cupsofcoffee/tetra/compare/0.6.1..0.6.2
741749
[0.6.1]: https://github.com/17cupsofcoffee/tetra/compare/0.6.0..0.6.1
742750
[0.6.0]: https://github.com/17cupsofcoffee/tetra/compare/0.5.8..0.6.0
743751
[0.5.8]: https://github.com/17cupsofcoffee/tetra/compare/0.5.7..0.5.8

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "tetra"
33
description = "A simple 2D game framework written in Rust"
4-
version = "0.6.1"
4+
version = "0.6.2"
55
edition = "2018"
66
authors = ["Joe Clay <[email protected]>"]
77
license = "MIT"

src/graphics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl GraphicsContext {
8686
window_width: i32,
8787
window_height: i32,
8888
) -> Result<GraphicsContext> {
89-
let vertex_buffer = device.new_vertex_buffer(MAX_VERTICES, 8, BufferUsage::Dynamic)?;
89+
let vertex_buffer = device.new_vertex_buffer(MAX_VERTICES, BufferUsage::Dynamic)?;
9090
let index_buffer = device.new_index_buffer(MAX_INDICES, BufferUsage::Static)?;
9191

9292
let indices: Vec<u32> = INDEX_ARRAY

src/graphics/mesh.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ impl VertexBuffer {
139139
vertices: &[Vertex],
140140
usage: BufferUsage,
141141
) -> Result<VertexBuffer> {
142-
let buffer = ctx.device.new_vertex_buffer(vertices.len(), 8, usage)?;
142+
let buffer = ctx.device.new_vertex_buffer(vertices.len(), usage)?;
143143

144-
ctx.device
145-
.set_vertex_buffer_data(&buffer, bytemuck::cast_slice(vertices), 0);
144+
ctx.device.set_vertex_buffer_data(&buffer, vertices, 0);
146145

147146
Ok(VertexBuffer {
148147
handle: Rc::new(buffer),
@@ -156,7 +155,7 @@ impl VertexBuffer {
156155
/// Panics if the offset is out of bounds.
157156
pub fn set_data(&self, ctx: &mut Context, vertices: &[Vertex], offset: usize) {
158157
ctx.device
159-
.set_vertex_buffer_data(&self.handle, bytemuck::cast_slice(vertices), offset);
158+
.set_vertex_buffer_data(&self.handle, vertices, offset);
160159
}
161160

162161
/// Creates a mesh using this buffer.

src/platform/device_gl.rs

+31-28
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
use std::cell::Cell;
2-
use std::mem;
32
use std::rc::Rc;
43

54
use glow::{Context as GlowContext, HasContext, PixelUnpackData};
65

76
use crate::error::{Result, TetraError};
8-
use crate::graphics::mesh::{BufferUsage, VertexWinding};
7+
use crate::graphics::mesh::{BufferUsage, Vertex, VertexWinding};
98
use crate::graphics::{BlendAlphaMode, BlendMode, FilterMode};
109
use crate::math::{Mat2, Mat3, Mat4, Vec2, Vec3, Vec4};
1110

12-
/// Utility function for calculating offsets/sizes.
13-
fn size<T>(elements: usize) -> i32 {
14-
(elements * mem::size_of::<T>()) as i32
15-
}
16-
1711
type BufferId = <GlowContext as HasContext>::Buffer;
1812
type ProgramId = <GlowContext as HasContext>::Program;
1913
type TextureId = <GlowContext as HasContext>::Texture;
@@ -149,7 +143,6 @@ impl GraphicsDevice {
149143
pub fn new_vertex_buffer(
150144
&mut self,
151145
count: usize,
152-
stride: usize,
153146
usage: BufferUsage,
154147
) -> Result<RawVertexBuffer> {
155148
unsafe {
@@ -163,16 +156,13 @@ impl GraphicsDevice {
163156
state: Rc::clone(&self.state),
164157
id,
165158
count,
166-
stride,
167159
};
168160

169161
self.bind_vertex_buffer(Some(&buffer));
170162

171-
self.state.gl.buffer_data_size(
172-
glow::ARRAY_BUFFER,
173-
size::<f32>(buffer.size()),
174-
usage.into(),
175-
);
163+
self.state
164+
.gl
165+
.buffer_data_size(glow::ARRAY_BUFFER, buffer.size() as i32, usage.into());
176166

177167
Ok(buffer)
178168
}
@@ -181,13 +171,13 @@ impl GraphicsDevice {
181171
pub fn set_vertex_buffer_data(
182172
&mut self,
183173
buffer: &RawVertexBuffer,
184-
data: &[f32],
174+
data: &[Vertex],
185175
offset: usize,
186176
) {
187177
self.bind_vertex_buffer(Some(buffer));
188178

189179
assert!(
190-
data.len() + offset <= buffer.size(),
180+
data.len() + offset <= buffer.count(),
191181
"tried to write out of bounds buffer data"
192182
);
193183

@@ -196,7 +186,7 @@ impl GraphicsDevice {
196186

197187
self.state.gl.buffer_sub_data_u8_slice(
198188
glow::ARRAY_BUFFER,
199-
size::<f32>(offset),
189+
(buffer.stride() * offset) as i32,
200190
bytemuck::cast_slice(data),
201191
);
202192
}
@@ -220,7 +210,7 @@ impl GraphicsDevice {
220210

221211
self.state.gl.buffer_data_size(
222212
glow::ELEMENT_ARRAY_BUFFER,
223-
size::<u32>(count),
213+
buffer.size() as i32,
224214
usage.into(),
225215
);
226216

@@ -241,7 +231,7 @@ impl GraphicsDevice {
241231

242232
self.state.gl.buffer_sub_data_u8_slice(
243233
glow::ELEMENT_ARRAY_BUFFER,
244-
size::<u32>(offset),
234+
(buffer.stride() * offset) as i32,
245235
bytemuck::cast_slice(data),
246236
);
247237
}
@@ -785,7 +775,7 @@ impl GraphicsDevice {
785775
glow::TRIANGLES,
786776
count as i32,
787777
glow::UNSIGNED_INT,
788-
size::<u32>(offset),
778+
(index_buffer.stride() * offset) as i32,
789779
);
790780
}
791781
}
@@ -806,7 +796,7 @@ impl GraphicsDevice {
806796
2,
807797
glow::FLOAT,
808798
false,
809-
size::<f32>(b.stride),
799+
b.stride() as i32,
810800
0,
811801
);
812802

@@ -815,17 +805,17 @@ impl GraphicsDevice {
815805
2,
816806
glow::FLOAT,
817807
false,
818-
size::<f32>(b.stride),
819-
size::<f32>(2),
808+
b.stride() as i32,
809+
8,
820810
);
821811

822812
self.state.gl.vertex_attrib_pointer_f32(
823813
2,
824814
4,
825815
glow::FLOAT,
826816
false,
827-
size::<f32>(b.stride),
828-
size::<f32>(4),
817+
b.stride() as i32,
818+
16,
829819
);
830820

831821
self.state.gl.enable_vertex_attrib_array(0);
@@ -1056,20 +1046,22 @@ pub struct RawVertexBuffer {
10561046
id: BufferId,
10571047

10581048
count: usize,
1059-
stride: usize,
10601049
}
10611050

10621051
impl RawVertexBuffer {
1052+
/// The number of vertices in the buffer.
10631053
pub fn count(&self) -> usize {
10641054
self.count
10651055
}
10661056

1057+
// The size of each vertex, in bytes.
10671058
pub fn stride(&self) -> usize {
1068-
self.stride
1059+
std::mem::size_of::<Vertex>()
10691060
}
10701061

1062+
/// The size of the buffer, in bytes.
10711063
pub fn size(&self) -> usize {
1072-
self.count * self.stride
1064+
self.count * self.stride()
10731065
}
10741066
}
10751067

@@ -1096,9 +1088,20 @@ pub struct RawIndexBuffer {
10961088
}
10971089

10981090
impl RawIndexBuffer {
1091+
/// The number of indices in the buffer.
10991092
pub fn count(&self) -> usize {
11001093
self.count
11011094
}
1095+
1096+
/// The size of each index, in bytes.
1097+
pub fn stride(&self) -> usize {
1098+
std::mem::size_of::<u32>()
1099+
}
1100+
1101+
/// The size of the buffer, in bytes.
1102+
pub fn size(&self) -> usize {
1103+
self.count * self.stride()
1104+
}
11021105
}
11031106

11041107
impl Drop for RawIndexBuffer {

0 commit comments

Comments
 (0)