Skip to content

Commit

Permalink
Add program binary functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sotaroikeda committed Nov 8, 2017
1 parent 8a4f08b commit cebd0a8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "gleam"
version = "0.4.12"
version = "0.4.13"
license = "Apache-2.0/MIT"
authors = ["The Servo Project Developers"]
build = "build.rs"
Expand Down
3 changes: 2 additions & 1 deletion build.rs
Expand Up @@ -17,7 +17,8 @@ fn main() {
"GL_EXT_debug_marker",
"GL_APPLE_client_storage",
"GL_APPLE_texture_range",
"GL_APPLE_fence"];
"GL_APPLE_fence",
"GL_ARB_get_program_binary"];
let gl_reg = Registry::new(Api::Gl, (3, 3), Profile::Core, Fallbacks::All, gl_extensions);
gl_reg.write_bindings(gl_generator::StructGenerator, &mut file_gl)
.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions src/gl.rs
Expand Up @@ -30,11 +30,11 @@ pub enum GlType {
}

impl Default for GlType {
#[cfg(any(target_os="android", target_os="ios"))]
#[cfg(target_os="android")]
fn default() -> GlType {
GlType::Gles
}
#[cfg(not(any(target_os="android", target_os="ios")))]
#[cfg(not(target_os="android"))]
fn default() -> GlType {
GlType::Gl
}
Expand All @@ -50,12 +50,10 @@ fn calculate_length(width: GLsizei, height: GLsizei, format: GLenum, pixel_type:

ffi::ALPHA => 1,
ffi::LUMINANCE => 1,
ffi::DEPTH_COMPONENT => 1,
_ => panic!("unsupported format for read_pixels"),
};
let depth = match pixel_type {
ffi::UNSIGNED_BYTE => 1,
ffi::FLOAT=> 4,
_ => panic!("unsupported pixel_type for read_pixels"),
};

Expand Down Expand Up @@ -376,13 +374,15 @@ pub trait Gl {
fn get_uniform_location(&self, program: GLuint, name: &str) -> c_int;
fn get_program_info_log(&self, program: GLuint) -> String;
fn get_program_iv(&self, program: GLuint, pname: GLenum) -> GLint;
fn get_program_binary(&self, program: GLuint) -> (Vec<u8>, GLenum);
fn program_binary(&self, program: GLuint, format: GLenum, binary: &[u8]);
fn program_parameter_i(&self, program: GLuint, pname: GLenum, value: GLint);
fn get_vertex_attrib_iv(&self, index: GLuint, pname: GLenum) -> GLint;
fn get_vertex_attrib_fv(&self, index: GLuint, pname: GLenum) -> Vec<GLfloat>;
fn get_vertex_attrib_pointer_v(&self, index: GLuint, pname: GLenum) -> GLsizeiptr;
fn get_buffer_parameter_iv(&self, target: GLuint, pname: GLenum) -> GLint;
fn get_shader_info_log(&self, shader: GLuint) -> String;
fn get_string(&self, which: GLenum) -> String;
fn get_string_i(&self, which: GLenum, index: GLuint) -> String;
fn get_shader_iv(&self, shader: GLuint, pname: GLenum) -> GLint;
fn get_shader_precision_format(&self,
shader_type: GLuint,
Expand Down
58 changes: 47 additions & 11 deletions src/gl_fns.rs
Expand Up @@ -1149,6 +1149,53 @@ impl Gl for GlFns {
}
}

fn get_program_binary(&self, program: GLuint) -> (Vec<u8>, GLenum) {
if !self.ffi_gl_.GetProgramBinary.is_loaded() {
return (Vec::new(), NONE);
}
let len = self.get_program_iv(program, ffi::PROGRAM_BINARY_LENGTH);
if len <= 0 {
return (Vec::new(), NONE);
}
let mut binary: Vec<u8> = Vec::with_capacity(len as usize);
let mut format = NONE;
let mut out_len = 0;
unsafe {
binary.set_len(len as usize);
self.ffi_gl_.GetProgramBinary(program,
len,
&mut out_len as *mut GLsizei,
&mut format,
binary.as_mut_ptr() as *mut c_void);
}
if len != out_len {
return (Vec::new(), NONE);
}

(binary, format)
}

fn program_binary(&self, program: GLuint, format: GLenum, binary: &[u8]) {
if !self.ffi_gl_.ProgramBinary.is_loaded() {
return;
}
unsafe {
self.ffi_gl_.ProgramBinary(program,
format,
binary.as_ptr() as *const c_void,
binary.len() as GLsizei);
}
}

fn program_parameter_i(&self, program: GLuint, pname: GLenum, value: GLint) {
if !self.ffi_gl_.ProgramParameteri.is_loaded() {
return;
}
unsafe {
self.ffi_gl_.ProgramParameteri(program, pname, value);
}
}

fn get_vertex_attrib_iv(&self, index: GLuint, pname: GLenum) -> GLint {
unsafe {
let mut result: GLint = 0 as GLint;
Expand Down Expand Up @@ -1205,17 +1252,6 @@ impl Gl for GlFns {
}
}

fn get_string_i(&self, which: GLenum, index: GLuint) -> String {
unsafe {
let llstr = self.ffi_gl_.GetStringi(which, index);
if !llstr.is_null() {
str::from_utf8_unchecked(CStr::from_ptr(llstr as *const c_char).to_bytes()).to_string()
} else {
"".to_string()
}
}
}

fn get_shader_iv(&self, shader: GLuint, pname: GLenum) -> GLint {
unsafe {
let mut result: GLint = 0 as GLint;
Expand Down
46 changes: 35 additions & 11 deletions src/gles_fns.rs
Expand Up @@ -1121,6 +1121,41 @@ impl Gl for GlesFns {
}
}

fn get_program_binary(&self, program: GLuint) -> (Vec<u8>, GLenum) {
let len = self.get_program_iv(program, ffi::PROGRAM_BINARY_LENGTH);
if len <= 0 {
return (Vec::new(), NONE);
}
let mut binary: Vec<u8> = Vec::with_capacity(len as usize);
let mut format = NONE;
let mut out_len = 0;
unsafe {
binary.set_len(len as usize);
self.ffi_gl_.GetProgramBinary(program,
len,
&mut out_len as *mut GLsizei,
&mut format,
binary.as_mut_ptr() as *mut c_void);
}
if len != out_len {
return (Vec::new(), NONE);
}

(binary, format)
}

fn program_binary(&self, program: GLuint, format: GLenum, binary: &[u8]) {
unsafe {
self.ffi_gl_.ProgramBinary(program, format, binary.as_ptr() as *const c_void, binary.len() as GLsizei);
}
}

fn program_parameter_i(&self, program: GLuint, pname: GLenum, value: GLint) {
unsafe {
self.ffi_gl_.ProgramParameteri(program, pname, value);
}
}

fn get_vertex_attrib_iv(&self, index: GLuint, pname: GLenum) -> GLint {
unsafe {
let mut result: GLint = 0 as GLint;
Expand Down Expand Up @@ -1177,17 +1212,6 @@ impl Gl for GlesFns {
}
}

fn get_string_i(&self, which: GLenum, index: GLuint) -> String {
unsafe {
let llstr = self.ffi_gl_.GetStringi(which, index);
if !llstr.is_null() {
str::from_utf8_unchecked(CStr::from_ptr(llstr as *const c_char).to_bytes()).to_string()
} else {
"".to_string()
}
}
}

fn get_shader_iv(&self, shader: GLuint, pname: GLenum) -> GLint {
unsafe {
let mut result: GLint = 0 as GLint;
Expand Down

0 comments on commit cebd0a8

Please sign in to comment.