use super::*;
pub(crate) fn gl_shader_type(kind: GlShaderKind) -> u32 {
match kind {
GlShaderKind::Vertex => WebGl2RenderingContext::VERTEX_SHADER,
GlShaderKind::Fragment => WebGl2RenderingContext::FRAGMENT_SHADER,
}
}
pub(crate) fn gl_primitive_mode(topology: PrimitiveTopology) -> u32 {
match topology {
PrimitiveTopology::PointList => WebGl2RenderingContext::POINTS,
PrimitiveTopology::LineList => WebGl2RenderingContext::LINES,
PrimitiveTopology::LineStrip => WebGl2RenderingContext::LINE_STRIP,
PrimitiveTopology::TriangleList => WebGl2RenderingContext::TRIANGLES,
PrimitiveTopology::TriangleStrip => WebGl2RenderingContext::TRIANGLE_STRIP,
}
}
pub fn gl_index_type(format: IndexFormat) -> (u32, u32) {
match format {
IndexFormat::Uint16 => (WebGl2RenderingContext::UNSIGNED_SHORT, GL_U16_SIZE),
IndexFormat::Uint32 => (WebGl2RenderingContext::UNSIGNED_INT, GL_U32_SIZE),
}
}
pub fn gl_buffer_target(usage: BufferUsage) -> u32 {
match usage {
BufferUsage::Index => WebGl2RenderingContext::ELEMENT_ARRAY_BUFFER,
BufferUsage::Uniform => WebGl2RenderingContext::UNIFORM_BUFFER,
_ => WebGl2RenderingContext::ARRAY_BUFFER,
}
}
pub fn gl_buffer_usage_hint(usage: BufferUsage) -> u32 {
match usage {
BufferUsage::CopyDestination | BufferUsage::MapWrite | BufferUsage::Storage => {
WebGl2RenderingContext::DYNAMIC_DRAW
}
_ => WebGl2RenderingContext::STATIC_DRAW,
}
}
pub fn gl_filter_mode(filter: FilterMode) -> u32 {
match filter {
FilterMode::Nearest => WebGl2RenderingContext::NEAREST,
FilterMode::Linear => WebGl2RenderingContext::LINEAR,
}
}
pub fn gl_min_filter(filter: FilterMode, mipmap: MipmapFilter) -> u32 {
match (filter, mipmap) {
(FilterMode::Nearest, MipmapFilter::Nearest) => {
WebGl2RenderingContext::NEAREST_MIPMAP_NEAREST
}
(FilterMode::Nearest, MipmapFilter::Linear) => {
WebGl2RenderingContext::NEAREST_MIPMAP_LINEAR
}
(FilterMode::Linear, MipmapFilter::Nearest) => {
WebGl2RenderingContext::LINEAR_MIPMAP_NEAREST
}
(FilterMode::Linear, MipmapFilter::Linear) => WebGl2RenderingContext::LINEAR_MIPMAP_LINEAR,
}
}
pub fn gl_address_mode(mode: AddressMode) -> u32 {
match mode {
AddressMode::ClampToEdge => GL_ADDRESS_CLAMP_TO_EDGE,
AddressMode::MirrorRepeat => GL_ADDRESS_MIRRORED_REPEAT,
AddressMode::Repeat => GL_ADDRESS_REPEAT,
}
}
pub fn gl_compare_function(compare: CompareFunction) -> u32 {
match compare {
CompareFunction::Never => WebGl2RenderingContext::NEVER,
CompareFunction::Less => WebGl2RenderingContext::LESS,
CompareFunction::Equal => WebGl2RenderingContext::EQUAL,
CompareFunction::LessEqual => WebGl2RenderingContext::LEQUAL,
CompareFunction::Greater => WebGl2RenderingContext::GREATER,
CompareFunction::NotEqual => WebGl2RenderingContext::NOTEQUAL,
CompareFunction::GreaterEqual => WebGl2RenderingContext::GEQUAL,
CompareFunction::Always => WebGl2RenderingContext::ALWAYS,
}
}
pub fn gl_blend_factor(factor: BlendFactor) -> u32 {
match factor {
BlendFactor::Zero => GL_BLEND_FACTOR_ZERO,
BlendFactor::One => GL_BLEND_FACTOR_ONE,
BlendFactor::SourceColor => WebGl2RenderingContext::SRC_COLOR,
BlendFactor::OneMinusSourceColor => WebGl2RenderingContext::ONE_MINUS_SRC_COLOR,
BlendFactor::DestinationColor => WebGl2RenderingContext::DST_COLOR,
BlendFactor::OneMinusDestinationColor => WebGl2RenderingContext::ONE_MINUS_DST_COLOR,
BlendFactor::SourceAlpha => GL_BLEND_FACTOR_SRC_ALPHA,
BlendFactor::OneMinusSourceAlpha => GL_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
BlendFactor::DestinationAlpha => WebGl2RenderingContext::DST_ALPHA,
BlendFactor::OneMinusDestinationAlpha => WebGl2RenderingContext::ONE_MINUS_DST_ALPHA,
BlendFactor::ConstantColor => WebGl2RenderingContext::CONSTANT_COLOR,
BlendFactor::OneMinusConstantColor => WebGl2RenderingContext::ONE_MINUS_CONSTANT_COLOR,
BlendFactor::ConstantAlpha => WebGl2RenderingContext::CONSTANT_ALPHA,
BlendFactor::OneMinusConstantAlpha => WebGl2RenderingContext::ONE_MINUS_CONSTANT_ALPHA,
BlendFactor::SourceAlphaSaturated => GL_BLEND_FACTOR_SRC_ALPHA_SATURATE,
}
}
pub fn gl_blend_equation(operation: BlendOperation) -> u32 {
match operation {
BlendOperation::Add => GL_BLEND_EQUATION_ADD,
BlendOperation::Subtract => WebGl2RenderingContext::FUNC_SUBTRACT,
BlendOperation::ReverseSubtract => WebGl2RenderingContext::FUNC_REVERSE_SUBTRACT,
BlendOperation::Min => WebGl2RenderingContext::MIN,
BlendOperation::Max => WebGl2RenderingContext::MAX,
}
}
pub fn gl_cull_face(mode: CullMode) -> u32 {
match mode {
CullMode::None => WebGl2RenderingContext::FRONT_AND_BACK,
CullMode::Front => WebGl2RenderingContext::FRONT,
CullMode::Back => WebGl2RenderingContext::BACK,
}
}
pub fn gl_front_face(face: FrontFace) -> u32 {
match face {
FrontFace::CounterClockwise => WebGl2RenderingContext::CCW,
FrontFace::Clockwise => WebGl2RenderingContext::CW,
}
}
pub fn gl_attribute_layout(format: VertexAttributeFormat) -> (u32, u32, bool) {
match format {
VertexAttributeFormat::Float32 => (1, GL_TYPE_FLOAT, false),
VertexAttributeFormat::Float32x2 => (2, GL_TYPE_FLOAT, false),
VertexAttributeFormat::Float32x3 => (3, GL_TYPE_FLOAT, false),
VertexAttributeFormat::Float32x4 => (4, GL_TYPE_FLOAT, false),
VertexAttributeFormat::Unorm8x4 => (4, GL_TYPE_UNSIGNED_BYTE, true),
VertexAttributeFormat::Uint32 => (1, GL_TYPE_UNSIGNED_INT, false),
VertexAttributeFormat::Sint32 => (1, GL_TYPE_INT, false),
}
}
pub fn gl_texture_layout(format: GpuTextureFormat) -> (i32, u32, u32) {
match format {
GpuTextureFormat::Rgba8Unorm | GpuTextureFormat::Bgra8Unorm => (
WebGl2RenderingContext::RGBA8 as i32,
WebGl2RenderingContext::RGBA,
GL_TYPE_UNSIGNED_BYTE,
),
GpuTextureFormat::Rgba16Float => (
WebGl2RenderingContext::RGBA16F as i32,
WebGl2RenderingContext::RGBA,
GL_TYPE_FLOAT,
),
GpuTextureFormat::Rgba32Float => (
WebGl2RenderingContext::RGBA32F as i32,
WebGl2RenderingContext::RGBA,
GL_TYPE_FLOAT,
),
GpuTextureFormat::R32Float => (GL_FORMAT_R32F as i32, GL_FORMAT_RED, GL_TYPE_FLOAT),
GpuTextureFormat::Depth24Plus => (
WebGl2RenderingContext::DEPTH_COMPONENT24 as i32,
WebGl2RenderingContext::DEPTH_COMPONENT,
WebGl2RenderingContext::UNSIGNED_INT,
),
GpuTextureFormat::Depth24PlusStencil8 => (
WebGl2RenderingContext::DEPTH24_STENCIL8 as i32,
WebGl2RenderingContext::DEPTH_STENCIL,
WebGl2RenderingContext::UNSIGNED_INT_24_8,
),
GpuTextureFormat::Depth32Float => (
WebGl2RenderingContext::DEPTH_COMPONENT32F as i32,
WebGl2RenderingContext::DEPTH_COMPONENT,
GL_TYPE_FLOAT,
),
}
}
pub fn gl_renderbuffer_format(format: GpuTextureFormat) -> u32 {
match format {
GpuTextureFormat::Rgba8Unorm | GpuTextureFormat::Bgra8Unorm => {
WebGl2RenderingContext::RGBA8
}
GpuTextureFormat::Rgba16Float => WebGl2RenderingContext::RGBA16F,
GpuTextureFormat::Rgba32Float => WebGl2RenderingContext::RGBA32F,
GpuTextureFormat::R32Float => GL_FORMAT_R32F,
GpuTextureFormat::Depth24Plus => WebGl2RenderingContext::DEPTH_COMPONENT24,
GpuTextureFormat::Depth24PlusStencil8 => WebGl2RenderingContext::DEPTH24_STENCIL8,
GpuTextureFormat::Depth32Float => WebGl2RenderingContext::DEPTH_COMPONENT32F,
}
}
pub fn gl_framebuffer_status(status: u32) -> GlFramebufferStatus {
match status {
WebGl2RenderingContext::FRAMEBUFFER_COMPLETE => GlFramebufferStatus::Complete,
WebGl2RenderingContext::FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT => {
GlFramebufferStatus::Incomplete
}
WebGl2RenderingContext::FRAMEBUFFER_INCOMPLETE_DIMENSIONS
| WebGl2RenderingContext::FRAMEBUFFER_INCOMPLETE_MULTISAMPLE => {
GlFramebufferStatus::Unsupported
}
_ => GlFramebufferStatus::Other,
}
}
pub fn gl_texture_unit(unit: u32) -> u32 {
WebGl2RenderingContext::TEXTURE0 + unit
}
pub fn gl_texture_target_2d() -> u32 {
GL_TEXTURE_TARGET_2D
}
pub fn gl_matrix4_into_f32(matrix: &Matrix4x4, out: &mut [f32]) {
let source: [f64; GL_MAT4_FLOATS] = matrix.get_elements();
for (index, value) in source.iter().enumerate() {
out[index] = *value as f32;
}
}
pub fn gl_flip_y(flip: bool) -> i32 {
if flip { 1 } else { 0 }
}