mojoshader_effects.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Fri, 01 Jan 2016 02:12:19 -0500
changeset 1150 02c0f0afb39a
parent 1104 9147482e1ec7
child 1152 8f7653f0dc37
permissions -rw-r--r--
- Add ability to build MojoShader as a shared library - Off by default, turn on BUILD_SHARED in CMake - Add ability to disable shader profiles from CMake - Add MOJOSHADER_glSetVertexAttribDivisor - Behaves like glSetVertexAttribute, but for glVertexAttribDivisor - Add MOJOSHADER_glGetVertexAttribLocation - Allows user to directly call glVertexAttribPointer in their program - Full Effect framework support - API is in a separate header, see mojoshader_effects.h for details - Off by default, turn on EFFECT_SUPPORT in CMake - #define MOJOSHADER_EFFECT_SUPPORT before including mojoshader.h - Add MOJOSHADER_PROFILE_GLSLES shader profile - Basically just GLSL120 without using builtins - Add ability to flip gl_Position via MOJOSHADER_glProgramViewportFlip - You probably shouldn't use this unless you're a project like FNA. - Off by default, turn on FLIP_VIEWPORT in CMake - #define MOJOSHADER_FLIP_RENDERTARGET before including mojoshader.h - Add ability to adjust Z coordinates for [0, 1]-like depth clipping - You probably shouldn't use this unless you're a project like FNA. - Off by default, turn on DEPTH_CLIPPING in CMake - Add ability to build MojoShader with XNA4 vertex texturing behavior - You probably shouldn't use this unless you're reimplementing XNA. - Off by default, turn on XNA4_VERTEXTEXTURE in CMake - Add DECLSPEC/MOJOSHADERCALL to functions/function pointers - This allows for easier interop with other languages, i.e. C# P/Invoke - Fix printing float values for locales where radix is not '.' - Various minor bugfixes, optimizations, Win32 buildfixes, etc.
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
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   264
    value->value_type = (MOJOSHADER_symbolType) type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   265
    value->value_class = (MOJOSHADER_symbolClass) valclass;
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);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   268
    value->element_count = numelements;
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
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   284
        value->column_count = columncount;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   285
        value->row_count = rowcount;
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
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   349
        /* TODO: Maybe this is like parse_ctab_typeinfo? -flibit */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   350
        assert(0 && "Effect struct value parsing not implemented!");
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   351
    } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   352
} // readvalue
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   353
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   354
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
   355
                            const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   356
                            const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   357
                            uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   358
                            MOJOSHADER_effectAnnotation **annotations,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   359
                            MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   360
                            MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   361
                            void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   362
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   363
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   364
    if (numannos == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   365
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   366
    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
   367
    *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
   368
    memset(*annotations, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   369
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   370
    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
   371
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   372
        MOJOSHADER_effectAnnotation *anno = &(*annotations)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   373
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   374
        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
   375
        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
   376
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   377
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   378
                  anno, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   379
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   380
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   381
} // readannotation
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   382
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   383
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
   384
                           const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   385
                           const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   386
                           uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   387
                           MOJOSHADER_effectParam **params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   388
                           MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   389
                           MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   390
                           void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   391
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   392
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   393
    if (numparams == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   394
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   395
    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
   396
    *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
   397
    memset(*params, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   398
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   399
    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
   400
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   401
        MOJOSHADER_effectParam *param = &(*params)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   402
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   403
        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
   404
        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
   405
        /*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
   406
        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
   407
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   408
        param->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   409
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   410
                        &param->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   411
                        m, d);
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
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   414
                  &param->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   415
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   416
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   417
} // readparameters
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   418
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   419
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
   420
                       const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   421
                       const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   422
                       uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   423
                       MOJOSHADER_effectState **states,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   424
                       MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   425
                       MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   426
                       void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   427
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   428
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   429
    if (numstates == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   430
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   431
    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
   432
    *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
   433
    memset(*states, '\0', siz);
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
    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
   436
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   437
        MOJOSHADER_effectState *state = &(*states)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   438
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   439
        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
   440
        /*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
   441
        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
   442
        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
   443
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   444
        state->type = (MOJOSHADER_renderStateType) type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   445
        readvalue(base, typeoffset, valoffset,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   446
                  &state->value, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   447
                  m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   448
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   449
} // readstates
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
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
   452
                       const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   453
                       const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   454
                       uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   455
                       MOJOSHADER_effectPass **passes,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   456
                       MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   457
                       MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   458
                       void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   459
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   460
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   461
    if (numpasses == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   462
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   463
    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
   464
    *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
   465
    memset(*passes, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   466
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   467
    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
   468
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   469
        MOJOSHADER_effectPass *pass = &(*passes)[i];
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
        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
   472
        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
   473
        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
   474
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   475
        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
   476
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   477
        pass->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   478
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   479
                        &pass->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   480
                        m, d);
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
        pass->state_count = numstates;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   483
        readstates(numstates, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   484
                   &pass->states, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   485
                   m, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   486
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   487
} // readpasses
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   488
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   489
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
   490
                           const uint8 *base,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   491
                           const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   492
                           uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   493
                           MOJOSHADER_effectTechnique **techniques,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   494
                           MOJOSHADER_effectObject *objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   495
                           MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   496
                           void *d)
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
{
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   498
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   499
    if (numtechniques == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   500
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   501
    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
   502
    *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
   503
    memset(*techniques, '\0', siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   504
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   505
    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
   506
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   507
        MOJOSHADER_effectTechnique *technique = &(*techniques)[i];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   508
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   509
        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
   510
        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
   511
        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
   512
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   513
        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
   514
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   515
        technique->annotation_count = numannos;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   516
        readannotations(numannos, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   517
                        &technique->annotations, objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   518
                        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
        technique->pass_count = numpasses;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   521
        readpasses(numpasses, base, ptr, len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   522
                   &technique->passes, 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
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   525
} // readtechniques
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   526
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   527
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
   528
                             const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   529
                             uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   530
                             MOJOSHADER_effect *effect,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   531
                             const char *profile,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   532
                             const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   533
                             const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   534
                             const MOJOSHADER_samplerMap *smap,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   535
                             const unsigned int smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   536
                             MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   537
                             MOJOSHADER_free f,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   538
                             void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   539
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   540
    int i, j;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   541
    if (numsmallobjects == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   542
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   543
    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
   544
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   545
        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
   546
        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
   547
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   548
        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
   549
        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
   550
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   551
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   552
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   553
                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
   554
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   555
                object->string.string = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   556
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   557
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   558
        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
   559
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   560
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   561
              || object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   562
              || object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   563
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   564
            // 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
   565
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   566
        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
   567
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   568
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   569
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   570
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   571
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   572
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   573
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   574
                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
   575
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   576
                object->mapping.name = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   577
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   578
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   579
        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
   580
              || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   581
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   582
            object->shader.technique = -1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   583
            object->shader.pass = -1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   584
            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
   585
                                                     swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   586
                                                     m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   587
            // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   588
            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
   589
                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
   590
                    object->shader.sampler_count++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   591
            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
   592
            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
   593
            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
   594
            uint32 curSampler = 0;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   595
            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
   596
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   597
                int par = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   598
                                        effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   599
                                        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
   600
                object->shader.params[j] = par;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   601
                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
   602
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   603
                    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
   604
                    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
   605
                    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
   606
                    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
   607
                    curSampler++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   608
                } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   609
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   610
            if (object->shader.shader->preshader)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   611
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   612
                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
   613
                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
   614
                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
   615
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   616
                    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
   617
                                                                       effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   618
                                                                       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
   619
                } // for
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
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   623
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   624
            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
   625
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   626
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   627
        /* 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
   628
        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
   629
        *ptr += blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   630
        *len -= blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   631
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   632
} // readstrings
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   633
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   634
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
   635
                             const uint32 numsmallobjects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   636
                             const uint8 **ptr,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   637
                             uint32 *len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   638
                             MOJOSHADER_effect *effect,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   639
                             const char *profile,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   640
                             const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   641
                             const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   642
                             const MOJOSHADER_samplerMap *smap,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   643
                             const unsigned int smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   644
                             MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   645
                             MOJOSHADER_free f,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   646
                             void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   647
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   648
    int i, j;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   649
    if (numlargeobjects == 0) return;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   650
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   651
    int numobjects = numsmallobjects + numlargeobjects + 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   652
    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
   653
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   654
        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
   655
        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
   656
        /*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
   657
        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
   658
        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
   659
        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
   660
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   661
        uint32 objectIndex;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   662
        if (technique == -1)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   663
            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
   664
        else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   665
            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
   666
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   667
        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
   668
        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
   669
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   670
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   671
            object->shader.technique = technique;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   672
            object->shader.pass = index;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   673
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   674
            const char *emitter = profile;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   675
            if (type == 2)
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
                /* This is a standalone preshader!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   678
                 * 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
   679
                 * vertex/fragment shader.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   680
                 */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   681
                object->shader.is_preshader = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   682
                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
   683
                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
   684
                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
   685
                object->shader.param_count = 1;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   686
                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
   687
                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
   688
                                                         effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   689
                                                         array);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   690
                f((void *) array, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   691
                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
   692
                                                                     m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   693
                // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   694
                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
   695
                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
   696
                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
   697
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   698
                    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
   699
                                                                       effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   700
                                                                       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
   701
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   702
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   703
            else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   704
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   705
                object->shader.shader = MOJOSHADER_parse(emitter, *ptr, length,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   706
                                                         swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   707
                                                         m, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   708
                // !!! FIXME: check for errors.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   709
                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
   710
                    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
   711
                        object->shader.sampler_count++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   712
                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
   713
                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
   714
                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
   715
                uint32 curSampler = 0;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   716
                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
   717
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   718
                    int par = findparameter(effect->params,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   719
                                            effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   720
                                            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
   721
                    object->shader.params[j] = par;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   722
                    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
   723
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   724
                        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
   725
                        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
   726
                        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
   727
                        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
   728
                        curSampler++;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   729
                    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   730
                } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   731
                if (object->shader.shader->preshader)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   732
                {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   733
                    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
   734
                    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
   735
                    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
   736
                    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   737
                        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
   738
                                                                           effect->param_count,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   739
                                                                           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
   740
                    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   741
                } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   742
            }
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   743
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   744
        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
   745
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   746
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   747
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   748
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   749
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   750
            if (length > 0)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   751
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   752
                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
   753
                memcpy(str, *ptr, length);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   754
                object->mapping.name = str;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   755
            } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   756
        } // else if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   757
        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
   758
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   759
            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
   760
        } // else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   761
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   762
        /* 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
   763
        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
   764
        *ptr += blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   765
        *len -= blocklen;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   766
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   767
} // readobjects
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   768
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   769
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
   770
                                          const unsigned char *buf,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   771
                                          const unsigned int _len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   772
                                          const MOJOSHADER_swizzle *swiz,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   773
                                          const unsigned int swizcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   774
                                          const MOJOSHADER_samplerMap *smap,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   775
                                          const unsigned int smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   776
                                          MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   777
                                          MOJOSHADER_free f,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   778
                                          void *d)
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
    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
   781
    uint32 len = (uint32) _len;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   782
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   783
    /* 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
   784
    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
   785
        return &MOJOSHADER_out_of_mem_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   787
    /* 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
   788
    if (m == NULL) m = MOJOSHADER_internal_malloc;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
    if (f == NULL) f = MOJOSHADER_internal_free;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   790
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   791
    /* malloc base effect structure */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   792
    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
   793
    if (retval == NULL)
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   794
        return &MOJOSHADER_out_of_mem_effect;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
    memset(retval, '\0', sizeof (*retval));
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   796
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   797
    /* 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
   798
    retval->malloc = m;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   799
    retval->free = f;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
    retval->malloc_data = d;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
    if (len < 8)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   805
    /* 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
   806
    const uint8 *base = NULL;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   807
    uint32 header = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   808
    if (header == 0xBCF00BCF)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   809
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   810
        /* 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
   811
         * 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
   812
         * -flibit
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   813
         */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   814
        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
   815
        ptr += skip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   816
        len += skip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   817
        header = readui32(&ptr, &len);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   818
    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   819
    if (header != 0xFEFF0901)
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   820
        goto parseEffect_notAnEffectsFile;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   821
    else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   822
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   823
        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
   824
        base = ptr;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
        if (offset > len)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
            goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   827
        ptr += offset;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   828
        len -= offset;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   829
    } // else
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   830
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   831
    if (len < 16)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   832
        goto parseEffect_unexpectedEOF;
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
    /* Parse structure counts */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   835
    const uint32 numparams = readui32(&ptr, &len);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   836
    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
   837
    /*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
   838
    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
   839
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   840
    /* 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
   841
    retval->object_count = numobjects;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   842
    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
   843
    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
   844
    if (retval->objects == NULL)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   845
        goto parseEffect_outOfMemory;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   846
    memset(retval->objects, '\0', siz);
1019
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
    /* Parse effect parameters */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   849
    retval->param_count = numparams;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   850
    readparameters(numparams, base, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   851
                   &retval->params, retval->objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   852
                   m, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   853
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   854
    /* Parse effect techniques */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   855
    retval->technique_count = numtechniques;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   856
    readtechniques(numtechniques, base, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   857
                   &retval->techniques, retval->objects,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   858
                   m, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   859
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   860
    /* Initial effect technique/pass */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   861
    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
   862
    retval->current_pass = -1;
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   863
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   864
    if (len < 8)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   865
        goto parseEffect_unexpectedEOF;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   866
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   867
    /* Parse object counts */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   868
    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
   869
    const int numlargeobjects = readui32(&ptr, &len);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   870
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   871
    /* Parse "small" object table */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   872
    readsmallobjects(numsmallobjects, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   873
                     retval,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   874
                     profile, swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   875
                     m, f, d);
1019
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 "large" object table. */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   878
    readlargeobjects(numlargeobjects, numsmallobjects, &ptr, &len,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   879
                     retval,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   880
                     profile, swiz, swizcount, smap, smapcount,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   881
                     m, f, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   882
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   883
    /* Store MojoShader profile in effect structure */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   884
    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
   885
    if (retval->profile == NULL)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   886
        goto parseEffect_outOfMemory;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   887
    strcpy((char *) retval->profile, profile);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   888
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   889
    return retval;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   890
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   891
// !!! FIXME: do something with this.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   892
parseEffect_notAnEffectsFile:
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   893
parseEffect_unexpectedEOF:
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   894
parseEffect_outOfMemory:
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   895
    MOJOSHADER_freeEffect(retval);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   896
    return &MOJOSHADER_out_of_mem_effect;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   897
} // MOJOSHADER_parseEffect
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   898
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   899
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   900
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
   901
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   902
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   903
    f((void *) value->name, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   904
    f((void *) value->semantic, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   905
    if (value->value_type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   906
     || value->value_type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   907
     || value->value_type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   908
     || value->value_type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   909
     || value->value_type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   910
        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
   911
            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
   912
    f(value->values, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   913
} // freevalue
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   914
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   915
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
void MOJOSHADER_freeEffect(const MOJOSHADER_effect *_effect)
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
{
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   918
    MOJOSHADER_effect *effect = (MOJOSHADER_effect *) _effect;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
    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
   920
        return;  // no-op.
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   921
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   922
    MOJOSHADER_free f = effect->free;
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   923
    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
   924
    int i, j, k;
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
    /* Free errors */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   927
    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
   928
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   929
        f((void *) effect->errors[i].error, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   930
        f((void *) effect->errors[i].filename, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   931
    } // for
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   932
    f((void *) effect->errors, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   933
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   934
    /* Free profile string */
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   935
    f((void *) effect->profile, d);
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   936
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   937
    /* 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
   938
    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
   939
    {
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   940
        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
   941
        freevalue(&param->value, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   942
        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
   943
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   944
            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
   945
        } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   946
        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
   947
    } // for
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   948
    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
   949
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   950
    /* 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
   951
    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
   952
    {
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   953
        MOJOSHADER_effectTechnique *technique = &effect->techniques[i];
1021
cad933999680 Fixed memory leak in MOJOSHADER_freeEffect().
Ryan C. Gordon <icculus@icculus.org>
parents: 1019
diff changeset
   954
        f((void *) technique->name, d);
1019
e8988ca01c6d Initial work on parsing binary Effects files.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   955
        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
   956
        {
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   957
            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
   958
            f((void *) pass->name, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   959
            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
   960
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   961
                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
   962
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   963
            f((void *) pass->states, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   964
            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
   965
            {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   966
                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
   967
            } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   968
            f((void *) pass->annotations, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   969
        } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   970
        f((void *) technique->passes, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   971
        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
   972
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   973
            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
   974
        } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   975
        f((void *) technique->annotations, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   976
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   977
    f((void *) effect->techniques, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   978
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   979
    /* Free object table */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   980
    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
   981
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   982
        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
   983
        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
   984
         || object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   985
        {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   986
            if (object->shader.is_preshader)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   987
                MOJOSHADER_freePreshader(object->shader.preshader, f, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   988
            else
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   989
                MOJOSHADER_freeParseData(object->shader.shader);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   990
            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
   991
            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
   992
            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
   993
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   994
        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
   995
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   996
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   997
              || object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   998
              || object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
   999
            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
  1000
        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
  1001
            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
  1002
    } // for
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1003
    f((void *) effect->objects, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1004
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1005
    /* Free base effect structure */
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1006
    f((void *) effect, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1007
} // MOJOSHADER_freeEffect
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1008
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1009
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1010
void copyvalue(MOJOSHADER_effectValue *dst,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1011
               MOJOSHADER_effectValue *src,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1012
               MOJOSHADER_malloc m,
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1013
               void *d)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1014
{
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1015
    int i;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1016
    uint32 siz = 0;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1017
    char *stringcopy = NULL;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1018
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1019
    // !!! FIXME: Out of memory check!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1020
    #define COPY_STRING(location) \
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1021
        if (src->location != NULL) \
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
            siz = strlen(src->location) + 1; \
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1024
            stringcopy = (char *) m(siz, d); \
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1025
            strcpy(stringcopy, src->location); \
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1026
            dst->location = stringcopy; \
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1027
        } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1028
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1029
    COPY_STRING(name)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1030
    COPY_STRING(semantic)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1031
    dst->element_count = src->element_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1032
    dst->row_count = src->row_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1033
    dst->column_count = src->column_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1034
    dst->value_class = src->value_class;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1035
    dst->value_type = src->value_type;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1036
    dst->value_count = src->value_count;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1037
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1038
    if (dst->value_class == MOJOSHADER_SYMCLASS_SCALAR
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1039
     || dst->value_class == MOJOSHADER_SYMCLASS_VECTOR
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1040
     || dst->value_class == MOJOSHADER_SYMCLASS_MATRIX_ROWS
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1041
     || dst->value_class == MOJOSHADER_SYMCLASS_MATRIX_COLUMNS)
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
        siz = dst->value_count * 4;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1044
        dst->values = m(siz, d);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1045
        // !!! FIXME: Out of memory check!
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1046
        memcpy(dst->values, src->values, siz);
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1047
    } // if
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1048
    else if (dst->value_class == MOJOSHADER_SYMCLASS_OBJECT)
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1049
    {
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1050
        if (dst->value_type == MOJOSHADER_SYMTYPE_SAMPLER
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1051
         || dst->value_type == MOJOSHADER_SYMTYPE_SAMPLER1D
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1104
diff changeset
  1052
         || dst->value_type == MOJOSHADER_SYMTYPE_SAMPLER2D