use std::fmt;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
use crate::sys;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GlVersion {
pub major: u32,
pub minor: u32,
}
impl GlVersion {
pub const V3_3: Self = Self::new(3, 3);
pub const V4_0: Self = Self::new(4, 0);
pub const V4_1: Self = Self::new(4, 1);
pub const V4_2: Self = Self::new(4, 2);
pub const V4_3: Self = Self::new(4, 3);
pub const V4_4: Self = Self::new(4, 4);
pub const V4_5: Self = Self::new(4, 5);
pub const V4_6: Self = Self::new(4, 6);
#[inline]
pub const fn new(major: u32, minor: u32) -> Self {
Self { major, minor }
}
}
impl fmt::Display for GlVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GlProfile {
Core,
Compatibility,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContextInfo {
pub version: GlVersion,
pub profile: GlProfile,
pub vendor: String,
pub renderer: String,
pub version_string: String,
pub glsl_version: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum DataType {
I8 = sys::GL_BYTE,
U8 = sys::GL_UNSIGNED_BYTE,
I16 = sys::GL_SHORT,
U16 = sys::GL_UNSIGNED_SHORT,
I32 = sys::GL_INT,
U32 = sys::GL_UNSIGNED_INT,
F16 = sys::GL_HALF_FLOAT,
F32 = sys::GL_FLOAT,
F64 = sys::GL_DOUBLE,
}
impl DataType {
#[inline]
pub const fn byte_size(self) -> usize {
match self {
Self::I8 | Self::U8 => 1,
Self::I16 | Self::U16 | Self::F16 => 2,
Self::I32 | Self::U32 | Self::F32 => 4,
Self::F64 => 8,
}
}
#[inline]
pub const fn is_integer(self) -> bool {
matches!(
self,
Self::I8 | Self::U8 | Self::I16 | Self::U16 | Self::I32 | Self::U32
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum IndexType {
U8 = sys::GL_UNSIGNED_BYTE,
U16 = sys::GL_UNSIGNED_SHORT,
U32 = sys::GL_UNSIGNED_INT,
}
impl IndexType {
#[inline]
pub const fn byte_size(self) -> usize {
match self {
Self::U8 => 1,
Self::U16 => 2,
Self::U32 => 4,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum PrimitiveMode {
Points = sys::GL_POINTS,
Lines = sys::GL_LINES,
LineLoop = sys::GL_LINE_LOOP,
LineStrip = sys::GL_LINE_STRIP,
Triangles = sys::GL_TRIANGLES,
TriangleStrip = sys::GL_TRIANGLE_STRIP,
TriangleFan = sys::GL_TRIANGLE_FAN,
Patches = sys::GL_PATCHES,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum BufferTarget {
Array = sys::GL_ARRAY_BUFFER,
ElementArray = sys::GL_ELEMENT_ARRAY_BUFFER,
Uniform = sys::GL_UNIFORM_BUFFER,
ShaderStorage = sys::GL_SHADER_STORAGE_BUFFER,
CopyRead = sys::GL_COPY_READ_BUFFER,
CopyWrite = sys::GL_COPY_WRITE_BUFFER,
PixelPack = sys::GL_PIXEL_PACK_BUFFER,
PixelUnpack = sys::GL_PIXEL_UNPACK_BUFFER,
DrawIndirect = sys::GL_DRAW_INDIRECT_BUFFER,
DispatchIndirect = sys::GL_DISPATCH_INDIRECT_BUFFER,
AtomicCounter = sys::GL_ATOMIC_COUNTER_BUFFER,
}
impl BufferTarget {
#[inline]
pub const fn is_indexed(self) -> bool {
matches!(
self,
Self::Uniform | Self::ShaderStorage | Self::AtomicCounter
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum BufferUsage {
StreamDraw = sys::GL_STREAM_DRAW,
StreamRead = sys::GL_STREAM_READ,
StreamCopy = sys::GL_STREAM_COPY,
StaticDraw = sys::GL_STATIC_DRAW,
StaticRead = sys::GL_STATIC_READ,
StaticCopy = sys::GL_STATIC_COPY,
DynamicDraw = sys::GL_DYNAMIC_DRAW,
DynamicRead = sys::GL_DYNAMIC_READ,
DynamicCopy = sys::GL_DYNAMIC_COPY,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum ShaderStage {
Vertex = sys::GL_VERTEX_SHADER,
Fragment = sys::GL_FRAGMENT_SHADER,
Geometry = sys::GL_GEOMETRY_SHADER,
TessControl = sys::GL_TESS_CONTROL_SHADER,
TessEvaluation = sys::GL_TESS_EVALUATION_SHADER,
Compute = sys::GL_COMPUTE_SHADER,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UniformLocation(pub(crate) i32);
impl UniformLocation {
#[inline]
pub const fn raw(self) -> i32 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AttribLocation(pub(crate) u32);
impl AttribLocation {
#[inline]
pub const fn raw(self) -> u32 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum TextureTarget {
Texture2D = sys::GL_TEXTURE_2D,
Texture3D = sys::GL_TEXTURE_3D,
CubeMap = sys::GL_TEXTURE_CUBE_MAP,
Texture2DArray = sys::GL_TEXTURE_2D_ARRAY,
Texture2DMultisample = sys::GL_TEXTURE_2D_MULTISAMPLE,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum InternalFormat {
R8 = sys::GL_R8,
Rg8 = sys::GL_RG8,
Rgb8 = sys::GL_RGB8,
Rgba8 = sys::GL_RGBA8,
Srgb8 = sys::GL_SRGB8,
Srgb8Alpha8 = sys::GL_SRGB8_ALPHA8,
R16f = sys::GL_R16F,
Rg16f = sys::GL_RG16F,
Rgb16f = sys::GL_RGB16F,
Rgba16f = sys::GL_RGBA16F,
R32f = sys::GL_R32F,
Rg32f = sys::GL_RG32F,
Rgb32f = sys::GL_RGB32F,
Rgba32f = sys::GL_RGBA32F,
R32ui = sys::GL_R32UI,
Rgba32ui = sys::GL_RGBA32UI,
Depth16 = sys::GL_DEPTH_COMPONENT16,
Depth24 = sys::GL_DEPTH_COMPONENT24,
Depth32f = sys::GL_DEPTH_COMPONENT32F,
Depth24Stencil8 = sys::GL_DEPTH24_STENCIL8,
Depth32fStencil8 = sys::GL_DEPTH32F_STENCIL8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum PixelFormat {
Red = sys::GL_RED,
Rg = sys::GL_RG,
Rgb = sys::GL_RGB,
Rgba = sys::GL_RGBA,
Bgr = sys::GL_BGR,
Bgra = sys::GL_BGRA,
DepthComponent = sys::GL_DEPTH_COMPONENT,
}
impl PixelFormat {
#[inline]
pub const fn channel_count(self) -> usize {
match self {
Self::Red | Self::DepthComponent => 1,
Self::Rg => 2,
Self::Rgb | Self::Bgr => 3,
Self::Rgba | Self::Bgra => 4,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum PixelType {
U8 = sys::GL_UNSIGNED_BYTE,
U16 = sys::GL_UNSIGNED_SHORT,
U32 = sys::GL_UNSIGNED_INT,
F16 = sys::GL_HALF_FLOAT,
F32 = sys::GL_FLOAT,
}
impl PixelType {
#[inline]
pub const fn component_bytes(self) -> usize {
match self {
Self::U8 => 1,
Self::U16 | Self::F16 => 2,
Self::U32 | Self::F32 => 4,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum TextureFilter {
Nearest = sys::GL_NEAREST,
Linear = sys::GL_LINEAR,
NearestMipmapNearest = sys::GL_NEAREST_MIPMAP_NEAREST,
LinearMipmapNearest = sys::GL_LINEAR_MIPMAP_NEAREST,
NearestMipmapLinear = sys::GL_NEAREST_MIPMAP_LINEAR,
LinearMipmapLinear = sys::GL_LINEAR_MIPMAP_LINEAR,
}
impl TextureFilter {
#[inline]
pub const fn is_valid_mag_filter(self) -> bool {
matches!(self, Self::Nearest | Self::Linear)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum TextureWrap {
Repeat = sys::GL_REPEAT,
ClampToEdge = sys::GL_CLAMP_TO_EDGE,
ClampToBorder = sys::GL_CLAMP_TO_BORDER,
MirroredRepeat = sys::GL_MIRRORED_REPEAT,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum FramebufferTarget {
Both = sys::GL_FRAMEBUFFER,
Read = sys::GL_READ_FRAMEBUFFER,
Draw = sys::GL_DRAW_FRAMEBUFFER,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FramebufferAttachment {
Color(u32),
Depth,
Stencil,
DepthStencil,
}
impl FramebufferAttachment {
pub(crate) const fn to_gl(self) -> Option<sys::GLenum> {
match self {
Self::Color(idx) if idx < 32 => Some(sys::GL_COLOR_ATTACHMENT0 + idx),
Self::Color(_) => None,
Self::Depth => Some(sys::GL_DEPTH_ATTACHMENT),
Self::Stencil => Some(sys::GL_STENCIL_ATTACHMENT),
Self::DepthStencil => Some(sys::GL_DEPTH_STENCIL_ATTACHMENT),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FramebufferStatus {
Complete,
Undefined,
IncompleteAttachment,
IncompleteMissingAttachment,
IncompleteDrawBuffer,
IncompleteReadBuffer,
Unsupported,
IncompleteMultisample,
IncompleteLayerTargets,
Unknown(u32),
}
impl FramebufferStatus {
#[inline]
pub const fn from_raw(raw: u32) -> Self {
match raw {
sys::GL_FRAMEBUFFER_COMPLETE => Self::Complete,
sys::GL_FRAMEBUFFER_UNDEFINED => Self::Undefined,
sys::GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT => Self::IncompleteAttachment,
sys::GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT => Self::IncompleteMissingAttachment,
sys::GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER => Self::IncompleteDrawBuffer,
sys::GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER => Self::IncompleteReadBuffer,
sys::GL_FRAMEBUFFER_UNSUPPORTED => Self::Unsupported,
sys::GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE => Self::IncompleteMultisample,
sys::GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS => Self::IncompleteLayerTargets,
other => Self::Unknown(other),
}
}
#[inline]
pub const fn is_complete(self) -> bool {
matches!(self, Self::Complete)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum Capability {
Blend = sys::GL_BLEND,
CullFace = sys::GL_CULL_FACE,
DepthTest = sys::GL_DEPTH_TEST,
ScissorTest = sys::GL_SCISSOR_TEST,
StencilTest = sys::GL_STENCIL_TEST,
Multisample = sys::GL_MULTISAMPLE,
FramebufferSrgb = sys::GL_FRAMEBUFFER_SRGB,
ProgramPointSize = sys::GL_PROGRAM_POINT_SIZE,
TextureCubeMapSeamless = sys::GL_TEXTURE_CUBE_MAP_SEAMLESS,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum CompareFunc {
Never = sys::GL_NEVER,
Less = sys::GL_LESS,
Equal = sys::GL_EQUAL,
LessOrEqual = sys::GL_LEQUAL,
Greater = sys::GL_GREATER,
NotEqual = sys::GL_NOTEQUAL,
GreaterOrEqual = sys::GL_GEQUAL,
Always = sys::GL_ALWAYS,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum BlendFactor {
Zero = sys::GL_ZERO,
One = sys::GL_ONE,
SrcColor = sys::GL_SRC_COLOR,
OneMinusSrcColor = sys::GL_ONE_MINUS_SRC_COLOR,
SrcAlpha = sys::GL_SRC_ALPHA,
OneMinusSrcAlpha = sys::GL_ONE_MINUS_SRC_ALPHA,
DstAlpha = sys::GL_DST_ALPHA,
OneMinusDstAlpha = sys::GL_ONE_MINUS_DST_ALPHA,
DstColor = sys::GL_DST_COLOR,
OneMinusDstColor = sys::GL_ONE_MINUS_DST_COLOR,
SrcAlphaSaturate = sys::GL_SRC_ALPHA_SATURATE,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum BlendEquation {
Add = sys::GL_FUNC_ADD,
Subtract = sys::GL_FUNC_SUBTRACT,
ReverseSubtract = sys::GL_FUNC_REVERSE_SUBTRACT,
Min = sys::GL_MIN,
Max = sys::GL_MAX,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum CullFace {
Front = sys::GL_FRONT,
Back = sys::GL_BACK,
FrontAndBack = sys::GL_FRONT_AND_BACK,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum FrontFace {
Clockwise = sys::GL_CW,
CounterClockwise = sys::GL_CCW,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum PolygonMode {
Point = sys::GL_POINT,
Line = sys::GL_LINE,
Fill = sys::GL_FILL,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Viewport {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
}
impl Viewport {
#[inline]
pub const fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
Self {
x,
y,
width,
height,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScissorRect {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
}
impl ScissorRect {
#[inline]
pub const fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
Self {
x,
y,
width,
height,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ClearColor {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl ClearColor {
pub const BLACK: Self = Self::new(0.0, 0.0, 0.0, 1.0);
pub const WHITE: Self = Self::new(1.0, 1.0, 1.0, 1.0);
pub const TRANSPARENT: Self = Self::new(0.0, 0.0, 0.0, 0.0);
#[inline]
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
}
macro_rules! define_bitflags {
(
$(#[$meta:meta])*
pub struct $name:ident(u32);
$(
$(#[$const_meta:meta])*
pub const $const_name:ident = $val:expr;
)*
) => {
$(#[$meta])*
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(transparent)]
pub struct $name(u32);
impl $name {
pub const EMPTY: Self = Self(0);
$(
$(#[$const_meta])*
pub const $const_name: Self = Self($val);
)*
#[inline]
pub const fn from_bits(bits: u32) -> Self {
Self(bits)
}
#[inline]
pub const fn bits(self) -> u32 {
self.0
}
#[inline]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[inline]
pub const fn contains(self, other: Self) -> bool {
(self.0 & other.0) == other.0
}
#[inline]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl BitOr for $name {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for $name {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl BitAnd for $name {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}
impl BitAndAssign for $name {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}
};
}
define_bitflags! {
pub struct ClearMask(u32);
pub const COLOR = sys::GL_COLOR_BUFFER_BIT;
pub const DEPTH = sys::GL_DEPTH_BUFFER_BIT;
pub const STENCIL = sys::GL_STENCIL_BUFFER_BIT;
}
define_bitflags! {
pub struct BufferStorageFlags(u32);
pub const DYNAMIC_STORAGE = sys::GL_DYNAMIC_STORAGE_BIT;
pub const MAP_READ = sys::GL_MAP_READ_BIT;
pub const MAP_WRITE = sys::GL_MAP_WRITE_BIT;
pub const MAP_PERSISTENT = sys::GL_MAP_PERSISTENT_BIT;
pub const MAP_COHERENT = sys::GL_MAP_COHERENT_BIT;
pub const CLIENT_STORAGE = sys::GL_CLIENT_STORAGE_BIT;
}
define_bitflags! {
pub struct MemoryBarrierFlags(u32);
pub const VERTEX_ATTRIB_ARRAY = sys::GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT;
pub const ELEMENT_ARRAY = sys::GL_ELEMENT_ARRAY_BARRIER_BIT;
pub const UNIFORM = sys::GL_UNIFORM_BARRIER_BIT;
pub const TEXTURE_FETCH = sys::GL_TEXTURE_FETCH_BARRIER_BIT;
pub const SHADER_IMAGE_ACCESS = sys::GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
pub const COMMAND = sys::GL_COMMAND_BARRIER_BIT;
pub const PIXEL_BUFFER = sys::GL_PIXEL_BUFFER_BARRIER_BIT;
pub const TEXTURE_UPDATE = sys::GL_TEXTURE_UPDATE_BARRIER_BIT;
pub const BUFFER_UPDATE = sys::GL_BUFFER_UPDATE_BARRIER_BIT;
pub const FRAMEBUFFER = sys::GL_FRAMEBUFFER_BARRIER_BIT;
pub const SHADER_STORAGE = sys::GL_SHADER_STORAGE_BARRIER_BIT;
pub const ALL = sys::GL_ALL_BARRIER_BITS;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DebugSource {
Api,
WindowSystem,
ShaderCompiler,
ThirdParty,
Application,
Other(u32),
}
impl DebugSource {
pub(crate) const fn from_raw(raw: u32) -> Self {
match raw {
sys::GL_DEBUG_SOURCE_API => Self::Api,
sys::GL_DEBUG_SOURCE_WINDOW_SYSTEM => Self::WindowSystem,
sys::GL_DEBUG_SOURCE_SHADER_COMPILER => Self::ShaderCompiler,
sys::GL_DEBUG_SOURCE_THIRD_PARTY => Self::ThirdParty,
sys::GL_DEBUG_SOURCE_APPLICATION => Self::Application,
other => Self::Other(other),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DebugType {
Error,
DeprecatedBehavior,
UndefinedBehavior,
Portability,
Performance,
Marker,
Other(u32),
}
impl DebugType {
pub(crate) const fn from_raw(raw: u32) -> Self {
match raw {
sys::GL_DEBUG_TYPE_ERROR => Self::Error,
sys::GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR => Self::DeprecatedBehavior,
sys::GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR => Self::UndefinedBehavior,
sys::GL_DEBUG_TYPE_PORTABILITY => Self::Portability,
sys::GL_DEBUG_TYPE_PERFORMANCE => Self::Performance,
sys::GL_DEBUG_TYPE_MARKER => Self::Marker,
other => Self::Other(other),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DebugSeverity {
High,
Medium,
Low,
Notification,
Unknown(u32),
}
impl DebugSeverity {
pub(crate) const fn from_raw(raw: u32) -> Self {
match raw {
sys::GL_DEBUG_SEVERITY_HIGH => Self::High,
sys::GL_DEBUG_SEVERITY_MEDIUM => Self::Medium,
sys::GL_DEBUG_SEVERITY_LOW => Self::Low,
sys::GL_DEBUG_SEVERITY_NOTIFICATION => Self::Notification,
other => Self::Unknown(other),
}
}
}