ombre 0.6.7

Shadowy game and graphics library for Rust
Documentation
//! Shader uniforms.

/// Uniform type.
#[derive(Clone, Copy, Debug)]
pub enum UniformKind {
    /// One 32-bit wide float (equivalent to `f32`).
    Float1,
    /// Two 32-bit wide floats (equivalent to `[f32; 2]`).
    Float2,
    /// Three 32-bit wide floats (equivalent to `[f32; 3]`).
    Float3,
    /// Four 32-bit wide floats (equivalent to `[f32; 4]`).
    Float4,
    /// One unsigned 32-bit integer (equivalent to `[u32; 1]`).
    Int1,
    /// Two unsigned 32-bit integers (equivalent to `[u32; 2]`).
    Int2,
    /// Three unsigned 32-bit integers (equivalent to `[u32; 3]`).
    Int3,
    /// Four unsigned 32-bit integers (equivalent to `[u32; 4]`).
    Int4,
    /// Four by four matrix of 32-bit floats.
    Mat4,
}

impl UniformKind {
    /// Byte size for a given uniform type.
    pub fn size(&self) -> usize {
        match self {
            UniformKind::Float1 => 4,
            UniformKind::Float2 => 8,
            UniformKind::Float3 => 12,
            UniformKind::Float4 => 16,
            UniformKind::Int1 => 4,
            UniformKind::Int2 => 8,
            UniformKind::Int3 => 12,
            UniformKind::Int4 => 16,
            UniformKind::Mat4 => 64,
        }
    }
}

/// Describes a shader uniform.
#[derive(Debug, Clone)]
pub struct UniformDescriptor {
    /// Uniform name.
    pub name: String,
    /// Uniform type.
    pub kind: UniformKind,
    /// Array length, for arrays.
    pub arity: usize,
}

/// A uniform block layout descriptor.
#[derive(Debug, Clone)]
pub struct UniformBlockLayout {
    /// The uniforms in the block.
    pub uniforms: Vec<UniformDescriptor>,
}

impl UniformDescriptor {
    /// New descriptor.
    pub fn new(name: &str, uniform_type: UniformKind) -> UniformDescriptor {
        UniformDescriptor {
            name: name.to_string(),
            kind: uniform_type,
            arity: 1,
        }
    }

    /// Specify uniform arity.
    pub fn arity(self, arity: usize) -> UniformDescriptor {
        UniformDescriptor { arity, ..self }
    }
}