gl/shader.rs
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 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
//! A module for handling shaders and programs.
use std::ffi::CStr;
use crate::error::Result;
/// The type of shader.
pub enum ShaderKind {
/// Represents a vertex shader.
Vertex = crate::c::GL_VERTEX_SHADER as _,
/// Represents a fragment shader.
Fragment = crate::c::GL_FRAGMENT_SHADER as _,
/// Represents a geometry shader.
Geometry = crate::c::GL_GEOMETRY_SHADER as _,
/// Represents a compute shader.
Compute = crate::c::GL_COMPUTE_SHADER as _,
}
/// A raw handle to the underlying Shader object.
pub struct Shader(crate::c::GLuint);
impl Shader {
/// Creates a new shader handle.
pub fn new(shader: ShaderKind) -> Self {
let value = unsafe { crate::c::glCreateShader(shader as _) };
Self(value)
}
/// Creates a new shader with full checking for errors.
pub fn new_checked(shader: ShaderKind) -> Result<Self> {
let value = unsafe { crate::c::glCreateShader(shader as _) };
if value == 0 {
match crate::error::Error::last_error() {
Some(err) => Err(err),
None => Ok(Self(value)),
}
} else {
Ok(Self(value))
}
}
/// Adds the specified shader source to the shader.
/// This is just a thin wrapper around [`glShaderSource`]
///
/// # Parameters
///
/// * `source` - The source code as a null-terminated [`CStr`]
///
/// [`glShaderSource`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glShaderSource.xhtml
pub fn shader_source_cstr(&mut self, source: &CStr) {
unsafe {
crate::c::glShaderSource(self.0, 1, &source.as_ptr(), std::ptr::null());
}
}
/// Adds the specified shader source to the shader.
/// This is just a thin wrapper around [`glShaderSource`]
///
/// # Parameters
///
/// * `source` - The source code of the shader
///
/// [`glShaderSource`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glShaderSource.xhtml
pub fn shader_source(&mut self, source: &str) {
let length = source.len() as _;
let str_ptr = source.as_ptr() as *const std::ffi::c_char;
unsafe { crate::c::glShaderSource(self.0, 1, &str_ptr, &length) };
}
/// Adds the specified shader sources to the shader.
///
/// This function is optimized to handle up to 8 sources without requiring
/// using dynamic memory allocation.
///
/// # Parameters
///
/// * `sources` - a collection of sources
pub fn shader_sources(&mut self, sources: &[&str]) {
match sources.len() {
0 => (),
1 => self.shader_source(sources[0]),
2 => self.shader_sources_fixed::<2>(sources),
3..=4 => self.shader_sources_fixed::<4>(sources),
5..=8 => self.shader_sources_fixed::<8>(sources),
_ => self.shader_sources_dynamic(sources),
}
}
fn shader_sources_dynamic(&mut self, sources: &[&str]) {
let lengths: Vec<_> = sources.iter().map(|s| s.len() as _).collect();
let ptrs: Vec<_> = sources
.iter()
.map(|s| s.as_ptr() as *const std::ffi::c_char)
.collect();
unsafe {
crate::c::glShaderSource(self.0, sources.len() as _, ptrs.as_ptr(), lengths.as_ptr());
}
}
fn shader_sources_fixed<const N: usize>(&mut self, sources: &[&str]) {
let mut ptrs: [*const std::ffi::c_char; N] = [std::ptr::null(); N];
let mut sizes: [crate::c::GLsizei; N] = [0; N];
for (i, source) in sources.iter().enumerate() {
ptrs[i] = source.as_ptr() as *const std::ffi::c_char;
sizes[i] = source.len() as _;
}
unsafe {
crate::c::glShaderSource(self.0, N as _, ptrs.as_ptr(), sizes.as_ptr());
}
}
/// Attempts to compile the shader.
///
/// This must be called after adding shader source(s).
pub fn compile(&mut self) -> Result<()> {
unsafe { crate::c::glCompileShader(self.0) }
if self.compile_status() {
Ok(())
} else {
Err(crate::error::Error::ShaderError(self.info_log()))
}
}
/// Gets the compile status for the shader.
/// This is a thin wrapper around [`glGetShaderiv`]
///
/// Returns `true` if the shader was compiled successfully.
///
/// [`glGetShaderiv`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetShader.xhtml
#[inline]
pub fn compile_status(&self) -> bool {
let mut status = 0;
unsafe {
crate::c::glGetShaderiv(self.0, crate::c::GL_COMPILE_STATUS, &mut status);
}
status != 0
}
/// Checks if this shader is flagged for deletion.
/// This is a thin wrapper around [`glGetShaderiv`]
///
/// Returns `true` if the shader is flagged for deletion.
///
/// [`glGetShaderiv`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetShader.xhtml
#[inline]
pub fn delete_status(&self) -> bool {
let mut status = 0;
unsafe {
crate::c::glGetShaderiv(self.0, crate::c::GL_DELETE_STATUS, &mut status);
}
status != 0
}
/// Gets the info log for the shader.
pub fn info_log(&self) -> String {
let mut length = 0;
unsafe {
crate::c::glGetShaderiv(self.0, crate::c::GL_INFO_LOG_LENGTH, &mut length);
}
let mut buffer = vec![0; length as usize];
unsafe {
crate::c::glGetShaderInfoLog(
self.0,
length,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut _,
);
}
String::from_utf8(buffer).unwrap()
}
/// Takes ownership of the underlying shader object. It becomes the
/// responsibility of the caller to call [`glDeleteShader`] on the returned
/// shader object.
///
/// # Safety
///
/// The caller must ensure that the shader object is deleted after calling
/// this function, otherwise there will be a resource-leak.
///
/// [`glDeleteShader`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDeleteShader.xhtml
#[inline]
pub unsafe fn release(self) -> crate::c::GLuint {
let value = self.0;
std::mem::forget(self);
value
}
}
impl Drop for Shader {
fn drop(&mut self) {
unsafe { crate::c::glDeleteShader(self.0) }
}
}
/// A handle to a uniform location.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UniformLocation(crate::c::GLint);
impl std::fmt::Display for UniformLocation {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.0.fmt(f)
}
}
/// A handle to an attribute location.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AttribLocation(crate::c::GLint);
/// A self-disposing handle to a Program object.
pub struct Program(crate::c::GLuint);
impl Program {
/// Creates a new program handle.
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let value = unsafe { crate::c::glCreateProgram() };
Self(value)
}
/// Creates a new program with full checking for errors.
pub fn new_checked() -> Result<Self> {
let value = unsafe { crate::c::glCreateProgram() };
if value == 0 {
match crate::error::Error::last_error() {
Some(err) => Err(err),
None => Ok(Self(value)),
}
} else {
Ok(Self(value))
}
}
/// Attaches the specified shader to the program.
/// This is a thin wrapper around [`glAttachShader`].
///
/// # Parameters
///
/// * `shader` - The shader to attach to the program
///
/// [`glAttachShader`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glAttachShader.xhtml
#[inline(always)]
pub fn attach_shader(&mut self, shader: &Shader) {
unsafe { crate::c::glAttachShader(self.0, shader.0) };
}
/// Links the program, returning whether the link was successful.
pub fn link(&mut self) -> Result<()> {
unsafe { crate::c::glLinkProgram(self.0) };
if self.link_status() {
Ok(())
} else {
Err(crate::error::Error::ShaderError(self.info_log()))
}
}
/// Gets the link status for the program.
/// This is a thin wrapper around [`glGetProgramiv`]
///
/// Returns `true` if the program was linked successfully.
///
/// [`glGetProgramiv`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetProgram.xhtml
#[inline]
pub fn link_status(&self) -> bool {
let mut status = 0;
unsafe {
crate::c::glGetProgramiv(self.0, crate::c::GL_LINK_STATUS, &mut status);
}
status != 0
}
/// Gets the info log for the program.
/// This is a thin wrapper around [`glGetProgramInfoLog`]
///
/// [`glGetProgramInfoLog`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glGetProgramInfoLog.xhtml
pub fn info_log(&self) -> String {
let mut length = 0;
unsafe {
crate::c::glGetProgramiv(self.0, crate::c::GL_INFO_LOG_LENGTH, &mut length);
}
let mut buffer = vec![0; length as usize];
unsafe {
crate::c::glGetProgramInfoLog(
self.0,
length,
std::ptr::null_mut(),
buffer.as_mut_ptr() as *mut _,
);
}
String::from_utf8(buffer).unwrap()
}
/// Takes ownership of the underlying program object. It becomes the
/// responsibility of the caller to call [`glDeleteProgram`] on the returned
/// program object.
///
/// # Safety
///
/// The caller must ensure that the program object is deleted after calling
/// this function, otherwise there will be a resource-leak.
///
/// [`glDeleteProgram`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDeleteProgram.xhtml
#[inline]
pub unsafe fn release(self) -> crate::c::GLuint {
let value = self.0;
std::mem::forget(self);
value
}
/// Binds the program.
/// This is a thin wrapper around [`glUseProgram`]
///
/// [`glUseProgram`]: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUseProgram.xhtml
#[inline(always)]
pub fn bind(&self) {
unsafe { crate::c::glUseProgram(self.0) }
}
/// Unbinds the program.
pub fn reset_binding() {
unsafe { crate::c::glUseProgram(0) }
}
/// Gets the location of the specified uniform.
pub fn uniform_location(&self, name: &str) -> UniformLocation {
let name = std::ffi::CString::new(name).unwrap();
self.uniform_location_cstr(&name)
}
/// Gets the location of the specified uniform.
pub fn uniform_location_cstr(&self, name: &CStr) -> UniformLocation {
let uniform = unsafe { crate::c::glGetUniformLocation(self.0, name.as_ptr()) };
UniformLocation(uniform)
}
/// Gets the location of the specified attribute.
pub fn attribute_location(&self, name: &str) -> AttribLocation {
let name = std::ffi::CString::new(name).unwrap();
self.attribute_location_cstr(&name)
}
/// Gets the location of the specified attribute.
pub fn attribute_location_cstr(&self, name: &CStr) -> AttribLocation {
let attrib = unsafe { crate::c::glGetAttribLocation(self.0, name.as_ptr()) };
AttribLocation(attrib)
}
/// Provides the contents of a single `f32` to the specified uniform location.
///
/// # Safety
///
/// The uniform location must be valid and the value must be a valid pointer
/// to a 4-element array of [`f32`].
///
/// # Parameters
///
/// * `location` - The location of the uniform
/// * `count` - The number of values to provide
/// * `vec` - A pointer to a 4-element array of [`f32`]
pub unsafe fn set_uniform_vector4_unchecked(
&self,
location: UniformLocation,
count: usize,
vec: *const f32,
) {
crate::c::glUniform4fv(location.0, count as _, vec);
}
/// Provides the contents of a 4x4 matrix to the specified uniform location.
///
/// # Safety
///
/// The uniform location must be valid and the matrix must be a valid pointer
/// to a 16-element array of `f32`.
///
/// # Parameters
///
/// * `location` - The location of the uniform
/// * `count` - The number of matrices to provide
/// * `mat` - A pointer to a 16-element array of `f32`
pub unsafe fn set_uniform_matrix4_unchecked(
&self,
location: UniformLocation,
count: usize,
mat: *const f32,
) {
unsafe {
crate::c::glUniformMatrix4fv(location.0, count as _, false, mat);
}
}
}
impl Drop for Program {
fn drop(&mut self) {
unsafe { crate::c::glDeleteProgram(self.0) }
}
}