Skip to content

Commit

Permalink
allow positional buffer updates
Browse files Browse the repository at this point in the history
  • Loading branch information
thefiredman committed Feb 8, 2024
1 parent 830864c commit b70c272
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,7 @@ pub trait RenderingBackend {
fn new_buffer(&mut self, type_: BufferType, usage: BufferUsage, data: BufferSource)
-> BufferId;
fn buffer_update(&mut self, buffer: BufferId, data: BufferSource);
fn buffer_update_at(&mut self, buffer: BufferId, data: BufferSource, at: usize);

/// Size of buffer in bytes.
/// For 1 element, u16 buffer this will return 2.
Expand Down
9 changes: 6 additions & 3 deletions src/graphics/gl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ impl RenderingBackend for GlContext {
BufferId(self.buffers.len() - 1)
}

fn buffer_update(&mut self, buffer: BufferId, data: BufferSource) {
fn buffer_update_at(&mut self, buffer: BufferId, data: BufferSource, at: usize) {
let data = match data {
BufferSource::Slice(data) => data,
_ => panic!("buffer_update expects BufferSource::slice"),
Expand All @@ -1201,11 +1201,14 @@ impl RenderingBackend for GlContext {
let gl_target = gl_buffer_target(&buffer.buffer_type);
self.cache.store_buffer_binding(gl_target);
self.cache
.bind_buffer(gl_target, buffer.gl_buf, buffer.index_type);
unsafe { glBufferSubData(gl_target, 0, size as _, data.ptr as _) };
.bind_buffer(gl_target, buffer.gl_buf, buffer.index_type); unsafe { glBufferSubData(gl_target, (at * size) as i64, size as _, data.ptr as _) };

Check failure on line 1204 in src/graphics/gl.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, armv7-unknown-linux-gnueabihf)

mismatched types

Check failure on line 1204 in src/graphics/gl.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, wasm32-unknown-unknown)

mismatched types

Check failure on line 1204 in src/graphics/gl.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

mismatched types

Check failure on line 1204 in src/graphics/gl.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

mismatched types
self.cache.restore_buffer_binding(gl_target);
}

fn buffer_update(&mut self, buffer: BufferId, data: BufferSource) {
self.buffer_update_at(buffer, data, 0);
}

/// Size of buffer in bytes
fn buffer_size(&mut self, buffer: BufferId) -> usize {
self.buffers[buffer.0].size
Expand Down
8 changes: 6 additions & 2 deletions src/graphics/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ impl RenderingBackend for MetalContext {
BufferId(self.buffers.len() - 1)
}

fn buffer_update(&mut self, buffer: BufferId, data: BufferSource) {
fn buffer_update_at(&mut self, buffer: BufferId, data: BufferSource, at: usize) {
let data = match data {
BufferSource::Slice(data) => data,
_ => panic!("buffer_update expects BufferSource::slice"),
Expand All @@ -639,11 +639,15 @@ impl RenderingBackend for MetalContext {
std::ptr::copy(data.ptr, dest, data.size);

#[cfg(target_os = "macos")]
msg_send_![buffer.raw[buffer.next_value], didModifyRange:NSRange::new(0, data.size as u64)];
msg_send_![buffer.raw[buffer.next_value], didModifyRange:NSRange::new((at * data.size) as u64, data.size as u64)];
}
buffer.value = buffer.next_value;
}

fn buffer_update(&mut self, buffer: BufferId, data: BufferSource) {
self.buffer_update_at(buffer, data, 0);
}

fn new_shader(
&mut self,
shader: ShaderSource,
Expand Down

0 comments on commit b70c272

Please sign in to comment.