Skip to content

Commit

Permalink
Add functions from get_program_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Feb 6, 2024
1 parent a84e58f commit 5c21970
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ pub enum CompressedPixelUnpackData<'a> {
Slice(&'a [u8]),
}

pub struct ProgramBinary {
pub buffer: Box<[u8]>,
pub format: u32,
}

pub trait HasContext {
type Shader: Copy + Clone + Debug + Eq + Hash + Ord + PartialEq + PartialOrd;
type Program: Copy + Clone + Debug + Eq + Hash + Ord + PartialEq + PartialOrd;
Expand Down Expand Up @@ -171,6 +176,12 @@ pub trait HasContext {
properties: &[u32],
) -> Vec<i32>;

unsafe fn program_binary_retrievable_hint(&self, program: Self::Program, value: bool);

unsafe fn get_program_binary(&self, program: Self::Program) -> Option<ProgramBinary>;

unsafe fn program_binary(&self, program: Self::Program, binary: &ProgramBinary);

unsafe fn get_active_uniforms(&self, program: Self::Program) -> u32;

unsafe fn get_active_uniform(
Expand Down
48 changes: 48 additions & 0 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,54 @@ impl HasContext for Context {
params
}

unsafe fn program_binary_retrievable_hint(&self, program: Self::Program, value: bool) {
let gl = &self.raw;
gl.ProgramParameteri(
program.0.get(),
crate::PROGRAM_BINARY_RETRIEVABLE_HINT,
value as i32,
)
}

unsafe fn get_program_binary(&self, program: Self::Program) -> Option<ProgramBinary> {
let gl = &self.raw;

// We don't need to error check here as if the call fails, length will be returned as 0.
let mut len = 0;
gl.GetProgramiv(program.0.get(), crate::PROGRAM_BINARY_LENGTH, &mut len);

let mut format = 0;
let mut buffer = vec![0u8; len as usize];

gl.GetProgramBinary(
program.0.get(),
len,
ptr::null_mut(),
&mut format,
buffer.as_mut_ptr() as *mut core::ffi::c_void,
);

if gl.GetError() == crate::NO_ERROR {
Some(ProgramBinary {
buffer: buffer.into_boxed_slice(),
format,
})
} else {
None
}
}

unsafe fn program_binary(&self, program: Self::Program, binary: &ProgramBinary) {
let gl = &self.raw;

gl.ProgramBinary(
program.0.get(),
binary.format,
binary.buffer.as_ptr() as *const core::ffi::c_void,
binary.buffer.len() as native_gl::types::GLsizei,
)
}

unsafe fn get_active_uniforms(&self, program: Self::Program) -> u32 {
let gl = &self.raw;
let mut count = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/web_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,18 @@ impl HasContext for Context {
panic!("get_program_resource_i32 not supported on webgl");
}

unsafe fn program_binary_retrievable_hint(&self, program: Self::Program, value: bool) {
panic!("Program binaries are not supported");
}

unsafe fn get_program_binary(&self, program: Self::Program) -> Option<ProgramBinary> {
panic!("Program binaries are not supported");
}

unsafe fn program_binary(&self, program: Self::Program, binary: &ProgramBinary) {
panic!("Program binaries are not supported");
}

unsafe fn get_active_uniforms(&self, program: Self::Program) -> u32 {
let programs = self.programs.borrow();
let raw_program = programs.get_unchecked(program);
Expand Down

0 comments on commit 5c21970

Please sign in to comment.