ui

source of pkg::alloy::gpu::shader::types

1// The shader compiler's types: interned, each with its SPIR-V id once2// declared, and laid out in memory by WGSL's rules for the buffers that3// hold them.45import std::string;6import std::vector;7import std::option;89import spirv;1011pub type Kind = enum {12    Void,13    Bool,14    I32,15    U32,16    F32,17    Vector, // `element` a scalar, `count` 2 to 418    Matrix, // `element` a column vector, `count` the columns19    Array, // `element`, `count` the length20    RuntimeArray, // `element`21    Struct, // `count` its index in `Types.structs`22    Texture, // a 2D texture of f32 samples23    Sampler,24    SampledImage,25};2627pub type Ty = struct {28    pub kind: Kind,29    pub element: u32,30    pub count: u32,31    pub id: u32, // its SPIR-V id, 0 until declared32};3334pub type Member = struct {35    pub name: String,36    pub ty: u32,37    pub offset: u32,38    pub location: Option<u32> = ::None,39    pub builtin: Option<BuiltIn> = ::None,40};4142pub type StructInfo = struct {43    pub name: String,44    pub first: u32, // its first in `Types.members`45    pub count: u32,46    pub size: u32,47    pub alignment: u32,48};4950pub type Types = struct {51    pub list: Vector<Ty>,52    pub members: Vector<Member>,53    pub structs: Vector<StructInfo>,54};5556pub const VOID: u32 = 0;57pub const BOOL: u32 = 1;58pub const I32: u32 = 2;59pub const U32: u32 = 3;60pub const F32: u32 = 4;6162pub fn Types::create() -> Types {63    var t = Types {64        .list = Vector<Ty>::empty(),65        .members = Vector<Member>::empty(),66        .structs = Vector<StructInfo>::empty()67    };68    t.intern(Kind::Void, 0, 0);69    t.intern(Kind::Bool, 0, 0);70    t.intern(Kind::I32, 0, 0);71    t.intern(Kind::U32, 0, 0);72    t.intern(Kind::F32, 0, 0);73    return move t;74}7576pub fn intern(self t: &var Types, kind: Kind, element: u32, count: u32) -> u32 {77    for ([..t.list.length()]) |k| {78        const ty = &t.list.get(k);79        if (ty.kind == kind && ty.element == element && ty.count == count) {80            return k to u32;81        }82    }83    t.list.push(Ty { .kind = kind, .element = element, .count = count, .id = 0 });84    return t.list.length() to u32 - 1;85}8687pub fn vector(self t: &var Types, element: u32, count: u32) -> u32 {88    return t.intern(Kind::Vector, element, count);89}9091pub fn get(self t: &Types, ty: u32) -> Ty {92    return t.list.get(ty to u64);93}9495pub fn kind(self t: &Types, ty: u32) -> Kind {96    return t.list.get(ty to u64).kind;97}9899// The scalar of a scalar or vector type, or the type itself.100pub fn scalar(self t: &Types, ty: u32) -> u32 {101    const info = t.get(ty);102    return if (info.kind == Kind::Vector) info.element else ty;103}104105// How many components: 1 for a scalar, the count for a vector.106pub fn width(self t: &Types, ty: u32) -> u32 {107    const info = t.get(ty);108    return if (info.kind == Kind::Vector) info.count else 1;109}110111pub fn is_numeric_scalar(self t: &Types, ty: u32) -> bool {112    return ty == I32 || ty == U32 || ty == F32;113}114115// The type with the scalar replaced: `vec3<f32>` for `vec3<u32>` and f32.116pub fn with_scalar(self t: &var Types, ty: u32, scalar: u32) -> u32 {117    const info = t.get(ty);118    if (info.kind == Kind::Vector) {119        return t.vector(scalar, info.count);120    }121    return scalar;122}123124pub fn struct_member(self t: &Types, ty: u32, k: u32) -> &Member {125    const info = &t.structs.get(t.get(ty).count to u64);126    return &t.members.get((info.first + k) to u64);127}128129pub fn member_count(self t: &Types, ty: u32) -> u32 {130    return t.structs.get(t.get(ty).count to u64).count;131}132133// WGSL's text for the type, for messages.134pub fn name_of(self t: &Types, ty: u32) -> String {135    const info = t.get(ty);136    return match (info.kind) {137        ::Void String::from(&"void");138        ::Bool String::from(&"bool");139        ::I32 String::from(&"i32");140        ::U32 String::from(&"u32");141        ::F32 String::from(&"f32");142        ::Vector f"vec{info.count}<{t.name_of(info.element)}>";143        ::Matrix f"mat{info.count}x{t.get(info.element).count}<f32>";144        ::Array f"array<{t.name_of(info.element)}, {info.count}>";145        ::RuntimeArray f"array<{t.name_of(info.element)}>";146        ::Struct copy t.structs.get(info.count to u64).name;147        ::Texture String::from(&"texture_2d<f32>");148        ::Sampler, ::SampledImage String::from(&"sampler");149    };150}151152/* --- Layout --- */153154// The type's alignment in buffers, by WGSL's rules.155pub fn alignment(self t: &Types, ty: u32) -> u32 {156    const info = t.get(ty);157    return match (info.kind) {158        ::Vector if (info.count == 2) 8 else 16;159        ::Matrix, ::Array, ::RuntimeArray t.alignment(info.element);160        ::Struct t.structs.get(info.count to u64).alignment;161        else 4;162    };163}164165// The type's size in buffers, by WGSL's rules.166pub fn size(self t: &Types, ty: u32) -> u32 {167    const info = t.get(ty);168    return match (info.kind) {169        ::Vector info.count * 4;170        ::Matrix, ::Array info.count * t.stride(info.element);171        ::Struct t.structs.get(info.count to u64).size;172        else 4;173    };174}175176// The distance between consecutive elements of the type in an array.177pub fn stride(self t: &Types, ty: u32) -> u32 {178    return round_up(t.alignment(ty), t.size(ty));179}180181pub fn round_up(alignment: u32, value: u32) -> u32 {182    return (value + alignment - 1) / alignment * alignment;183}