mojoshader_effects.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Thu, 27 Aug 2020 17:18:38 -0400
changeset 1302 974d2cc3558e
parent 1270 fb28ba997299
permissions -rw-r--r--
vulkan: Fix overlapping pixel/vertex shaders where one shader is NULL
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/**
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *  Direct3D shaders.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  This file written by Ryan C. Gordon.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 */
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#define __MOJOSHADER_INTERNAL__ 1
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include "mojoshader_internal.h"
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    13
#ifdef MOJOSHADER_EFFECT_SUPPORT
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    14
1237
da180fe5b4b7 Add MOJOSHADER_USE_SDL_STDLIB, to avoid C runtime dependencies
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1231
diff changeset
    15
#ifndef MOJOSHADER_USE_SDL_STDLIB
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    16
#include <math.h>
1237
da180fe5b4b7 Add MOJOSHADER_USE_SDL_STDLIB, to avoid C runtime dependencies
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1231
diff changeset
    17
#endif /* MOJOSHADER_USE_SDL_STDLIB */
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    18
1038
9b23f9a8aa31 Allow preshader support to be #ifdef'd out, to reduce binary size.
Ryan C. Gordon <icculus@icculus.org>
parents: 1037
diff changeset
    19
void MOJOSHADER_runPreshader(const MOJOSHADER_preshader *preshader,
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    20
                             float *outregs)
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    21
{
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    22
    const float *inregs = preshader->registers;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    23
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    24
    // this is fairly straightforward, as there aren't any branching
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    25
    //  opcodes in the preshader instruction set (at the moment, at least).
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    26
    const int scalarstart = (int) MOJOSHADER_PRESHADEROP_SCALAR_OPS;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    27
1047
6b92ce2fc809 Don't try to allocate a temp array if the preshader has no temps.
Ryan C. Gordon <icculus@icculus.org>
parents: 1046
diff changeset
    28
    double *temps = NULL;
6b92ce2fc809 Don't try to allocate a temp array if the preshader has no temps.
Ryan C. Gordon <icculus@icculus.org>
parents: 1046
diff changeset
    29
    if (preshader->temp_count > 0)
6b92ce2fc809 Don't try to allocate a temp array if the preshader has no temps.
Ryan C. Gordon <icculus@icculus.org>
parents: 1046
diff changeset
    30
    {
6b92ce2fc809 Don't try to allocate a temp array if the preshader has no temps.
Ryan C. Gordon <icculus@icculus.org>
parents: 1046
diff changeset
    31
        temps = (double *) alloca(sizeof (double) * preshader->temp_count);
6b92ce2fc809 Don't try to allocate a temp array if the preshader has no temps.
Ryan C. Gordon <icculus@icculus.org>
parents: 1046
diff changeset
    32
        memset(temps, '\0', sizeof (double) * preshader->temp_count);
6b92ce2fc809 Don't try to allocate a temp array if the preshader has no temps.
Ryan C. Gordon <icculus@icculus.org>
parents: 1046
diff changeset
    33
    } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    34
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    35
    double dst[4] = { 0, 0, 0, 0 };
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    36
    double src[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    37
    const double *src0 = &src[0][0];
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    38
    const double *src1 = &src[1][0];
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    39
    const double *src2 = &src[2][0];
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    40
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    41
    MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    42
    int instit;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    43
1231
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    44
#if 0 // FIXME: Do we need to do this or is the compiler smart enough?
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    45
    // Clear preshader output registers first!
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    46
    for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    47
    {
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    48
        const MOJOSHADER_preshaderOperand *operand = &inst->operands[inst->operand_count - 1];
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    49
        if (operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT)
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    50
            memset(&outregs[operand->index], '\0', sizeof(float) * 4);
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    51
    } // for
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    52
    inst = preshader->instructions;
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    53
#endif
76468460693e Add FIXME for preshader output registers
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1218
diff changeset
    54
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    55
    for (instit = 0; instit < preshader->instruction_count; instit++, inst++)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    56
    {
1046
097aefb03885 Place preshader destination register last in operand list.
Ryan C. Gordon <icculus@icculus.org>
parents: 1045
diff changeset
    57
        const MOJOSHADER_preshaderOperand *operand = inst->operands;
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    58
        const int elems = inst->element_count;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    59
        const int elemsbytes = sizeof (double) * elems;
1053
513af64373ad Maybe fix scalar operations in preshader interpreter.
Ryan C. Gordon <icculus@icculus.org>
parents: 1047
diff changeset
    60
        const int isscalarop = (inst->opcode >= scalarstart);
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    61
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    62
        assert(elems >= 0);
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    63
        assert(elems <= 4);
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    64
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    65
        // load up our operands...
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    66
        int opiter, elemiter;
1046
097aefb03885 Place preshader destination register last in operand list.
Ryan C. Gordon <icculus@icculus.org>
parents: 1045
diff changeset
    67
        for (opiter = 0; opiter < inst->operand_count-1; opiter++, operand++)
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    68
        {
1053
513af64373ad Maybe fix scalar operations in preshader interpreter.
Ryan C. Gordon <icculus@icculus.org>
parents: 1047
diff changeset
    69
            const int isscalar = ((isscalarop) && (opiter == 0));
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    70
            const unsigned int index = operand->index;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    71
            switch (operand->type)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    72
            {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    73
                case MOJOSHADER_PRESHADEROPERAND_LITERAL:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    74
                {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    75
                    if (!isscalar)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    76
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    77
                        assert((index + elems) <= preshader->literal_count);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    78
                        memcpy(&src[opiter][0], &preshader->literals[index], elemsbytes);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    79
                    } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    80
                    else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    81
                    {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    82
                        for (elemiter = 0; elemiter < elems; elemiter++)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    83
                            src[opiter][elemiter] = preshader->literals[index];
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    84
                    } // else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    85
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    86
                } // case
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    87
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    88
                case MOJOSHADER_PRESHADEROPERAND_INPUT:
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    89
                    if (operand->array_register_count > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    90
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    91
                        int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    92
                        const int *regsi = (const int *) inregs;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    93
                        int arrIndex = regsi[((index >> 4) * 4) + ((index >> 2) & 3)];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    94
                        for (i = 0; i < operand->array_register_count; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    95
                        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    96
                            arrIndex = regsi[operand->array_registers[i] + arrIndex];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    97
                        }
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    98
                        src[opiter][0] = arrIndex;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    99
                    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   100
                    else if (isscalar)
1043
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   101
                        src[opiter][0] = inregs[index];
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   102
                    else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   103
                    {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   104
                        int cpy;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   105
                        for (cpy = 0; cpy < elems; cpy++)
1043
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   106
                            src[opiter][cpy] = inregs[index+cpy];
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   107
                    } // else
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   108
                    break;
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   109
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   110
                case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   111
                    if (isscalar)
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   112
                        src[opiter][0] = outregs[index];
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   113
                    else
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   114
                    {
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   115
                        int cpy;
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   116
                        for (cpy = 0; cpy < elems; cpy++)
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   117
                            src[opiter][cpy] = outregs[index+cpy];
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   118
                    } // else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   119
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   120
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   121
                case MOJOSHADER_PRESHADEROPERAND_TEMP:
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   122
                    if (temps != NULL)
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   123
                    {
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   124
                        if (isscalar)
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   125
                            src[opiter][0] = temps[index];
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   126
                        else
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   127
                            memcpy(src[opiter], temps + index, elemsbytes);
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   128
                    } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   129
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   130
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   131
                default:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   132
                    assert(0 && "unexpected preshader operand type.");
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   133
                    return;
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   134
            } // switch
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   135
        } // for
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   136
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   137
        // run the actual instruction, store result to dst.
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   138
        int i;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   139
        switch (inst->opcode)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   140
        {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   141
            #define OPCODE_CASE(op, val) \
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   142
                case MOJOSHADER_PRESHADEROP_##op: \
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   143
                    for (i = 0; i < elems; i++) { dst[i] = val; } \
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   144
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   145
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   146
            //OPCODE_CASE(NOP, 0.0)  // not a real instruction.
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   147
            OPCODE_CASE(MOV, src0[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   148
            OPCODE_CASE(NEG, -src0[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   149
            OPCODE_CASE(RCP, 1.0 / src0[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   150
            OPCODE_CASE(FRC, src0[i] - floor(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   151
            OPCODE_CASE(EXP, exp(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   152
            OPCODE_CASE(LOG, log(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   153
            OPCODE_CASE(RSQ, 1.0 / sqrt(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   154
            OPCODE_CASE(SIN, sin(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   155
            OPCODE_CASE(COS, cos(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   156
            OPCODE_CASE(ASIN, asin(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   157
            OPCODE_CASE(ACOS, acos(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   158
            OPCODE_CASE(ATAN, atan(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   159
            OPCODE_CASE(MIN, (src0[i] < src1[i]) ? src0[i] : src1[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   160
            OPCODE_CASE(MAX, (src0[i] > src1[i]) ? src0[i] : src1[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   161
            OPCODE_CASE(LT, (src0[i] < src1[i]) ? 1.0 : 0.0)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   162
            OPCODE_CASE(GE, (src0[i] >= src1[i]) ? 1.0 : 0.0)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   163
            OPCODE_CASE(ADD, src0[i] + src1[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   164
            OPCODE_CASE(MUL,  src0[i] * src1[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   165
            OPCODE_CASE(ATAN2, atan2(src0[i], src1[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   166
            OPCODE_CASE(DIV, src0[i] / src1[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   167
            OPCODE_CASE(CMP, (src0[i] >= 0.0) ? src1[i] : src2[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   168
            //OPCODE_CASE(NOISE, ???)  // !!! FIXME: don't know what this does
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   169
            //OPCODE_CASE(MOVC, ???)  // !!! FIXME: don't know what this does
1074
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   170
            OPCODE_CASE(MIN_SCALAR, (src0[0] < src1[i]) ? src0[0] : src1[i])
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   171
            OPCODE_CASE(MAX_SCALAR, (src0[0] > src1[i]) ? src0[0] : src1[i])
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   172
            OPCODE_CASE(LT_SCALAR, (src0[0] < src1[i]) ? 1.0 : 0.0)
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   173
            OPCODE_CASE(GE_SCALAR, (src0[0] >= src1[i]) ? 1.0 : 0.0)
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   174
            OPCODE_CASE(ADD_SCALAR, src0[0] + src1[i])
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   175
            OPCODE_CASE(MUL_SCALAR, src0[0] * src1[i])
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   176
            OPCODE_CASE(ATAN2_SCALAR, atan2(src0[0], src1[i]))
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   177
            OPCODE_CASE(DIV_SCALAR, src0[0] / src1[i])
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   178
            //OPCODE_CASE(DOT_SCALAR)  // !!! FIXME: isn't this just a MUL?
3fdc7cbd0c37 Corrected scalar opcode behavior in the preshader virtual machine.
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   179
            //OPCODE_CASE(NOISE_SCALAR, ???)  // !!! FIXME: ?
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   180
            #undef OPCODE_CASE
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   181
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   182
            case MOJOSHADER_PRESHADEROP_DOT:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   183
            {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   184
                double final = 0.0;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   185
                for (i = 0; i < elems; i++)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   186
                    final += src0[i] * src1[i];
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   187
                for (i = 0; i < elems; i++)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   188
                    dst[i] = final;  // !!! FIXME: is this right?
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   189
                break;
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   190
            } // case
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   191
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   192
            default:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   193
                assert(0 && "Unhandled preshader opcode!");
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   194
                break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   195
        } // switch
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   196
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   197
        // Figure out where dst wants to be stored.
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   198
        if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   199
        {
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   200
            assert(preshader->temp_count >=
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   201
                    operand->index + (elemsbytes / sizeof (double)));
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   202
            memcpy(temps + operand->index, dst, elemsbytes);
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   203
        } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   204
        else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   205
        {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   206
            assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   207
            for (i = 0; i < elems; i++)
1043
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   208
                outregs[operand->index + i] = (float) dst[i];
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   209
        } // else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   210
    } // for
1038
9b23f9a8aa31 Allow preshader support to be #ifdef'd out, to reduce binary size.
Ryan C. Gordon <icculus@icculus.org>
parents: 1037
diff changeset
   211
} // MOJOSHADER_runPreshader
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
static MOJOSHADER_effect MOJOSHADER_out_of_mem_effect = {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    1, &MOJOSHADER_out_of_mem_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
};
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   216
static MOJOSHADER_error MOJOSHADER_need_a_backend_error = {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   217
    "Need a MOJOSHADER_effectShaderContext", NULL, MOJOSHADER_POSITION_NONE
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   218
};
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   219
static MOJOSHADER_effect MOJOSHADER_need_a_backend_effect = {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   220
    1, &MOJOSHADER_need_a_backend_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   221
};
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   222
static MOJOSHADER_error MOJOSHADER_unexpected_eof_error = {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   223
    "Unexpected EOF", NULL, MOJOSHADER_POSITION_NONE
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   224
};
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   225
static MOJOSHADER_effect MOJOSHADER_unexpected_eof_effect = {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   226
    1, &MOJOSHADER_unexpected_eof_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   227
};
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   228
static MOJOSHADER_error MOJOSHADER_not_an_effect_error = {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   229
    "Not an Effects Framework binary", NULL, MOJOSHADER_POSITION_NONE
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   230
};
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   231
static MOJOSHADER_effect MOJOSHADER_not_an_effect_effect = {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   232
    1, &MOJOSHADER_not_an_effect_error, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   233
};
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   234
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   235
static void push_errors(ErrorList *list, MOJOSHADER_error *errors, int len)
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   236
{
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   237
    int i;
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   238
    for (i = 0; i < len; i += 1)
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   239
        errorlist_add(list, errors[i].filename, errors[i].error_position, errors[i].error);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   240
} // push_errors
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
static uint32 readui32(const uint8 **_ptr, uint32 *_len)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
{
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    uint32 retval = 0;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
    if (*_len < sizeof (retval))
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        *_len = 0;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
    else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        const uint32 *ptr = (const uint32 *) *_ptr;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
        retval = SWAP32(*ptr);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
        *_ptr += sizeof (retval);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
        *_len -= sizeof (retval);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    } // else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    return retval;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
} // readui32
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   257
static char *readstring(const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   258
                        const uint32 offset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   259
                        MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   260
                        void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   261
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   262
    // !!! FIXME: sanity checks!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   263
    // !!! FIXME: verify this doesn't go past EOF looking for a null.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   264
    const char *str = ((const char *) base) + offset;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   265
    const uint32 len = *((const uint32 *) str);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   266
    char *strptr = NULL;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   267
    if (len == 0) return NULL; /* No length? No string. */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   268
    strptr = (char *) m(len, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   269
    memcpy(strptr, str + 4, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   270
    return strptr;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   271
} // readstring
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   272
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   273
static int findparameter(const MOJOSHADER_effectParam *params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   274
                         const uint32 param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   275
                         const char *name)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   276
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   277
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   278
    for (i = 0; i < param_count; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   279
        if (strcmp(name, params[i].value.name) == 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   280
            return i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   281
    assert(0 && "Parameter not found!");
1205
59d969911962 Return statements for effectFind functions
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1204
diff changeset
   282
    return -1;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   283
}
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   284
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   285
static void readvalue(const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   286
                      const uint32 typeoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   287
                      const uint32 valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   288
                      MOJOSHADER_effectValue *value,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   289
                      MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   290
                      MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   291
                      void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   292
{
1173
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   293
    int i, j, k;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   294
    const uint8 *typeptr = base + typeoffset;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   295
    const uint8 *valptr = base + valoffset;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   296
    unsigned int typelen = 9999999;  // !!! FIXME
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   297
    const uint32 type = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   298
    const uint32 valclass = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   299
    const uint32 name = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   300
    const uint32 semantic = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   301
    const uint32 numelements = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   302
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   303
    value->type.parameter_type = (MOJOSHADER_symbolType) type;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   304
    value->type.parameter_class = (MOJOSHADER_symbolClass) valclass;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   305
    value->name = readstring(base, name, m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   306
    value->semantic = readstring(base, semantic, m, d);
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   307
    value->type.elements = numelements;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   308
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   309
    /* Class sanity check */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   310
    assert(valclass >= MOJOSHADER_SYMCLASS_SCALAR && valclass <= MOJOSHADER_SYMCLASS_STRUCT);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   311
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   312
    if (valclass == MOJOSHADER_SYMCLASS_SCALAR
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   313
     || valclass == MOJOSHADER_SYMCLASS_VECTOR
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   314
     || valclass == MOJOSHADER_SYMCLASS_MATRIX_ROWS
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   315
     || valclass == MOJOSHADER_SYMCLASS_MATRIX_COLUMNS)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   316
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   317
        /* These classes only ever contain scalar values */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   318
        assert(type >= MOJOSHADER_SYMTYPE_BOOL && type <= MOJOSHADER_SYMTYPE_FLOAT);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   319
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   320
        const uint32 columncount = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   321
        const uint32 rowcount = readui32(&typeptr, &typelen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   322
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   323
        value->type.columns = columncount;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   324
        value->type.rows = rowcount;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   325
1173
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   326
        uint32 siz = 4 * rowcount;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   327
        if (numelements > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   328
            siz *= numelements;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   329
        value->value_count = siz;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   330
        siz *= 4;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   331
        value->values = m(siz, d);
1173
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   332
        memset(value->values, '\0', siz);
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   333
        siz /= 16;
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   334
        for (i = 0; i < siz; i++)
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   335
            memcpy(value->valuesF + (i << 2), valptr + ((columncount << 2) * i), columncount << 2);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   336
    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   337
    else if (valclass == MOJOSHADER_SYMCLASS_OBJECT)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   338
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   339
        /* This class contains either samplers or "objects" */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   340
        assert(type >= MOJOSHADER_SYMTYPE_STRING && type <= MOJOSHADER_SYMTYPE_VERTEXSHADER);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   341
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   342
        if (type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   343
         || type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   344
         || type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   345
         || type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   346
         || type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   347
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   348
            unsigned int vallen = 9999999; // !!! FIXME
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   349
            const uint32 numstates = readui32(&valptr, &vallen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   350
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   351
            value->value_count = numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   352
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   353
            const uint32 siz = sizeof(MOJOSHADER_effectSamplerState) * numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   354
            value->values = m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   355
            memset(value->values, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   356
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   357
            for (i = 0; i < numstates; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   358
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   359
                MOJOSHADER_effectSamplerState *state = &value->valuesSS[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   360
                const uint32 stype = readui32(&valptr, &vallen) & ~0xA0;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   361
                /*const uint32 FIXME =*/ readui32(&valptr, &vallen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   362
                const uint32 statetypeoffset = readui32(&valptr, &vallen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   363
                const uint32 statevaloffset = readui32(&valptr, &vallen);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   364
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   365
                state->type = (MOJOSHADER_samplerStateType) stype;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   366
                readvalue(base, statetypeoffset, statevaloffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   367
                          &state->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   368
                          m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   369
                if (stype == MOJOSHADER_SAMP_TEXTURE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   370
                    objects[state->value.valuesI[0]].type = (MOJOSHADER_symbolType) type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   371
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   372
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   373
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   374
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   375
            uint32 numobjects = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   376
            if (numelements > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   377
                numobjects = numelements;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   378
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   379
            value->value_count = numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   380
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   381
            const uint32 siz = 4 * numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   382
            value->values = m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   383
            memcpy(value->values, valptr, siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   384
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   385
            for (i = 0; i < value->value_count; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   386
                objects[value->valuesI[i]].type = (MOJOSHADER_symbolType) type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   387
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   388
    } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   389
    else if (valclass == MOJOSHADER_SYMCLASS_STRUCT)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   390
    {
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   391
        uint32 siz;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   392
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   393
        value->type.member_count = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   394
        siz = value->type.member_count * sizeof (MOJOSHADER_symbolStructMember);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   395
        value->type.members = (MOJOSHADER_symbolStructMember *) m(siz, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   396
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   397
        uint32 structsize = 0;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   398
        for (i = 0; i < value->type.member_count; i++)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   399
        {
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   400
            MOJOSHADER_symbolStructMember *mem = &value->type.members[i];
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   401
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   402
            mem->info.parameter_type = (MOJOSHADER_symbolType) readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   403
            mem->info.parameter_class = (MOJOSHADER_symbolClass) readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   404
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   405
            const uint32 memname = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   406
            /*const uint32 memsemantic =*/ readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   407
            mem->name = readstring(base, memname, m, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   408
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   409
            mem->info.elements = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   410
            mem->info.columns = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   411
            mem->info.rows = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   412
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   413
            // !!! FIXME: Nested structs! -flibit
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   414
            assert(mem->info.parameter_class >= MOJOSHADER_SYMCLASS_SCALAR
1204
21422b28cfd4 Matrix members inside struct parameters are also valid
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1186
diff changeset
   415
                && mem->info.parameter_class <= MOJOSHADER_SYMCLASS_MATRIX_COLUMNS);
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   416
            assert(mem->info.parameter_type >= MOJOSHADER_SYMTYPE_BOOL
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   417
                && mem->info.parameter_type <= MOJOSHADER_SYMTYPE_FLOAT);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   418
            mem->info.member_count = 0;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   419
            mem->info.members = NULL;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   420
1173
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   421
            uint32 memsize = 4 * mem->info.rows;
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   422
            if (mem->info.elements > 0)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   423
                memsize *= mem->info.elements;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   424
            structsize += memsize;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   425
        } // for
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   426
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   427
        value->type.columns = structsize;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   428
        value->type.rows = 1;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   429
        value->value_count = structsize;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   430
        if (numelements > 0)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   431
            value->value_count *= numelements;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   432
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   433
        siz = value->value_count * 4;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   434
        value->values = m(siz, d);
1173
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   435
        memset(value->values, '\0', siz);
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   436
        int dst_offset = 0, src_offset = 0;
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   437
        i = 0;
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   438
        do
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   439
        {
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   440
            for (j = 0; j < value->type.member_count; j++)
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   441
            {
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   442
                siz = value->type.members[j].info.rows * value->type.members[j].info.elements;
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   443
                for (k = 0; k < siz; k++)
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   444
                {
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   445
                    memcpy(value->valuesF + dst_offset,
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   446
                           typeptr + src_offset, /* Yes, typeptr. -flibit */
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   447
                           value->type.members[j].info.columns << 2);
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   448
                    dst_offset += 4;
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   449
                    src_offset += value->type.members[j].info.columns << 2;
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   450
                } // for
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   451
            }
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1168
diff changeset
   452
        } while (++i < numelements);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   453
    } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   454
} // readvalue
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   455
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   456
static void readannotations(const uint32 numannos,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   457
                            const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   458
                            const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   459
                            uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   460
                            MOJOSHADER_effectAnnotation **annotations,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   461
                            MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   462
                            MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   463
                            void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   464
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   465
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   466
    if (numannos == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   467
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   468
    const uint32 siz = sizeof(MOJOSHADER_effectAnnotation) * numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   469
    *annotations = (MOJOSHADER_effectAnnotation *) m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   470
    memset(*annotations, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   471
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   472
    for (i = 0; i < numannos; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   473
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   474
        MOJOSHADER_effectAnnotation *anno = &(*annotations)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   475
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   476
        const uint32 typeoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   477
        const uint32 valoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   478
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   479
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   480
                  anno, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   481
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   482
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   483
} // readannotation
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   484
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   485
static void readparameters(const uint32 numparams,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   486
                           const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   487
                           const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   488
                           uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   489
                           MOJOSHADER_effectParam **params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   490
                           MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   491
                           MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   492
                           void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   493
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   494
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   495
    if (numparams == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   496
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   497
    uint32 siz = sizeof(MOJOSHADER_effectParam) * numparams;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   498
    *params = (MOJOSHADER_effectParam *) m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   499
    memset(*params, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   500
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   501
    for (i = 0; i < numparams; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   502
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   503
        MOJOSHADER_effectParam *param = &(*params)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   504
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   505
        const uint32 typeoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   506
        const uint32 valoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   507
        /*const uint32 flags =*/ readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   508
        const uint32 numannos = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   509
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   510
        param->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   511
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   512
                        &param->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   513
                        m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   514
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   515
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   516
                  &param->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   517
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   518
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   519
} // readparameters
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   520
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   521
static void readstates(const uint32 numstates,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   522
                       const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   523
                       const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   524
                       uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   525
                       MOJOSHADER_effectState **states,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   526
                       MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   527
                       MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   528
                       void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   529
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   530
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   531
    if (numstates == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   532
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   533
    const uint32 siz = sizeof (MOJOSHADER_effectState) * numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   534
    *states = (MOJOSHADER_effectState *) m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   535
    memset(*states, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   536
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   537
    for (i = 0; i < numstates; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   538
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   539
        MOJOSHADER_effectState *state = &(*states)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   540
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   541
        const uint32 type = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   542
        /*const uint32 FIXME =*/ readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   543
        const uint32 typeoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   544
        const uint32 valoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   545
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   546
        state->type = (MOJOSHADER_renderStateType) type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   547
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   548
                  &state->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   549
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   550
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   551
} // readstates
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   552
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   553
static void readpasses(const uint32 numpasses,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   554
                       const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   555
                       const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   556
                       uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   557
                       MOJOSHADER_effectPass **passes,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   558
                       MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   559
                       MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   560
                       void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   561
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   562
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   563
    if (numpasses == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   564
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   565
    const uint32 siz = sizeof (MOJOSHADER_effectPass) * numpasses;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   566
    *passes = (MOJOSHADER_effectPass *) m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   567
    memset(*passes, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   568
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   569
    for (i = 0; i < numpasses; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   570
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   571
        MOJOSHADER_effectPass *pass = &(*passes)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   572
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   573
        const uint32 passnameoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   574
        const uint32 numannos = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   575
        const uint32 numstates = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   576
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   577
        pass->name = readstring(base, passnameoffset, m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   578
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   579
        pass->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   580
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   581
                        &pass->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   582
                        m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   583
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   584
        pass->state_count = numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   585
        readstates(numstates, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   586
                   &pass->states, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   587
                   m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   588
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   589
} // readpasses
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   590
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   591
static void readtechniques(const uint32 numtechniques,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   592
                           const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   593
                           const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   594
                           uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   595
                           MOJOSHADER_effectTechnique **techniques,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   596
                           MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   597
                           MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   598
                           void *d)
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
{
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   600
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   601
    if (numtechniques == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   602
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   603
    const uint32 siz = sizeof (MOJOSHADER_effectTechnique) * numtechniques;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   604
    *techniques = (MOJOSHADER_effectTechnique *) m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   605
    memset(*techniques, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   606
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   607
    for (i = 0; i < numtechniques; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   608
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   609
        MOJOSHADER_effectTechnique *technique = &(*techniques)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   610
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   611
        const uint32 nameoffset = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   612
        const uint32 numannos = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   613
        const uint32 numpasses = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   614
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   615
        technique->name = readstring(base, nameoffset, m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   616
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   617
        technique->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   618
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   619
                        &technique->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   620
                        m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   621
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   622
        technique->pass_count = numpasses;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   623
        readpasses(numpasses, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   624
                   &technique->passes, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   625
                   m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   626
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   627
} // readtechniques
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   628
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   629
static void readsmallobjects(const uint32 numsmallobjects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   630
                             const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   631
                             uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   632
                             MOJOSHADER_effect *effect,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   633
                             const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   634
                             const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   635
                             const MOJOSHADER_samplerMap *smap,
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   636
                             const unsigned int smapcount,
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   637
                             ErrorList *errors)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   638
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   639
    int i, j;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   640
    MOJOSHADER_parseData *pd;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   641
    MOJOSHADER_malloc m = effect->ctx.m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   642
    void *d = effect->ctx.malloc_data;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   643
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   644
    if (numsmallobjects == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   645
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   646
    for (i = 1; i < numsmallobjects + 1; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   647
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   648
        const uint32 index = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   649
        const uint32 length = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   650
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   651
        MOJOSHADER_effectObject *object = &effect->objects[index];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   652
        if (object->type == MOJOSHADER_SYMTYPE_STRING)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   653
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   654
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   655
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   656
                char *str = (char *) m(length, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   657
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   658
                object->string.string = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   659
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   660
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   661
        else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   662
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   663
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   664
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
1186
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   665
              || object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   666
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   667
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   668
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   669
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   670
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   671
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   672
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   673
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   674
                char *str = (char *) m(length, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   675
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   676
                object->mapping.name = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   677
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   678
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   679
        else if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   680
              || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   681
        {
1168
ddd9304d03a8 Give shaders in Effects files a unique main function name.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   682
            char mainfn[32];
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   683
            snprintf(mainfn, sizeof(mainfn), "ShaderFunction%u", (unsigned int) index);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   684
            object->shader.technique = -1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   685
            object->shader.pass = -1;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   686
            object->shader.shader = effect->ctx.compileShader(mainfn, *ptr, length,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   687
                                                              swiz, swizcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   688
                                                              smap, smapcount);
1270
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   689
            if (object->shader.shader == NULL)
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   690
            {
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   691
                // Bail ASAP, so we can get the error to the application
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   692
                errorlist_add(errors, NULL, 0, effect->ctx.getError());
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   693
                return;
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   694
            } // if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   695
            pd = effect->ctx.getParseData(object->shader.shader);
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   696
            if (pd->error_count > 0)
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   697
            {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   698
                // Bail ASAP, so we can get the error to the application
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   699
                push_errors(errors, pd->errors, pd->error_count);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   700
                return;
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   701
            } // if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   702
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   703
            for (j = 0; j < pd->symbol_count; j++)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   704
                if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   705
                    object->shader.sampler_count++;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   706
            object->shader.param_count = pd->symbol_count;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   707
            object->shader.params = (uint32 *) m(object->shader.param_count * sizeof (uint32), d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   708
            object->shader.samplers = (MOJOSHADER_samplerStateRegister *) m(object->shader.sampler_count * sizeof (MOJOSHADER_samplerStateRegister), d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   709
            uint32 curSampler = 0;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   710
            for (j = 0; j < pd->symbol_count; j++)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   711
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   712
                int par = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   713
                                        effect->param_count,
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   714
                                        pd->symbols[j].name);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   715
                object->shader.params[j] = par;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   716
                if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   717
                {
1185
7ff14b0252a0 Use param name ptr for sampler_name
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1183
diff changeset
   718
                    object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   719
                    object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   720
                    object->shader.samplers[curSampler].sampler_state_count = effect->params[par].value.value_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   721
                    object->shader.samplers[curSampler].sampler_states = effect->params[par].value.valuesSS;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   722
                    curSampler++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   723
                } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   724
            } // for
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   725
            if (pd->preshader)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   726
            {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   727
                object->shader.preshader_param_count = pd->preshader->symbol_count;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   728
                object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   729
                for (j = 0; j < pd->preshader->symbol_count; j++)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   730
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   731
                    object->shader.preshader_params[j] = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   732
                                                                       effect->param_count,
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   733
                                                                       pd->preshader->symbols[j].name);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   734
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   735
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   736
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   737
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   738
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   739
            assert(0 && "Small object type unknown!");
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   740
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   741
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   742
        /* Object block is always a multiple of four */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   743
        const uint32 blocklen = (length + 3) - ((length - 1) % 4);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   744
        *ptr += blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   745
        *len -= blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   746
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   747
} // readstrings
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   748
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   749
static void readlargeobjects(const uint32 numlargeobjects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   750
                             const uint32 numsmallobjects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   751
                             const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   752
                             uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   753
                             MOJOSHADER_effect *effect,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   754
                             const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   755
                             const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   756
                             const MOJOSHADER_samplerMap *smap,
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   757
                             const unsigned int smapcount,
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   758
                             ErrorList *errors)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   759
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   760
    int i, j;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   761
    MOJOSHADER_parseData *pd;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   762
    MOJOSHADER_malloc m = effect->ctx.m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   763
    MOJOSHADER_free f = effect->ctx.f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   764
    void *d = effect->ctx.malloc_data;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   765
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   766
    if (numlargeobjects == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   767
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   768
    int numobjects = numsmallobjects + numlargeobjects + 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   769
    for (i = numsmallobjects + 1; i < numobjects; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   770
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   771
        const uint32 technique = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   772
        const uint32 index = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   773
        /*const uint32 FIXME =*/ readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   774
        const uint32 state = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   775
        const uint32 type = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   776
        const uint32 length = readui32(ptr, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   777
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   778
        uint32 objectIndex;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   779
        if (technique == -1)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   780
            objectIndex = effect->params[index].value.valuesSS[state].value.valuesI[0];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   781
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   782
            objectIndex = effect->techniques[technique].passes[index].states[state].value.valuesI[0];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   783
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   784
        MOJOSHADER_effectObject *object = &effect->objects[objectIndex];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   785
        if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   786
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   787
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   788
            object->shader.technique = technique;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   789
            object->shader.pass = index;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   790
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   791
            if (type == 2)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   792
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   793
                /* This is a standalone preshader!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   794
                 * It exists solely for effect passes that do not use a single
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   795
                 * vertex/fragment shader.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   796
                 */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   797
                object->shader.is_preshader = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   798
                const uint32 start = *((uint32 *) *ptr) + 4;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   799
                const char *array = readstring(*ptr, 0, m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   800
                object->shader.param_count = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   801
                object->shader.params = (uint32 *) m(sizeof (uint32), d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   802
                object->shader.params[0] = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   803
                                                         effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   804
                                                         array);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   805
                f((void *) array, d);
1183
ec44ee868688 Minor fixes from FNA branch
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1173
diff changeset
   806
                object->shader.preshader = MOJOSHADER_parsePreshader(*ptr + start, length,
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   807
                                                                     m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   808
                // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   809
                object->shader.preshader_param_count = object->shader.preshader->symbol_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   810
                object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   811
                for (j = 0; j < object->shader.preshader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   812
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   813
                    object->shader.preshader_params[j] = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   814
                                                                       effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   815
                                                                       object->shader.preshader->symbols[j].name);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   816
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   817
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   818
            else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   819
            {
1168
ddd9304d03a8 Give shaders in Effects files a unique main function name.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   820
                char mainfn[32];
ddd9304d03a8 Give shaders in Effects files a unique main function name.
Ryan C. Gordon <icculus@icculus.org>
parents: 1167
diff changeset
   821
                snprintf(mainfn, sizeof (mainfn), "ShaderFunction%u", (unsigned int) objectIndex);
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   822
                object->shader.shader = effect->ctx.compileShader(mainfn, *ptr, length,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   823
                                                                  swiz, swizcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   824
                                                                  smap, smapcount);
1270
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   825
                if (object->shader.shader == NULL)
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   826
                {
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   827
                    // Bail ASAP, so we can get the error to the application
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   828
                    errorlist_add(errors, NULL, 0, effect->ctx.getError());
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   829
                    return;
fb28ba997299 Add getError to effectShaderContext
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1269
diff changeset
   830
                } // if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   831
                pd = effect->ctx.getParseData(object->shader.shader);
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   832
                if (pd->error_count > 0)
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   833
                {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   834
                    // Bail ASAP, so we can get the error to the application
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   835
                    push_errors(errors, pd->errors, pd->error_count);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   836
                    return;
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   837
                } // if
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   838
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   839
                for (j = 0; j < pd->symbol_count; j++)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   840
                    if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   841
                        object->shader.sampler_count++;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   842
                object->shader.param_count = pd->symbol_count;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   843
                object->shader.params = (uint32 *) m(object->shader.param_count * sizeof (uint32), d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   844
                object->shader.samplers = (MOJOSHADER_samplerStateRegister *) m(object->shader.sampler_count * sizeof (MOJOSHADER_samplerStateRegister), d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   845
                uint32 curSampler = 0;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   846
                for (j = 0; j < pd->symbol_count; j++)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   847
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   848
                    int par = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   849
                                            effect->param_count,
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   850
                                            pd->symbols[j].name);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   851
                    object->shader.params[j] = par;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   852
                    if (pd->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   853
                    {
1185
7ff14b0252a0 Use param name ptr for sampler_name
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1183
diff changeset
   854
                        object->shader.samplers[curSampler].sampler_name = effect->params[par].value.name;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   855
                        object->shader.samplers[curSampler].sampler_register = pd->symbols[j].register_index;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   856
                        object->shader.samplers[curSampler].sampler_state_count = effect->params[par].value.value_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   857
                        object->shader.samplers[curSampler].sampler_states = effect->params[par].value.valuesSS;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   858
                        curSampler++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   859
                    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   860
                } // for
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   861
                if (pd->preshader)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   862
                {
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   863
                    object->shader.preshader_param_count = pd->preshader->symbol_count;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   864
                    object->shader.preshader_params = (uint32 *) m(object->shader.preshader_param_count * sizeof (uint32), d);
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   865
                    for (j = 0; j < pd->preshader->symbol_count; j++)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   866
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   867
                        object->shader.preshader_params[j] = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   868
                                                                           effect->param_count,
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   869
                                                                           pd->preshader->symbols[j].name);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   870
                    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   871
                } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   872
            }
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   873
        } // if
1186
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   874
        else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   875
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   876
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   877
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   878
              || object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE
b5c8483ff647 Read SYMTYPE_TEXTURE* large objs and read their names (thanks Maik!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1185
diff changeset
   879
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   880
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   881
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   882
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   883
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   884
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   885
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   886
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   887
                char *str = (char *) m(length, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   888
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   889
                object->mapping.name = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   890
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   891
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   892
        else if (object->type != MOJOSHADER_SYMTYPE_VOID) // FIXME: Why? -flibit
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   893
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   894
            assert(0 && "Large object type unknown!");
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   895
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   896
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   897
        /* Object block is always a multiple of four */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   898
        const uint32 blocklen = (length + 3) - ((length - 1) % 4);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   899
        *ptr += blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   900
        *len -= blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   901
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   902
} // readobjects
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   903
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   904
MOJOSHADER_effect *MOJOSHADER_compileEffect(const unsigned char *buf,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   905
                                            const unsigned int _len,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   906
                                            const MOJOSHADER_swizzle *swiz,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   907
                                            const unsigned int swizcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   908
                                            const MOJOSHADER_samplerMap *smap,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   909
                                            const unsigned int smapcount,
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   910
                                            const MOJOSHADER_effectShaderContext *ctx)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   911
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   912
    const uint8 *ptr = (const uint8 *) buf;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   913
    uint32 len = (uint32) _len;
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   914
    ErrorList *errors;
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   915
    MOJOSHADER_malloc m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   916
    MOJOSHADER_free f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   917
    void *d;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   918
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   919
    /* Need a backend! */
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   920
    if (ctx == NULL)
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   921
        return &MOJOSHADER_need_a_backend_effect;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   922
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   923
    /* Supply both m and f, or neither */
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   924
    if ( ((ctx->m == NULL) && (ctx->f != NULL))
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   925
      || ((ctx->m != NULL) && (ctx->f == NULL)) )
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   926
        return &MOJOSHADER_out_of_mem_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   928
    /* Use default malloc/free if m/f were not passed */
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   929
    if (ctx->m == NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   930
        m = MOJOSHADER_internal_malloc;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   931
    else
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   932
        m = ctx->m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   933
    if (ctx->f == NULL)
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   934
        f = MOJOSHADER_internal_free;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   935
    else
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   936
        f = ctx->f;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   937
    d = ctx->malloc_data;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   938
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   939
    /* malloc base effect structure */
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   940
    MOJOSHADER_effect *retval = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect),
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   941
                                                        ctx->malloc_data);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   942
    if (retval == NULL)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   943
        return &MOJOSHADER_out_of_mem_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   944
    memset(retval, '\0', sizeof (*retval));
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   945
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   946
    /* Store ctx in effect structure */
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   947
    memcpy(&retval->ctx, ctx, sizeof(MOJOSHADER_effectShaderContext));
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   948
    retval->ctx.m = m;
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
   949
    retval->ctx.f = f;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   950
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   951
    if (len < 8)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   952
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   953
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   954
    /* Read in header magic, seek to initial offset */
1037
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
   955
    const uint8 *base = NULL;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   956
    uint32 header = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   957
    if (header == 0xBCF00BCF)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   958
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   959
        /* The Effect compiler provided with XNA4 adds some extra mess at the
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   960
         * beginning of the file. It's useless though, so just skip it.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   961
         * -flibit
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   962
         */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   963
        const uint32 skip = readui32(&ptr, &len) - 8;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   964
        ptr += skip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   965
        len += skip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   966
        header = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   967
    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   968
    if (header != 0xFEFF0901)
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   969
    {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   970
        MOJOSHADER_deleteEffect(retval);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   971
        return &MOJOSHADER_not_an_effect_effect;
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
   972
    } // if
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   973
    else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   974
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   975
        const uint32 offset = readui32(&ptr, &len);
1037
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
   976
        base = ptr;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   977
        if (offset > len)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   978
            goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   979
        ptr += offset;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
        len -= offset;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   981
    } // else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   982
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   983
    if (len < 16)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   984
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   985
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   986
    /* Parse structure counts */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   987
    const uint32 numparams = readui32(&ptr, &len);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   988
    const uint32 numtechniques = readui32(&ptr, &len);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   989
    /*const uint32 FIXME =*/ readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   990
    const uint32 numobjects = readui32(&ptr, &len);
1037
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
   991
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   992
    /* Alloc structures now, so object types can be stored */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   993
    retval->object_count = numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   994
    const uint32 siz = sizeof (MOJOSHADER_effectObject) * numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   995
    retval->objects = (MOJOSHADER_effectObject *) m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   996
    if (retval->objects == NULL)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   997
        goto parseEffect_outOfMemory;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   998
    memset(retval->objects, '\0', siz);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   999
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1000
    /* Parse effect parameters */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1001
    retval->param_count = numparams;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1002
    readparameters(numparams, base, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1003
                   &retval->params, retval->objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1004
                   m, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1005
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1006
    /* Parse effect techniques */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1007
    retval->technique_count = numtechniques;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1008
    readtechniques(numtechniques, base, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1009
                   &retval->techniques, retval->objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1010
                   m, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1011
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1012
    /* Initial effect technique/pass */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1013
    retval->current_technique = &retval->techniques[0];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1014
    retval->current_pass = -1;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1015
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1016
    if (len < 8)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1017
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1018
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1019
    /* Parse object counts */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1020
    const int numsmallobjects = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1021
    const int numlargeobjects = readui32(&ptr, &len);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1022
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1023
    errors = errorlist_create(m, f, d);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1024
    if (errors == NULL)
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1025
        goto parseEffect_outOfMemory;
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1026
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1027
    /* Parse "small" object table */
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
  1028
    readsmallobjects(numsmallobjects, &ptr, &len, retval,
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1029
                     swiz, swizcount, smap, smapcount, errors);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1030
    if (errorlist_count(errors) == 0)
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1031
    {
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1032
        /* Parse "large" object table. */
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1033
        readlargeobjects(numlargeobjects, numsmallobjects, &ptr, &len, retval,
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1034
                         swiz, swizcount, smap, smapcount, errors);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1035
    } // if
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1036
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1037
    retval->error_count = errorlist_count(errors);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1038
    retval->errors = errorlist_flatten(errors);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1039
    errorlist_destroy(errors);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1040
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1041
    return retval;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1042
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1043
parseEffect_unexpectedEOF:
1269
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1044
    MOJOSHADER_deleteEffect(retval);
5ed167069622 Improve error handling in Effect implementation
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1247
diff changeset
  1045
    return &MOJOSHADER_unexpected_eof_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1046
parseEffect_outOfMemory:
1245
ad9a16c8b023 Major refactor of the Effects API.
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1237
diff changeset
  1047
    MOJOSHADER_deleteEffect(retval);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1048
    return &MOJOSHADER_out_of_mem_effect;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1049
} // MOJOSHADER_parseEffect
e8988ca01c6d Initial work on parsing binary Effects