use std::fmt;
use crate::sys;
use crate::types::{FramebufferStatus, GlVersion, ShaderStage};
pub type Result<T> = std::result::Result<T, GlError>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct GlErrorCode(pub u32);
impl GlErrorCode {
pub const NO_ERROR: Self = Self(sys::GL_NO_ERROR);
pub const INVALID_ENUM: Self = Self(sys::GL_INVALID_ENUM);
pub const INVALID_VALUE: Self = Self(sys::GL_INVALID_VALUE);
pub const INVALID_OPERATION: Self = Self(sys::GL_INVALID_OPERATION);
pub const STACK_OVERFLOW: Self = Self(sys::GL_STACK_OVERFLOW);
pub const STACK_UNDERFLOW: Self = Self(sys::GL_STACK_UNDERFLOW);
pub const OUT_OF_MEMORY: Self = Self(sys::GL_OUT_OF_MEMORY);
pub const INVALID_FRAMEBUFFER_OPERATION: Self = Self(sys::GL_INVALID_FRAMEBUFFER_OPERATION);
pub const CONTEXT_LOST: Self = Self(sys::GL_CONTEXT_LOST);
#[inline]
pub const fn raw(self) -> u32 {
self.0
}
#[inline]
pub const fn is_ok(self) -> bool {
self.0 == sys::GL_NO_ERROR
}
pub const fn name(self) -> &'static str {
match self.0 {
sys::GL_NO_ERROR => "GL_NO_ERROR",
sys::GL_INVALID_ENUM => "GL_INVALID_ENUM",
sys::GL_INVALID_VALUE => "GL_INVALID_VALUE",
sys::GL_INVALID_OPERATION => "GL_INVALID_OPERATION",
sys::GL_STACK_OVERFLOW => "GL_STACK_OVERFLOW",
sys::GL_STACK_UNDERFLOW => "GL_STACK_UNDERFLOW",
sys::GL_OUT_OF_MEMORY => "GL_OUT_OF_MEMORY",
sys::GL_INVALID_FRAMEBUFFER_OPERATION => "GL_INVALID_FRAMEBUFFER_OPERATION",
sys::GL_CONTEXT_LOST => "GL_CONTEXT_LOST",
_ => "GL_ERROR_UNKNOWN",
}
}
}
impl fmt::Display for GlErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (0x{:04X})", self.name(), self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GlError {
LibraryLoadFailed {
reason: String,
},
MissingFunction {
name: &'static str,
},
ContextCreationFailed {
reason: String,
},
UnsupportedVersion {
required: GlVersion,
actual: GlVersion,
},
GlCallFailed {
operation: &'static str,
code: GlErrorCode,
},
ShaderCompileFailed {
stage: ShaderStage,
log: String,
},
ProgramLinkFailed {
log: String,
},
ProgramValidateFailed {
log: String,
},
FramebufferIncomplete {
status: FramebufferStatus,
},
InvalidString {
parameter: &'static str,
},
InvalidParameter {
parameter: &'static str,
reason: &'static str,
},
OutOfBounds {
context: &'static str,
requested: usize,
available: usize,
},
IntegerOverflow {
context: &'static str,
},
AlreadyMapped,
MapFailed {
operation: &'static str,
},
ObjectCreationFailed {
object: &'static str,
},
BufferSizeMismatch {
expected: usize,
actual: usize,
},
SyncWaitFailed,
}
impl fmt::Display for GlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::LibraryLoadFailed { reason } => {
write!(f, "failed to load OpenGL library: {reason}")
}
Self::MissingFunction { name } => {
write!(f, "required OpenGL function `{name}` is not available")
}
Self::ContextCreationFailed { reason } => {
write!(f, "failed to create OpenGL context: {reason}")
}
Self::UnsupportedVersion { required, actual } => {
write!(
f,
"unsupported OpenGL version: required {required}, found {actual}"
)
}
Self::GlCallFailed { operation, code } => {
write!(f, "OpenGL call `{operation}` failed with {code}")
}
Self::ShaderCompileFailed { stage, log } => {
write!(f, "{stage:?} shader compilation failed:\n{log}")
}
Self::ProgramLinkFailed { log } => {
write!(f, "shader program linking failed:\n{log}")
}
Self::ProgramValidateFailed { log } => {
write!(f, "shader program validation failed:\n{log}")
}
Self::FramebufferIncomplete { status } => {
write!(f, "framebuffer is incomplete: {status:?}")
}
Self::InvalidString { parameter } => {
write!(f, "parameter `{parameter}` contains an interior NUL byte")
}
Self::InvalidParameter { parameter, reason } => {
write!(f, "invalid parameter `{parameter}`: {reason}")
}
Self::OutOfBounds {
context,
requested,
available,
} => {
write!(
f,
"out of bounds in {context}: requested {requested}, available {available}"
)
}
Self::IntegerOverflow { context } => {
write!(f, "integer overflow in {context}")
}
Self::AlreadyMapped => write!(f, "buffer is already mapped"),
Self::MapFailed { operation } => write!(f, "{operation} returned a null pointer"),
Self::ObjectCreationFailed { object } => {
write!(f, "OpenGL failed to allocate {object} object (handle 0)")
}
Self::BufferSizeMismatch { expected, actual } => {
write!(
f,
"buffer size mismatch: expected at least {expected} bytes, got {actual} bytes"
)
}
Self::SyncWaitFailed => write!(f, "glClientWaitSync returned GL_WAIT_FAILED"),
}
}
}
impl std::error::Error for GlError {}
#[inline]
pub(crate) fn checked_sizei(value: usize, context: &'static str) -> Result<sys::GLsizei> {
sys::GLsizei::try_from(value).map_err(|_| GlError::IntegerOverflow { context })
}
#[inline]
pub(crate) fn checked_sizeiptr(value: usize, context: &'static str) -> Result<sys::GLsizeiptr> {
sys::GLsizeiptr::try_from(value).map_err(|_| GlError::IntegerOverflow { context })
}
#[inline]
pub(crate) fn checked_intptr(value: usize, context: &'static str) -> Result<sys::GLintptr> {
sys::GLintptr::try_from(value).map_err(|_| GlError::IntegerOverflow { context })
}