1// SPIR-V's numbers, the ones the shader compiler emits and the CPU2// device's interpreter and the MSL writer read, and the instruction3// words: an instruction is its word count in the top half of its first4// word, its opcode in the bottom half, then its operands.56import std::vector;7import std::option;8import std::string;910pub const MAGIC: u32 = 0x07230203;11pub const VERSION_1_3: u32 = 0x00010300;1213// The instructions the compiler writes.14pub type Opcode = enum : u16 {15 Nop = 0,16 Name = 5,17 MemberName = 6,18 ExtInstImport = 11,19 ExtInst = 12,20 MemoryModel = 14,21 EntryPoint = 15,22 ExecutionMode = 16,23 Capability = 17,24 TypeVoid = 19,25 TypeBool = 20,26 TypeInt = 21,27 TypeFloat = 22,28 TypeVector = 23,29 TypeMatrix = 24,30 TypeImage = 25,31 TypeSampler = 26,32 TypeSampledImage = 27,33 TypeArray = 28,34 TypeRuntimeArray = 29,35 TypeStruct = 30,36 TypePointer = 32,37 TypeFunction = 33,38 ConstantTrue = 41,39 ConstantFalse = 42,40 Constant = 43,41 ConstantComposite = 44,42 ConstantNull = 46,43 Function = 54,44 FunctionParameter = 55,45 FunctionEnd = 56,46 FunctionCall = 57,47 Variable = 59,48 Load = 61,49 Store = 62,50 AccessChain = 65,51 Decorate = 71,52 MemberDecorate = 72,53 VectorShuffle = 79,54 CompositeConstruct = 80,55 CompositeExtract = 81,56 CompositeInsert = 82,57 SampledImage = 86,58 ImageSampleImplicitLod = 87,59 ImageSampleExplicitLod = 88,60 ConvertFToU = 109,61 ConvertFToS = 110,62 ConvertSToF = 111,63 ConvertUToF = 112,64 Bitcast = 124,65 SNegate = 126,66 FNegate = 127,67 IAdd = 128,68 FAdd = 129,69 ISub = 130,70 FSub = 131,71 IMul = 132,72 FMul = 133,73 UDiv = 134,74 SDiv = 135,75 FDiv = 136,76 UMod = 137,77 SRem = 138,78 FRem = 140,79 VectorTimesScalar = 142,80 MatrixTimesScalar = 143,81 VectorTimesMatrix = 144,82 MatrixTimesVector = 145,83 MatrixTimesMatrix = 146,84 Dot = 148,85 Any = 154,86 All = 155,87 LogicalEqual = 164,88 LogicalNotEqual = 165,89 LogicalOr = 166,90 LogicalAnd = 167,91 LogicalNot = 168,92 Select = 169,93 IEqual = 170,94 INotEqual = 171,95 UGreaterThan = 172,96 SGreaterThan = 173,97 UGreaterThanEqual = 174,98 SGreaterThanEqual = 175,99 ULessThan = 176,100 SLessThan = 177,101 ULessThanEqual = 178,102 SLessThanEqual = 179,103 FOrdEqual = 180,104 FOrdNotEqual = 182,105 FOrdLessThan = 184,106 FOrdGreaterThan = 186,107 FOrdLessThanEqual = 188,108 FOrdGreaterThanEqual = 190,109 ShiftRightLogical = 194,110 ShiftRightArithmetic = 195,111 ShiftLeftLogical = 196,112 BitwiseOr = 197,113 BitwiseXor = 198,114 BitwiseAnd = 199,115 Not = 200,116 DPdx = 207,117 DPdy = 208,118 Fwidth = 209,119 LoopMerge = 246,120 SelectionMerge = 247,121 Label = 248,122 Branch = 249,123 BranchConditional = 250,124 Kill = 252,125 Return = 253,126 ReturnValue = 254,127};128129// GLSL.std.450's instructions, the operand of an ExtInst.130pub type Glsl = enum : u32 {131 RoundEven = 2,132 Trunc = 3,133 FAbs = 4,134 SAbs = 5,135 FSign = 6,136 Floor = 8,137 Ceil = 9,138 Fract = 10,139 Sin = 13,140 Cos = 14,141 Tan = 15,142 Asin = 16,143 Acos = 17,144 Atan = 18,145 Atan2 = 25,146 Pow = 26,147 Exp = 27,148 Log = 28,149 Exp2 = 29,150 Log2 = 30,151 Sqrt = 31,152 InverseSqrt = 32,153 FMin = 37,154 UMin = 38,155 SMin = 39,156 FMax = 40,157 UMax = 41,158 SMax = 42,159 FClamp = 43,160 UClamp = 44,161 SClamp = 45,162 FMix = 46,163 Step = 48,164 SmoothStep = 49,165 Fma = 50,166 Length = 66,167 Distance = 67,168 Cross = 68,169 Normalize = 69,170 Reflect = 71,171};172173pub type Decoration = enum : u32 {174 Block = 2,175 ColMajor = 5,176 ArrayStride = 6,177 MatrixStride = 7,178 BuiltIn = 11,179 Flat = 14,180 NonWritable = 24,181 Location = 30,182 Binding = 33,183 DescriptorSet = 34,184 Offset = 35,185};186187// Built-in variables.188pub type BuiltIn = enum : u32 {189 Position = 0,190 FragCoord = 15,191 FrontFacing = 17,192 SampleId = 18,193 FragDepth = 22,194 NumWorkgroups = 24,195 WorkgroupId = 26,196 LocalInvocationId = 27,197 GlobalInvocationId = 28,198 LocalInvocationIndex = 29,199 VertexIndex = 42,200 InstanceIndex = 43,201};202203pub type StorageClass = enum : u32 {204 UniformConstant = 0,205 Input = 1,206 Uniform = 2,207 Output = 3,208 Workgroup = 4,209 Private = 6,210 Function = 7,211 StorageBuffer = 12,212};213214// Whether a variable of the class is a resource a pipeline binds.215pub fn is_resource(self storage: &StorageClass) -> bool => match (storage) {216 ::Uniform, ::StorageBuffer, ::UniformConstant true;217 else false;218};219220pub type ExecutionMode = enum : u32 {221 OriginUpperLeft = 7,222 LocalSize = 17,223};224225pub type Capability = enum : u32 {226 Shader = 1,227};228229// An instruction of a module: what it does, and where its operands are230// in the module's words.231pub type Instruction = struct {232 pub opcode: Opcode,233 pub at: u64,234 pub end: u64,235};236237pub fn count(self i: &Instruction) -> u64 => i.end - i.at;238239// The module's instructions, after its header; None when one has no240// words or an opcode the compiler does not write.241pub fn instructions(words: &[u32]) -> Option<Vector<Instruction>> {242 var out = Vector<Instruction>::empty();243 var at: u64 = 5;244 while (at < words.length()) {245 const length = (words[at] >> 16) to u64;246 const opcode = if (Opcode::from((words[at] & 0xFFFF) to u16) is ::Some |o|) o else {247 return ::None;248 };249 if (length == 0) {250 return ::None;251 }252 out.push(Instruction { .opcode = opcode, .at = at + 1, .end = at + length });253 at += length;254 }255 return ::Some(move out);256}257258// Appends an instruction: its opcode and operands.259pub fn instruction(words: &var Vector<u32>, opcode: Opcode, operands: &[u32]) {260 words.push(((operands.length() + 1) to u32 << 16) | opcode to u32);261 words.append(&operands);262}263264// Appends a decoration of the id, with its operands.265pub fn decorate(words: &var Vector<u32>, id: u32, decoration: Decoration, operands: &[u32]) {266 words.push(((operands.length() + 3) to u32 << 16) | Opcode::Decorate to u32);267 words.append(&[id, decoration to u32]);268 words.append(&operands);269}270271// Appends a decoration of the struct's member, with its operands.272pub fn decorate_member(273 words: &var Vector<u32>,274 id: u32,275 member: u32,276 decoration: Decoration,277 operands: &[u32]278) {279 words.push(((operands.length() + 4) to u32 << 16) | Opcode::MemberDecorate to u32);280 words.append(&[id, member, decoration to u32]);281 words.append(&operands);282}283284// The text as a SPIR-V literal string: UTF-8, NUL-terminated, padded to285// whole words.286pub fn string_words(text: &[u8], words: &var Vector<u32>) {287 const length = text.length() + 1;288 var word: u32 = 0;289 for ([..length]) |k| {290 const byte: u32 = if (k < text.length()) text[k] to u32 else 0;291 word |= byte << ((k % 4) * 8) to u32;292 if (k % 4 == 3) {293 words.push(word);294 word = 0;295 }296 }297 if (length % 4 != 0) {298 words.push(word);299 }300}301302// The literal string the words start with.303pub fn word_text(words: &[u32]) -> String {304 var text = String::empty();305 for (words) |w| {306 for ([..4]) |k| {307 const byte = ((w >> (k * 8) to u32) & 0xFF) to u8;308 if (byte == 0) {309 return move text;310 }311 text.push(byte);312 }313 }314 return move text;315}