1// A compiled shader as the program carries it: bytes `#shader` makes2// while compiling, read back when the program creates the shader. Words3// of 4 bytes, little-endian:4//5// "ALSH", the format's version (3)6// the entry points: their count, then each one's stage, workgroup7// size (3 words), name length in bytes and name, padded to words,8// and the resources it uses: their count, then their indices9// the resources: their count, then each one's group, binding, kind10// and size11// the SPIR-V module: its word count, then its words12// the WGSL source, for the browser: its length in bytes, then the13// bytes, padded to words14// the MSL source, for Metal (shader/msl.alloy), the same way1516import std::string;17import std::vector;18import std::option;1920import compiler;2122pub const BLOB_MAGIC: u32 = 0x48534C41;2324pub fn encode(compiled: &Compiled, wgsl: &[u8], msl: &[u8]) -> Vector<u8> {25 var words = Vector<u32>::from(&[BLOB_MAGIC, 3, compiled.entries.length() to u32]);26 for (compiled.entries.view()) |&e| {27 words.push(e.stage to u32);28 words.append(&e.workgroup);29 put_text(&var words, &e.name.view());30 words.push(e.resources.length() to u32);31 words.append(&e.resources.view());32 }33 words.push(compiled.resources.length() to u32);34 for (compiled.resources.view()) |&r| {35 words.append(&[r.group, r.binding, r.kind to u32, r.size]);36 }37 words.push(compiled.words.length() to u32);38 words.append(&compiled.words.view());39 put_text(&var words, &wgsl);40 put_text(&var words, &msl);41 var bytes = Vector<u8>::empty();42 for (words.view()) |w| {43 bytes.push((w & 0xFF) to u8);44 bytes.push(((w >> 8) & 0xFF) to u8);45 bytes.push(((w >> 16) & 0xFF) to u8);46 bytes.push((w >> 24) to u8);47 }48 return move bytes;49}5051fn put_text(words: &var Vector<u32>, text: &[u8]) {52 words.push(text.length() to u32);53 var word: u32 = 0;54 for ([..text.length()]) |k| {55 word |= text[k] to u32 << ((k % 4) * 8) to u32;56 if (k % 4 == 3) {57 words.push(word);58 word = 0;59 }60 }61 if (text.length() % 4 != 0) {62 words.push(word);63 }64}6566// A shader read back from its bytes.67pub type Decoded = struct {68 pub entries: Vector<EntryPoint> = Vector<EntryPoint>::empty(),69 pub resources: Vector<ShaderResource> = Vector<ShaderResource>::empty(),70 pub spirv: Vector<u32> = Vector<u32>::empty(),71 pub wgsl: String = String::empty(),72 pub msl: String = String::empty(),73};7475pub fn decode(bytes: &[u8]) -> Option<Decoded> {76 if (bytes.length() % 4 != 0 || bytes.length() < 8) {77 return ::None;78 }79 var words = Vector<u32>::empty();80 for ([..bytes.length() / 4]) |k| {81 const at = k * 4;82 words.push(83 bytes[at] to u32 | (84 bytes[at + 1] to u32 << 885 ) | (bytes[at + 2] to u32 << 16) | (bytes[at + 3] to u32 << 24)86 );87 }88 const w = &words.view();89 if (w[0] != BLOB_MAGIC || w[1] != 3) {90 return ::None;91 }92 var out = Decoded {};93 var at: u64 = 2;94 const entry_count = w[at];95 at += 1;96 for ([..entry_count to u64]) |k| {97 const stage = if (Stage::from(w[at]) is ::Some |s|) s else {98 return ::None;99 };100 const workgroup: [u32 : 3] = [w[at + 1], w[at + 2], w[at + 3]];101 at += 4;102 var name = take_text(&w, &var at);103 const used = w[at] to u64;104 var resources = Vector<u32>::from(&w[at + 1..at + 1 + used]);105 at += 1 + used;106 out.entries.push(EntryPoint {107 .name = move name,108 .stage = stage,109 .workgroup = workgroup,110 .resources = move resources111 });112 }113 const resource_count = w[at];114 at += 1;115 for ([..resource_count to u64]) |k| {116 const kind = if (ResourceKind::from(w[at + 2]) is ::Some |r|) r else {117 return ::None;118 };119 out.resources.push(ShaderResource {120 .group = w[at],121 .binding = w[at + 1],122 .kind = kind,123 .size = w[at + 3]124 });125 at += 4;126 }127 const spirv_count = w[at] to u64;128 at += 1;129 out.spirv.append(&w[at..at + spirv_count]);130 at += spirv_count;131 out.wgsl = take_text(&w, &var at);132 out.msl = take_text(&w, &var at);133 return ::Some(move out);134}135136fn take_text(w: &[u32], at: &var u64) -> String {137 const length = w[at] to u64;138 at += 1;139 var text = String::empty();140 for ([..length]) |k| {141 text.push(((w[at + k / 4] >> ((k % 4) * 8) to u32) & 0xFF) to u8);142 }143 at += (length + 3) / 4;144 return move text;145}