| 1 | /** |
| 2 | * MojoShader; generate shader programs from bytecode of compiled |
| 3 | * Direct3D shaders. |
| 4 | * |
| 5 | * Please see the file LICENSE.txt in the source's root directory. |
| 6 | * |
| 7 | * This file written by Ryan C. Gordon. |
| 8 | */ |
| 9 | |
| 10 | #ifndef MOJOSHADER_PROFILE_SPIRV_H |
| 11 | #define MOJOSHADER_PROFILE_SPIRV_H |
| 12 | |
| 13 | #if SUPPORT_PROFILE_SPIRV |
| 14 | |
| 15 | #define MOJOSHADER_SPIRV_VS_SAMPLER_SET 0 |
| 16 | #define MOJOSHADER_SPIRV_VS_UNIFORM_SET 1 |
| 17 | #define MOJOSHADER_SPIRV_PS_SAMPLER_SET 2 |
| 18 | #define MOJOSHADER_SPIRV_PS_UNIFORM_SET 3 |
| 19 | |
| 20 | // For baked-in constants in SPIR-V we want to store scalar values that we can |
| 21 | // use in composites, since OpConstantComposite uses result ids constituates |
| 22 | // rather than value literals. |
| 23 | // We'll store these lists grouped by type and have the lists themselves |
| 24 | // ordered by value in the ctx.spirv struct. |
| 25 | typedef struct ComponentList |
| 26 | { |
| 27 | // result id from OpConstant |
| 28 | uint32 id; |
| 29 | union { |
| 30 | float f; |
| 31 | int i; |
| 32 | uint32 u; |
| 33 | } v; |
| 34 | struct ComponentList *next; |
| 35 | } ComponentList; |
| 36 | |
| 37 | typedef struct SpirvLoopInfo |
| 38 | { |
| 39 | uint32 tid_counter; |
| 40 | uint32 id_counter; |
| 41 | uint32 id_counter_next; |
| 42 | uint32 id_aL; |
| 43 | uint32 id_label_header; |
| 44 | uint32 id_label_continue; |
| 45 | uint32 id_label_merge; |
| 46 | } SpirvLoopInfo; |
| 47 | |
| 48 | typedef enum SpirvType |
| 49 | { |
| 50 | ST_FLOAT = 0, |
| 51 | ST_SINT = 1, |
| 52 | ST_UINT = 2, |
| 53 | ST_BOOL = 3, |
| 54 | } SpirvType; |
| 55 | |
| 56 | typedef enum SpirvStorageClass |
| 57 | { |
| 58 | SC_INPUT = 0, |
| 59 | SC_OUTPUT = 1, |
| 60 | SC_PRIVATE = 2, |
| 61 | SC_UNIFORM_CONSTANT = 3, |
| 62 | } SpirvStorageClass; |
| 63 | |
| 64 | /* Not all type parameter combinations are actually used, but it's all rounded up to 64 so |
| 65 | * it's easier to work with. |
| 66 | */ |
| 67 | typedef enum SpirvTypeIdx |
| 68 | { |
| 69 | STI_VOID = 0, |
| 70 | STI_FUNC_VOID = 1, |
| 71 | STI_FUNC_LIT = 2, |
| 72 | STI_IMAGE2D = 3, |
| 73 | STI_IMAGE3D = 4, |
| 74 | STI_IMAGECUBE = 5, |
| 75 | STI_PTR_IMAGE2D = 6, |
| 76 | STI_PTR_IMAGE3D = 7, |
| 77 | STI_PTR_IMAGECUBE = 8, |
| 78 | STI_PTR_VEC2_I = 9, // special case, needed only for point coord input. |
| 79 | |
| 80 | // 6 unused entries |
| 81 | |
| 82 | // 4 base types * 4 vector sizes = 16 entries |
| 83 | STI_FLOAT = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 0, |
| 84 | STI_VEC2 = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 1, |
| 85 | STI_VEC3 = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 2, |
| 86 | STI_VEC4 = (0 << 5) | (1 << 4) | (ST_FLOAT << 2) | 3, |
| 87 | STI_INT = (0 << 5) | (1 << 4) | (ST_SINT << 2) | 0, |
| 88 | STI_IVEC2 = (0 << 5) | (1 << 4) | (ST_SINT << 2) | 1, |
| 89 | STI_IVEC3 = (0 << 5) | (1 << 4) | (ST_SINT << 2) | 2, |
| 90 | STI_IVEC4 = (0 << 5) | (1 << 4) | (ST_SINT << 2) | 3, |
| 91 | STI_UINT = (0 << 5) | (1 << 4) | (ST_UINT << 2) | 0, |
| 92 | STI_UVEC2 = (0 << 5) | (1 << 4) | (ST_UINT << 2) | 1, |
| 93 | STI_UVEC3 = (0 << 5) | (1 << 4) | (ST_UINT << 2) | 2, |
| 94 | STI_UVEC4 = (0 << 5) | (1 << 4) | (ST_UINT << 2) | 3, |
| 95 | STI_BOOL = (0 << 5) | (1 << 4) | (ST_BOOL << 2) | 0, |
| 96 | STI_BVEC2 = (0 << 5) | (1 << 4) | (ST_BOOL << 2) | 1, |
| 97 | STI_BVEC3 = (0 << 5) | (1 << 4) | (ST_BOOL << 2) | 2, |
| 98 | STI_BVEC4 = (0 << 5) | (1 << 4) | (ST_BOOL << 2) | 3, |
| 99 | |
| 100 | // 2 dims (vec4 + scalar) * 4 base types * 4 storage classes |
| 101 | STI_PTR_FLOAT_I = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_INPUT, |
| 102 | STI_PTR_FLOAT_O = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_OUTPUT, |
| 103 | STI_PTR_FLOAT_P = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_PRIVATE, |
| 104 | STI_PTR_FLOAT_U = (1 << 5) | (0 << 4) | (ST_FLOAT << 2) | SC_UNIFORM_CONSTANT, |
| 105 | STI_PTR_INT_I = (1 << 5) | (0 << 4) | (ST_SINT << 2) | SC_INPUT, |
| 106 | STI_PTR_INT_O = (1 << 5) | (0 << 4) | (ST_SINT << 2) | SC_OUTPUT, |
| 107 | STI_PTR_INT_P = (1 << 5) | (0 << 4) | (ST_SINT << 2) | SC_PRIVATE, |
| 108 | STI_PTR_INT_U = (1 << 5) | (0 << 4) | (ST_SINT << 2) | SC_UNIFORM_CONSTANT, |
| 109 | STI_PTR_UINT_I = (1 << 5) | (0 << 4) | (ST_UINT << 2) | SC_INPUT, |
| 110 | STI_PTR_UINT_O = (1 << 5) | (0 << 4) | (ST_UINT << 2) | SC_OUTPUT, |
| 111 | STI_PTR_UINT_P = (1 << 5) | (0 << 4) | (ST_UINT << 2) | SC_PRIVATE, |
| 112 | STI_PTR_UINT_U = (1 << 5) | (0 << 4) | (ST_UINT << 2) | SC_UNIFORM_CONSTANT, |
| 113 | STI_PTR_BOOL_I = (1 << 5) | (0 << 4) | (ST_BOOL << 2) | SC_INPUT, |
| 114 | STI_PTR_BOOL_O = (1 << 5) | (0 << 4) | (ST_BOOL << 2) | SC_OUTPUT, |
| 115 | STI_PTR_BOOL_P = (1 << 5) | (0 << 4) | (ST_BOOL << 2) | SC_PRIVATE, |
| 116 | STI_PTR_BOOL_U = (1 << 5) | (0 << 4) | (ST_BOOL << 2) | SC_UNIFORM_CONSTANT, |
| 117 | STI_PTR_VEC4_I = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_INPUT, |
| 118 | STI_PTR_VEC4_O = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_OUTPUT, |
| 119 | STI_PTR_VEC4_P = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_PRIVATE, |
| 120 | STI_PTR_VEC4_U = (1 << 5) | (1 << 4) | (ST_FLOAT << 2) | SC_UNIFORM_CONSTANT, |
| 121 | STI_PTR_IVEC4_I = (1 << 5) | (1 << 4) | (ST_SINT << 2) | SC_INPUT, |
| 122 | STI_PTR_IVEC4_O = (1 << 5) | (1 << 4) | (ST_SINT << 2) | SC_OUTPUT, |
| 123 | STI_PTR_IVEC4_P = (1 << 5) | (1 << 4) | (ST_SINT << 2) | SC_PRIVATE, |
| 124 | STI_PTR_IVEC4_U = (1 << 5) | (1 << 4) | (ST_SINT << 2) | SC_UNIFORM_CONSTANT, |
| 125 | STI_PTR_UVEC4_I = (1 << 5) | (1 << 4) | (ST_UINT << 2) | SC_INPUT, |
| 126 | STI_PTR_UVEC4_O = (1 << 5) | (1 << 4) | (ST_UINT << 2) | SC_OUTPUT, |
| 127 | STI_PTR_UVEC4_P = (1 << 5) | (1 << 4) | (ST_UINT << 2) | SC_PRIVATE, |
| 128 | STI_PTR_UVEC4_U = (1 << 5) | (1 << 4) | (ST_UINT << 2) | SC_UNIFORM_CONSTANT, |
| 129 | STI_PTR_BVEC4_I = (1 << 5) | (1 << 4) | (ST_BOOL << 2) | SC_INPUT, |
| 130 | STI_PTR_BVEC4_O = (1 << 5) | (1 << 4) | (ST_BOOL << 2) | SC_OUTPUT, |
| 131 | STI_PTR_BVEC4_P = (1 << 5) | (1 << 4) | (ST_BOOL << 2) | SC_PRIVATE, |
| 132 | STI_PTR_BVEC4_U = (1 << 5) | (1 << 4) | (ST_BOOL << 2) | SC_UNIFORM_CONSTANT, |
| 133 | |
| 134 | // 2 + 6 + 16 + 32 = 56 entries (+ 8 unused) |
| 135 | |
| 136 | // Helpers |
| 137 | STI_LENGTH_, |
| 138 | |
| 139 | STI_MISC_START_ = 0, |
| 140 | STI_MISC_END_ = 8, |
| 141 | STI_CORE_START_ = (0 << 5) | (1 << 4), |
| 142 | STI_PTR_START_ = (1 << 5) | (0 << 4), |
| 143 | STI_CORE_END_ = STI_PTR_START_, |
| 144 | STI_PTR_END_ = STI_LENGTH_, |
| 145 | } SpirvTypeIdx; |
| 146 | |
| 147 | // In addition to result ID we also need type ID (can't assume everything is vec4). |
| 148 | typedef struct SpirvResult |
| 149 | { |
| 150 | uint32 tid; |
| 151 | uint32 id; |
| 152 | } SpirvResult; |
| 153 | |
| 154 | // This doesn't 100% correspond to glslangValidator semantics. It just says which mode to use at |
| 155 | // runtime (different from compile-time support being enabled). Technically, we could generate the |
| 156 | // same for both, but that would require GL code to use UBOs. |
| 157 | typedef enum SpirvMode |
| 158 | { |
| 159 | SPIRV_MODE_GL, |
| 160 | SPIRV_MODE_VK, |
| 161 | } SpirvMode; |
| 162 | |
| 163 | typedef struct SpirvContext |
| 164 | { |
| 165 | #if SUPPORT_PROFILE_GLSPIRV |
| 166 | uint32 id_vs_main_end; |
| 167 | #endif // SUPPORT_PROFILE_GLSPIRV |
| 168 | SpirvMode mode; |
| 169 | uint32 hasdepth; |
| 170 | // ext. glsl instructions have been imported |
| 171 | uint32 idext; |
| 172 | uint32 idmax; |
| 173 | uint32 idmain; |
| 174 | uint32 id_func_lit; |
| 175 | uint32 inoutcount; |
| 176 | uint32 id_var_fragcoord; |
| 177 | uint32 id_var_vpos; |
| 178 | uint32 id_var_frontfacing; |
| 179 | uint32 id_var_vface; |
| 180 | uint32 id_var_texcoord0_input; |
| 181 | uint32 id_var_texcoord0_private; |
| 182 | // ids for types so we can reuse them after they're declared |
| 183 | uint32 tid[STI_LENGTH_]; |
| 184 | uint32 idtrue; |
| 185 | uint32 idfalse; |
| 186 | uint32 id_0_0[4]; |
| 187 | uint32 id_0_125[4]; |
| 188 | uint32 id_0_25[4]; |
| 189 | uint32 id_0_5[4]; |
| 190 | uint32 id_1_0[4]; |
| 191 | uint32 id_2_0[4]; |
| 192 | uint32 id_4_0[4]; |
| 193 | uint32 id_8_0[4]; |
| 194 | uint32 id_flt_max[4]; |
| 195 | struct { |
| 196 | uint32 idvec4; |
| 197 | uint32 idivec4; |
| 198 | uint32 idbool; |
| 199 | } uniform_arrays; |
| 200 | uint32 id_uniform_block; |
| 201 | struct { |
| 202 | uint32 idvec4; |
| 203 | } constant_arrays; |
| 204 | struct { |
| 205 | ComponentList f; |
| 206 | ComponentList i; |
| 207 | ComponentList u; |
| 208 | } cl; |
| 209 | |
| 210 | SpirvPatchTable patch_table; |
| 211 | |
| 212 | // Required only on ps_1_3 and below, which only has 4 registers for this purpose. |
| 213 | struct { |
| 214 | uint32 idtexbem; |
| 215 | uint32 idtexbeml; |
| 216 | } sampler_extras[4]; |
| 217 | |
| 218 | // TEX opcode in ps_1_3 and below has one implicit texcoord input attribute for each texture |
| 219 | // register. We use this array to hold SSA id of this input attribute (see emit_SPIRV_global |
| 220 | // for details). |
| 221 | uint32 id_implicit_input[4]; |
| 222 | |
| 223 | int loop_stack_idx; |
| 224 | SpirvLoopInfo loop_stack[32]; |
| 225 | } SpirvContext; |
| 226 | |
| 227 | #endif // if SUPPORT_PROFILE_SPIRV |
| 228 | |
| 229 | #endif |