Skip to content

Commit

Permalink
Use Buffer instead of NativeBuffer for WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
Speykious committed Jul 16, 2024
1 parent f111d3a commit c55799c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 6 additions & 6 deletions inox2d-opengl/src/gl_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use glow::{HasContext, NativeBuffer};
use glow::HasContext;

use inox2d::render::RenderCtx;

use super::OpenglRendererError;

unsafe fn upload_array_to_gl<T>(gl: &glow::Context, array: &[T], target: u32, usage: u32) -> NativeBuffer {
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));
Expand All @@ -23,8 +23,8 @@ pub trait RenderCtxOpenglExt {
&self,
gl: &glow::Context,
vao: glow::VertexArray,
) -> Result<NativeBuffer, OpenglRendererError>;
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context, buffer: NativeBuffer);
) -> Result<glow::Buffer, OpenglRendererError>;
unsafe fn upload_deforms_to_gl(&self, gl: &glow::Context, buffer: glow::Buffer);
}

impl RenderCtxOpenglExt for RenderCtx {
Expand All @@ -41,7 +41,7 @@ impl RenderCtxOpenglExt for RenderCtx {
&self,
gl: &glow::Context,
vao: glow::VertexArray,
) -> Result<NativeBuffer, 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 Down Expand Up @@ -71,7 +71,7 @@ impl RenderCtxOpenglExt for RenderCtx {
///
/// 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, buffer: NativeBuffer) {
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(
Expand Down
13 changes: 8 additions & 5 deletions inox2d-opengl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::ops::Deref;

use gl_buffer::RenderCtxOpenglExt;
use glam::{uvec2, Mat4, UVec2, Vec2, Vec3};
use glow::{HasContext, NativeBuffer};
use glow::HasContext;
use inox2d::texture::{decode_model_textures, TextureId};

use inox2d::math::camera::Camera;
Expand Down Expand Up @@ -129,7 +129,7 @@ pub struct OpenglRenderer {
part_mask_shader: PartMaskShader,
composite_shader: CompositeShader,
composite_mask_shader: CompositeMaskShader,
deform_buffer: NativeBuffer,
deform_buffer: Option<glow::Buffer>,

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

textures: Vec::new(),
deform_buffer: NativeBuffer(std::num::NonZeroU32::MIN),
deform_buffer: None,
};

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

fn prepare(&mut self, model: &Model) -> Result<(), Self::Error> {
self.deform_buffer = 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 @@ -422,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, self.deform_buffer);
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

0 comments on commit c55799c

Please sign in to comment.