use std::sync::Arc;
use crate::backend::ProgramHandle;
use crate::error::Result;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShaderStages {
Vertex,
Fragment,
Both,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ProjectionMode {
#[default]
None,
PerDraw,
Own,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Variants<'a> {
#[default]
All,
Standing,
PerVertex,
Defined(&'a str),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BlockMemberType {
Float,
Int,
Uint,
Vec2,
Vec3,
Vec4,
Mat4,
Vec4Array(u32),
}
#[derive(Clone, Copy, Debug)]
pub struct BlockMember<'a> {
pub name: &'a str,
pub ty: BlockMemberType,
pub offset: u32,
}
impl<'a> BlockMember<'a> {
#[must_use]
pub const fn new(name: &'a str, ty: BlockMemberType, offset: u32) -> Self {
Self { name, ty, offset }
}
}
#[derive(Clone, Copy, Debug)]
pub struct BlockDesc<'a> {
pub name: &'a str,
pub byte_size: u32,
pub members: &'a [BlockMember<'a>],
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BufferKind {
Uniform,
DrawUniform,
Storage,
VerticesUint,
VerticesFloat,
}
#[derive(Clone, Copy, Debug)]
pub struct BufferDesc<'a> {
pub name: &'a str,
pub kind: BufferKind,
pub slot: u32,
pub stages: ShaderStages,
pub variants: Variants<'a>,
pub block: Option<BlockDesc<'a>>,
pub storage_stride: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AttributeFormat {
Byte,
Byte2,
Byte3,
Byte4,
UByte,
UByte2,
UByte3,
UByte4,
Short,
Short2,
Short3,
Short4,
UShort,
UShort2,
UShort3,
UShort4,
Int,
Int2,
Int3,
Int4,
UInt,
UInt2,
UInt3,
UInt4,
Float,
Float2,
Float3,
Float4,
UByte4Normalized,
}
#[derive(Clone, Copy, Debug)]
pub struct AttributeDesc<'a> {
pub name: &'a str,
pub format: AttributeFormat,
pub variants: Variants<'a>,
}
impl<'a> AttributeDesc<'a> {
#[must_use]
pub const fn new(name: &'a str, format: AttributeFormat) -> Self {
Self { name, format, variants: Variants::All }
}
#[must_use]
pub const fn standing(name: &'a str, format: AttributeFormat) -> Self {
Self { name, format, variants: Variants::Standing }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextureRead {
Filtered,
Unfiltered,
Texels,
}
#[derive(Clone, Copy, Debug)]
pub struct TextureDesc<'a> {
pub name: &'a str,
pub read: TextureRead,
pub stages: ShaderStages,
pub variants: Variants<'a>,
}
impl TextureDesc<'_> {
pub const UNUSED: Self =
Self { name: "", read: TextureRead::Filtered, stages: ShaderStages::Fragment, variants: Variants::All };
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GlslPrecision {
Low,
Medium,
High,
}
#[derive(Clone, Copy, Debug)]
pub struct WgslSource<'a> {
pub vertex: &'a str,
pub fragment: &'a str,
pub prelude: &'a str,
}
#[derive(Clone, Copy, Debug)]
pub struct MslSource<'a> {
pub vertex_main_function: &'a str,
pub fragment_main_function: &'a str,
pub source: &'a str,
pub fast_math: bool,
}
#[derive(Clone, Copy, Debug)]
pub struct GlslSource<'a> {
pub vertex: &'a str,
pub fragment: &'a str,
}
#[derive(Clone, Copy, Debug)]
pub struct GlslEsMemberPrecision<'a> {
pub block_name: &'a str,
pub precision: GlslPrecision,
pub member_names: &'a [&'a str],
}
#[derive(Clone, Copy, Debug)]
pub struct GlslEsAttributePrecision<'a> {
pub precision: GlslPrecision,
pub attribute_names: &'a [&'a str],
}
#[derive(Clone, Copy, Debug)]
pub struct GlslEsSource<'a> {
pub vertex: &'a str,
pub fragment: &'a str,
pub member_precisions: &'a [GlslEsMemberPrecision<'a>],
pub attribute_precisions: &'a [GlslEsAttributePrecision<'a>],
pub highp_samplers: &'a [&'a str],
}
#[derive(Clone, Copy, Debug)]
pub struct ProgramDesc<'a> {
pub name: &'a str,
pub buffers: &'a [BufferDesc<'a>],
pub paint_params: Option<ShaderStages>,
pub projection: ProjectionMode,
pub attributes: &'a [AttributeDesc<'a>],
pub instance_attributes: &'a [AttributeDesc<'a>],
pub textures: &'a [TextureDesc<'a>],
pub properties: &'a [&'a str],
pub wgsl: Option<WgslSource<'a>>,
pub msl: Option<MslSource<'a>>,
pub glsl: Option<GlslSource<'a>>,
pub glsl_es: Option<GlslEsSource<'a>>,
}
#[derive(Clone, Debug)]
pub struct Program {
inner: Arc<dyn ProgramHandle>,
}
impl Program {
#[doc(hidden)]
#[must_use]
pub fn from_handle(inner: Arc<dyn ProgramHandle>) -> Self {
Self { inner }
}
#[doc(hidden)]
#[must_use]
pub fn handle(&self) -> &dyn ProgramHandle {
&*self.inner
}
pub fn buffer_id(&self, name: &str) -> Result<u32> {
self.inner.buffer_id(name)
}
pub fn texture_slot(&self, name: &str) -> Result<u32> {
self.inner.texture_slot(name)
}
}