//tp UniformBuffer /// Creates a UniformBuffer that may contain the data for a number of program Uniforms /// /// For simplicity in OpenGl/WebGl this also creates the backing GlBuffer /// /// A program's uniform is bound to a *range* of one of these pub struct UniformBuffer gl_buffer: G::Buffer, byte_length: u32, phantom: PhantomData, } impl UniformBuffer where G:Gl { pub fn of_data(context: &mut G, app_handle: u32, data: &[F], is_dynamic:bool, ) -> Result { let buffer = data.as_ptr(); let byte_length = std::mem::size_of::() * data.len(); let mut gl: gl::types::GLuint = 0; unsafe { gl::GenBuffers(1, (&mut gl) as *mut gl::types::GLuint); gl::BindBuffer(gl::UNIFORM_BUFFER, gl); gl::BufferData( gl::UNIFORM_BUFFER, byte_length as gl::types::GLsizeiptr, buffer as *const gl::types::GLvoid, gl::STATIC_DRAW, ); gl::BindBuffer(gl::UNIFORM_BUFFER, 0); // unbind to protect println!("Uniform buffer {} bound @{:?}+{}", gl, buffer, byte_length); } self.gl = Rc::new(gl); } //mp program_bind_uniform_buffer fn program_bind_uniform_buffer_range(&mut self, program: &::Program, uniform_buffer_id: usize, uniform_buffer: &Self::GlUniformBuffer, byte_offset : u32, byte_length : u32, ) -> Result { if let Some(u) = shader_program.uniform(model3d_gl::UniformId::Buffer(uniform_buffer_id)) { unsafe { println!("Bind to {}", u); gl::UniformBlockBinding(shader_program.id(), u as u32, uniform_buffer_id, // was material_gl.gl_buffer() for material ); gl::BindBufferRange( gl::UNIFORM_BUFFER, u as u32, uniform_buffer_id, // for material was its gl_buffer() byte_offset, byte_length, ); } } model3d_gl::opengl_utils::check_errors().expect("Bound uniform for material"); } //fp update uniform buffer fn uniform_buffer_update_data(&mut self, uniform_buffer:&Self::GlUniformBuffer, data: &[F], byte_offset:u32) { let buffer = data.as_ptr(); let byte_length = std::mem::size_of::() * data.len(); // unsafe { // gl::BindBuffer(gl::UNIFORM_BUFFER, uniform_buffer.gl_buffer()); // gl::BufferSubData( // gl::UNIFORM_BUFFER, // byte_offset, // byte_length, // buffer as *const std::os::raw::c_void, // ); // }