Skip to content

Commit

Permalink
Bind deform buffer before uploading (#99)
Browse files Browse the repository at this point in the history
Fixes deforms not working if anything else has bound to the
GL_ARRAY_BUFFER
  • Loading branch information
Speykious authored Jul 16, 2024
2 parents e633656 + 590cea2 commit ba65e19
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
18 changes: 11 additions & 7 deletions inox2d-opengl/src/gl_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ use inox2d::render::RenderCtx;

use super::OpenglRendererError;

unsafe fn upload_array_to_gl<T>(gl: &glow::Context, array: &[T], target: u32, usage: u32) {
unsafe fn upload_array_to_gl<T>(gl: &glow::Context, array: &[T], target: u32, usage: u32) -> glow::Buffer {
let bytes: &[u8] = core::slice::from_raw_parts(array.as_ptr() as *const u8, std::mem::size_of_val(array));
let buffer = gl.create_buffer().unwrap();
gl.bind_buffer(target, Some(buffer));
gl.buffer_data_u8_slice(target, bytes, usage);
buffer
}

unsafe fn reupload_array_to_gl<T>(gl: &glow::Context, array: &[T], target: u32, start_idx: usize, end_idx: usize) {
Expand All @@ -22,8 +23,8 @@ pub trait RenderCtxOpenglExt {
&self,
gl: &glow::Context,
vao: glow::VertexArray,
) -> Result<glow::VertexArray, OpenglRendererError>;
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context);
) -> Result<glow::Buffer, OpenglRendererError>;
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context, buffer: glow::Buffer);
}

impl RenderCtxOpenglExt for RenderCtx {
Expand All @@ -40,7 +41,7 @@ impl RenderCtxOpenglExt for RenderCtx {
&self,
gl: &glow::Context,
vao: glow::VertexArray,
) -> Result<glow::VertexArray, OpenglRendererError> {
) -> Result<glow::Buffer, OpenglRendererError> {
gl.bind_vertex_array(Some(vao));

upload_array_to_gl(gl, &self.vertex_buffers.verts, glow::ARRAY_BUFFER, glow::STATIC_DRAW);
Expand All @@ -51,7 +52,8 @@ impl RenderCtxOpenglExt for RenderCtx {
gl.vertex_attrib_pointer_f32(1, 2, glow::FLOAT, false, 0, 0);
gl.enable_vertex_attrib_array(1);

upload_array_to_gl(gl, &self.vertex_buffers.deforms, glow::ARRAY_BUFFER, glow::DYNAMIC_DRAW);
let deform_buffer =
upload_array_to_gl(gl, &self.vertex_buffers.deforms, glow::ARRAY_BUFFER, glow::DYNAMIC_DRAW);
gl.vertex_attrib_pointer_f32(2, 2, glow::FLOAT, false, 0, 0);
gl.enable_vertex_attrib_array(2);

Expand All @@ -62,14 +64,16 @@ impl RenderCtxOpenglExt for RenderCtx {
glow::STATIC_DRAW,
);

Ok(vao)
Ok(deform_buffer)
}

/// # Safety
///
/// unsafe as initiating GL calls. can be safely called for multiple times,
/// but only needed once after deform update and before rendering.
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context) {
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context, buffer: glow::Buffer) {
gl.bind_buffer(glow::ARRAY_BUFFER, Some(buffer));

reupload_array_to_gl(
gl,
&self.vertex_buffers.deforms,
Expand Down
9 changes: 7 additions & 2 deletions inox2d-opengl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub struct OpenglRenderer {
part_mask_shader: PartMaskShader,
composite_shader: CompositeShader,
composite_mask_shader: CompositeMaskShader,
deform_buffer: Option<glow::Buffer>,

textures: Vec<Texture>,
}
Expand Down Expand Up @@ -183,6 +184,7 @@ impl OpenglRenderer {
composite_mask_shader,

textures: Vec::new(),
deform_buffer: None,
};

// Set emission strength once (it doesn't change anywhere else)
Expand Down Expand Up @@ -360,7 +362,7 @@ impl InoxRenderer for OpenglRenderer {
type Error = OpenglRendererError;

fn prepare(&mut self, model: &Model) -> Result<(), Self::Error> {
unsafe { model.puppet.render_ctx.setup_gl_buffers(&self.gl, self.vao)? };
self.deform_buffer = Some(unsafe { model.puppet.render_ctx.setup_gl_buffers(&self.gl, self.vao)? });

match self.upload_model_textures(&model.textures) {
Ok(_) => Ok(()),
Expand Down Expand Up @@ -420,7 +422,10 @@ impl InoxRenderer for OpenglRenderer {
fn render(&self, puppet: &Puppet) {
let gl = &self.gl;
unsafe {
puppet.render_ctx.upload_deforms_to_gl(gl);
if let Some(deform_buffer) = self.deform_buffer {
puppet.render_ctx.upload_deforms_to_gl(gl, deform_buffer);
}

gl.enable(glow::BLEND);
gl.disable(glow::DEPTH_TEST);
}
Expand Down
4 changes: 4 additions & 0 deletions inox2d-opengl/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ impl Texture {
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_MAG_FILTER, glow::LINEAR as i32);
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_S, glow::CLAMP_TO_BORDER as i32);
gl.tex_parameter_i32(glow::TEXTURE_2D, glow::TEXTURE_WRAP_T, glow::CLAMP_TO_BORDER as i32);

// Texture parameters for f32 slices are not supported on WASM yet.
#[cfg(not(target_arch = "wasm32"))]
gl.tex_parameter_f32_slice(glow::TEXTURE_2D, glow::TEXTURE_BORDER_COLOR, &[0.0; 4]);

gl.tex_image_2d(
glow::TEXTURE_2D,
0,
Expand Down

0 comments on commit ba65e19

Please sign in to comment.