1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
use alloc::string::String; use alloc::vec::Vec; use alloc::btree_map::BTreeMap; use core::str::{self, Utf8Error}; use core::ptr; use core::ops::Drop; use regex::Regex; use gl; use gl::types::*; use super::{AttributeKind, UniformKind, GLAttribute, GLUniform}; pub struct GLProgram { id: GLuint, uniforms: BTreeMap<String, GLUniform>, attributes: BTreeMap<String, GLAttribute>, } impl GLProgram { #[inline] pub fn new(vertex: &str, fragment: &str) -> Self { let mut program = GLProgram { id: 0, uniforms: BTreeMap::new(), attributes: BTreeMap::new(), }; program.set(vertex, fragment); program } #[inline] pub fn new_mutiple(vertex: &[&str], fragment: &[&str]) -> Self { let mut program = GLProgram { id: 0, uniforms: BTreeMap::new(), attributes: BTreeMap::new(), }; program.set_mutiple(vertex, fragment); program } #[inline(always)] pub fn id(&self) -> GLuint { self.id } #[inline] pub fn bind(&self) -> &Self { unsafe { gl::UseProgram(self.id); } self } #[inline] pub fn unbind(&self) -> &Self { unsafe { gl::UseProgram(0); } self } #[inline(always)] pub fn has_uniform(&self, name: &str) -> bool { self.uniforms.contains_key(name) } #[inline(always)] pub fn get_uniform(&self, name: &str) -> &GLUniform { match self.uniforms.get(name) { Some(ref uniform) => uniform, None => panic!("No uniform named {:?} found", name), } } #[inline(always)] pub fn uniforms(&self) -> &BTreeMap<String, GLUniform> {&self.uniforms} #[inline(always)] pub fn uniforms_mut(&mut self) -> &mut BTreeMap<String, GLUniform> {&mut self.uniforms} #[inline(always)] pub fn has_attribute(&self, name: &str) -> bool { self.attributes.contains_key(name) } #[inline(always)] pub fn get_attribute(&self, name: &str) -> &GLAttribute { match self.attributes.get(name) { Some(ref attribute) => attribute, None => panic!("No attribute named {:?} found", name), } } #[inline(always)] pub fn attributes(&self) -> &BTreeMap<String, GLAttribute> {&self.attributes} #[inline(always)] pub fn attributes_mut(&mut self) -> &mut BTreeMap<String, GLAttribute> {&mut self.attributes} #[inline] pub fn set(&mut self, vertex: &str, fragment: &str) -> &mut Self { let vs = compile_shader(vertex, gl::VERTEX_SHADER); let fs = compile_shader(fragment, gl::FRAGMENT_SHADER); let id = link_program(vs, fs); self.set_program_id(id) } #[inline] pub fn set_mutiple(&mut self, vertex: &[&str], fragment: &[&str]) -> &mut Self { let vs = compile_shaders(vertex, gl::VERTEX_SHADER); let fs = compile_shaders(fragment, gl::FRAGMENT_SHADER); let id = link_program(vs, fs); self.set_program_id(id) } #[inline] fn set_program_id(&mut self, id: GLuint) -> &mut Self { { let ref mut uniforms = self.uniforms; let ref mut attributes = self.attributes; if self.id != 0 { uniforms.clear(); attributes.clear(); unsafe { gl::DeleteProgram(self.id); } } self.id = id; parse_uniforms(id, uniforms); parse_attributes(id, attributes); } self } } impl Drop for GLProgram { #[inline] fn drop(&mut self) { if self.id != 0 { unsafe { gl::DeleteProgram(self.id); } } } } #[inline] fn parse_uniforms(program: GLuint, uniforms: &mut BTreeMap<String, GLUniform>) { let mut max_length = 0; let mut active_length = 0; unsafe { gl::GetProgramiv(program, gl::ACTIVE_UNIFORM_MAX_LENGTH, &mut max_length); gl::GetProgramiv(program, gl::ACTIVE_UNIFORMS, &mut active_length); } for i in 0..active_length { let mut length = 0; let mut count = 0; let mut kind = 0; let mut buf = Vec::with_capacity(max_length as usize); let location; unsafe { let buf_ptr = buf.as_mut_ptr() as *mut GLchar; gl::GetActiveUniform(program, i as GLuint, max_length, &mut length, &mut count, &mut kind, buf_ptr); buf.set_len(length as usize); location = gl::GetUniformLocation(program, buf_ptr); } let mut name = match string_from_utf8(&buf) { Ok(string) => string, Err(vec) => panic!("Could not convert uniform name from buffer: {:?}", vec) }; if name.chars().nth(name.len() - 1).expect("Unexpected empty uniform name") == ']' { let new_name = match Regex::new(r"(.*)\[\d+\]") .expect("regex failed to compile") .captures(&name) { Some(cap) => match cap.get(1) { Some(name) => Some(String::from(name.as_str())), None => None, }, None => None, }; if let Some(value) = new_name { name = value; } } uniforms.insert(name, GLUniform::new(UniformKind::from_gl(kind), count as usize, location as usize)); } } #[inline] fn string_from_utf8(vec: &Vec<u8>) -> Result<String, Utf8Error> { match str::from_utf8(vec) { Ok(s) => Ok(String::from(s)), Err(e) => Err(e), } } #[inline] fn parse_attributes(program: GLuint, attributes: &mut BTreeMap<String, GLAttribute>) { let mut max_length = 0; let mut active_length = 0; unsafe { gl::GetProgramiv(program, gl::ACTIVE_ATTRIBUTE_MAX_LENGTH, &mut max_length); gl::GetProgramiv(program, gl::ACTIVE_ATTRIBUTES, &mut active_length); } for i in 0..active_length { let mut length = 0; let mut count = 0; let mut kind = 0; let mut buf = Vec::with_capacity(max_length as usize); let buf_ptr = buf.as_mut_ptr() as *mut GLchar; let location; unsafe { gl::GetActiveAttrib(program, i as u32, max_length, &mut length, &mut count, &mut kind, buf_ptr); buf.set_len(length as usize); location = gl::GetAttribLocation(program, buf_ptr); } let name = match string_from_utf8(&buf) { Ok(string) => string, Err(vec) => panic!("Could not convert attribute name from buffer: {:?}", vec) }; attributes.insert(name, GLAttribute::new(AttributeKind::from_gl(kind), count as usize, location as usize)); } } #[inline] pub fn link_program(vertex_shader: GLuint, fragment_shader: GLuint) -> GLuint { let program = unsafe { gl::CreateProgram() }; unsafe { gl::AttachShader(program, vertex_shader); gl::DeleteShader(vertex_shader); gl::AttachShader(program, fragment_shader); gl::DeleteShader(fragment_shader); gl::LinkProgram(program); gl::ValidateProgram(program); gl::UseProgram(program); } check_program_status(program) } #[inline] pub fn check_program_status(program: GLuint) -> GLuint { let mut status = 0; unsafe { gl::GetProgramiv(program, gl::LINK_STATUS, &mut status) }; if status != (gl::TRUE as GLint) { let mut len: GLint = 0; unsafe { gl::GetProgramiv(program, gl::INFO_LOG_LENGTH, &mut len); } let mut buf = Vec::with_capacity(len as usize); unsafe { buf.set_len(len as usize); gl::GetProgramInfoLog(program, len, ptr::null_mut(), buf.as_mut_ptr() as *mut GLchar); } panic!("{}", str::from_utf8(&buf).ok().expect("ProgramInfoLog not valid utf8")); } program } #[inline] pub fn compile_shader(source: &str, kind: GLenum) -> GLuint { let shader = unsafe { gl::CreateShader(kind) }; unsafe { let ptr: *const GLchar = source.as_bytes().as_ptr() as *const GLchar; let len = source.len() as GLint; gl::ShaderSource(shader, 1, &ptr, &len); gl::CompileShader(shader); } check_shader_status(shader) } #[inline] pub fn compile_shaders(sources: &[&str], kind: GLenum) -> GLuint { let shader = unsafe { gl::CreateShader(kind) }; unsafe { for source in sources.iter() { let ptr: *const GLchar = source.as_bytes().as_ptr() as *const GLchar; let len = source.len() as GLint; gl::ShaderSource(shader, 1, &ptr, &len); gl::CompileShader(shader); } } check_shader_status(shader) } #[inline] pub fn check_shader_status(shader: GLuint) -> GLuint { let mut status = 0; unsafe { gl::GetShaderiv(shader, gl::COMPILE_STATUS, &mut status) }; if status != (gl::TRUE as GLint) { let mut len = 0; unsafe { gl::GetShaderiv(shader, gl::INFO_LOG_LENGTH, &mut len); } let mut buf = Vec::with_capacity(len as usize); unsafe { buf.set_len(len as usize); gl::GetShaderInfoLog(shader, len, ptr::null_mut(), buf.as_mut_ptr() as *mut GLchar); } panic!("{}", str::from_utf8(&buf).ok().expect("ShaderInfoLog not valid utf8")); } shader }