mln-rs-plugin 0.0.22

Safe Rust SDK for writing MapLibre layer and source plugins
Documentation
//! Host-agnostic shader program descriptors and owned [`Program`] handles.

use std::sync::Arc;

use crate::backend::ProgramHandle;
use crate::error::Result;

/// Which shader stages read a buffer, paint parameters, or texture.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShaderStages {
    Vertex,
    Fragment,
    Both,
}

/// How a shader program receives its tile/frame projection uniforms.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ProjectionMode {
    #[default]
    None,
    PerDraw,
    Own,
}

/// Which compiled shader variants include a buffer, attribute, or texture.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Variants<'a> {
    #[default]
    All,
    Standing,
    PerVertex,
    Defined(&'a str),
}

/// Member type inside a shader uniform or storage block.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BlockMemberType {
    Float,
    Int,
    Uint,
    Vec2,
    Vec3,
    Vec4,
    Mat4,
    Vec4Array(u32),
}

/// Field description inside a [`BlockDesc`].
#[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 }
    }
}

/// Std140/std430 buffer block layout description.
#[derive(Clone, Copy, Debug)]
pub struct BlockDesc<'a> {
    pub name: &'a str,
    pub byte_size: u32,
    pub members: &'a [BlockMember<'a>],
}

/// Binding kind for a shader buffer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BufferKind {
    Uniform,
    DrawUniform,
    Storage,
    VerticesUint,
    VerticesFloat,
}

/// Descriptor for one uniform, storage, or vertex-data buffer declared by a program.
#[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,
}

/// Vertex or instance attribute data format.
#[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,
}

/// Vertex or instance attribute declared by a program.
#[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 }
    }
}

/// How a shader samples a texture binding.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextureRead {
    Filtered,
    Unfiltered,
    Texels,
}

/// Texture binding slot declared by a program.
///
/// Use [`TextureDesc::UNUSED`] for an unoccupied slot to keep binding indices
/// aligned with shader source declarations.
#[derive(Clone, Copy, Debug)]
pub struct TextureDesc<'a> {
    pub name: &'a str,
    pub read: TextureRead,
    pub stages: ShaderStages,
    pub variants: Variants<'a>,
}

impl TextureDesc<'_> {
    /// Placeholder for an unused texture slot.
    pub const UNUSED: Self =
        Self { name: "", read: TextureRead::Filtered, stages: ShaderStages::Fragment, variants: Variants::All };
}

/// GLSL ES precision qualifier.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GlslPrecision {
    Low,
    Medium,
    High,
}

/// WGSL shader source strings.
#[derive(Clone, Copy, Debug)]
pub struct WgslSource<'a> {
    pub vertex: &'a str,
    pub fragment: &'a str,
    pub prelude: &'a str,
}

/// Metal Shading Language source strings.
#[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,
}

/// Vulkan GLSL 450 shader source strings.
#[derive(Clone, Copy, Debug)]
pub struct GlslSource<'a> {
    pub vertex: &'a str,
    pub fragment: &'a str,
}

/// GLSL ES precision override for members of a uniform block.
#[derive(Clone, Copy, Debug)]
pub struct GlslEsMemberPrecision<'a> {
    pub block_name: &'a str,
    pub precision: GlslPrecision,
    pub member_names: &'a [&'a str],
}

/// GLSL ES precision override for vertex attributes.
#[derive(Clone, Copy, Debug)]
pub struct GlslEsAttributePrecision<'a> {
    pub precision: GlslPrecision,
    pub attribute_names: &'a [&'a str],
}

/// GLSL ES 3.00 shader source strings and precision overrides.
#[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],
}

/// Complete multi-backend shader program descriptor.
#[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>>,
}

/// Owned handle to a validated host shader program.
#[derive(Clone, Debug)]
pub struct Program {
    inner: Arc<dyn ProgramHandle>,
}

impl Program {
    /// Wraps a host-provided program handle.
    #[doc(hidden)]
    #[must_use]
    pub fn from_handle(inner: Arc<dyn ProgramHandle>) -> Self {
        Self { inner }
    }

    /// Accesses the underlying host handle (for host backend downcasting).
    #[doc(hidden)]
    #[must_use]
    pub fn handle(&self) -> &dyn ProgramHandle {
        &*self.inner
    }

    /// Resolves the binding slot ID of a named buffer declared by this program.
    pub fn buffer_id(&self, name: &str) -> Result<u32> {
        self.inner.buffer_id(name)
    }

    /// Resolves the binding slot index of a named texture declared by this program.
    pub fn texture_slot(&self, name: &str) -> Result<u32> {
        self.inner.texture_slot(name)
    }
}