mojoshader.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 Aug 2008 05:54:49 -0400
changeset 450 6a9faf398c1d
parent 446 217be2ec9419
child 451 43175917011f
permissions -rw-r--r--
Allow overriding of swizzle on vertex attributes during bytecode parsing. This lets you compile a shader that you know wants ARGB color data, for example, when you're definitely going to pass it RGBA. The parser will handle this by changing the swizzle on that input register (including handling swizzling explicit swizzles), at no extra cost over not overriding swizzling; it does not generate any extra shader opcodes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     1
/**
35
5749d174b537 [svn] Might as well keep the tradition alive.
icculus
parents: 34
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
5749d174b537 [svn] Might as well keep the tradition alive.
icculus
parents: 34
diff changeset
     3
 *  Direct3D shaders.
7
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     4
 *
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     6
 *
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     7
 *  This file written by Ryan C. Gordon.
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     8
 */
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
     9
322
92b8f994bdc5 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 320
diff changeset
    10
// !!! FIXME: this file really needs to be split up.
92b8f994bdc5 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 320
diff changeset
    11
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    12
// !!! FIXME: I keep changing coding styles for symbols and typedefs.
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    13
7
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
    14
// Shader bytecode format is described at MSDN:
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
    15
//  http://msdn2.microsoft.com/en-us/library/ms800307.aspx
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
    16
1
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    17
#include <stdio.h>
11
c9bb617d9b77 [svn] Pass around a context struct, so we can start tracking state, etc.
icculus
parents: 9
diff changeset
    18
#include <string.h>
1
99ac16070d07 [svn] Initial add.
icculus
parents:
diff changeset
    19
#include <stdlib.h>
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    20
#include <stdarg.h>
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
    21
#include <assert.h>
2
c37210f5e87b [svn] Bunch More Work. Parse out instruction tokens, fail in a bunch of new stubs.
icculus
parents: 1
diff changeset
    22
35
5749d174b537 [svn] Might as well keep the tradition alive.
icculus
parents: 34
diff changeset
    23
#include "mojoshader.h"
9
aea1b254e6a5 [svn] Patched to compile.
icculus
parents: 8
diff changeset
    24
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
    25
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
    26
// This is the highest shader version we currently support.
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
    27
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
    28
#define MAX_SHADER_MAJOR 3
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
    29
#define MAX_SHADER_MINOR 0
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
    30
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    31
67
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
    32
// If SUPPORT_PROFILE_* isn't defined, we assume an implicit desire to support.
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
    33
//  You get all the profiles unless you go out of your way to disable them.
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    34
14
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    35
#ifndef SUPPORT_PROFILE_D3D
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    36
#define SUPPORT_PROFILE_D3D 1
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    37
#endif
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    38
109
48e95cf41505 Added "passthrough" profile, which just sends the bytecode through unchanged;
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
    39
#ifndef SUPPORT_PROFILE_PASSTHROUGH
48e95cf41505 Added "passthrough" profile, which just sends the bytecode through unchanged;
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
    40
#define SUPPORT_PROFILE_PASSTHROUGH 1
48e95cf41505 Added "passthrough" profile, which just sends the bytecode through unchanged;
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
    41
#endif
48e95cf41505 Added "passthrough" profile, which just sends the bytecode through unchanged;
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
    42
14
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    43
#ifndef SUPPORT_PROFILE_GLSL
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    44
#define SUPPORT_PROFILE_GLSL 1
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    45
#endif
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    46
323
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 322
diff changeset
    47
#ifndef SUPPORT_PROFILE_ARB1
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 322
diff changeset
    48
#define SUPPORT_PROFILE_ARB1 1
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 322
diff changeset
    49
#endif
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 322
diff changeset
    50
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    51
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    52
// Get basic wankery out of the way here...
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    53
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    54
typedef unsigned int uint;  // this is a printf() helper. don't use for code.
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    55
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    56
#ifdef _MSC_VER
424
f8ab84a91329 Patched to compile on Windows.
Ryan C. Gordon <icculus@icculus.org>
parents: 421
diff changeset
    57
#include <malloc.h>
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    58
#define snprintf _snprintf
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    59
typedef unsigned __int8 uint8;
398
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
    60
typedef unsigned __int16 uint16;
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    61
typedef unsigned __int32 uint32;
436
304072996bda Windows typedef for "int32" was incorrectly flagged "unsigned"
Ryan C. Gordon <icculus@icculus.org>
parents: 434
diff changeset
    62
typedef __int32 int32;
291
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 290
diff changeset
    63
// Warning Level 4 considered harmful.  :)
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 290
diff changeset
    64
#pragma warning(disable: 4100)  // "unreferenced formal parameter"
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 290
diff changeset
    65
#pragma warning(disable: 4389)  // "signed/unsigned mismatch"
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    66
#else
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    67
#include <stdint.h>
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    68
typedef uint8_t uint8;
398
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
    69
typedef uint16_t uint16;
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    70
typedef uint32_t uint32;
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
    71
typedef int32_t int32;
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    72
#endif
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
    73
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    74
#ifdef __GNUC__
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    75
#define ISPRINTF(x,y) __attribute__((format (printf, x, y)))
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    76
#else
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    77
#define ISPRINTF(x,y)
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    78
#endif
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
    79
14
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    80
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    81
45
9565678766aa [svn] Endline string wankery.
icculus
parents: 44
diff changeset
    82
#ifdef _WINDOWS  // !!! FIXME: bleh
9565678766aa [svn] Endline string wankery.
icculus
parents: 44
diff changeset
    83
const char *endline_str = "\r\n";
9565678766aa [svn] Endline string wankery.
icculus
parents: 44
diff changeset
    84
#else
9565678766aa [svn] Endline string wankery.
icculus
parents: 44
diff changeset
    85
const char *endline_str = "\n";
9565678766aa [svn] Endline string wankery.
icculus
parents: 44
diff changeset
    86
#endif
9565678766aa [svn] Endline string wankery.
icculus
parents: 44
diff changeset
    87
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
    88
// we need to reference this by explicit value occasionally.
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
    89
#define OPCODE_RET 28
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
    90
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
    91
// Special-case return values from the parsing pipeline...
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
    92
#define FAIL (-1)
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
    93
#define NOFAIL (-2)
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
    94
#define END_OF_STREAM (-3)
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
    95
14
7075b6e31471 [svn] More proper split into profiles.
icculus
parents: 13
diff changeset
    96
7
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
    97
// Byteswap magic...
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
    98
4
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
    99
#if ((defined __GNUC__) && (defined __POWERPC__))
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   100
    static inline uint32 SWAP32(uint32 x)
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   101
    {
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   102
        __asm__ __volatile__("lwbrx %0,0,%1" : "=r" (x) : "r" (&x));
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   103
        return x;
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   104
    } // SWAP32
398
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   105
    static inline uint16 SWAP16(uint16 x)
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   106
    {
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   107
        __asm__ __volatile__("lhbrx %0,0,%1" : "=r" (x) : "r" (&x));
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   108
        return x;
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   109
    } // SWAP16
4
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   110
#elif defined(__POWERPC__)
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   111
    static inline uint32 SWAP32(uint32 x)
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   112
    {
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   113
        return ( (((x) >> 24) & 0x000000FF) | (((x) >>  8) & 0x0000FF00) |
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   114
                 (((x) <<  8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) );
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   115
    } // SWAP32
398
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   116
    static inline uint16 SWAP16(uint16 x)
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   117
    {
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   118
        return ( (((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00) );
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   119
    } // SWAP16
4
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   120
#else
398
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   121
#   define SWAP16(x) (x)
4
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   122
#   define SWAP32(x) (x)
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   123
#endif
e0b2c5b7210c [svn] Implemented byteswapping.
icculus
parents: 2
diff changeset
   124
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   125
typedef enum
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   126
{
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   127
    REG_TYPE_TEMP = 0,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   128
    REG_TYPE_INPUT = 1,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   129
    REG_TYPE_CONST = 2,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   130
    REG_TYPE_ADDRESS = 3,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   131
    REG_TYPE_TEXTURE = 3,  // ALSO 3!
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   132
    REG_TYPE_RASTOUT = 4,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   133
    REG_TYPE_ATTROUT = 5,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   134
    REG_TYPE_TEXCRDOUT = 6,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   135
    REG_TYPE_OUTPUT = 6,  // ALSO 6!
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   136
    REG_TYPE_CONSTINT = 7,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   137
    REG_TYPE_COLOROUT = 8,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   138
    REG_TYPE_DEPTHOUT = 9,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   139
    REG_TYPE_SAMPLER = 10,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   140
    REG_TYPE_CONST2 = 11,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   141
    REG_TYPE_CONST3 = 12,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   142
    REG_TYPE_CONST4 = 13,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   143
    REG_TYPE_CONSTBOOL = 14,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   144
    REG_TYPE_LOOP = 15,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   145
    REG_TYPE_TEMPFLOAT16 = 16,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   146
    REG_TYPE_MISCTYPE = 17,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   147
    REG_TYPE_LABEL = 18,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   148
    REG_TYPE_PREDICATE = 19,
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   149
    REG_TYPE_MAX = 19
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   150
} RegisterType;
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   151
148
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   152
typedef enum
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   153
{
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   154
    TEXTURE_TYPE_2D = 2,
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   155
    TEXTURE_TYPE_CUBE = 3,
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   156
    TEXTURE_TYPE_VOLUME = 4,
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   157
} TextureType;
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   158
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   159
// predeclare.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   160
typedef struct Context Context;
405
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   161
struct ConstantsList;
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   162
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   163
// one emit function for each opcode in each profile.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   164
typedef void (*emit_function)(Context *ctx);
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   165
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   166
// one emit function for starting output in each profile.
361
9fa6652cacbd First (untested) work on nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 360
diff changeset
   167
typedef void (*emit_start)(Context *ctx, const char *profilestr);
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   168
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   169
// one emit function for ending output in each profile.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   170
typedef void (*emit_end)(Context *ctx);
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   171
400
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   172
// one emit function for phase opcode output in each profile.
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   173
typedef void (*emit_phase)(Context *ctx);
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   174
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   175
// one emit function for finalizing output in each profile.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   176
typedef void (*emit_finalize)(Context *ctx);
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   177
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   178
// one emit function for global definitions in each profile.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   179
typedef void (*emit_global)(Context *ctx, RegisterType regtype, int regnum);
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   180
280
61b2abd9c927 Relative addressing fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 279
diff changeset
   181
// one emit function for relative uniform arrays in each profile.
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   182
typedef void (*emit_array)(Context *ctx, int base, int size);
280
61b2abd9c927 Relative addressing fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 279
diff changeset
   183
405
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   184
// one emit function for relative constants arrays in each profile.
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   185
typedef void (*emit_const_array)(Context *ctx,
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   186
                                 const struct ConstantsList *constslist,
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   187
                                 int base, int size);
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   188
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   189
// one emit function for uniforms in each profile.
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   190
typedef void (*emit_uniform)(Context *ctx, RegisterType regtype, int regnum,
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   191
                             int arraybase, int arraysize);
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   192
148
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   193
// one emit function for samplers in each profile.
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   194
typedef void (*emit_sampler)(Context *ctx, int stage, TextureType ttype);
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   195
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   196
// one emit function for attributes in each profile.
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   197
typedef void (*emit_attribute)(Context *ctx, RegisterType regtype, int regnum,
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 430
diff changeset
   198
                               MOJOSHADER_usage usage, int index, int wmask,
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 430
diff changeset
   199
                               int flags);
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   200
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   201
// one args function for each possible sequence of opcode arguments.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   202
typedef int (*args_function)(Context *ctx);
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   203
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   204
// one state function for each opcode where we have state machine updates.
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   205
typedef void (*state_function)(Context *ctx);
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   206
347
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   207
// one function for varnames in each profile.
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   208
typedef const char *(*varname_function)(Context *c, RegisterType t, int num);
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   209
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   210
// one function for const var array in each profile.
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   211
typedef const char *(*const_array_varname_function)(Context *c, int base, int size);
347
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   212
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   213
typedef struct
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   214
{
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   215
    const char *name;
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   216
    emit_start start_emitter;
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   217
    emit_end end_emitter;
400
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   218
    emit_phase phase_emitter;
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   219
    emit_global global_emitter;
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   220
    emit_array array_emitter;
405
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   221
    emit_const_array const_array_emitter;
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   222
    emit_uniform uniform_emitter;
148
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   223
    emit_sampler sampler_emitter;
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   224
    emit_attribute attribute_emitter;
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   225
    emit_finalize finalize_emitter;
347
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   226
    varname_function get_varname;
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 346
diff changeset
   227
    const_array_varname_function get_const_array_varname;
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   228
} Profile;
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   229
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   230
typedef enum
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   231
{
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   232
    RASTOUT_TYPE_POSITION = 0,
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   233
    RASTOUT_TYPE_FOG = 1,
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   234
    RASTOUT_TYPE_POINT_SIZE = 2,
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   235
    RASTOUT_TYPE_MAX = 2
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   236
} RastOutType;
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   237
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   238
typedef enum
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   239
{
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   240
    MISCTYPE_TYPE_POSITION = 0,
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   241
    MISCTYPE_TYPE_FACE = 1,
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   242
    MISCTYPE_TYPE_MAX = 1
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   243
} MiscTypeType;
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   244
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   245
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   246
// A simple linked list of strings, so we can build the final output without
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   247
//  realloc()ing for each new line, and easily insert lines into the middle
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   248
//  of the output without much trouble.
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   249
typedef struct OutputListNode
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   250
{
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   251
    char *str;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   252
    struct OutputListNode *next;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   253
} OutputListNode;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   254
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   255
typedef struct OutputList
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   256
{
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   257
    OutputListNode head;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   258
    OutputListNode *tail;
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   259
} OutputList;
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   260
405
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   261
typedef struct ConstantsList
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   262
{
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   263
    MOJOSHADER_constant constant;
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   264
    struct ConstantsList *next;
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   265
} ConstantsList;
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   266
399
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   267
typedef struct VariableList
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   268
{
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   269
    MOJOSHADER_uniformType type;
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   270
    int index;
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   271
    int count;
405
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   272
    ConstantsList *constant;
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   273
    int used;
399
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   274
    struct VariableList *next;
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   275
} VariableList;
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   276
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   277
typedef struct RegisterList
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   278
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   279
    RegisterType regtype;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   280
    int regnum;
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   281
    MOJOSHADER_usage usage;
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   282
    int index;
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   283
    int writemask;
248
568c8f9d7cb9 Don't overload meaning of RegisterList::usage for loop tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   284
    int misc;
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   285
    const VariableList *array;
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   286
    struct RegisterList *next;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   287
} RegisterList;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   288
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   289
// result modifiers.
425
8117098722f2 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 424
diff changeset
   290
// !!! FIXME: why isn't this an enum?
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   291
#define MOD_SATURATE 0x01
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   292
#define MOD_PP 0x02
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   293
#define MOD_CENTROID 0x04
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   294
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   295
// source modifiers.
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   296
typedef enum
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   297
{
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   298
    SRCMOD_NONE,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   299
    SRCMOD_NEGATE,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   300
    SRCMOD_BIAS,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   301
    SRCMOD_BIASNEGATE,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   302
    SRCMOD_SIGN,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   303
    SRCMOD_SIGNNEGATE,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   304
    SRCMOD_COMPLEMENT,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   305
    SRCMOD_X2,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   306
    SRCMOD_X2NEGATE,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   307
    SRCMOD_DZ,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   308
    SRCMOD_DW,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   309
    SRCMOD_ABS,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   310
    SRCMOD_ABSNEGATE,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   311
    SRCMOD_NOT,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   312
    SRCMOD_TOTAL
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   313
} SourceMod;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   314
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   315
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   316
typedef struct
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   317
{
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   318
    const uint32 *token;   // this is the unmolested token in the stream.
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   319
    int regnum;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   320
    int relative;
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   321
    int writemask;   // xyzw or rgba (all four, not split out).
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   322
    int writemask0;  // x or red
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   323
    int writemask1;  // y or green
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   324
    int writemask2;  // z or blue
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   325
    int writemask3;  // w or alpha
316
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   326
    int orig_writemask;   // writemask before mojoshader tweaks it.
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   327
    int result_mod;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   328
    int result_shift;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   329
    RegisterType regtype;
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   330
} DestArgInfo;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   331
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   332
typedef struct
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   333
{
21
d43449cf3cb2 [svn] Implemented destination argument output in D3D profile. Untested.
icculus
parents: 20
diff changeset
   334
    const uint32 *token;   // this is the unmolested token in the stream.
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   335
    int regnum;
22
91ab1ed38d63 [svn] Fixed parse_args().
icculus
parents: 21
diff changeset
   336
    int swizzle;  // xyzw (all four, not split out).
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   337
    int swizzle_x;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   338
    int swizzle_y;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   339
    int swizzle_z;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   340
    int swizzle_w;
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
   341
    SourceMod src_mod;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   342
    RegisterType regtype;
152
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
   343
    int relative;
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
   344
    RegisterType relative_regtype;
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
   345
    int relative_regnum;
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
   346
    int relative_component;
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   347
    const VariableList *relative_array;
20
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   348
} SourceArgInfo;
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   349
bb2e8f285acc [svn] Bunch More Work...higher level parses dest/src tokens before it goes to the
icculus
parents: 19
diff changeset
   350
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   351
141
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
   352
#define SCRATCH_BUFFER_SIZE 128
343
5d7861fb677d Increase number of scratch buffers.
Ryan C. Gordon <icculus@icculus.org>
parents: 342
diff changeset
   353
#define SCRATCH_BUFFERS 32
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   354
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   355
// !!! FIXME: the scratch buffers make Context pretty big.
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   356
// !!! FIXME:  might be worth having one set of static scratch buffers that
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   357
// !!! FIXME:  are mutex protected?
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   358
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   359
// Context...this is state that changes as we parse through a shader...
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   360
struct Context
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   361
{
35
5749d174b537 [svn] Might as well keep the tradition alive.
icculus
parents: 34
diff changeset
   362
    MOJOSHADER_malloc malloc;
5749d174b537 [svn] Might as well keep the tradition alive.
icculus
parents: 34
diff changeset
   363
    MOJOSHADER_free free;
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   364
    void *malloc_data;
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   365
    const uint32 *tokens;
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   366
    uint32 tokencount;
450
6a9faf398c1d Allow overriding of swizzle on vertex attributes during bytecode parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 446
diff changeset
   367
    const MOJOSHADER_swizzle *swizzles;
6a9faf398c1d Allow overriding of swizzle on vertex attributes during bytecode parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 446
diff changeset
   368
    unsigned int swizzles_count;
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   369
    OutputList *output;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   370
    OutputList globals;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   371
    OutputList helpers;
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   372
    OutputList subroutines;
189
d31efd640c13 Attribute fixes for GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 187
diff changeset
   373
    OutputList mainline_intro;
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   374
    OutputList mainline;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   375
    OutputList ignore;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   376
    OutputList *output_stack[2];
109
48e95cf41505 Added "passthrough" profile, which just sends the bytecode through unchanged;
Ryan C. Gordon <icculus@icculus.org>
parents: 108
diff changeset
   377
    uint8 *output_bytes;  // can be used instead of the OutputLists.
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   378
    int indent_stack[2];
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   379
    int output_stack_len;
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   380
    int output_len; // total strlen; prevents walking the lists just to malloc.
40
f54f1635ac8a [svn] Filling in some initial GLSL output. Not even close to done!
icculus
parents: 39
diff changeset
   381
    int indent;
334
5aebcea77f47 Cleaned up the shader type string code.
Ryan C. Gordon <icculus@icculus.org>
parents: 333
diff changeset
   382
    const char *shader_type_str;
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   383
    const char *endline;
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   384
    int endline_len;
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   385
    const char *failstr;
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   386
    char scratch[SCRATCH_BUFFERS][SCRATCH_BUFFER_SIZE];
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   387
    int scratchidx;  // current scratch buffer.
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   388
    int profileid;
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   389
    const Profile *profile;
96
bc416e2b9de1 Removed convenience typedef.
Ryan C. Gordon <icculus@icculus.org>
parents: 95
diff changeset
   390
    MOJOSHADER_shaderType shader_type;
46
ff5a0ec44f00 [svn] Lots and lots of work on the public API.
icculus
parents: 45
diff changeset
   391
    uint8 major_ver;
ff5a0ec44f00 [svn] Lots and lots of work on the public API.
icculus
parents: 45
diff changeset
   392
    uint8 minor_ver;
161
a0e1920ce909 Removed "dest_args" array...it's only ever one structure.
Ryan C. Gordon <icculus@icculus.org>
parents: 160
diff changeset
   393
    DestArgInfo dest_arg;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   394
    SourceArgInfo source_args[5];
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
   395
    SourceArgInfo predicate_arg;  // for predicated instructions.
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   396
    uint32 dwords[4];
398
a5faebe97da4 Initial work on parsing the CTAB comment block.
Ryan C. Gordon <icculus@icculus.org>
parents: 397
diff changeset
   397
    uint32 version_token;
46
ff5a0ec44f00 [svn] Lots and lots of work on the public API.
icculus
parents: 45
diff changeset
   398
    int instruction_count;
28
bdb89e56bb91 [svn] Comparison opcodes...
icculus
parents: 27
diff changeset
   399
    uint32 instruction_controls;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   400
    uint32 previous_opcode;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   401
    int loops;
114
3b8cf84b46b8 Implemented REP and ENDREP in the GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 112
diff changeset
   402
    int reps;
382
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   403
    int max_reps;
118
6aa56b497f4e Sorta implemented CMP for GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 117
diff changeset
   404
    int cmps;
329
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   405
    int scratch_registers;
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   406
    int max_scratch_registers;
382
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   407
    int branch_labels_stack_index;
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   408
    int branch_labels_stack[32];
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   409
    int assigned_branch_labels;
332
8c7544035bd0 More work on arb1 profile. Attributes and outputs, etc.
Ryan C. Gordon <icculus@icculus.org>
parents: 331
diff changeset
   410
    int assigned_vertex_attributes;
336
169b595c95fd Fixed relative addressing in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
   411
    int last_address_reg_component;
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   412
    RegisterList used_registers;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   413
    RegisterList defined_registers;
278
5c432d216078 Report hardcoded constants in MOJOSHADER_parseData.
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   414
    int constant_count;
5c432d216078 Report hardcoded constants in MOJOSHADER_parseData.
Ryan C. Gordon <icculus@icculus.org>
parents: 268
diff changeset
   415
    ConstantsList *constants;
95
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   416
    int uniform_count;
56af2093eefe Implemented uniform reporting.
Ryan C. Gordon <icculus@icculus.org>
parents: 93
diff changeset
   417
    RegisterList uniforms;
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   418
    int attribute_count;
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   419
    RegisterList attributes;
148
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   420
    int sampler_count;
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   421
    RegisterList samplers;
399
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   422
    VariableList *variables;  // variables to register mapping.
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 430
diff changeset
   423
    int centroid_allowed:1;
399
dc2e64bd03ad Extract useful information from the ctab.
Ryan C. Gordon <icculus@icculus.org>
parents: 398
diff changeset
   424
    int have_ctab:1;
405
e2cffb40e8b8 Build arrays of constants if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 404
diff changeset
   425
    int determined_constants_arrays:1;
362
1aa8a74bb5a5 Moved some booleans to true bitfields.
Ryan C. Gordon <icculus@icculus.org>
parents: 361
diff changeset
   426
    int predicated:1;
1aa8a74bb5a5 Moved some booleans to true bitfields.
Ryan C. Gordon <icculus@icculus.org>
parents: 361
diff changeset
   427
    int support_nv2:1;
421
bfd3d95273ec First piece of work on nv3 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 419
diff changeset
   428
    int support_nv3:1;
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 430
diff changeset
   429
    int support_nv4:1;
407
620d48c5d13a Added framework for GLSL 1.20 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 405
diff changeset
   430
    int support_glsl120:1;
362
1aa8a74bb5a5 Moved some booleans to true bitfields.
Ryan C. Gordon <icculus@icculus.org>
parents: 361
diff changeset
   431
    int glsl_generated_lit_opcode:1;
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   432
};
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   433
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   434
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   435
// Convenience functions for allocators...
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   436
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   437
static MOJOSHADER_parseData out_of_mem_data = {
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   438
    "Out of memory", 0, 0, 0, 0, MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   439
};
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   440
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   441
static const char *out_of_mem_str = "Out of memory";
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   442
static inline int out_of_memory(Context *ctx)
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   443
{
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   444
    if (ctx->failstr == NULL)
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   445
        ctx->failstr = out_of_mem_str;  // fail() would call malloc().
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   446
    return FAIL;
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   447
} // out_of_memory
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   448
307
42f6a7ba69e2 Fixes for Visual Studio level 4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 306
diff changeset
   449
static inline void *Malloc(Context *ctx, const size_t len)
42f6a7ba69e2 Fixes for Visual Studio level 4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 306
diff changeset
   450
{
42f6a7ba69e2 Fixes for Visual Studio level 4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 306
diff changeset
   451
    void *retval = ctx->malloc((int) len, ctx->malloc_data);
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   452
    if (retval == NULL)
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   453
        out_of_memory(ctx);
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   454
    return retval;
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   455
} // Malloc
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   456
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   457
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   458
static inline void Free(Context *ctx, void *ptr)
126
556779e8a6d7 Minor tweaks to Malloc() and Free() convenience functions.
Ryan C. Gordon <icculus@icculus.org>
parents: 124
diff changeset
   459
{
556779e8a6d7 Minor tweaks to Malloc() and Free() convenience functions.
Ryan C. Gordon <icculus@icculus.org>
parents: 124
diff changeset
   460
    if (ptr != NULL)  // check for NULL in case of dumb free() impl.
99
20d0bb294e9e Check for NULL in Free().
Ryan C. Gordon <icculus@icculus.org>
parents: 98
diff changeset
   461
        ctx->free(ptr, ctx->malloc_data);
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   462
} // Free
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   463
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   464
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   465
// jump between output sections in the context...
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   466
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   467
static inline void push_output(Context *ctx, OutputList *section)
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   468
{
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   469
    assert(ctx->output_stack_len < STATICARRAYLEN(ctx->output_stack));
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   470
    ctx->output_stack[ctx->output_stack_len] = ctx->output;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   471
    ctx->indent_stack[ctx->output_stack_len] = ctx->indent;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   472
    ctx->output_stack_len++;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   473
    ctx->output = section;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   474
    ctx->indent = 0;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   475
} // push_output
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   476
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   477
static inline void pop_output(Context *ctx)
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   478
{
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   479
    assert(ctx->output_stack_len > 0);
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   480
    ctx->output_stack_len--;
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   481
    ctx->output = ctx->output_stack[ctx->output_stack_len];
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   482
    ctx->indent = ctx->indent_stack[ctx->output_stack_len];
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   483
} // pop_output
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   484
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   485
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   486
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   487
// Shader model version magic...
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   488
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   489
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   490
{
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   491
    return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   492
} // version_ui32
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   493
151
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   494
static inline int shader_version_supported(const uint8 maj, const uint8 min)
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   495
{
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   496
    return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   497
} // shader_version_supported
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   498
151
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   499
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   500
                                         const uint8 min)
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   501
{
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   502
    return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   503
} // shader_version_atleast
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   504
400
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   505
static inline int shader_version_exactly(const Context *ctx, const uint8 maj,
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   506
                                         const uint8 min)
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   507
{
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   508
    return ((ctx->major_ver == maj) && (ctx->minor_ver == min));
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   509
} // shader_version_exactly
05e384a14cd6 Implemented support for phase token.
Ryan C. Gordon <icculus@icculus.org>
parents: 399
diff changeset
   510
151
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   511
static inline int shader_is_pixel(const Context *ctx)
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   512
{
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   513
    return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   514
} // shader_is_pixel
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   515
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   516
static inline int shader_is_vertex(const Context *ctx)
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   517
{
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   518
    return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   519
} // shader_is_vertex
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   520
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   521
48
86dc2c772d26 [svn] Enabled -Wall, found some bugs.
icculus
parents: 46
diff changeset
   522
static inline int isfail(const Context *ctx)
44
5d56cf8b4571 [svn] Cleaned up fail check, parse_args and state machine semantics, and added check
icculus
parents: 43
diff changeset
   523
{
5d56cf8b4571 [svn] Cleaned up fail check, parse_args and state machine semantics, and added check
icculus
parents: 43
diff changeset
   524
    return (ctx->failstr != NULL);
5d56cf8b4571 [svn] Cleaned up fail check, parse_args and state machine semantics, and added check
icculus
parents: 43
diff changeset
   525
} // isfail
5d56cf8b4571 [svn] Cleaned up fail check, parse_args and state machine semantics, and added check
icculus
parents: 43
diff changeset
   526
5d56cf8b4571 [svn] Cleaned up fail check, parse_args and state machine semantics, and added check
icculus
parents: 43
diff changeset
   527
344
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   528
static inline char *get_scratch_buffer(Context *ctx)
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   529
{
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   530
    if ((ctx->scratchidx >= SCRATCH_BUFFERS) && !isfail(ctx))
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   531
    {
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   532
        // can't call fail() here, since it calls back into here.
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   533
        const char *errstr = "BUG: overflowed scratch buffers";
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   534
        char *failstr = (char *) Malloc(ctx, strlen(errstr) + 1);
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   535
        if (failstr != NULL)
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   536
        {
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   537
            strcpy(failstr, errstr);
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   538
            ctx->failstr = failstr;
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   539
        } // if
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   540
    } // if
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   541
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   542
    ctx->scratchidx = (ctx->scratchidx + 1) % SCRATCH_BUFFERS;
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   543
    return ctx->scratch[ctx->scratchidx];
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   544
} // get_scratch_buffer
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   545
94cf695babc6 Better sanity checking on scratch buffer array.
Ryan C. Gordon <icculus@icculus.org>
parents: 343
diff changeset
   546
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   547
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   548
static int failf(Context *ctx, const char *fmt, ...)
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   549
{
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   550
    if (ctx->failstr == NULL)  // don't change existing error.
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   551
    {
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   552
        char *scratch = get_scratch_buffer(ctx);
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   553
        va_list ap;
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   554
        va_start(ap, fmt);
141
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
   555
        const int len = vsnprintf(scratch, SCRATCH_BUFFER_SIZE, fmt, ap);
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   556
        va_end(ap);
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   557
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   558
        char *failstr = (char *) Malloc(ctx, len + 1);
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   559
        if (failstr != NULL)
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   560
        {
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   561
            // see comments about scratch buffer overflow in output_line().
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   562
            if (len < SCRATCH_BUFFER_SIZE)
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   563
                strcpy(failstr, scratch);  // copy it over.
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   564
            else
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   565
            {
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   566
                va_start(ap, fmt);
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   567
                vsnprintf(failstr, len + 1, fmt, ap);  // rebuild it.
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   568
                va_end(ap);
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   569
            } // else
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   570
            ctx->failstr = failstr;
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   571
        } // if
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   572
    } // if
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   573
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   574
    return FAIL;
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   575
} // failf
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   576
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   577
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   578
static inline int fail(Context *ctx, const char *reason)
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   579
{
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   580
    return failf(ctx, "%s", reason);
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   581
} // fail
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   582
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   583
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   584
static int output_line(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   585
static int output_line(Context *ctx, const char *fmt, ...)
7
18bf77bb8c07 [svn] Cleanups, split things out.
icculus
parents: 6
diff changeset
   586
{
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   587
    OutputListNode *item = NULL;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   588
44
5d56cf8b4571 [svn] Cleaned up fail check, parse_args and state machine semantics, and added check
icculus
parents: 43
diff changeset
   589
    if (isfail(ctx))
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   590
        return FAIL;  // we failed previously, don't go on...
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   591
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   592
    char *scratch = get_scratch_buffer(ctx);
40
f54f1635ac8a [svn] Filling in some initial GLSL output. Not even close to done!
icculus
parents: 39
diff changeset
   593
f54f1635ac8a [svn] Filling in some initial GLSL output. Not even close to done!
icculus
parents: 39
diff changeset
   594
    const int indent = ctx->indent;
41
b857eacdbc68 [svn] More GLSL work.
icculus
parents: 40
diff changeset
   595
    if (indent > 0)
b857eacdbc68 [svn] More GLSL work.
icculus
parents: 40
diff changeset
   596
        memset(scratch, '\t', indent);
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   597
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   598
    va_list ap;
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   599
    va_start(ap, fmt);
40
f54f1635ac8a [svn] Filling in some initial GLSL output. Not even close to done!
icculus
parents: 39
diff changeset
   600
    const int len = vsnprintf(scratch+indent, SCRATCH_BUFFER_SIZE-indent, fmt, ap) + indent;
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   601
    va_end(ap);
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   602
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   603
    item = (OutputListNode *) Malloc(ctx, sizeof (OutputListNode));
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   604
    if (item == NULL)
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   605
        return FAIL;
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   606
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   607
    item->str = (char *) Malloc(ctx, len + 1);
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   608
    if (item->str == NULL)
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   609
    {
98
08b137beb1e3 Whoops, wrong free() call.
Ryan C. Gordon <icculus@icculus.org>
parents: 97
diff changeset
   610
        Free(ctx, item);
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   611
        return FAIL;
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   612
    } // if
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   613
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   614
    // If we overflowed our scratch buffer, that's okay. We were going to
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   615
    //  allocate anyhow...the scratch buffer just lets us avoid a second
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   616
    //  run of vsnprintf().
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   617
    if (len < SCRATCH_BUFFER_SIZE)
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   618
        strcpy(item->str, scratch);  // copy it over.
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   619
    else
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   620
    {
41
b857eacdbc68 [svn] More GLSL work.
icculus
parents: 40
diff changeset
   621
        if (indent > 0)
b857eacdbc68 [svn] More GLSL work.
icculus
parents: 40
diff changeset
   622
            memset(item->str, '\t', indent);
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   623
        va_start(ap, fmt);
40
f54f1635ac8a [svn] Filling in some initial GLSL output. Not even close to done!
icculus
parents: 39
diff changeset
   624
        vsnprintf(item->str+indent, len + 1, fmt, ap);  // rebuild it.
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   625
        va_end(ap);
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   626
    } // else
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   627
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   628
    item->next = NULL;
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   629
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   630
    ctx->output->tail->next = item;
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   631
    ctx->output->tail = item;
18
0dbdb2be8bf8 [svn] Bunch More Work. Cleanups, added app-supplied allocators, flexible output, etc.
icculus
parents: 17
diff changeset
   632
    ctx->output_len += len + ctx->endline_len;
54
b2b64d39df0f [svn] Split up output into several lists, so GLSL profile can insert lines of text
icculus
parents: 51
diff changeset
   633
12
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   634
    return 0;
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   635
} // output_line
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   636
d81712dd1a46 [svn] Cleaned up and improved output and fail state.
icculus
parents: 11
diff changeset
   637
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   638
// this is just to stop gcc whining.
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   639
static inline int output_blank_line(Context *ctx)
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   640
{
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   641
    return output_line(ctx, "%s", "");
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   642
} // output_blank_line
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   643
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
   644
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   645
// !!! FIXME: this is sort of nasty.
72
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   646
static void floatstr(Context *ctx, char *buf, size_t bufsize, float f,
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   647
                     int leavedecimal)
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   648
{
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   649
    const size_t len = snprintf(buf, bufsize, "%f", f);
72
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   650
    if ((len+2) >= bufsize)
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   651
        fail(ctx, "BUG: internal buffer is too small");
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   652
    else
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   653
    {
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   654
        char *end = buf + len;
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   655
        char *ptr = strchr(buf, '.');
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   656
        if (ptr == NULL)
72
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   657
        {
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   658
            if (leavedecimal)
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   659
                strcat(buf, ".0");
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   660
            return;  // done.
72
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   661
        } // if
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   662
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   663
        while (--end != ptr)
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   664
        {
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   665
            if (*end != '0')
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   666
            {
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   667
                end++;
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   668
                break;
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   669
            } // if
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   670
        } // while
72
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   671
        if ((leavedecimal) && (end == ptr))
b193a3182dcd Tweak floatstr() to produce strings the GLSL profile can use.
Ryan C. Gordon <icculus@icculus.org>
parents: 71
diff changeset
   672
            end += 2;
43
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   673
        *end = '\0';  // chop extra '0' or all decimal places off.
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   674
    } // else
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   675
} // floatstr
2f5d9a656dff [svn] Bunch More Work. Cleaned up some lingering opcode drama, and state machine
icculus
parents: 42
diff changeset
   676
17
78d605d6641d [svn] Starting to fill in D3D profile...
icculus
parents: 16
diff changeset
   677
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   678
// Deal with register lists...  !!! FIXME: I sort of hate this.
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   679
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   680
static void free_reglist(MOJOSHADER_free f, void *d, RegisterList *item)
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   681
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   682
    while (item != NULL)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   683
    {
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   684
        RegisterList *next = item->next;
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   685
        f(item, d);
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   686
        item = next;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   687
    } // while
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   688
} // free_reglist
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   689
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   690
static inline uint32 reg_to_ui32(const RegisterType regtype, const int regnum)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   691
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   692
    return ( ((uint32) regtype) | (((uint32) regnum) << 16) );
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   693
} // reg_to_uint32
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   694
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   695
static RegisterList *reglist_insert(Context *ctx, RegisterList *prev,
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   696
                                    const RegisterType regtype,
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   697
                                    const int regnum)
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   698
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   699
    const uint32 newval = reg_to_ui32(regtype, regnum);
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   700
    RegisterList *item = prev->next;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   701
    while (item != NULL)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   702
    {
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   703
        const uint32 val = reg_to_ui32(item->regtype, item->regnum);
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   704
        if (newval == val)
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   705
            return item;  // already set, so we're done.
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   706
        else if (newval < val)  // insert it here.
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   707
            break;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   708
        else // if (newval > val)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   709
        {
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   710
            // keep going, we're not to the insertion point yet.
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   711
            prev = item;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   712
            item = item->next;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   713
        } // else
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   714
    } // while
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   715
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   716
    // we need to insert an entry after (prev).
97
4a41e3d17297 Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents: 96
diff changeset
   717
    item = (RegisterList *) Malloc(ctx, sizeof (RegisterList));
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   718
    if (item != NULL)
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   719
    {
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   720
        item->regtype = regtype;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   721
        item->regnum = regnum;
248
568c8f9d7cb9 Don't overload meaning of RegisterList::usage for loop tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   722
        item->usage = MOJOSHADER_USAGE_UNKNOWN;
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   723
        item->index = 0;
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   724
        item->writemask = 0;
248
568c8f9d7cb9 Don't overload meaning of RegisterList::usage for loop tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 235
diff changeset
   725
        item->misc = 0;
402
933d71481f5b Better relative addressing support.
Ryan C. Gordon <icculus@icculus.org>
parents: 401
diff changeset
   726
        item->array = NULL;
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   727
        item->next = prev->next;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   728
        prev->next = item;
194
8de489efc811 Malloc() now handles calling out_of_memory() if necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 193
diff changeset
   729
    } // if
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   730
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   731
    return item;
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   732
} // reglist_insert
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   733
450
6a9faf398c1d Allow overriding of swizzle on vertex attributes during bytecode parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 446
diff changeset
   734
static RegisterList *reglist_find(const RegisterList *prev,
6a9faf398c1d Allow overriding of swizzle on vertex attributes during bytecode parsing.
Ryan C. Gordon <icculus@icculus.org>
parents: 446
diff changeset
   735
                                  const RegisterType rtype, const int regnum)
122
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   736
{
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   737
    const uint32 newval = reg_to_ui32(rtype, regnum);
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   738
    RegisterList *item = prev->next;
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   739
    while (item != NULL)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   740
    {
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   741
        const uint32 val = reg_to_ui32(item->regtype, item->regnum);
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   742
        if (newval == val)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   743
            return item;  // here it is.
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   744
        else if (newval < val)  // should have been here if it existed.
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   745
            return NULL;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   746
        else // if (newval > val)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   747
        {
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   748
            // keep going, we're not to the insertion point yet.
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   749
            prev = item;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   750
            item = item->next;
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   751
        } // else
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   752
    } // while
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   753
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   754
    return NULL;  // wasn't in the list.
122
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   755
} // reglist_find
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   756
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   757
static inline const RegisterList *reglist_exists(RegisterList *prev,
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   758
                                                 const RegisterType regtype,
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   759
                                                 const int regnum)
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   760
{
e9da4ede0a27 Hopefully fixed loop register inheriting to subroutines.
Ryan C. Gordon <icculus@icculus.org>
parents: 121
diff changeset
   761
    return (reglist_find(prev, regtype, regnum));
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   762
} // reglist_exists
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   763
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   764
static inline void set_used_register(Context *ctx, const RegisterType regtype,
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   765
                                     const int regnum)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   766
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   767
    reglist_insert(ctx, &ctx->used_registers, regtype, regnum);
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   768
} // set_used_register
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   769
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   770
static inline int get_used_register(Context *ctx, const RegisterType regtype,
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   771
                                    const int regnum)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   772
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   773
    return (reglist_exists(&ctx->used_registers, regtype, regnum) != NULL);
83
ce46250e553d Add defined/declared registers to the appropriate register list.
Ryan C. Gordon <icculus@icculus.org>
parents: 82
diff changeset
   774
} // get_used_register
82
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   775
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   776
static inline void set_defined_register(Context *ctx, const RegisterType rtype,
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   777
                                        const int regnum)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   778
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   779
    reglist_insert(ctx, &ctx->defined_registers, rtype, regnum);
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   780
} // set_defined_register
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   781
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   782
static inline int get_defined_register(Context *ctx, const RegisterType rtype,
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   783
                                       const int regnum)
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   784
{
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   785
    return (reglist_exists(&ctx->defined_registers, rtype, regnum) != NULL);
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   786
} // get_defined_register
dc7ad4cea75b Keep a list of used/defined registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 81
diff changeset
   787
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   788
static void add_attribute_register(Context *ctx, const RegisterType rtype,
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   789
                                const int regnum, const MOJOSHADER_usage usage,
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 430
diff changeset
   790
                                const int index, const int writemask, int flags)
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   791
{
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   792
    RegisterList *item = reglist_insert(ctx, &ctx->attributes, rtype, regnum);
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   793
    item->usage = usage;
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   794
    item->index = index;
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   795
    item->writemask = writemask;
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 430
diff changeset
   796
    item->misc = flags;
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   797
} // add_attribute_register
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   798
148
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   799
static inline void add_sampler(Context *ctx, const RegisterType rtype,
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   800
                               const int regnum, const TextureType ttype)
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   801
{
297
16ec5a14777d Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 296
diff changeset
   802
    // !!! FIXME: make sure it doesn't exist?
148
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   803
    RegisterList *item = reglist_insert(ctx, &ctx->samplers, rtype, regnum);
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   804
    item->index = (int) ttype;
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   805
} // add_sampler
645003ec6623 Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents: 145
diff changeset
   806
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   807
295
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   808
static inline int writemask_xyzw(const int writemask)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   809
{
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   810
    return (writemask == 0xF);  // 0xF == 1111. No explicit mask (full!).
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   811
} // writemask_xyzw
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   812
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   813
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   814
static inline int writemask_xyz(const int writemask)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   815
{
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   816
    return (writemask == 0x7);  // 0x7 == 0111. (that is: xyz)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   817
} // writemask_xyz
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   818
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   819
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   820
static inline int writemask_xy(const int writemask)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   821
{
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   822
    return (writemask == 0x3);  // 0x3 == 0011. (that is: xy)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   823
} // writemask_xy
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   824
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   825
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   826
static inline int writemask_x(const int writemask)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   827
{
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   828
    return (writemask == 0x1);  // 0x1 == 0001. (that is: x)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   829
} // writemask_x
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   830
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   831
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   832
static inline int writemask_y(const int writemask)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   833
{
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   834
    return (writemask == 0x2);  // 0x1 == 0010. (that is: y)
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   835
} // writemask_y
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   836
54ef3ccdfa58 Cleaned up explicit writemask tests.
Ryan C. Gordon <icculus@icculus.org>
parents: 294
diff changeset
   837
121
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   838
static inline int replicate_swizzle(const int swizzle)
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   839
{
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   840
    return ( (((swizzle >> 0) & 0x3) == ((swizzle >> 2) & 0x3)) &&
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   841
             (((swizzle >> 2) & 0x3) == ((swizzle >> 4) & 0x3)) &&
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   842
             (((swizzle >> 4) & 0x3) == ((swizzle >> 6) & 0x3)) );
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   843
} // replicate_swizzle
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   844
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   845
316
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   846
static inline int scalar_register(const RegisterType regtype, const int regnum)
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   847
{
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   848
    switch (regtype)
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   849
    {
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   850
        case REG_TYPE_DEPTHOUT:
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   851
        case REG_TYPE_CONSTBOOL:
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   852
        case REG_TYPE_PREDICATE:
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   853
        case REG_TYPE_LOOP:
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   854
            return 1;
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   855
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   856
        case REG_TYPE_MISCTYPE:
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   857
            if ( ((const MiscTypeType) regnum) == MISCTYPE_TYPE_FACE )
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   858
                return 1;
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   859
            return 0;
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   860
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   861
        default: break;
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   862
    } // switch
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   863
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   864
    return 0;
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   865
} // scalar_register
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   866
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
   867
292
f6c1a2ec6030 Added >= ps_2_0 state for TEXLD opcode, cleaned up swizzle checks.
Ryan C. Gordon <icculus@icculus.org>
parents: 291
diff changeset
   868
static inline int no_swizzle(const int swizzle)
f6c1a2ec6030 Added >= ps_2_0 state for TEXLD opcode, cleaned up swizzle checks.
Ryan C. Gordon <icculus@icculus.org>
parents: 291
diff changeset
   869
{
296
3744671ea39e Fixed logic error.
Ryan C. Gordon <icculus@icculus.org>
parents: 295
diff changeset
   870
    return (swizzle == 0xE4);  // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
292
f6c1a2ec6030 Added >= ps_2_0 state for TEXLD opcode, cleaned up swizzle checks.
Ryan C. Gordon <icculus@icculus.org>
parents: 291
diff changeset
   871
} // no_swizzle
f6c1a2ec6030 Added >= ps_2_0 state for TEXLD opcode, cleaned up swizzle checks.
Ryan C. Gordon <icculus@icculus.org>
parents: 291
diff changeset
   872
f6c1a2ec6030 Added >= ps_2_0 state for TEXLD opcode, cleaned up swizzle checks.
Ryan C. Gordon <icculus@icculus.org>
parents: 291
diff changeset
   873
165
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
   874
static inline int vecsize_from_writemask(const int m)
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
   875
{
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
   876
    return (m & 1) + ((m >> 1) & 1) + ((m >> 2) & 1) + ((m >> 3) & 1);
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
   877
} // vecsize_from_writemask
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
   878
329
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   879
static int allocate_scratch_register(Context *ctx)
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   880
{
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   881
    const int retval = ctx->scratch_registers++;
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   882
    if (retval >= ctx->max_scratch_registers)
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   883
        ctx->max_scratch_registers = retval + 1;
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   884
    return retval;
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   885
} // allocate_scratch_register
e2688732204e Generalized allocation of scratch registers in arb1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 328
diff changeset
   886
382
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   887
static int allocate_branch_label(Context *ctx)
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   888
{
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   889
    return ctx->assigned_branch_labels++;
ab4167f50aad Initial shot at REP/ENDREP in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 381
diff changeset
   890
} // allocate_branch_label
368
a34d36ff9228 Implemented IF, ELSE, and ENDIF in nv2 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 367
diff changeset
   891
121
37de8c6bb262 Fixed comparisons in GLSL profile and improved validation.
Ryan C. Gordon <icculus@icculus.org>
parents: 120
diff changeset
   892
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   893
// D3D stuff that's used in more than just the d3d profile...
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   894
141
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
   895
static const char swizzle_channels[] = { 'x', 'y', 'z', 'w' };
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
   896
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
   897
104
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   898
static const char *usagestrs[] = {
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   899
    "_position", "_blendweight", "_blendindices", "_normal", "_psize",
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   900
    "_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   901
    "_color", "_fog", "_depth", "_sample"
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   902
};
7019f99f17d0 Attribute parsing is closer to correct now.
Ryan C. Gordon <icculus@icculus.org>
parents: 103
diff changeset
   903
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   904
static const char *get_D3D_register_string(Context *ctx,
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   905
                                           RegisterType regtype,
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   906
                                           int regnum, char *regnum_str,
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   907
                                           size_t regnum_size)
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   908
{
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   909
    const char *retval = NULL;
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   910
    int has_number = 1;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   911
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   912
    switch (regtype)
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   913
    {
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   914
        case REG_TYPE_TEMP:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   915
            retval = "r";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   916
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   917
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   918
        case REG_TYPE_INPUT:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   919
            retval = "v";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   920
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   921
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   922
        case REG_TYPE_CONST:
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   923
            retval = "c";
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   924
            break;
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   925
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   926
        case REG_TYPE_ADDRESS:  // (or REG_TYPE_TEXTURE, same value.)
151
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   927
            retval = shader_is_vertex(ctx) ? "a" : "t";
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   928
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   929
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   930
        case REG_TYPE_RASTOUT:
34
ca4b727ad2ce [svn] Chopped out a bunch of D3D2GLSL_ crap.
icculus
parents: 33
diff changeset
   931
            switch ((RastOutType) regnum)
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   932
            {
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   933
                case RASTOUT_TYPE_POSITION: retval = "oPos"; break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   934
                case RASTOUT_TYPE_FOG: retval = "oFog"; break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   935
                case RASTOUT_TYPE_POINT_SIZE: retval = "oPts"; break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   936
            } // switch
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   937
            has_number = 0;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   938
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   939
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   940
        case REG_TYPE_ATTROUT:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   941
            retval = "oD";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   942
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   943
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   944
        case REG_TYPE_OUTPUT: // (or REG_TYPE_TEXCRDOUT, same value.)
151
1667680fe402 Cleaned up tests for shader type and version.
Ryan C. Gordon <icculus@icculus.org>
parents: 150
diff changeset
   945
            if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   946
                retval = "o";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   947
            else
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   948
                retval = "oT";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   949
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   950
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   951
        case REG_TYPE_CONSTINT:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   952
            retval = "i";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   953
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   954
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   955
        case REG_TYPE_COLOROUT:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   956
            retval = "oC";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   957
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   958
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   959
        case REG_TYPE_DEPTHOUT:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   960
            retval = "oDepth";
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   961
            has_number = 0;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   962
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   963
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   964
        case REG_TYPE_SAMPLER:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   965
            retval = "s";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   966
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   967
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   968
        case REG_TYPE_CONSTBOOL:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   969
            retval = "b";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   970
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   971
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   972
        case REG_TYPE_LOOP:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   973
            retval = "aL";
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   974
            has_number = 0;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   975
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   976
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   977
        case REG_TYPE_MISCTYPE:
317
74a9f3ae4534 Support for vFace and vPos registers.
Ryan C. Gordon <icculus@icculus.org>
parents: 316
diff changeset
   978
            switch ((const MiscTypeType) regnum)
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   979
            {
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   980
                case MISCTYPE_TYPE_POSITION: retval = "vPos"; break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   981
                case MISCTYPE_TYPE_FACE: retval = "vFace"; break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   982
            } // switch
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
   983
            has_number = 0;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   984
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   985
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   986
        case REG_TYPE_LABEL:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   987
            retval = "l";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   988
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   989
73
c368f50d89d2 Shrank chatty enum name.
Ryan C. Gordon <icculus@icculus.org>
parents: 72
diff changeset
   990
        case REG_TYPE_PREDICATE:
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   991
            retval = "p";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
   992
            break;
234
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   993
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   994
        //case REG_TYPE_TEMPFLOAT16:  // !!! FIXME: don't know this asm string
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   995
        default:
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   996
            fail(ctx, "unknown register type");
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   997
            retval = "???";
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   998
            has_number = 0;
c05582e582ad Cleaned up the CONST/CONST2/CONST3/CONST4 tapdance.
Ryan C. Gordon <icculus@icculus.org>
parents: 233
diff changeset
   999
            break;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1000
    } // switch
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1001
31
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
  1002
    if (has_number)
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
  1003
        snprintf(regnum_str, regnum_size, "%u", (uint) regnum);
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
  1004
    else
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
  1005
        regnum_str[0] = '\0';
ee4ff9d30b67 [svn] DCL support!
icculus
parents: 30
diff changeset
  1006
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1007
    return retval;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1008
} // get_D3D_register_string
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1009
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1010
67
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1011
#define AT_LEAST_ONE_PROFILE 0
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1012
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1013
#if !SUPPORT_PROFILE_D3D
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1014
#define PROFILE_EMITTER_D3D(op)
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1015
#else
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1016
#undef AT_LEAST_ONE_PROFILE
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1017
#define AT_LEAST_ONE_PROFILE 1
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1018
#define PROFILE_EMITTER_D3D(op) emit_D3D_##op,
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1019
165
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
  1020
static const char *make_D3D_srcarg_string_in_buf(Context *ctx,
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1021
                                                    const SourceArgInfo *arg,
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1022
                                                    char *buf, size_t buflen)
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1023
{
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1024
    const char *premod_str = "";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1025
    const char *postmod_str = "";
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1026
    switch (arg->src_mod)
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1027
    {
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1028
        case SRCMOD_NEGATE:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1029
            premod_str = "-";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1030
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1031
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1032
        case SRCMOD_BIASNEGATE:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1033
            premod_str = "-";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1034
            // fall through.
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1035
        case SRCMOD_BIAS:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1036
            postmod_str = "_bias";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1037
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1038
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1039
        case SRCMOD_SIGNNEGATE:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1040
            premod_str = "-";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1041
            // fall through.
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1042
        case SRCMOD_SIGN:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1043
            postmod_str = "_bx2";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1044
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1045
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1046
        case SRCMOD_COMPLEMENT:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1047
            premod_str = "1-";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1048
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1049
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1050
        case SRCMOD_X2NEGATE:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1051
            premod_str = "-";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1052
            // fall through.
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1053
        case SRCMOD_X2:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1054
            postmod_str = "_x2";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1055
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1056
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1057
        case SRCMOD_DZ:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1058
            postmod_str = "_dz";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1059
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1060
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1061
        case SRCMOD_DW:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1062
            postmod_str = "_dw";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1063
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1064
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1065
        case SRCMOD_ABSNEGATE:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1066
            premod_str = "-";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1067
            // fall through.
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1068
        case SRCMOD_ABS:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1069
            postmod_str = "_abs";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1070
            break;
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1071
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1072
        case SRCMOD_NOT:
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1073
            premod_str = "!";
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1074
            break;
48
86dc2c772d26 [svn] Enabled -Wall, found some bugs.
icculus
parents: 46
diff changeset
  1075
86dc2c772d26 [svn] Enabled -Wall, found some bugs.
icculus
parents: 46
diff changeset
  1076
        case SRCMOD_NONE:
86dc2c772d26 [svn] Enabled -Wall, found some bugs.
icculus
parents: 46
diff changeset
  1077
        case SRCMOD_TOTAL:
86dc2c772d26 [svn] Enabled -Wall, found some bugs.
icculus
parents: 46
diff changeset
  1078
             break;  // stop compiler whining.
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1079
    } // switch
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1080
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1081
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1082
    char regnum_str[16];
56
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
  1083
    const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
  1084
                                                      arg->regnum, regnum_str,
1e8b180d3eb1 [svn] Bunch of work from the transatlantic airplane flight...since this is a ton of
icculus
parents: 55
diff changeset
  1085
                                                      sizeof (regnum_str));
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1086
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1087
    if (regtype_str == NULL)
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1088
    {
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1089
        fail(ctx, "Unknown source register type.");
68
f00ba7fcd0f8 Better const char * correction.
Ryan C. Gordon <icculus@icculus.org>
parents: 67
diff changeset
  1090
        return "";
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1091
    } // if
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1092
152
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1093
    const char *rel_lbracket = "";
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1094
    const char *rel_rbracket = "";
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1095
    char rel_swizzle[4] = { '\0' };
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1096
    char rel_regnum_str[16] = { '\0' };
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1097
    const char *rel_regtype_str = "";
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1098
    if (arg->relative)
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1099
    {
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1100
        rel_swizzle[0] = '.';
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1101
        rel_swizzle[1] = swizzle_channels[arg->relative_component];
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1102
        rel_swizzle[2] = '\0';
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1103
        rel_lbracket = "[";
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1104
        rel_rbracket = "]";
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1105
        rel_regtype_str = get_D3D_register_string(ctx, arg->relative_regtype,
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1106
                                                  arg->relative_regnum,
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1107
                                                  rel_regnum_str,
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1108
                                                  sizeof (rel_regnum_str));
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1109
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1110
        if (regtype_str == NULL)
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1111
        {
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1112
            fail(ctx, "Unknown relative source register type.");
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1113
            return "";
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1114
        } // if
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1115
    } // if
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1116
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1117
    char swizzle_str[6];
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1118
    int i = 0;
316
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
  1119
    const int scalar = scalar_register(arg->regtype, arg->regnum);
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
  1120
    if (!scalar && !no_swizzle(arg->swizzle))
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1121
    {
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1122
        swizzle_str[i++] = '.';
141
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
  1123
        swizzle_str[i++] = swizzle_channels[arg->swizzle_x];
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
  1124
        swizzle_str[i++] = swizzle_channels[arg->swizzle_y];
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
  1125
        swizzle_str[i++] = swizzle_channels[arg->swizzle_z];
997857107c39 Attempt to optimize CMP and CND in GLSL profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 140
diff changeset
  1126
        swizzle_str[i++] = swizzle_channels[arg->swizzle_w];
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1127
25
9664b0f62af6 [svn] Simplified and corrected source arg swizzle.
icculus
parents: 24
diff changeset
  1128
        // .xyzz is the same as .xyz, .z is the same as .zzzz, etc.
9664b0f62af6 [svn] Simplified and corrected source arg swizzle.
icculus
parents: 24
diff changeset
  1129
        while (swizzle_str[i-1] == swizzle_str[i-2])
9664b0f62af6 [svn] Simplified and corrected source arg swizzle.
icculus
parents: 24
diff changeset
  1130
            i--;
24
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1131
    } // if
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1132
    swizzle_str[i] = '\0';
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1133
    assert(i < sizeof (swizzle_str));
2177c6bee93d [svn] First shot at source args in D3D profile.
icculus
parents: 22
diff changeset
  1134
152
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1135
    snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s",
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1136
             premod_str, regtype_str, regnum_str, postmod_str,
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1137
             rel_lbracket, rel_regtype_str, rel_regnum_str, rel_swizzle,
cc7c38dfe145 Relative addressing work.
Ryan C. Gordon <icculus@icculus.org>
parents: 151
diff changeset
  1138
             rel_rbracket, swizzle_str);
67
cc42106d11ec Fixed dest arg output.
Ryan C. Gordon <icculus@icculus.org>
parents: 66
diff changeset
  1139
    // !!! FIXME: make sure the scratch buffer was large enough.
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1140
    return buf;
165
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
  1141
} // make_D3D_srcarg_string_in_buf
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1142
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1143
161
a0e1920ce909 Removed "dest_args" array...it's only ever one structure.
Ryan C. Gordon <icculus@icculus.org>
parents: 160
diff changeset
  1144
static const char *make_D3D_destarg_string(Context *ctx)
a0e1920ce909 Removed "dest_args" array...it's only ever one structure.
Ryan C. Gordon <icculus@icculus.org>
parents: 160
diff changeset
  1145
{
a0e1920ce909 Removed "dest_args" array...it's only ever one structure.
Ryan C. Gordon <icculus@icculus.org>
parents: 160
diff changeset
  1146
    const DestArgInfo *arg = &ctx->dest_arg;
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1147
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1148
    const char *result_shift_str = "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1149
    switch (arg->result_shift)
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1150
    {
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1151
        case 0x1: result_shift_str = "_x2"; break;
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1152
        case 0x2: result_shift_str = "_x4"; break;
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1153
        case 0x3: result_shift_str = "_x8"; break;
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1154
        case 0xD: result_shift_str = "_d8"; break;
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1155
        case 0xE: result_shift_str = "_d4"; break;
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1156
        case 0xF: result_shift_str = "_d2"; break;
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1157
    } // switch
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1158
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1159
    const char *sat_str = (arg->result_mod & MOD_SATURATE) ? "_sat" : "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1160
    const char *pp_str = (arg->result_mod & MOD_PP) ? "_pp" : "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1161
    const char *cent_str = (arg->result_mod & MOD_CENTROID) ? "_centroid" : "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1162
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1163
    char regnum_str[16];
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1164
    const char *regtype_str = get_D3D_register_string(ctx, arg->regtype,
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1165
                                                      arg->regnum, regnum_str,
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1166
                                                      sizeof (regnum_str));
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1167
    if (regtype_str == NULL)
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1168
    {
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1169
        fail(ctx, "Unknown destination register type.");
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1170
        return "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1171
    } // if
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1172
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1173
    char writemask_str[6];
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1174
    int i = 0;
316
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
  1175
    const int scalar = scalar_register(arg->regtype, arg->regnum);
93e70dbdba48 Deal with scalar D3D registers more properly.
Ryan C. Gordon <icculus@icculus.org>
parents: 315
diff changeset
  1176
    if (!scalar && !writemask_xyzw(arg->writemask))
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1177
    {
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1178
        writemask_str[i++] = '.';
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1179
        if (arg->writemask0) writemask_str[i++] = 'x';
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1180
        if (arg->writemask1) writemask_str[i++] = 'y';
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1181
        if (arg->writemask2) writemask_str[i++] = 'z';
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1182
        if (arg->writemask3) writemask_str[i++] = 'w';
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1183
    } // if
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1184
    writemask_str[i] = '\0';
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1185
    assert(i < sizeof (writemask_str));
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1186
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1187
    const char *pred_left = "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1188
    const char *pred_right = "";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1189
    char pred[32] = { '\0' };
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1190
    if (ctx->predicated)
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1191
    {
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1192
        pred_left = "(";
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1193
        pred_right = ") ";
165
708de76f4c36 More swizzle/writemask work.
Ryan C. Gordon <icculus@icculus.org>
parents: 164
diff changeset
  1194
        make_D3D_srcarg_string_in_buf(ctx, &ctx->predicate_arg,
139
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1195
                                         pred, sizeof (pred));
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1196
    } // if
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1197
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1198
    // may turn out something like "_x2_sat_pp_centroid (!p0.x) r0.xyzw" ...
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1199
    char *retval = get_scratch_buffer(ctx);
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1200
    snprintf(retval, SCRATCH_BUFFER_SIZE, "%s%s%s%s %s%s%s%s%s%s",
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1201
             result_shift_str, sat_str, pp_str, cent_str,
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1202
             pred_left, pred, pred_right,
a7695f68cbff First shot at predicated instruction support.
Ryan C. Gordon <icculus@icculus.org>
parents: 138
diff changeset
  1203
             regtype_str, regnum_str, writemask_str);