Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow positional buffer updates #419

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 _, size as _, data.ptr as _) };
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
Loading