mojoshader_effects.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 28 May 2016 15:58:05 -0400
changeset 1164 006194daea99
parent 1156 6e760a19f456
child 1165 0249808e3444
permissions -rw-r--r--
MOJOSHADER_parsePreshader() cleanup. Merged Ethan's copied/improved code with the original, and documented the APIs. Changed MOJOSHADER_freePreshader() to not need explicit deallocators specified.
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
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    15
#include <math.h>
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    16
1038
9b23f9a8aa31 Allow preshader support to be #ifdef'd out, to reduce binary size.
Ryan C. Gordon <icculus@icculus.org>
parents: 1037
diff changeset
    17
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
    18
                             float *outregs)
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    19
{
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    20
    const float *inregs = preshader->registers;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    21
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    22
    // 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
    23
    //  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
    24
    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
    25
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
    26
    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
    27
    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
    28
    {
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
        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
    30
        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
    31
    } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    32
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    33
    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
    34
    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
    35
    const double *src0 = &src[0][0];
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    36
    const double *src1 = &src[1][0];
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    37
    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
    38
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    39
    MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    40
    int instit;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    41
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    42
    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
    43
    {
1046
097aefb03885 Place preshader destination register last in operand list.
Ryan C. Gordon <icculus@icculus.org>
parents: 1045
diff changeset
    44
        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
    45
        const int elems = inst->element_count;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    46
        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
    47
        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
    48
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    49
        assert(elems >= 0);
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    50
        assert(elems <= 4);
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    51
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    52
        // load up our operands...
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    53
        int opiter, elemiter;
1046
097aefb03885 Place preshader destination register last in operand list.
Ryan C. Gordon <icculus@icculus.org>
parents: 1045
diff changeset
    54
        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
    55
        {
1053
513af64373ad Maybe fix scalar operations in preshader interpreter.
Ryan C. Gordon <icculus@icculus.org>
parents: 1047
diff changeset
    56
            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
    57
            const unsigned int index = operand->index;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    58
            switch (operand->type)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    59
            {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    60
                case MOJOSHADER_PRESHADEROPERAND_LITERAL:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    61
                {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    62
                    if (!isscalar)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    63
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    64
                        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
    65
                        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
    66
                    } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    67
                    else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    68
                    {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    69
                        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
    70
                            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
    71
                    } // else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    72
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    73
                } // case
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
                case MOJOSHADER_PRESHADEROPERAND_INPUT:
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    76
                    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
    77
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    78
                        int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    79
                        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
    80
                        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
    81
                        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
    82
                        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    83
                            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
    84
                        }
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    85
                        src[opiter][0] = arrIndex;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    86
                    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
    87
                    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
    88
                        src[opiter][0] = inregs[index];
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    89
                    else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    90
                    {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    91
                        int cpy;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
    92
                        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
    93
                            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
    94
                    } // else
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
    95
                    break;
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
    96
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
    97
                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
    98
                    if (isscalar)
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
    99
                        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
   100
                    else
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   101
                    {
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   102
                        int cpy;
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1041
diff changeset
   103
                        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
   104
                            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
   105
                    } // else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   106
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   107
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   108
                case MOJOSHADER_PRESHADEROPERAND_TEMP:
1063
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   109
                    if (temps != NULL)
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   110
                    {
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   111
                        if (isscalar)
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   112
                            src[opiter][0] = temps[index];
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   113
                        else
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   114
                            memcpy(src[opiter], temps + index, elemsbytes);
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   115
                    } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   116
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   117
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   118
                default:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   119
                    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
   120
                    return;
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   121
            } // switch
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   122
        } // for
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   123
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   124
        // 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
   125
        int i;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   126
        switch (inst->opcode)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   127
        {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   128
            #define OPCODE_CASE(op, val) \
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   129
                case MOJOSHADER_PRESHADEROP_##op: \
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   130
                    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
   131
                    break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   132
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   133
            //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
   134
            OPCODE_CASE(MOV, src0[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   135
            OPCODE_CASE(NEG, -src0[i])
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   136
            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
   137
            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
   138
            OPCODE_CASE(EXP, exp(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   139
            OPCODE_CASE(LOG, log(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   140
            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
   141
            OPCODE_CASE(SIN, sin(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   142
            OPCODE_CASE(COS, cos(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   143
            OPCODE_CASE(ASIN, asin(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   144
            OPCODE_CASE(ACOS, acos(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   145
            OPCODE_CASE(ATAN, atan(src0[i]))
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   146
            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
   147
            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
   148
            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
   149
            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
   150
            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
   151
            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
   152
            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
   153
            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
   154
            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
   155
            //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
   156
            //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
   157
            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
   158
            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
   159
            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
   160
            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
   161
            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
   162
            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
   163
            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
   164
            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
   165
            //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
   166
            //OPCODE_CASE(NOISE_SCALAR, ???)  // !!! FIXME: ?
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   167
            #undef OPCODE_CASE
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   168
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   169
            case MOJOSHADER_PRESHADEROP_DOT:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   170
            {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   171
                double final = 0.0;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   172
                for (i = 0; i < elems; i++)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   173
                    final += src0[i] * src1[i];
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   174
                for (i = 0; i < elems; i++)
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   175
                    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
   176
                break;
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   177
            } // case
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   178
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   179
            default:
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   180
                assert(0 && "Unhandled preshader opcode!");
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   181
                break;
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   182
        } // switch
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
        // 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
   185
        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
   186
        {
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   187
            assert(preshader->temp_count >=
b10281d6eb45 Several preshader fixes and static analysis tweaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
   188
                    operand->index + (elemsbytes / sizeof (double)));
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   189
            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
   190
        } // if
1036
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   191
        else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   192
        {
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   193
            assert(operand->type == MOJOSHADER_PRESHADEROPERAND_OUTPUT);
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   194
            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
   195
                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
   196
        } // else
8a3597b44ff7 First shot at preshader interpreter. Completely untested!
Ryan C. Gordon <icculus@icculus.org>
parents: 1021
diff changeset
   197
    } // 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
   198
} // MOJOSHADER_runPreshader
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
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
   201
    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
   202
};
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
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
   205
{
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    uint32 retval = 0;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    if (*_len < sizeof (retval))
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
        *_len = 0;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
    else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        const uint32 *ptr = (const uint32 *) *_ptr;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
        retval = SWAP32(*ptr);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        *_ptr += sizeof (retval);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        *_len -= sizeof (retval);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    } // else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    return retval;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
} // readui32
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   219
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
   220
                        const uint32 offset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   221
                        MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   222
                        void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   223
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   224
    // !!! FIXME: sanity checks!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   225
    // !!! 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
   226
    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
   227
    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
   228
    char *strptr = NULL;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   229
    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
   230
    strptr = (char *) m(len, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   231
    memcpy(strptr, str + 4, len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   232
    return strptr;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   233
} // readstring
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   234
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   235
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
   236
                         const uint32 param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   237
                         const char *name)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   238
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   239
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   240
    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
   241
        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
   242
            return i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   243
    assert(0 && "Parameter not found!");
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   244
}
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   245
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   246
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
   247
                      const uint32 typeoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   248
                      const uint32 valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   249
                      MOJOSHADER_effectValue *value,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   250
                      MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   251
                      MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   252
                      void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   253
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   254
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   255
    const uint8 *typeptr = base + typeoffset;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   256
    const uint8 *valptr = base + valoffset;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   257
    unsigned int typelen = 9999999;  // !!! FIXME
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   258
    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
   259
    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
   260
    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
   261
    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
   262
    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
   263
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   264
    value->type.parameter_type = (MOJOSHADER_symbolType) type;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   265
    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
   266
    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
   267
    value->semantic = readstring(base, semantic, m, d);
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   268
    value->type.elements = numelements;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   269
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   270
    /* Class sanity check */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   271
    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
   272
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   273
    if (valclass == MOJOSHADER_SYMCLASS_SCALAR
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   274
     || valclass == MOJOSHADER_SYMCLASS_VECTOR
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   275
     || valclass == MOJOSHADER_SYMCLASS_MATRIX_ROWS
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   276
     || valclass == MOJOSHADER_SYMCLASS_MATRIX_COLUMNS)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   277
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   278
        /* 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
   279
        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
   280
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   281
        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
   282
        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
   283
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   284
        value->type.columns = columncount;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   285
        value->type.rows = rowcount;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   286
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   287
        uint32 siz = columncount * rowcount;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   288
        if (numelements > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   289
            siz *= numelements;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   290
        value->value_count = siz;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   291
        siz *= 4;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   292
        value->values = m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   293
        memcpy(value->values, valptr, siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   294
    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   295
    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
   296
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   297
        /* 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
   298
        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
   299
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   300
        if (type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   301
         || type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   302
         || type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   303
         || type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   304
         || type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   305
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   306
            unsigned int vallen = 9999999; // !!! FIXME
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   307
            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
   308
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   309
            value->value_count = numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   310
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   311
            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
   312
            value->values = m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   313
            memset(value->values, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   314
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   315
            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
   316
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   317
                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
   318
                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
   319
                /*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
   320
                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
   321
                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
   322
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   323
                state->type = (MOJOSHADER_samplerStateType) stype;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   324
                readvalue(base, statetypeoffset, statevaloffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   325
                          &state->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   326
                          m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   327
                if (stype == MOJOSHADER_SAMP_TEXTURE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   328
                    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
   329
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   330
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   331
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   332
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   333
            uint32 numobjects = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   334
            if (numelements > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   335
                numobjects = numelements;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   336
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   337
            value->value_count = numobjects;
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
            const uint32 siz = 4 * numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   340
            value->values = m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   341
            memcpy(value->values, valptr, siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   342
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   343
            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
   344
                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
   345
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   346
    } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   347
    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
   348
    {
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   349
        uint32 siz;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   350
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   351
        value->type.member_count = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   352
        siz = value->type.member_count * sizeof (MOJOSHADER_symbolStructMember);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   353
        value->type.members = (MOJOSHADER_symbolStructMember *) m(siz, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   354
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   355
        uint32 structsize = 0;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   356
        for (i = 0; i < value->type.member_count; i++)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   357
        {
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   358
            MOJOSHADER_symbolStructMember *mem = &value->type.members[i];
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   359
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   360
            mem->info.parameter_type = (MOJOSHADER_symbolType) readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   361
            mem->info.parameter_class = (MOJOSHADER_symbolClass) readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   362
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   363
            const uint32 memname = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   364
            /*const uint32 memsemantic =*/ readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   365
            mem->name = readstring(base, memname, m, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   366
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   367
            mem->info.elements = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   368
            mem->info.columns = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   369
            mem->info.rows = readui32(&typeptr, &typelen);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   370
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   371
            // !!! FIXME: Nested structs! -flibit
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   372
            assert(mem->info.parameter_class >= MOJOSHADER_SYMCLASS_SCALAR
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   373
                && mem->info.parameter_class <= MOJOSHADER_SYMCLASS_VECTOR);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   374
            assert(mem->info.parameter_type >= MOJOSHADER_SYMTYPE_BOOL
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   375
                && mem->info.parameter_type <= MOJOSHADER_SYMTYPE_FLOAT);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   376
            mem->info.member_count = 0;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   377
            mem->info.members = NULL;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   378
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   379
            uint32 memsize = mem->info.columns * mem->info.rows;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   380
            if (mem->info.elements > 0)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   381
                memsize *= mem->info.elements;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   382
            structsize += memsize;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   383
        } // for
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   384
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   385
        value->type.columns = structsize;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   386
        value->type.rows = 1;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   387
        value->value_count = structsize;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   388
        if (numelements > 0)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   389
            value->value_count *= numelements;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   390
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   391
        siz = value->value_count * 4;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   392
        value->values = m(siz, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   393
        memcpy(value->values, typeptr, siz); /* Yes, typeptr. -flibit */
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   394
    } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   395
} // readvalue
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   396
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   397
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
   398
                            const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   399
                            const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   400
                            uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   401
                            MOJOSHADER_effectAnnotation **annotations,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   402
                            MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   403
                            MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   404
                            void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   405
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   406
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   407
    if (numannos == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   408
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   409
    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
   410
    *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
   411
    memset(*annotations, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   412
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   413
    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
   414
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   415
        MOJOSHADER_effectAnnotation *anno = &(*annotations)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   416
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   417
        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
   418
        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
   419
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   420
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   421
                  anno, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   422
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   423
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   424
} // readannotation
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   425
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   426
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
   427
                           const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   428
                           const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   429
                           uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   430
                           MOJOSHADER_effectParam **params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   431
                           MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   432
                           MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   433
                           void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   434
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   435
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   436
    if (numparams == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   437
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   438
    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
   439
    *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
   440
    memset(*params, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   441
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   442
    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
   443
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   444
        MOJOSHADER_effectParam *param = &(*params)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   445
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   446
        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
   447
        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
   448
        /*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
   449
        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
   450
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   451
        param->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   452
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   453
                        &param->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   454
                        m, d);
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
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   457
                  &param->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   458
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   459
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   460
} // readparameters
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   461
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   462
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
   463
                       const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   464
                       const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   465
                       uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   466
                       MOJOSHADER_effectState **states,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   467
                       MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   468
                       MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   469
                       void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   470
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   471
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   472
    if (numstates == 0) return;
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
    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
   475
    *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
   476
    memset(*states, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   477
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   478
    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
   479
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   480
        MOJOSHADER_effectState *state = &(*states)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   481
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   482
        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
   483
        /*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
   484
        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
   485
        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
   486
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   487
        state->type = (MOJOSHADER_renderStateType) type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   488
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   489
                  &state->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   490
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   491
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   492
} // readstates
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
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
   495
                       const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   496
                       const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   497
                       uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   498
                       MOJOSHADER_effectPass **passes,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   499
                       MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   500
                       MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   501
                       void *d)
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
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   504
    if (numpasses == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   505
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   506
    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
   507
    *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
   508
    memset(*passes, '\0', siz);
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
    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
   511
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   512
        MOJOSHADER_effectPass *pass = &(*passes)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   513
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   514
        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
   515
        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
   516
        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
   517
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   518
        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
   519
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   520
        pass->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   521
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   522
                        &pass->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   523
                        m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   524
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   525
        pass->state_count = numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   526
        readstates(numstates, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   527
                   &pass->states, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   528
                   m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   529
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   530
} // readpasses
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   531
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   532
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
   533
                           const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   534
                           const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   535
                           uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   536
                           MOJOSHADER_effectTechnique **techniques,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   537
                           MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   538
                           MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   539
                           void *d)
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
{
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   541
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   542
    if (numtechniques == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   543
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   544
    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
   545
    *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
   546
    memset(*techniques, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   547
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   548
    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
   549
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   550
        MOJOSHADER_effectTechnique *technique = &(*techniques)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   551
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   552
        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
   553
        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
   554
        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
   555
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   556
        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
   557
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   558
        technique->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   559
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   560
                        &technique->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   561
                        m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   562
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   563
        technique->pass_count = numpasses;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   564
        readpasses(numpasses, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   565
                   &technique->passes, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   566
                   m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   567
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   568
} // readtechniques
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   569
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   570
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
   571
                             const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   572
                             uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   573
                             MOJOSHADER_effect *effect,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   574
                             const char *profile,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   575
                             const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   576
                             const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   577
                             const MOJOSHADER_samplerMap *smap,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   578
                             const unsigned int smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   579
                             MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   580
                             MOJOSHADER_free f,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   581
                             void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   582
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   583
    int i, j;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   584
    if (numsmallobjects == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   585
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   586
    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
   587
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   588
        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
   589
        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
   590
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   591
        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
   592
        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
   593
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   594
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   595
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   596
                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
   597
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   598
                object->string.string = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   599
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   600
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   601
        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
   602
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   603
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   604
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   605
              || object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE)
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
            // No-op. Why is this even here?
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   608
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   609
        else if (object->type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   610
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   611
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   612
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   613
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
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
            if (length > 0)
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
                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
   618
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   619
                object->mapping.name = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   620
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   621
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   622
        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
   623
              || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   624
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   625
            object->shader.technique = -1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   626
            object->shader.pass = -1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   627
            object->shader.shader = MOJOSHADER_parse(profile, *ptr, length,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   628
                                                     swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   629
                                                     m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   630
            // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   631
            for (j = 0; j < object->shader.shader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   632
                if (object->shader.shader->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   633
                    object->shader.sampler_count++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   634
            object->shader.param_count = object->shader.shader->symbol_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   635
            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
   636
            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
   637
            uint32 curSampler = 0;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   638
            for (j = 0; j < object->shader.shader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   639
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   640
                int par = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   641
                                        effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   642
                                        object->shader.shader->symbols[j].name);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   643
                object->shader.params[j] = par;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   644
                if (object->shader.shader->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
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
                    object->shader.samplers[curSampler].sampler_name = object->shader.shader->symbols[j].name;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   647
                    object->shader.samplers[curSampler].sampler_register = object->shader.shader->symbols[j].register_index;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   648
                    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
   649
                    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
   650
                    curSampler++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   651
                } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   652
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   653
            if (object->shader.shader->preshader)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   654
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   655
                object->shader.preshader_param_count = object->shader.shader->preshader->symbol_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   656
                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
   657
                for (j = 0; j < object->shader.shader->preshader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   658
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   659
                    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
   660
                                                                       effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   661
                                                                       object->shader.shader->preshader->symbols[j].name);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   662
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   663
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   664
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   665
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   666
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   667
            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
   668
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   669
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   670
        /* 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
   671
        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
   672
        *ptr += blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   673
        *len -= blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   674
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   675
} // readstrings
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   676
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   677
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
   678
                             const uint32 numsmallobjects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   679
                             const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   680
                             uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   681
                             MOJOSHADER_effect *effect,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   682
                             const char *profile,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   683
                             const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   684
                             const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   685
                             const MOJOSHADER_samplerMap *smap,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   686
                             const unsigned int smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   687
                             MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   688
                             MOJOSHADER_free f,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   689
                             void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   690
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   691
    int i, j;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   692
    if (numlargeobjects == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   693
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   694
    int numobjects = numsmallobjects + numlargeobjects + 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   695
    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
   696
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   697
        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
   698
        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
   699
        /*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
   700
        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
   701
        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
   702
        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
   703
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   704
        uint32 objectIndex;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   705
        if (technique == -1)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   706
            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
   707
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   708
            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
   709
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   710
        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
   711
        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
   712
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   713
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   714
            object->shader.technique = technique;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   715
            object->shader.pass = index;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   716
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   717
            const char *emitter = profile;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   718
            if (type == 2)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   719
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   720
                /* This is a standalone preshader!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   721
                 * 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
   722
                 * vertex/fragment shader.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   723
                 */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   724
                object->shader.is_preshader = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   725
                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
   726
                const uint32 end = 24; // FIXME: Why? -flibit
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   727
                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
   728
                object->shader.param_count = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   729
                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
   730
                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
   731
                                                         effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   732
                                                         array);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   733
                f((void *) array, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   734
                object->shader.preshader = MOJOSHADER_parsePreshader(*ptr + start, length - end,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   735
                                                                     m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   736
                // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   737
                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
   738
                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
   739
                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
   740
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   741
                    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
   742
                                                                       effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   743
                                                                       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
   744
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   745
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   746
            else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   747
            {
1156
6e760a19f456 Added support for producing shader language source code for Apple's Metal API.
Ryan C. Gordon <icculus@icculus.org>
parents: 1152
diff changeset
   748
                object->shader.shader = MOJOSHADER_parse(emitter, NULL, *ptr, length,
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   749
                                                         swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   750
                                                         m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   751
                // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   752
                for (j = 0; j < object->shader.shader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   753
                    if (object->shader.shader->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   754
                        object->shader.sampler_count++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   755
                object->shader.param_count = object->shader.shader->symbol_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   756
                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
   757
                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
   758
                uint32 curSampler = 0;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   759
                for (j = 0; j < object->shader.shader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   760
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   761
                    int par = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   762
                                            effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   763
                                            object->shader.shader->symbols[j].name);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   764
                    object->shader.params[j] = par;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   765
                    if (object->shader.shader->symbols[j].register_set == MOJOSHADER_SYMREGSET_SAMPLER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   766
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   767
                        object->shader.samplers[curSampler].sampler_name = object->shader.shader->symbols[j].name;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   768
                        object->shader.samplers[curSampler].sampler_register = object->shader.shader->symbols[j].register_index;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   769
                        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
   770
                        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
   771
                        curSampler++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   772
                    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   773
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   774
                if (object->shader.shader->preshader)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   775
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   776
                    object->shader.preshader_param_count = object->shader.shader->preshader->symbol_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   777
                    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
   778
                    for (j = 0; j < object->shader.shader->preshader->symbol_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   779
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   780
                        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
   781
                                                                           effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   782
                                                                           object->shader.shader->preshader->symbols[j].name);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   783
                    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   784
                } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   785
            }
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   786
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   787
        else if (object->type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   788
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   789
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   790
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   791
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
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
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   794
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   795
                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
   796
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   797
                object->mapping.name = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   798
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   799
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   800
        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
   801
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   802
            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
   803
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   804
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   805
        /* 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
   806
        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
   807
        *ptr += blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   808
        *len -= blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   809
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   810
} // readobjects
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   811
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   812
MOJOSHADER_effect *MOJOSHADER_parseEffect(const char *profile,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   813
                                          const unsigned char *buf,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   814
                                          const unsigned int _len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   815
                                          const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   816
                                          const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   817
                                          const MOJOSHADER_samplerMap *smap,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   818
                                          const unsigned int smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   819
                                          MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   820
                                          MOJOSHADER_free f,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   821
                                          void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   822
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   823
    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
   824
    uint32 len = (uint32) _len;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   825
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   826
    /* Supply both m and f, or neither */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   827
    if ( ((m == NULL) && (f != NULL)) || ((m != NULL) && (f == NULL)) )
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   828
        return &MOJOSHADER_out_of_mem_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   830
    /* Use default malloc/free if m/f were not passed */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   831
    if (m == NULL) m = MOJOSHADER_internal_malloc;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   832
    if (f == NULL) f = MOJOSHADER_internal_free;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   833
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   834
    /* malloc base effect structure */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   835
    MOJOSHADER_effect *retval = (MOJOSHADER_effect *) m(sizeof (MOJOSHADER_effect), d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
    if (retval == NULL)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   837
        return &MOJOSHADER_out_of_mem_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   838
    memset(retval, '\0', sizeof (*retval));
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   839
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   840
    /* Store m/f/d in effect structure */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   841
    retval->malloc = m;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   842
    retval->free = f;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   843
    retval->malloc_data = d;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   844
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   845
    if (len < 8)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   846
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   847
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   848
    /* 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
   849
    const uint8 *base = NULL;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   850
    uint32 header = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   851
    if (header == 0xBCF00BCF)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   852
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   853
        /* 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
   854
         * 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
   855
         * -flibit
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   856
         */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   857
        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
   858
        ptr += skip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   859
        len += skip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   860
        header = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   861
    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   862
    if (header != 0xFEFF0901)
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   863
        goto parseEffect_notAnEffectsFile;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   864
    else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   865
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
        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
   867
        base = ptr;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   868
        if (offset > len)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   869
            goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
        ptr += offset;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   871
        len -= offset;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   872
    } // else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   873
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   874
    if (len < 16)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   875
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   876
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   877
    /* Parse structure counts */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
    const uint32 numparams = readui32(&ptr, &len);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   879
    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
   880
    /*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
   881
    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
   882
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   883
    /* 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
   884
    retval->object_count = numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   885
    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
   886
    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
   887
    if (retval->objects == NULL)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   888
        goto parseEffect_outOfMemory;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   889
    memset(retval->objects, '\0', siz);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   890
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   891
    /* Parse effect parameters */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   892
    retval->param_count = numparams;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   893
    readparameters(numparams, base, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   894
                   &retval->params, retval->objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   895
                   m, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   897
    /* Parse effect techniques */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   898
    retval->technique_count = numtechniques;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   899
    readtechniques(numtechniques, base, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   900
                   &retval->techniques, retval->objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   901
                   m, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   902
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   903
    /* Initial effect technique/pass */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   904
    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
   905
    retval->current_pass = -1;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   906
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   907
    if (len < 8)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   908
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   909
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   910
    /* Parse object counts */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   911
    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
   912
    const int numlargeobjects = readui32(&ptr, &len);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   913
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   914
    /* Parse "small" object table */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   915
    readsmallobjects(numsmallobjects, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   916
                     retval,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   917
                     profile, swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   918
                     m, f, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   920
    /* Parse "large" object table. */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   921
    readlargeobjects(numlargeobjects, numsmallobjects, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   922
                     retval,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   923
                     profile, swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   924
                     m, f, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   925
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   926
    /* Store MojoShader profile in effect structure */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
    retval->profile = (char *) m(strlen(profile) + 1, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   928
    if (retval->profile == NULL)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   929
        goto parseEffect_outOfMemory;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   930
    strcpy((char *) retval->profile, profile);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   931
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
    return retval;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   934
// !!! FIXME: do something with this.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   935
parseEffect_notAnEffectsFile:
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   936
parseEffect_unexpectedEOF:
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   937
parseEffect_outOfMemory:
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   938
    MOJOSHADER_freeEffect(retval);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   939
    return &MOJOSHADER_out_of_mem_effect;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   940
} // MOJOSHADER_parseEffect
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   941
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   942
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   943
void freetypeinfo(MOJOSHADER_symbolTypeInfo *typeinfo,
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   944
                  MOJOSHADER_free f, void *d)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   945
{
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   946
    int i;
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   947
    for (i = 0; i < typeinfo->member_count; i++)
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   948
    {
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   949
        f((void *) typeinfo->members[i].name, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   950
        freetypeinfo(&typeinfo->members[i].info, f, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   951
    } // for
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   952
    f((void *) typeinfo->members, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   953
} // freetypeinfo
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   954
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   955
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   956
void freevalue(MOJOSHADER_effectValue *value, MOJOSHADER_free f, void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   957
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   958
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   959
    f((void *) value->name, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   960
    f((void *) value->semantic, d);
1152
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   961
    freetypeinfo(&value->type, f, d);
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   962
    if (value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   963
     || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER1D
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   964
     || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER2D
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   965
     || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER3D
8f7653f0dc37 Effect struct parameter support
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1150
diff changeset
   966
     || value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   967
        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
   968
            freevalue(&value->valuesSS[i].value, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   969
    f(value->values, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   970
} // freevalue
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   971
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   972
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   973
void MOJOSHADER_freeEffect(const MOJOSHADER_effect *_effect)
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
    MOJOSHADER_effect *effect = (MOJOSHADER_effect *) _effect;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   976
    if ((effect == NULL) || (effect == &MOJOSHADER_out_of_mem_effect))
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   977
        return;  // no-op.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   978
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   979
    MOJOSHADER_free f = effect->free;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   980
    void *d = effect->malloc_data;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   981
    int i, j, k;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   982
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   983
    /* Free errors */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   984
    for (i = 0; i < effect->error_count; i++)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   985
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   986
        f((void *) effect->errors[i].error, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   987
        f((void *) effect->errors[i].filename, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   988
    } // for
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   989
    f((void *) effect->errors, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   990
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   991
    /* Free profile string */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   992
    f((void *) effect->profile, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   993
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   994
    /* Free parameters, including annotations */
1037
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
   995
    for (i = 0; i < effect->param_count; i++)
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
   996
    {
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   997
        MOJOSHADER_effectParam *param = &effect->params[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   998
        freevalue(&param->value, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   999
        for (j = 0; j < param->annotation_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1000
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1001
            freevalue(&param->annotations[j], f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1002
        } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1003
        f((void *) param->annotations, d);
1037
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
  1004
    } // for
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1005
    f((void *) effect->params, d);
1037
b102a563d9cb Filled in and fixed some stuff in the Effect parser.
Ryan C. Gordon <icculus@icculus.org>
parents: 1036
diff changeset
  1006
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1007
    /* Free techniques, including passes and all annotations */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1008
    for (i = 0; i < effect->technique_count; i++)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1009
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1010
        MOJOSHADER_effectTechnique *technique = &effect->techniques[i];
1021
cad933999680 Fixed memory leak in MOJOSHADER_freeEffect().
Ryan C. Gordon <icculus@icculus.org>
parents: 1019
diff changeset
  1011
        f((void *) technique->name, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1012
        for (j = 0; j < technique->pass_count; j++)
1021
cad933999680 Fixed memory leak in MOJOSHADER_freeEffect().
Ryan C. Gordon <icculus@icculus.org>
parents: 1019
diff changeset
  1013
        {
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1014
            MOJOSHADER_effectPass *pass = &technique->passes[j];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1015
            f((void *) pass->name, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1016
            for (k = 0; k < pass->state_count; k++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1017
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1018
                freevalue(&pass->states[k].value, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1019
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1020
            f((void *) pass->states, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1021
            for (k = 0; k < pass->annotation_count; k++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1022
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1023
                freevalue(&pass->annotations[k], f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1024
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1025
            f((void *) pass->annotations, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1026
        } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1027
        f((void *) technique->passes, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1028
        for (j = 0; j < technique->annotation_count; j++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1029
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1030
            freevalue(&technique->annotations[j], f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1031
        } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1032
        f((void *) technique->annotations, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1033
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1034
    f((void *) effect->techniques, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1035
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1036
    /* Free object table */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1037
    for (i = 0; i < effect->object_count; i++)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1038
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1039
        MOJOSHADER_effectObject *object = &effect->objects[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1040
        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
  1041
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1042
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1043
            if (object->shader.is_preshader)
1164
006194daea99 MOJOSHADER_parsePreshader() cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
  1044
                MOJOSHADER_freePreshader(object->shader.preshader);
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1045
            else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1046
                MOJOSHADER_freeParseData(object->shader.shader);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1047
            f((void *) object->shader.params, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1048
            f((void *) object->shader.samplers, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1049
            f((void *) object->shader.preshader_params, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1050
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1051
        else if (object->type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1052
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1053
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1054
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1055
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1056
            f((void *) object->mapping.name, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1057
        else 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
  1058
            f((void *) object->string.string, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1059
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: