| 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_H |
| 11 | #define MOJOSHADER_PROFILE_H |
| 12 | |
| 13 | #include "../mojoshader_internal.h" |
| 14 | |
| 15 | #if SUPPORT_PROFILE_SPIRV |
| 16 | #include "mojoshader_profile_spirv.h" |
| 17 | #endif |
| 18 | |
| 19 | typedef struct ConstantsList |
| 20 | { |
| 21 | MOJOSHADER_constant constant; |
| 22 | struct ConstantsList *next; |
| 23 | } ConstantsList; |
| 24 | |
| 25 | typedef struct VariableList |
| 26 | { |
| 27 | MOJOSHADER_uniformType type; |
| 28 | int index; |
| 29 | int count; |
| 30 | ConstantsList *constant; |
| 31 | int used; |
| 32 | int emit_position; // used in some profiles. |
| 33 | struct VariableList *next; |
| 34 | } VariableList; |
| 35 | |
| 36 | typedef struct RegisterList |
| 37 | { |
| 38 | RegisterType regtype; |
| 39 | int regnum; |
| 40 | MOJOSHADER_usage usage; |
| 41 | unsigned int index; |
| 42 | int writemask; |
| 43 | int misc; |
| 44 | int written; |
| 45 | #if SUPPORT_PROFILE_SPIRV |
| 46 | struct { |
| 47 | uint32 iddecl; |
| 48 | int is_ssa; // FIXME(krolli): Is there an existing way to tell constants and uniforms apart? |
| 49 | } spirv; |
| 50 | #endif |
| 51 | const VariableList *array; |
| 52 | struct RegisterList *next; |
| 53 | } RegisterList; |
| 54 | |
| 55 | typedef struct |
| 56 | { |
| 57 | const uint32 *token; // this is the unmolested token in the stream. |
| 58 | int regnum; |
| 59 | int swizzle; // xyzw (all four, not split out). |
| 60 | int swizzle_x; |
| 61 | int swizzle_y; |
| 62 | int swizzle_z; |
| 63 | int swizzle_w; |
| 64 | SourceMod src_mod; |
| 65 | RegisterType regtype; |
| 66 | int relative; |
| 67 | RegisterType relative_regtype; |
| 68 | int relative_regnum; |
| 69 | int relative_component; |
| 70 | const VariableList *relative_array; |
| 71 | } SourceArgInfo; |
| 72 | |
| 73 | struct Profile; // predeclare. |
| 74 | |
| 75 | typedef struct CtabData |
| 76 | { |
| 77 | int have_ctab; |
| 78 | int symbol_count; |
| 79 | MOJOSHADER_symbol *symbols; |
| 80 | } CtabData; |
| 81 | |
| 82 | // Context...this is state that changes as we parse through a shader... |
| 83 | typedef struct Context |
| 84 | { |
| 85 | int isfail; |
| 86 | int out_of_memory; |
| 87 | MOJOSHADER_malloc malloc; |
| 88 | MOJOSHADER_free free; |
| 89 | void *malloc_data; |
| 90 | int current_position; |
| 91 | const uint32 *orig_tokens; |
| 92 | const uint32 *tokens; |
| 93 | uint32 tokencount; |
| 94 | int know_shader_size; |
| 95 | const MOJOSHADER_swizzle *swizzles; |
| 96 | unsigned int swizzles_count; |
| 97 | const MOJOSHADER_samplerMap *samplermap; |
| 98 | unsigned int samplermap_count; |
| 99 | Buffer *output; |
| 100 | Buffer *preflight; |
| 101 | Buffer *globals; |
| 102 | Buffer *inputs; |
| 103 | Buffer *outputs; |
| 104 | Buffer *helpers; |
| 105 | Buffer *subroutines; |
| 106 | Buffer *mainline_intro; |
| 107 | Buffer *mainline_arguments; |
| 108 | Buffer *mainline_top; |
| 109 | Buffer *mainline; |
| 110 | Buffer *postflight; |
| 111 | Buffer *ignore; |
| 112 | Buffer *output_stack[3]; |
| 113 | int indent_stack[3]; |
| 114 | int output_stack_len; |
| 115 | int indent; |
| 116 | const char *shader_type_str; |
| 117 | const char *endline; |
| 118 | const char *mainfn; |
| 119 | int endline_len; |
| 120 | int profileid; |
| 121 | const struct Profile *profile; |
| 122 | MOJOSHADER_shaderType shader_type; |
| 123 | uint8 major_ver; |
| 124 | uint8 minor_ver; |
| 125 | DestArgInfo dest_arg; |
| 126 | SourceArgInfo source_args[5]; |
| 127 | SourceArgInfo predicate_arg; // for predicated instructions. |
| 128 | uint32 dwords[4]; |
| 129 | uint32 version_token; |
| 130 | int instruction_count; |
| 131 | uint32 instruction_controls; |
| 132 | uint32 previous_opcode; |
| 133 | int coissue; |
| 134 | int loops; |
| 135 | int reps; |
| 136 | int max_reps; |
| 137 | int cmps; |
| 138 | int scratch_registers; |
| 139 | int max_scratch_registers; |
| 140 | int branch_labels_stack_index; |
| 141 | int branch_labels_stack[32]; |
| 142 | int assigned_branch_labels; |
| 143 | int assigned_vertex_attributes; |
| 144 | int last_address_reg_component; |
| 145 | RegisterList used_registers; |
| 146 | RegisterList defined_registers; |
| 147 | ErrorList *errors; |
| 148 | int constant_count; |
| 149 | ConstantsList *constants; |
| 150 | int uniform_count; |
| 151 | int uniform_float4_count; |
| 152 | int uniform_int4_count; |
| 153 | int uniform_bool_count; |
| 154 | RegisterList uniforms; |
| 155 | int attribute_count; |
| 156 | RegisterList attributes; |
| 157 | int sampler_count; |
| 158 | RegisterList samplers; |
| 159 | VariableList *variables; // variables to register mapping. |
| 160 | int centroid_allowed; |
| 161 | CtabData ctab; |
| 162 | int have_relative_input_registers; |
| 163 | int have_multi_color_outputs; |
| 164 | int determined_constants_arrays; |
| 165 | int predicated; |
| 166 | int uses_pointsize; |
| 167 | int uses_fog; |
| 168 | int need_max_float; |
| 169 | |
| 170 | // !!! FIXME: move these into SUPPORT_PROFILE sections. |
| 171 | int glsl_generated_lit_helper; |
| 172 | int glsl_generated_texlod_setup; |
| 173 | int glsl_generated_texm3x3spec_helper; |
| 174 | int arb1_wrote_position; |
| 175 | // !!! FIXME: move these into SUPPORT_PROFILE sections. |
| 176 | |
| 177 | int have_preshader; |
| 178 | int ignores_ctab; |
| 179 | int reset_texmpad; |
| 180 | int texm3x2pad_dst0; |
| 181 | int texm3x2pad_src0; |
| 182 | int texm3x3pad_dst0; |
| 183 | int texm3x3pad_src0; |
| 184 | int texm3x3pad_dst1; |
| 185 | int texm3x3pad_src1; |
| 186 | MOJOSHADER_preshader *preshader; |
| 187 | |
| 188 | #if SUPPORT_PROFILE_ARB1_NV |
| 189 | int profile_supports_nv2; |
| 190 | int profile_supports_nv3; |
| 191 | int profile_supports_nv4; |
| 192 | #endif |
| 193 | #if SUPPORT_PROFILE_GLSL120 |
| 194 | int profile_supports_glsl120; |
| 195 | #endif |
| 196 | #if SUPPORT_PROFILE_GLSLES |
| 197 | int profile_supports_glsles; |
| 198 | #endif |
| 199 | #if SUPPORT_PROFILE_GLSLES3 |
| 200 | int profile_supports_glsles3; |
| 201 | #endif |
| 202 | |
| 203 | #if SUPPORT_PROFILE_METAL |
| 204 | int metal_need_header_common; |
| 205 | int metal_need_header_math; |
| 206 | int metal_need_header_relational; |
| 207 | int metal_need_header_geometric; |
| 208 | int metal_need_header_graphics; |
| 209 | int metal_need_header_texture; |
| 210 | #endif |
| 211 | |
| 212 | #if SUPPORT_PROFILE_SPIRV |
| 213 | int branch_labels_patch_stack[32]; |
| 214 | SpirvContext spirv; |
| 215 | #endif |
| 216 | #if SUPPORT_PROFILE_GLSPIRV |
| 217 | int profile_supports_glspirv; |
| 218 | #endif |
| 219 | |
| 220 | #if SUPPORT_PROFILE_HLSL |
| 221 | char hlsl_outpos_name[16]; |
| 222 | #endif |
| 223 | } Context; |
| 224 | |
| 225 | // Use these macros so we can remove all bits of these profiles from the build. |
| 226 | #if SUPPORT_PROFILE_ARB1_NV |
| 227 | #define support_nv2(ctx) ((ctx)->profile_supports_nv2) |
| 228 | #define support_nv3(ctx) ((ctx)->profile_supports_nv3) |
| 229 | #define support_nv4(ctx) ((ctx)->profile_supports_nv4) |
| 230 | #else |
| 231 | #define support_nv2(ctx) (0) |
| 232 | #define support_nv3(ctx) (0) |
| 233 | #define support_nv4(ctx) (0) |
| 234 | #endif |
| 235 | |
| 236 | #if SUPPORT_PROFILE_GLSL120 |
| 237 | #define support_glsl120(ctx) ((ctx)->profile_supports_glsl120) |
| 238 | #else |
| 239 | #define support_glsl120(ctx) (0) |
| 240 | #endif |
| 241 | |
| 242 | #if SUPPORT_PROFILE_GLSLES3 |
| 243 | #define support_glsles3(ctx) ((ctx)->profile_supports_glsles3) |
| 244 | #else |
| 245 | #define support_glsles3(ctx) (0) |
| 246 | #endif |
| 247 | |
| 248 | #if SUPPORT_PROFILE_GLSLES |
| 249 | #define support_glsles(ctx) ((ctx)->profile_supports_glsles || support_glsles3(ctx)) |
| 250 | #else |
| 251 | #define support_glsles(ctx) (0) |
| 252 | #endif |
| 253 | |
| 254 | // Profile entry points... |
| 255 | |
| 256 | // one emit function for each opcode in each profile. |
| 257 | typedef void (*emit_function)(Context *ctx); |
| 258 | |
| 259 | // one emit function for starting output in each profile. |
| 260 | typedef void (*emit_start)(Context *ctx, const char *profilestr); |
| 261 | |
| 262 | // one emit function for ending output in each profile. |
| 263 | typedef void (*emit_end)(Context *ctx); |
| 264 | |
| 265 | // one emit function for phase opcode output in each profile. |
| 266 | typedef void (*emit_phase)(Context *ctx); |
| 267 | |
| 268 | // one emit function for finalizing output in each profile. |
| 269 | typedef void (*emit_finalize)(Context *ctx); |
| 270 | |
| 271 | // one emit function for global definitions in each profile. |
| 272 | typedef void (*emit_global)(Context *ctx, RegisterType regtype, int regnum); |
| 273 | |
| 274 | // one emit function for relative uniform arrays in each profile. |
| 275 | typedef void (*emit_array)(Context *ctx, VariableList *var); |
| 276 | |
| 277 | // one emit function for relative constants arrays in each profile. |
| 278 | typedef void (*emit_const_array)(Context *ctx, |
| 279 | const struct ConstantsList *constslist, |
| 280 | int base, int size); |
| 281 | |
| 282 | // one emit function for uniforms in each profile. |
| 283 | typedef void (*emit_uniform)(Context *ctx, RegisterType regtype, int regnum, |
| 284 | const VariableList *var); |
| 285 | |
| 286 | // one emit function for samplers in each profile. |
| 287 | typedef void (*emit_sampler)(Context *ctx, int stage, TextureType ttype, |
| 288 | int texbem); |
| 289 | |
| 290 | // one emit function for attributes in each profile. |
| 291 | typedef void (*emit_attribute)(Context *ctx, RegisterType regtype, int regnum, |
| 292 | MOJOSHADER_usage usage, int index, int wmask, |
| 293 | int flags); |
| 294 | |
| 295 | // one args function for each possible sequence of opcode arguments. |
| 296 | typedef int (*args_function)(Context *ctx); |
| 297 | |
| 298 | // one state function for each opcode where we have state machine updates. |
| 299 | typedef void (*state_function)(Context *ctx); |
| 300 | |
| 301 | // one function for varnames in each profile. |
| 302 | typedef const char *(*varname_function)(Context *c, RegisterType t, int num); |
| 303 | |
| 304 | // one function for const var array in each profile. |
| 305 | typedef const char *(*const_array_varname_function)(Context *c, int base, int size); |
| 306 | |
| 307 | typedef struct Profile |
| 308 | { |
| 309 | const char *name; |
| 310 | emit_start start_emitter; |
| 311 | emit_end end_emitter; |
| 312 | emit_phase phase_emitter; |
| 313 | emit_global global_emitter; |
| 314 | emit_array array_emitter; |
| 315 | emit_const_array const_array_emitter; |
| 316 | emit_uniform uniform_emitter; |
| 317 | emit_sampler sampler_emitter; |
| 318 | emit_attribute attribute_emitter; |
| 319 | emit_finalize finalize_emitter; |
| 320 | varname_function get_varname; |
| 321 | const_array_varname_function get_const_array_varname; |
| 322 | } Profile; |
| 323 | |
| 324 | // Common utilities... |
| 325 | |
| 326 | void out_of_memory(Context *ctx); |
| 327 | void *Malloc(Context *ctx, const size_t len); |
| 328 | char *StrDup(Context *ctx, const char *str); |
| 329 | void Free(Context *ctx, void *ptr); |
| 330 | void * MOJOSHADERCALL MallocBridge(int bytes, void *data); |
| 331 | void MOJOSHADERCALL FreeBridge(void *ptr, void *data); |
| 332 | |
| 333 | int set_output(Context *ctx, Buffer **section); |
| 334 | void push_output(Context *ctx, Buffer **section); |
| 335 | void pop_output(Context *ctx); |
| 336 | |
| 337 | uint32 ver_ui32(const uint8 major, const uint8 minor); |
| 338 | int shader_version_supported(const uint8 maj, const uint8 min); |
| 339 | int shader_version_atleast(const Context *ctx, const uint8 maj, |
| 340 | const uint8 min); |
| 341 | int shader_version_exactly(const Context *ctx, const uint8 maj, |
| 342 | const uint8 min); |
| 343 | int shader_is_pixel(const Context *ctx); |
| 344 | int shader_is_vertex(const Context *ctx); |
| 345 | |
| 346 | int isfail(const Context *ctx); |
| 347 | void failf(Context *ctx, const char *fmt, ...); |
| 348 | void fail(Context *ctx, const char *reason); |
| 349 | |
| 350 | void output_line(Context *ctx, const char *fmt, ...); |
| 351 | void output_blank_line(Context *ctx); |
| 352 | |
| 353 | void floatstr(Context *ctx, char *buf, size_t bufsize, float f, |
| 354 | int leavedecimal); |
| 355 | |
| 356 | RegisterList *reglist_insert(Context *ctx, RegisterList *prev, |
| 357 | const RegisterType regtype, |
| 358 | const int regnum); |
| 359 | RegisterList *reglist_find(const RegisterList *prev, |
| 360 | const RegisterType rtype, |
| 361 | const int regnum); |
| 362 | RegisterList *set_used_register(Context *ctx, |
| 363 | const RegisterType regtype, |
| 364 | const int regnum, |
| 365 | const int written); |
| 366 | void set_defined_register(Context *ctx, const RegisterType rtype, |
| 367 | const int regnum); |
| 368 | |
| 369 | int writemask_xyzw(const int writemask); |
| 370 | int writemask_xyz(const int writemask); |
| 371 | int writemask_xy(const int writemask); |
| 372 | int writemask_x(const int writemask); |
| 373 | int writemask_y(const int writemask); |
| 374 | int replicate_swizzle(const int swizzle); |
| 375 | int no_swizzle(const int swizzle); |
| 376 | int vecsize_from_writemask(const int m); |
| 377 | void set_dstarg_writemask(DestArgInfo *dst, const int mask); |
| 378 | |
| 379 | int isscalar(Context *ctx, const MOJOSHADER_shaderType shader_type, |
| 380 | const RegisterType rtype, const int rnum); |
| 381 | |
| 382 | static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' }; |
| 383 | |
| 384 | const char *get_D3D_register_string(Context *ctx, |
| 385 | RegisterType regtype, |
| 386 | int regnum, char *regnum_str, |
| 387 | size_t regnum_size); |
| 388 | |
| 389 | // !!! FIXME: These should stay in the mojoshader_profile_d3d file |
| 390 | // !!! FIXME: but ARB1 relies on them, so we have to move them here. |
| 391 | // !!! FIXME: If/when we kill off ARB1, we can move these back. |
| 392 | const char *get_D3D_varname_in_buf(Context *ctx, RegisterType rt, |
| 393 | int regnum, char *buf, |
| 394 | const size_t len); |
| 395 | const char *get_D3D_varname(Context *ctx, RegisterType rt, int regnum); |
| 396 | |
| 397 | #endif |