| 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_EFFECTS_H |
| 11 | #define MOJOSHADER_EFFECTS_H |
| 12 | |
| 13 | #ifdef MOJOSHADER_EFFECT_SUPPORT |
| 14 | |
| 15 | /* MOJOSHADER_effectState types... */ |
| 16 | |
| 17 | typedef enum MOJOSHADER_renderStateType |
| 18 | { |
| 19 | /* Note that we are NOT using the actual RS values from D3D here. |
| 20 | * For some reason, in the binary data, it's 0-based. |
| 21 | * Even worse, it doesn't even seem to be in order. |
| 22 | * Here is the list of changes compared to the real D3DRS enum: |
| 23 | * - All of the RS_WRAP values are in a row, not separate! |
| 24 | * |
| 25 | * -flibit |
| 26 | */ |
| 27 | MOJOSHADER_RS_ZENABLE, |
| 28 | MOJOSHADER_RS_FILLMODE, |
| 29 | MOJOSHADER_RS_SHADEMODE, |
| 30 | MOJOSHADER_RS_ZWRITEENABLE, |
| 31 | MOJOSHADER_RS_ALPHATESTENABLE, |
| 32 | MOJOSHADER_RS_LASTPIXEL, |
| 33 | MOJOSHADER_RS_SRCBLEND, |
| 34 | MOJOSHADER_RS_DESTBLEND, |
| 35 | MOJOSHADER_RS_CULLMODE, |
| 36 | MOJOSHADER_RS_ZFUNC, |
| 37 | MOJOSHADER_RS_ALPHAREF, |
| 38 | MOJOSHADER_RS_ALPHAFUNC, |
| 39 | MOJOSHADER_RS_DITHERENABLE, |
| 40 | MOJOSHADER_RS_ALPHABLENDENABLE, |
| 41 | MOJOSHADER_RS_FOGENABLE, |
| 42 | MOJOSHADER_RS_SPECULARENABLE, |
| 43 | MOJOSHADER_RS_FOGCOLOR, |
| 44 | MOJOSHADER_RS_FOGTABLEMODE, |
| 45 | MOJOSHADER_RS_FOGSTART, |
| 46 | MOJOSHADER_RS_FOGEND, |
| 47 | MOJOSHADER_RS_FOGDENSITY, |
| 48 | MOJOSHADER_RS_RANGEFOGENABLE, |
| 49 | MOJOSHADER_RS_STENCILENABLE, |
| 50 | MOJOSHADER_RS_STENCILFAIL, |
| 51 | MOJOSHADER_RS_STENCILZFAIL, |
| 52 | MOJOSHADER_RS_STENCILPASS, |
| 53 | MOJOSHADER_RS_STENCILFUNC, |
| 54 | MOJOSHADER_RS_STENCILREF, |
| 55 | MOJOSHADER_RS_STENCILMASK, |
| 56 | MOJOSHADER_RS_STENCILWRITEMASK, |
| 57 | MOJOSHADER_RS_TEXTUREFACTOR, |
| 58 | MOJOSHADER_RS_WRAP0, |
| 59 | MOJOSHADER_RS_WRAP1, |
| 60 | MOJOSHADER_RS_WRAP2, |
| 61 | MOJOSHADER_RS_WRAP3, |
| 62 | MOJOSHADER_RS_WRAP4, |
| 63 | MOJOSHADER_RS_WRAP5, |
| 64 | MOJOSHADER_RS_WRAP6, |
| 65 | MOJOSHADER_RS_WRAP7, |
| 66 | MOJOSHADER_RS_WRAP8, |
| 67 | MOJOSHADER_RS_WRAP9, |
| 68 | MOJOSHADER_RS_WRAP10, |
| 69 | MOJOSHADER_RS_WRAP11, |
| 70 | MOJOSHADER_RS_WRAP12, |
| 71 | MOJOSHADER_RS_WRAP13, |
| 72 | MOJOSHADER_RS_WRAP14, |
| 73 | MOJOSHADER_RS_WRAP15, |
| 74 | MOJOSHADER_RS_CLIPPING, |
| 75 | MOJOSHADER_RS_LIGHTING, |
| 76 | MOJOSHADER_RS_AMBIENT, |
| 77 | MOJOSHADER_RS_FOGVERTEXMODE, |
| 78 | MOJOSHADER_RS_COLORVERTEX, |
| 79 | MOJOSHADER_RS_LOCALVIEWER, |
| 80 | MOJOSHADER_RS_NORMALIZENORMALS, |
| 81 | MOJOSHADER_RS_DIFFUSEMATERIALSOURCE, |
| 82 | MOJOSHADER_RS_SPECULARMATERIALSOURCE, |
| 83 | MOJOSHADER_RS_AMBIENTMATERIALSOURCE, |
| 84 | MOJOSHADER_RS_EMISSIVEMATERIALSOURCE, |
| 85 | MOJOSHADER_RS_VERTEXBLEND, |
| 86 | MOJOSHADER_RS_CLIPPLANEENABLE, |
| 87 | MOJOSHADER_RS_POINTSIZE, |
| 88 | MOJOSHADER_RS_POINTSIZE_MIN, |
| 89 | MOJOSHADER_RS_POINTSPRITEENABLE, |
| 90 | MOJOSHADER_RS_POINTSCALEENABLE, |
| 91 | MOJOSHADER_RS_POINTSCALE_A, |
| 92 | MOJOSHADER_RS_POINTSCALE_B, |
| 93 | MOJOSHADER_RS_POINTSCALE_C, |
| 94 | MOJOSHADER_RS_MULTISAMPLEANTIALIAS, |
| 95 | MOJOSHADER_RS_MULTISAMPLEMASK, |
| 96 | MOJOSHADER_RS_PATCHEDGESTYLE, |
| 97 | MOJOSHADER_RS_DEBUGMONITORTOKEN, |
| 98 | MOJOSHADER_RS_POINTSIZE_MAX, |
| 99 | MOJOSHADER_RS_INDEXEDVERTEXBLENDENABLE, |
| 100 | MOJOSHADER_RS_COLORWRITEENABLE, |
| 101 | MOJOSHADER_RS_TWEENFACTOR, |
| 102 | MOJOSHADER_RS_BLENDOP, |
| 103 | MOJOSHADER_RS_POSITIONDEGREE, |
| 104 | MOJOSHADER_RS_NORMALDEGREE, |
| 105 | MOJOSHADER_RS_SCISSORTESTENABLE, |
| 106 | MOJOSHADER_RS_SLOPESCALEDEPTHBIAS, |
| 107 | MOJOSHADER_RS_ANTIALIASEDLINEENABLE, |
| 108 | MOJOSHADER_RS_MINTESSELLATIONLEVEL, |
| 109 | MOJOSHADER_RS_MAXTESSELLATIONLEVEL, |
| 110 | MOJOSHADER_RS_ADAPTIVETESS_X, |
| 111 | MOJOSHADER_RS_ADAPTIVETESS_Y, |
| 112 | MOJOSHADER_RS_ADAPTIVETESS_Z, |
| 113 | MOJOSHADER_RS_ADAPTIVETESS_W, |
| 114 | MOJOSHADER_RS_ENABLEADAPTIVETESSELLATION, |
| 115 | MOJOSHADER_RS_TWOSIDEDSTENCILMODE, |
| 116 | MOJOSHADER_RS_CCW_STENCILFAIL, |
| 117 | MOJOSHADER_RS_CCW_STENCILZFAIL, |
| 118 | MOJOSHADER_RS_CCW_STENCILPASS, |
| 119 | MOJOSHADER_RS_CCW_STENCILFUNC, |
| 120 | MOJOSHADER_RS_COLORWRITEENABLE1, |
| 121 | MOJOSHADER_RS_COLORWRITEENABLE2, |
| 122 | MOJOSHADER_RS_COLORWRITEENABLE3, |
| 123 | MOJOSHADER_RS_BLENDFACTOR, |
| 124 | MOJOSHADER_RS_SRGBWRITEENABLE, |
| 125 | MOJOSHADER_RS_DEPTHBIAS, |
| 126 | MOJOSHADER_RS_SEPARATEALPHABLENDENABLE, |
| 127 | MOJOSHADER_RS_SRCBLENDALPHA, |
| 128 | MOJOSHADER_RS_DESTBLENDALPHA, |
| 129 | MOJOSHADER_RS_BLENDOPALPHA, |
| 130 | |
| 131 | /* These aren't really "states", but these numbers are |
| 132 | * referred to by MOJOSHADER_effectStateType as such. |
| 133 | */ |
| 134 | MOJOSHADER_RS_VERTEXSHADER = 146, |
| 135 | MOJOSHADER_RS_PIXELSHADER = 147 |
| 136 | } MOJOSHADER_renderStateType; |
| 137 | |
| 138 | typedef enum MOJOSHADER_zBufferType |
| 139 | { |
| 140 | MOJOSHADER_ZB_FALSE, |
| 141 | MOJOSHADER_ZB_TRUE, |
| 142 | MOJOSHADER_ZB_USEW |
| 143 | } MOJOSHADER_zBufferType; |
| 144 | |
| 145 | typedef enum MOJOSHADER_fillMode |
| 146 | { |
| 147 | MOJOSHADER_FILL_POINT = 1, |
| 148 | MOJOSHADER_FILL_WIREFRAME = 2, |
| 149 | MOJOSHADER_FILL_SOLID = 3 |
| 150 | } MOJOSHADER_fillMode; |
| 151 | |
| 152 | typedef enum MOJOSHADER_shadeMode |
| 153 | { |
| 154 | MOJOSHADER_SHADE_FLAT = 1, |
| 155 | MOJOSHADER_SHADE_GOURAUD = 2, |
| 156 | MOJOSHADER_SHADE_PHONG = 3 |
| 157 | } MOJOSHADER_shadeMode; |
| 158 | |
| 159 | typedef enum MOJOSHADER_blendMode |
| 160 | { |
| 161 | MOJOSHADER_BLEND_ZERO = 1, |
| 162 | MOJOSHADER_BLEND_ONE = 2, |
| 163 | MOJOSHADER_BLEND_SRCCOLOR = 3, |
| 164 | MOJOSHADER_BLEND_INVSRCCOLOR = 4, |
| 165 | MOJOSHADER_BLEND_SRCALPHA = 5, |
| 166 | MOJOSHADER_BLEND_INVSRCALPHA = 6, |
| 167 | MOJOSHADER_BLEND_DESTALPHA = 7, |
| 168 | MOJOSHADER_BLEND_INVDESTALPHA = 8, |
| 169 | MOJOSHADER_BLEND_DESTCOLOR = 9, |
| 170 | MOJOSHADER_BLEND_INVDESTCOLOR = 10, |
| 171 | MOJOSHADER_BLEND_SRCALPHASAT = 11, |
| 172 | MOJOSHADER_BLEND_BOTHSRCALPHA = 12, |
| 173 | MOJOSHADER_BLEND_BOTHINVSRCALPHA = 13, |
| 174 | MOJOSHADER_BLEND_BLENDFACTOR = 14, |
| 175 | MOJOSHADER_BLEND_INVBLENDFACTOR = 15, |
| 176 | MOJOSHADER_BLEND_SRCCOLOR2 = 16, |
| 177 | MOJOSHADER_BLEND_INVSRCCOLOR2 = 17 |
| 178 | } MOJOSHADER_blendMode; |
| 179 | |
| 180 | typedef enum MOJOSHADER_cullMode |
| 181 | { |
| 182 | MOJOSHADER_CULL_NONE = 1, |
| 183 | MOJOSHADER_CULL_CW = 2, |
| 184 | MOJOSHADER_CULL_CCW = 3 |
| 185 | } MOJOSHADER_cullMode; |
| 186 | |
| 187 | typedef enum MOJOSHADER_compareFunc |
| 188 | { |
| 189 | MOJOSHADER_CMP_NEVER = 1, |
| 190 | MOJOSHADER_CMP_LESS = 2, |
| 191 | MOJOSHADER_CMP_EQUAL = 3, |
| 192 | MOJOSHADER_CMP_LESSEQUAL = 4, |
| 193 | MOJOSHADER_CMP_GREATER = 5, |
| 194 | MOJOSHADER_CMP_NOTEQUAL = 6, |
| 195 | MOJOSHADER_CMP_GREATEREQUAL = 7, |
| 196 | MOJOSHADER_CMP_ALWAYS = 8 |
| 197 | } MOJOSHADER_compareFunc; |
| 198 | |
| 199 | typedef enum MOJOSHADER_fogMode |
| 200 | { |
| 201 | MOJOSHADER_FOG_NONE, |
| 202 | MOJOSHADER_FOG_EXP, |
| 203 | MOJOSHADER_FOG_EXP2, |
| 204 | MOJOSHADER_FOG_LINEAR |
| 205 | } MOJOSHADER_fogMode; |
| 206 | |
| 207 | typedef enum MOJOSHADER_stencilOp |
| 208 | { |
| 209 | MOJOSHADER_STENCILOP_KEEP = 1, |
| 210 | MOJOSHADER_STENCILOP_ZERO = 2, |
| 211 | MOJOSHADER_STENCILOP_REPLACE = 3, |
| 212 | MOJOSHADER_STENCILOP_INCRSAT = 4, |
| 213 | MOJOSHADER_STENCILOP_DECRSAT = 5, |
| 214 | MOJOSHADER_STENCILOP_INVERT = 6, |
| 215 | MOJOSHADER_STENCILOP_INCR = 7, |
| 216 | MOJOSHADER_STENCILOP_DECR = 8 |
| 217 | } MOJOSHADER_stencilOp; |
| 218 | |
| 219 | typedef enum MOJOSHADER_materialColorSource |
| 220 | { |
| 221 | MOJOSHADER_MCS_MATERIAL, |
| 222 | MOJOSHADER_MCS_COLOR1, |
| 223 | MOJOSHADER_MCS_COLOR2 |
| 224 | } MOJOSHADER_materialColorSource; |
| 225 | |
| 226 | typedef enum MOJOSHADER_vertexBlendFlags |
| 227 | { |
| 228 | MOJOSHADER_VBF_DISABLE = 0, |
| 229 | MOJOSHADER_VBF_1WEIGHTS = 1, |
| 230 | MOJOSHADER_VBF_2WEIGHTS = 2, |
| 231 | MOJOSHADER_VBF_3WEIGHTS = 3, |
| 232 | MOJOSHADER_VBF_TWEENING = 255, |
| 233 | MOJOSHADER_VBF_0WEIGHTS = 256 |
| 234 | } MOJOSHADER_vertexBlendFlags; |
| 235 | |
| 236 | typedef enum MOJOSHADER_patchedEdgeStyle |
| 237 | { |
| 238 | MOJOSHADER_PATCHEDGE_DISCRETE, |
| 239 | MOJOSHADER_PATCHEDGE_CONTINUOUS |
| 240 | } MOJOSHADER_patchedEdgeStyle; |
| 241 | |
| 242 | typedef enum MOJOSHADER_debugMonitorTokens |
| 243 | { |
| 244 | MOJOSHADER_DMT_ENABLE, |
| 245 | MOJOSHADER_DMT_DISABLE |
| 246 | } MOJOSHADER_debugMonitorTokens; |
| 247 | |
| 248 | typedef enum MOJOSHADER_blendOp |
| 249 | { |
| 250 | MOJOSHADER_BLENDOP_ADD = 1, |
| 251 | MOJOSHADER_BLENDOP_SUBTRACT = 2, |
| 252 | MOJOSHADER_BLENDOP_REVSUBTRACT = 3, |
| 253 | MOJOSHADER_BLENDOP_MIN = 4, |
| 254 | MOJOSHADER_BLENDOP_MAX = 5 |
| 255 | } MOJOSHADER_blendOp; |
| 256 | |
| 257 | typedef enum MOJOSHADER_degreeType |
| 258 | { |
| 259 | MOJOSHADER_DEGREE_LINEAR = 1, |
| 260 | MOJOSHADER_DEGREE_QUADRATIC = 2, |
| 261 | MOJOSHADER_DEGREE_CUBIC = 3, |
| 262 | MOJOSHADER_DEGREE_QUINTIC = 5 |
| 263 | } MOJOSHADER_degreeType; |
| 264 | |
| 265 | |
| 266 | /* MOJOSHADER_effectSamplerState types... */ |
| 267 | |
| 268 | typedef enum MOJOSHADER_samplerStateType |
| 269 | { |
| 270 | MOJOSHADER_SAMP_UNKNOWN0 = 0, |
| 271 | MOJOSHADER_SAMP_UNKNOWN1 = 1, |
| 272 | MOJOSHADER_SAMP_UNKNOWN2 = 2, |
| 273 | MOJOSHADER_SAMP_UNKNOWN3 = 3, |
| 274 | MOJOSHADER_SAMP_TEXTURE = 4, |
| 275 | MOJOSHADER_SAMP_ADDRESSU = 5, |
| 276 | MOJOSHADER_SAMP_ADDRESSV = 6, |
| 277 | MOJOSHADER_SAMP_ADDRESSW = 7, |
| 278 | MOJOSHADER_SAMP_BORDERCOLOR = 8, |
| 279 | MOJOSHADER_SAMP_MAGFILTER = 9, |
| 280 | MOJOSHADER_SAMP_MINFILTER = 10, |
| 281 | MOJOSHADER_SAMP_MIPFILTER = 11, |
| 282 | MOJOSHADER_SAMP_MIPMAPLODBIAS = 12, |
| 283 | MOJOSHADER_SAMP_MAXMIPLEVEL = 13, |
| 284 | MOJOSHADER_SAMP_MAXANISOTROPY = 14, |
| 285 | MOJOSHADER_SAMP_SRGBTEXTURE = 15, |
| 286 | MOJOSHADER_SAMP_ELEMENTINDEX = 16, |
| 287 | MOJOSHADER_SAMP_DMAPOFFSET = 17 |
| 288 | } MOJOSHADER_samplerStateType; |
| 289 | |
| 290 | typedef enum MOJOSHADER_textureAddress |
| 291 | { |
| 292 | MOJOSHADER_TADDRESS_WRAP = 1, |
| 293 | MOJOSHADER_TADDRESS_MIRROR = 2, |
| 294 | MOJOSHADER_TADDRESS_CLAMP = 3, |
| 295 | MOJOSHADER_TADDRESS_BORDER = 4, |
| 296 | MOJOSHADER_TADDRESS_MIRRORONCE = 5 |
| 297 | } MOJOSHADER_textureAddress; |
| 298 | |
| 299 | typedef enum MOJOSHADER_textureFilterType |
| 300 | { |
| 301 | MOJOSHADER_TEXTUREFILTER_NONE, |
| 302 | MOJOSHADER_TEXTUREFILTER_POINT, |
| 303 | MOJOSHADER_TEXTUREFILTER_LINEAR, |
| 304 | MOJOSHADER_TEXTUREFILTER_ANISOTROPIC, |
| 305 | MOJOSHADER_TEXTUREFILTER_PYRAMIDALQUAD, |
| 306 | MOJOSHADER_TEXTUREFILTER_GAUSSIANQUAD, |
| 307 | MOJOSHADER_TEXTUREFILTER_CONVOLUTIONMONO |
| 308 | } MOJOSHADER_textureFilterType; |
| 309 | |
| 310 | |
| 311 | /* Effect value types... */ |
| 312 | |
| 313 | typedef struct MOJOSHADER_effectSamplerState MOJOSHADER_effectSamplerState; |
| 314 | |
| 315 | typedef struct MOJOSHADER_effectValue |
| 316 | { |
| 317 | const char *name; |
| 318 | const char *semantic; |
| 319 | MOJOSHADER_symbolTypeInfo type; |
| 320 | unsigned int value_count; |
| 321 | MOJOSHADERNAMELESS union |
| 322 | { |
| 323 | /* Raw value types */ |
| 324 | void *values; |
| 325 | int *valuesI; |
| 326 | float *valuesF; |
| 327 | /* As used by MOJOSHADER_effectState */ |
| 328 | MOJOSHADER_zBufferType *valuesZBT; |
| 329 | MOJOSHADER_fillMode *valuesFiM; |
| 330 | MOJOSHADER_shadeMode *valuesSM; |
| 331 | MOJOSHADER_blendMode *valuesBM; |
| 332 | MOJOSHADER_cullMode *valuesCM; |
| 333 | MOJOSHADER_compareFunc *valuesCF; |
| 334 | MOJOSHADER_fogMode *valuesFoM; |
| 335 | MOJOSHADER_stencilOp *valuesSO; |
| 336 | MOJOSHADER_materialColorSource *valuesMCS; |
| 337 | MOJOSHADER_vertexBlendFlags *valuesVBF; |
| 338 | MOJOSHADER_patchedEdgeStyle *valuesPES; |
| 339 | MOJOSHADER_debugMonitorTokens *valuesDMT; |
| 340 | MOJOSHADER_blendOp *valuesBO; |
| 341 | MOJOSHADER_degreeType *valuesDT; |
| 342 | /* As used by MOJOSHADER_effectSamplerState */ |
| 343 | MOJOSHADER_textureAddress *valuesTA; |
| 344 | MOJOSHADER_textureFilterType *valuesTFT; |
| 345 | /* As used by MOJOSHADER_effectParameter */ |
| 346 | MOJOSHADER_effectSamplerState *valuesSS; |
| 347 | }; |
| 348 | } MOJOSHADER_effectValue; |
| 349 | |
| 350 | typedef struct MOJOSHADER_effectState |
| 351 | { |
| 352 | MOJOSHADER_renderStateType type; |
| 353 | MOJOSHADER_effectValue value; |
| 354 | } MOJOSHADER_effectState; |
| 355 | |
| 356 | struct MOJOSHADER_effectSamplerState |
| 357 | { |
| 358 | MOJOSHADER_samplerStateType type; |
| 359 | MOJOSHADER_effectValue value; |
| 360 | }; |
| 361 | |
| 362 | typedef MOJOSHADER_effectValue MOJOSHADER_effectAnnotation; |
| 363 | |
| 364 | |
| 365 | /* Effect interface structures... */ |
| 366 | |
| 367 | typedef struct MOJOSHADER_effectParam |
| 368 | { |
| 369 | MOJOSHADER_effectValue value; |
| 370 | unsigned int annotation_count; |
| 371 | MOJOSHADER_effectAnnotation *annotations; |
| 372 | } MOJOSHADER_effectParam; |
| 373 | |
| 374 | typedef struct MOJOSHADER_effectPass |
| 375 | { |
| 376 | const char *name; |
| 377 | unsigned int state_count; |
| 378 | MOJOSHADER_effectState *states; |
| 379 | unsigned int annotation_count; |
| 380 | MOJOSHADER_effectAnnotation* annotations; |
| 381 | } MOJOSHADER_effectPass; |
| 382 | |
| 383 | typedef struct MOJOSHADER_effectTechnique |
| 384 | { |
| 385 | const char *name; |
| 386 | unsigned int pass_count; |
| 387 | MOJOSHADER_effectPass *passes; |
| 388 | unsigned int annotation_count; |
| 389 | MOJOSHADER_effectAnnotation* annotations; |
| 390 | } MOJOSHADER_effectTechnique; |
| 391 | |
| 392 | |
| 393 | /* Effect "objects"... */ |
| 394 | |
| 395 | /* Defined later in the state change types... */ |
| 396 | typedef struct MOJOSHADER_samplerStateRegister MOJOSHADER_samplerStateRegister; |
| 397 | |
| 398 | typedef struct MOJOSHADER_effectShader |
| 399 | { |
| 400 | MOJOSHADER_symbolType type; |
| 401 | unsigned int technique; |
| 402 | unsigned int pass; |
| 403 | unsigned int is_preshader; |
| 404 | unsigned int preshader_param_count; |
| 405 | unsigned int *preshader_params; |
| 406 | unsigned int param_count; |
| 407 | unsigned int *params; |
| 408 | unsigned int sampler_count; |
| 409 | MOJOSHADER_samplerStateRegister *samplers; |
| 410 | MOJOSHADERNAMELESS union |
| 411 | { |
| 412 | void *shader; /* glShader, mtlShader, etc. */ |
| 413 | const MOJOSHADER_preshader *preshader; |
| 414 | }; |
| 415 | } MOJOSHADER_effectShader; |
| 416 | |
| 417 | typedef struct MOJOSHADER_effectSamplerMap |
| 418 | { |
| 419 | MOJOSHADER_symbolType type; |
| 420 | const char *name; |
| 421 | } MOJOSHADER_effectSamplerMap; |
| 422 | |
| 423 | typedef struct MOJOSHADER_effectString |
| 424 | { |
| 425 | MOJOSHADER_symbolType type; |
| 426 | const char *string; |
| 427 | } MOJOSHADER_effectString; |
| 428 | |
| 429 | typedef struct MOJOSHADER_effectTexture |
| 430 | { |
| 431 | MOJOSHADER_symbolType type; |
| 432 | /* FIXME: Does this even do anything? */ |
| 433 | } MOJOSHADER_effectTexture; |
| 434 | |
| 435 | typedef union MOJOSHADER_effectObject |
| 436 | { |
| 437 | MOJOSHADER_symbolType type; |
| 438 | MOJOSHADERNAMELESS union |
| 439 | { |
| 440 | MOJOSHADER_effectShader shader; |
| 441 | MOJOSHADER_effectSamplerMap mapping; |
| 442 | MOJOSHADER_effectString string; |
| 443 | MOJOSHADER_effectTexture texture; |
| 444 | }; |
| 445 | } MOJOSHADER_effectObject; |
| 446 | |
| 447 | |
| 448 | /* Effect state change types... */ |
| 449 | |
| 450 | /* Used to store sampler states with accompanying sampler registers */ |
| 451 | struct MOJOSHADER_samplerStateRegister |
| 452 | { |
| 453 | const char *sampler_name; |
| 454 | unsigned int sampler_register; |
| 455 | unsigned int sampler_state_count; |
| 456 | const MOJOSHADER_effectSamplerState *sampler_states; |
| 457 | }; |
| 458 | |
| 459 | /* |
| 460 | * Used to acquire the desired render state by the effect pass. |
| 461 | */ |
| 462 | typedef struct MOJOSHADER_effectStateChanges |
| 463 | { |
| 464 | /* Render state changes caused by effect technique */ |
| 465 | unsigned int render_state_change_count; |
| 466 | const MOJOSHADER_effectState *render_state_changes; |
| 467 | |
| 468 | /* Sampler state changes caused by effect technique */ |
| 469 | unsigned int sampler_state_change_count; |
| 470 | const MOJOSHADER_samplerStateRegister *sampler_state_changes; |
| 471 | |
| 472 | /* Vertex sampler state changes caused by effect technique */ |
| 473 | unsigned int vertex_sampler_state_change_count; |
| 474 | const MOJOSHADER_samplerStateRegister *vertex_sampler_state_changes; |
| 475 | } MOJOSHADER_effectStateChanges; |
| 476 | |
| 477 | |
| 478 | /* |
| 479 | * VTable system for building/running effect shaders... |
| 480 | */ |
| 481 | |
| 482 | typedef void* (MOJOSHADERCALL * MOJOSHADER_compileShaderFunc)( |
| 483 | const void *ctx, |
| 484 | const char *mainfn, |
| 485 | const unsigned char *tokenbuf, |
| 486 | const unsigned int bufsize, |
| 487 | const MOJOSHADER_swizzle *swiz, |
| 488 | const unsigned int swizcount, |
| 489 | const MOJOSHADER_samplerMap *smap, |
| 490 | const unsigned int smapcount |
| 491 | ); |
| 492 | typedef void (MOJOSHADERCALL * MOJOSHADER_shaderAddRefFunc)(void* shader); |
| 493 | typedef void (MOJOSHADERCALL * MOJOSHADER_deleteShaderFunc)( |
| 494 | const void *ctx, |
| 495 | void* shader |
| 496 | ); |
| 497 | typedef MOJOSHADER_parseData* (MOJOSHADERCALL * MOJOSHADER_getParseDataFunc)( |
| 498 | void *shader |
| 499 | ); |
| 500 | typedef void (MOJOSHADERCALL * MOJOSHADER_bindShadersFunc)( |
| 501 | const void *ctx, |
| 502 | void *vshader, |
| 503 | void *pshader |
| 504 | ); |
| 505 | typedef void (MOJOSHADERCALL * MOJOSHADER_getBoundShadersFunc)( |
| 506 | const void *ctx, |
| 507 | void **vshader, |
| 508 | void **pshader |
| 509 | ); |
| 510 | typedef void (MOJOSHADERCALL * MOJOSHADER_mapUniformBufferMemoryFunc)( |
| 511 | const void *ctx, |
| 512 | float **vsf, int **vsi, unsigned char **vsb, |
| 513 | float **psf, int **psi, unsigned char **psb |
| 514 | ); |
| 515 | typedef void (MOJOSHADERCALL * MOJOSHADER_unmapUniformBufferMemoryFunc)( |
| 516 | const void *ctx |
| 517 | ); |
| 518 | typedef const char* (MOJOSHADERCALL * MOJOSHADER_getErrorFunc)( |
| 519 | const void *ctx |
| 520 | ); |
| 521 | |
| 522 | typedef struct MOJOSHADER_effectShaderContext |
| 523 | { |
| 524 | /* Shader Backend */ |
| 525 | MOJOSHADER_compileShaderFunc compileShader; |
| 526 | MOJOSHADER_shaderAddRefFunc shaderAddRef; |
| 527 | MOJOSHADER_deleteShaderFunc deleteShader; |
| 528 | MOJOSHADER_getParseDataFunc getParseData; |
| 529 | MOJOSHADER_bindShadersFunc bindShaders; |
| 530 | MOJOSHADER_getBoundShadersFunc getBoundShaders; |
| 531 | MOJOSHADER_mapUniformBufferMemoryFunc mapUniformBufferMemory; |
| 532 | MOJOSHADER_unmapUniformBufferMemoryFunc unmapUniformBufferMemory; |
| 533 | MOJOSHADER_getErrorFunc getError; |
| 534 | |
| 535 | /* Shader context */ |
| 536 | const void *shaderContext; |
| 537 | |
| 538 | /* Allocator */ |
| 539 | MOJOSHADER_malloc m; |
| 540 | MOJOSHADER_free f; |
| 541 | void *malloc_data; |
| 542 | } MOJOSHADER_effectShaderContext; |
| 543 | |
| 544 | |
| 545 | /* |
| 546 | * Structure used to return data from parsing of an effect file... |
| 547 | */ |
| 548 | /* !!! FIXME: most of these ints should be unsigned. */ |
| 549 | typedef struct MOJOSHADER_effect |
| 550 | { |
| 551 | /* |
| 552 | * Public members. These are the fields your application cares about! |
| 553 | */ |
| 554 | |
| 555 | /* |
| 556 | * The number of elements pointed to by (errors). |
| 557 | */ |
| 558 | int error_count; |
| 559 | |
| 560 | /* |
| 561 | * (error_count) elements of data that specify errors that were generated |
| 562 | * by parsing this shader. |
| 563 | * This can be NULL if there were no errors or if (error_count) is zero. |
| 564 | */ |
| 565 | MOJOSHADER_error *errors; |
| 566 | |
| 567 | /* |
| 568 | * The number of params pointed to by (params). |
| 569 | */ |
| 570 | int param_count; |
| 571 | |
| 572 | /* |
| 573 | * (param_count) elements of data that specify parameter bind points for |
| 574 | * this effect. |
| 575 | * This can be NULL on error or if (param_count) is zero. |
| 576 | */ |
| 577 | MOJOSHADER_effectParam *params; |
| 578 | |
| 579 | /* |
| 580 | * The number of elements pointed to by (techniques). |
| 581 | */ |
| 582 | int technique_count; |
| 583 | |
| 584 | /* |
| 585 | * (technique_count) elements of data that specify techniques used in |
| 586 | * this effect. Each technique contains a series of passes, and each pass |
| 587 | * specifies state and shaders that affect rendering. |
| 588 | * This can be NULL on error or if (technique_count) is zero. |
| 589 | */ |
| 590 | MOJOSHADER_effectTechnique *techniques; |
| 591 | |
| 592 | /* |
| 593 | * The number of elements pointed to by (objects). |
| 594 | */ |
| 595 | int object_count; |
| 596 | |
| 597 | /* |
| 598 | * (object_count) elements of data that specify objects used in |
| 599 | * this effect. |
| 600 | * This can be NULL on error or if (object_count) is zero. |
| 601 | */ |
| 602 | MOJOSHADER_effectObject *objects; |
| 603 | |
| 604 | /* |
| 605 | * Semi-public members. These might be useful, but are better to access from |
| 606 | * a function, not directly. |
| 607 | */ |
| 608 | |
| 609 | /* |
| 610 | * The technique currently being rendered by this effect. |
| 611 | */ |
| 612 | const MOJOSHADER_effectTechnique *current_technique; |
| 613 | |
| 614 | /* |
| 615 | * The index of the current pass being rendered by this effect. |
| 616 | */ |
| 617 | int current_pass; |
| 618 | |
| 619 | /* |
| 620 | * Private Members. Do not touch anything below this line! |
| 621 | */ |
| 622 | |
| 623 | /* |
| 624 | * Value used to determine whether or not to restore the previous shader |
| 625 | * state after rendering an effect, as requested by application. |
| 626 | */ |
| 627 | int restore_shader_state; |
| 628 | |
| 629 | /* |
| 630 | * The structure provided by the appliation to store the state changes. |
| 631 | */ |
| 632 | MOJOSHADER_effectStateChanges *state_changes; |
| 633 | |
| 634 | /* |
| 635 | * Values used to store the current shader state during execution. |
| 636 | */ |
| 637 | MOJOSHADER_effectShader *current_vert_raw; |
| 638 | MOJOSHADER_effectShader *current_pixl_raw; |
| 639 | void *current_vert; |
| 640 | void *current_pixl; |
| 641 | |
| 642 | /* |
| 643 | * Values used to restore shader state after the effect has ended. |
| 644 | */ |
| 645 | void *prev_vertex_shader; |
| 646 | void *prev_pixel_shader; |
| 647 | |
| 648 | /* |
| 649 | * This is the shader implementation you passed to MOJOSHADER_compileEffect(). |
| 650 | */ |
| 651 | MOJOSHADER_effectShaderContext ctx; |
| 652 | } MOJOSHADER_effect; |
| 653 | |
| 654 | |
| 655 | /* Effect compiling interface... */ |
| 656 | |
| 657 | /* Fully compile/link the shaders found within the effect. |
| 658 | * |
| 659 | * (tokenbuf) is a buffer of Direct3D shader bytecode. |
| 660 | * (bufsize) is the size, in bytes, of the bytecode buffer. |
| 661 | * (swiz), (swizcount), (smap), and (smapcount) are passed to |
| 662 | * MOJOSHADER_parse() unmolested. |
| 663 | * (ctx) contains all the function pointers needed to create and bind shaders |
| 664 | * for a specific backend (OpenGL, Metal, etc). |
| 665 | * |
| 666 | * This function returns a MOJOSHADER_effect*, containing effect data which |
| 667 | * includes shaders usable with the provided backend. |
| 668 | * |
| 669 | * This call is only as thread safe as the backend functions! |
| 670 | */ |
| 671 | DECLSPEC MOJOSHADER_effect *MOJOSHADER_compileEffect(const unsigned char *tokenbuf, |
| 672 | const unsigned int bufsize, |
| 673 | const MOJOSHADER_swizzle *swiz, |
| 674 | const unsigned int swizcount, |
| 675 | const MOJOSHADER_samplerMap *smap, |
| 676 | const unsigned int smapcount, |
| 677 | const MOJOSHADER_effectShaderContext *ctx); |
| 678 | |
| 679 | /* Delete the shaders that were allocated for an effect. |
| 680 | * |
| 681 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 682 | * |
| 683 | * This call is only as thread safe as the backend functions! |
| 684 | */ |
| 685 | DECLSPEC void MOJOSHADER_deleteEffect(const MOJOSHADER_effect *effect); |
| 686 | |
| 687 | /* Copies an effect, including current parameter/technique data. |
| 688 | * |
| 689 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 690 | * |
| 691 | * This function returns a MOJOSHADER_effect*, containing effect data which |
| 692 | * includes shaders usable with the provided backend. |
| 693 | * |
| 694 | * This call is only as thread safe as the backend functions! |
| 695 | */ |
| 696 | DECLSPEC MOJOSHADER_effect *MOJOSHADER_cloneEffect(const MOJOSHADER_effect *effect); |
| 697 | |
| 698 | |
| 699 | /* Effect parameter interface... */ |
| 700 | |
| 701 | /* Set the constant value for the specified effect parameter. |
| 702 | * |
| 703 | * This function maps to ID3DXEffect::SetRawValue. |
| 704 | * |
| 705 | * (parameter) is a parameter obtained from a MOJOSHADER_effect*. |
| 706 | * (data) is the constant values to be applied to the parameter. |
| 707 | * (offset) is the offset, in bytes, of the parameter data being modified. |
| 708 | * (len) is the size, in bytes, of the data buffer being applied. |
| 709 | * |
| 710 | * This function is thread safe. |
| 711 | */ |
| 712 | DECLSPEC void MOJOSHADER_effectSetRawValueHandle(const MOJOSHADER_effectParam *parameter, |
| 713 | const void *data, |
| 714 | const unsigned int offset, |
| 715 | const unsigned int len); |
| 716 | |
| 717 | /* Set the constant value for the effect parameter, specified by name. |
| 718 | * Note: this function is slower than MOJOSHADER_effectSetRawValueHandle(), |
| 719 | * but we still provide it to fully map to ID3DXEffect. |
| 720 | * |
| 721 | * This function maps to ID3DXEffect::SetRawValue. |
| 722 | * |
| 723 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 724 | * (name) is the human-readable name of the parameter being modified. |
| 725 | * (data) is the constant values to be applied to the parameter. |
| 726 | * (offset) is the offset, in bytes, of the parameter data being modified. |
| 727 | * (len) is the size, in bytes, of the data buffer being applied. |
| 728 | * |
| 729 | * This function is thread safe. |
| 730 | */ |
| 731 | DECLSPEC void MOJOSHADER_effectSetRawValueName(const MOJOSHADER_effect *effect, |
| 732 | const char *name, |
| 733 | const void *data, |
| 734 | const unsigned int offset, |
| 735 | const unsigned int len); |
| 736 | |
| 737 | |
| 738 | /* Effect technique interface... */ |
| 739 | |
| 740 | /* Get the current technique in use by an effect. |
| 741 | * |
| 742 | * This function maps to ID3DXEffect::GetCurrentTechnique. |
| 743 | * |
| 744 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 745 | * |
| 746 | * This function returns the technique currently used by the given effect. |
| 747 | * |
| 748 | * This function is thread safe. |
| 749 | */ |
| 750 | DECLSPEC const MOJOSHADER_effectTechnique *MOJOSHADER_effectGetCurrentTechnique(const MOJOSHADER_effect *effect); |
| 751 | |
| 752 | /* Set the current technique to be used an effect. |
| 753 | * |
| 754 | * This function maps to ID3DXEffect::SetTechnique. |
| 755 | * |
| 756 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 757 | * (technique) is the technique to be used by the effect when rendered. |
| 758 | * |
| 759 | * This function is thread safe. |
| 760 | */ |
| 761 | DECLSPEC void MOJOSHADER_effectSetTechnique(MOJOSHADER_effect *effect, |
| 762 | const MOJOSHADER_effectTechnique *technique); |
| 763 | |
| 764 | /* Get the next technique in an effect's list. |
| 765 | * |
| 766 | * This function maps to ID3DXEffect::FindNextValidTechnique. |
| 767 | * |
| 768 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 769 | * (technique) can either be a technique found in the given effect, or NULL to |
| 770 | * find the first technique in the given effect. |
| 771 | * |
| 772 | * This function returns either the next technique after the passed technique, |
| 773 | * or the first technique if the passed technique is NULL. |
| 774 | * |
| 775 | * This function is thread safe. |
| 776 | */ |
| 777 | DECLSPEC const MOJOSHADER_effectTechnique *MOJOSHADER_effectFindNextValidTechnique(const MOJOSHADER_effect *effect, |
| 778 | const MOJOSHADER_effectTechnique *technique); |
| 779 | |
| 780 | |
| 781 | /* Effect rendering interface... */ |
| 782 | |
| 783 | /* Prepare the effect for rendering with the currently applied technique. |
| 784 | * |
| 785 | * This function maps to ID3DXEffect::Begin. |
| 786 | * |
| 787 | * In addition to the expected Begin parameters, we also include a parameter |
| 788 | * to pass in a MOJOSHADER_effectRenderState. Rather than change the render |
| 789 | * state within MojoShader itself we will simply provide what the effect wants |
| 790 | * and allow you to use this information with your own renderer. |
| 791 | * MOJOSHADER_effectBeginPass will update with the render state desired by |
| 792 | * the current effect pass. |
| 793 | * |
| 794 | * Note that we only provide the ability to preserve the shader state, but NOT |
| 795 | * the ability to preserve the render/sampler states. You are expected to |
| 796 | * track your own GL state and restore these states as needed for your |
| 797 | * application. |
| 798 | * |
| 799 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 800 | * (numPasses) will be filled with the number of passes that this technique |
| 801 | * will need to fully render. |
| 802 | * (saveShaderState) is a boolean value informing the effect whether or not to |
| 803 | * restore the shader bindings after calling MOJOSHADER_effectEnd. |
| 804 | * (renderState) will be filled by the effect to inform you of the render state |
| 805 | * changes introduced by the technique and its passes. |
| 806 | * |
| 807 | * This call is only as thread safe as the backend functions! |
| 808 | */ |
| 809 | DECLSPEC void MOJOSHADER_effectBegin(MOJOSHADER_effect *effect, |
| 810 | unsigned int *numPasses, |
| 811 | int saveShaderState, |
| 812 | MOJOSHADER_effectStateChanges *stateChanges); |
| 813 | |
| 814 | /* Begin an effect pass from the currently applied technique. |
| 815 | * |
| 816 | * This function maps to ID3DXEffect::BeginPass. |
| 817 | * |
| 818 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 819 | * (pass) is the index of the effect pass as found in the current technique. |
| 820 | * |
| 821 | * This call is only as thread safe as the backend functions! |
| 822 | */ |
| 823 | DECLSPEC void MOJOSHADER_effectBeginPass(MOJOSHADER_effect *effect, |
| 824 | unsigned int pass); |
| 825 | |
| 826 | /* Push render state changes that occurred within an actively rendering pass. |
| 827 | * |
| 828 | * This function maps to ID3DXEffect::CommitChanges. |
| 829 | * |
| 830 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 831 | * |
| 832 | * This call is only as thread safe as the backend functions! |
| 833 | */ |
| 834 | DECLSPEC void MOJOSHADER_effectCommitChanges(MOJOSHADER_effect *effect); |
| 835 | |
| 836 | /* End an effect pass from the currently applied technique. |
| 837 | * |
| 838 | * This function maps to ID3DXEffect::EndPass. |
| 839 | * |
| 840 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 841 | * |
| 842 | * This call is only as thread safe as the backend functions! |
| 843 | */ |
| 844 | DECLSPEC void MOJOSHADER_effectEndPass(MOJOSHADER_effect *effect); |
| 845 | |
| 846 | /* Complete rendering the effect technique, and restore the render state. |
| 847 | * |
| 848 | * This function maps to ID3DXEffect::End. |
| 849 | * |
| 850 | * (effect) is a MOJOSHADER_effect* obtained from MOJOSHADER_compileEffect(). |
| 851 | * |
| 852 | * This call is only as thread safe as the backend functions! |
| 853 | */ |
| 854 | DECLSPEC void MOJOSHADER_effectEnd(MOJOSHADER_effect *effect); |
| 855 | |
| 856 | |
| 857 | /* Profile-specific functions... */ |
| 858 | |
| 859 | /* |
| 860 | * Compile a MTLLibrary that contains all shaders of the given effect. |
| 861 | * |
| 862 | * This call requires a valid MOJOSHADER_mtlContext to have been created, |
| 863 | * or it will crash your program. See MOJOSHADER_mtlCreateContext(). |
| 864 | * |
| 865 | * Returns NULL on error, the generated MTLLibrary on success. |
| 866 | */ |
| 867 | DECLSPEC void *MOJOSHADER_mtlCompileLibrary(MOJOSHADER_effect *effect); |
| 868 | |
| 869 | /* |
| 870 | * Free the MTLLibrary given by (library). |
| 871 | */ |
| 872 | DECLSPEC void MOJOSHADER_mtlDeleteLibrary(void *library); |
| 873 | |
| 874 | |
| 875 | #endif /* MOJOSHADER_EFFECT_SUPPORT */ |
| 876 | |
| 877 | #endif /* MOJOSHADER_EFFECTS_H */ |