mojoshader_opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 Apr 2008 02:22:29 -0400
branchtrunk
changeset 206 7027d889acdd
parent 205 8583f89985df
child 207 d9cee469e080
permissions -rw-r--r--
Implemented MOJOSHADER_glProgramReady().
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
/* !!! FIXME: merge this in?
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
static GLenum opengl_attr_type(const MOJOSHADER_attributeType type)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
    switch (type)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
        case MOJOSHADER_ATTRIBUTE_BYTE: return GL_BYTE;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
        case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_UNSIGNED_BYTE;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
        case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
        case MOJOSHADER_ATTRIBUTE_USHORT: return GL_USHORT;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
        case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
        case MOJOSHADER_ATTRIBUTE_UINT: return GL_UNSIGNED_INT;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
        case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
        case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    } // switch
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
    return GL_NONE;  // oh well. Raises a GL error later.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
} // opengl_attr_type
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
static GLenum opengl_attr_index(const MOJOSHADER_usage usage, const int index)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    switch (usage)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
        case MOJOSHADER_USAGE_POSITION,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
        case MOJOSHADER_USAGE_BLENDWEIGHT,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
        case MOJOSHADER_USAGE_BLENDINDICES,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
        case MOJOSHADER_USAGE_NORMAL,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
        case MOJOSHADER_USAGE_POINTSIZE,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
        case MOJOSHADER_USAGE_TEXCOORD,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
        case MOJOSHADER_USAGE_TANGENT,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
        case MOJOSHADER_USAGE_BINORMAL,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        case MOJOSHADER_USAGE_TESSFACTOR,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
        case MOJOSHADER_USAGE_POSITIONT,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
        case MOJOSHADER_USAGE_COLOR,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
        case MOJOSHADER_USAGE_FOG,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        case MOJOSHADER_USAGE_DEPTH,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
        case MOJOSHADER_USAGE_SAMPLE,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
        case MOJOSHADER_USAGE_TOTAL: return GL_NONE;  // stop compiler whining.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    } // switch
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    return GL_NONE;  // will fail later.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
} // opengl_attr_index
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
void MOJOSHADER_glSetVertexDeclaration(MOJOSHADER_usage usage, int index,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
                                       unsigned int size,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
                                       MOJOSHADER_attributeType type,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
                                       int normalized, unsigned int stride,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
                                       const void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    const GLuint gl_index = opengl_attr_index(usage, index);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
    const GLenum gl_type = opengl_attr_type(type);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    const GLboolean gl_normalized = (normalized) ? GL_TRUE : GL_FALSE;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    pglVertexAttribPointer(gl_index, size, gl_type, gl_normalized,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
                           stride, ptr);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
} // MOJOSHADER_glSetVertexDeclaration
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
*/
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
#include <stdio.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
#include <string.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
#include <stdlib.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
#include <stdint.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
#include <stdarg.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
#include <assert.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
#include "mojoshader.h"
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
// Get basic wankery out of the way here...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
typedef unsigned int uint;  // this is a printf() helper. don't use for code.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
typedef uint8_t uint8;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
typedef uint32_t uint32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
typedef int32_t int32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
struct MOJOSHADER_glShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    const MOJOSHADER_parseData *parseData;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    GLuint handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    87
typedef struct
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    88
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
    89
    MOJOSHADER_shaderType shader_type;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    90
    MOJOSHADER_uniform *uniform;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    91
    GLint location;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    92
} UniformMap;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    93
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
struct MOJOSHADER_glProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    const MOJOSHADER_glShader *vertex;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    const MOJOSHADER_glShader *fragment;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
    GLuint handle;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    99
    uint32 uniform_count;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   100
    UniformMap uniforms;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
// Allocators...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
static MOJOSHADER_malloc malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
static MOJOSHADER_free free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
static void *malloc_data = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
// The constant register files...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
// Man, it kills me how much memory this takes...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
static float vs_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
static int vs_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
static uint8 vs_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
static float ps_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
static int ps_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
static uint8 ps_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
// GL stuff...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
static MOJOSHADER_glProgram *bound_program = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
// #define this to force app to supply an allocator, so there's no reference
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
//  to the C runtime's malloc() and free()...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
#if MOJOSHADER_FORCE_ALLOCATOR
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
#define internal_malloc NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
#define internal_free NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
#else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
static void *internal_malloc(int bytes, void *d) { return malloc(bytes); }
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
static void internal_free(void *ptr, void *d) { free(ptr); }
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
#endif
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
static inline void *Malloc(const size_t len)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    return malloc_fn(len, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
} // Malloc
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
static inline void Free(void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    return free_fn(ptr, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
} // Free
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
int MOJOSHADER_glInit(const char *_profile,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
                      void *(*lookup)(const char *fnname),
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
                      MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) != 0)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        profile = MOJOSHADER_PROFILE_GLSL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
        return 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
    // !!! FIXME: lookup glGetString(), check extensions.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
    malloc_fn = (m == NULL) ? internal_malloc : m;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
    free_fn = (f == NULL) ? internal_free : f;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
    malloc_data = d;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    // !!! FIXME: lookup other entry points.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
    memset(vs_register_file_f, '\0', sizeof (vs_register_file_f));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    memset(vs_register_file_i, '\0', sizeof (vs_register_file_i));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    memset(vs_register_file_b, '\0', sizeof (vs_register_file_b));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    memset(ps_register_file_f, '\0', sizeof (ps_register_file_f));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    memset(ps_register_file_i, '\0', sizeof (ps_register_file_i));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
    memset(ps_register_file_b, '\0', sizeof (ps_register_file_b));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    MOJOSHADER_glBindProgram(NULL);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    return 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
} // MOJOSHADER_glInit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
                                                const unsigned int bufsize)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    MOJOSHADER_glShader *retval = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
                                                      bufsize, malloc_fn,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
                                                      free_fn, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    if (pd->error != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        MOJOSHADER_freeParseData(pd);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        return NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    if (retval == NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
        MOJOSHADER_freeParseData(pd);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
        return NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
    
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
    GLint ok = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER_ARB : GL_VERTEX_SHADER_ARB;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    GLint shaderlen = (GLint) pd->output_len;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    const GLhandleARB shader = pglCreateShaderObjectARB(shader_type);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    pglShaderSourceARB(shader, 1, (const GLcharARB **) &pd->output, &shaderlen);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
    pglCompileShaderARB(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
    pglGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
    if (!ok)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        GLcharARB err[1024];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
        GLsizei len = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
        //glGetInfoLogARB(shader, sizeof (err), &len, err);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
        //printf("FAIL: %s glsl compile: %s\n", fname, err);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
        pglDeleteObjectARB(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
        MOJOSHADER_freeParseData(pd);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
        Free(retval);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
        return NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    retval->parseData = pd;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    retval->handle = shader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    retval->refcount = 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
    return retval;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
} // MOJOSHADER_glCompileShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
static void shader_unref(MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
    if (shader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
        {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
            pglDeleteObjectARB(shader->handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
            MOJOSHADER_freeParseData(shader->parseData);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
            Free(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
} // shader_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
static void program_unref(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
        {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
            pglDeleteObjectARB(program->handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
            shader_unref(program->vertex);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
            shader_unref(program->fragment);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   250
            Free(program->uniforms);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
            Free(program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
} // program_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   257
static void lookup_uniforms(MOJOSHADER_glProgram *program,
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   258
                            MOJOSHADER_glShader *shader)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   259
{
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   260
    int i;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   261
    const MOJOSHADER_parseData *pd = shader->parseData;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   262
    const MOJOSHADER_uniforms *u = pd->uniforms;
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   263
    const MOJOSHADER_shaderType shader_type = pd->shader_type;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   264
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   265
    for (i = 0; i < pd->uniform_count; i++)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   266
    {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   267
        const GLint loc = pglGetUniformLocationARB(program->handle, u[i].name);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   268
        if (loc != -1)  // maybe the Uniform was optimized out?
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   269
        {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   270
            UniformMap *map = &program->uniforms[program->uniform_count];
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   271
            map->shader_type = shader_type;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   272
            map->uniform = &u[i];
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   273
            map->location = loc;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   274
            program->uniform_count++;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   275
        } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   276
    } // for
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   277
} // lookup_uniforms
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   278
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   279
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
                                               MOJOSHADER_glShader *pshader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
{
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   283
    MOJOSHADER_glProgram *retval = NULL;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   284
    GLuint program = 0;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   285
    int numregs = 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   287
    if (vshader != NULL) numregs += vshader->parseData->uniform_count;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   288
    if (pshader != NULL) numregs += pshader->parseData->uniform_count;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   289
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   290
    // !!! FIXME: actually link.
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   292
    retval = (MOJOSHADER_glProgram *) Malloc(sizeof (MOJOSHADER_glProgram));
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   293
    if (retval == NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   294
        goto link_program_fail;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   295
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   296
    memset(retval, '\0', sizeof (MOJOSHADER_glProgram));
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   297
    retval->uniforms = (UniformMap *) Malloc(sizeof (UniformMap) * numregs);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   298
    if (retval->uniforms == NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   299
        goto link_program_fail;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   300
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   301
    retval->handle = program;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   302
    retval->vertex = vshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   303
    retval->fragment = pshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   304
    retval->refcount = 1;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    if (vshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   307
    {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   308
        lookup_uniforms(retval, vshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
        vshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   310
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   311
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    if (pshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   313
    {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   314
        lookup_uniforms(retval, pshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
        pshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   316
    } // if
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   317
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   318
    return retval;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   319
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   320
link_program_fail:
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   321
    if (retval != NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   322
    {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   323
        if (retval->uniforms != NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   324
            Free(retval->uniforms);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   325
        Free(retval);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   326
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   327
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   328
    if (program != 0)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   329
        pglDeleteObjectARB(program);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   330
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
} // MOJOSHADER_glLinkProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    GLuint handle = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
        handle = program->handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
        program->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   342
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    // !!! FIXME: unbind client-side arrays.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    pglUseProgramObjectARB(handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    program_unref(bound_program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
    bound_program = program;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
} // MOJOSHADER_glBindProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   351
static inline uint maxuint(const uint a, const uint b)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   352
{
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   353
    return ((a > b) ? a : b);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   354
} // maxuint
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   355
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   356
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
void MOJOSHADER_glSetVertexShaderUniformF(unsigned int idx, const float *data,
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   358
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   360
    const uint maxregs = STATICARRAYLEN(vs_register_file_f) / 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   361
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   362
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   363
        const uint cpy = maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   364
        memcpy(vs_register_file_f + (idx * 4), data, cpy);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   365
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
} // MOJOSHADER_glSetVertexShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
void MOJOSHADER_glSetVertexShaderUniformI(unsigned int idx, const int *data,
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   370
                                          unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   372
    const uint maxregs = STATICARRAYLEN(vs_register_file_i) / 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   373
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   374
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   375
        const uint cpy = maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   376
        memcpy(vs_register_file_i + (idx * 4), data, cpy);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   377
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
} // MOJOSHADER_glSetVertexShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   380
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   381
void MOJOSHADER_glSetVertexShaderUniformB(unsigned int idx, const int *data,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   384
    const uint maxregs = STATICARRAYLEN(vs_register_file_f) / 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   385
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   386
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   387
        uint8 *wptr = vs_register_file_b + idx;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   388
        uint8 *endptr = wptr + maxuint(maxregs - idx, bcount);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   389
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   390
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   391
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
} // MOJOSHADER_glSetVertexShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   395
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   396
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   398
    const uint maxregs = STATICARRAYLEN(ps_register_file_f) / 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   399
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   400
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   401
        const uint cpy = maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   402
        memcpy(ps_register_file_f + (idx * 4), data, cpy);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   403
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
} // MOJOSHADER_glSetPixelShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   406
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   407
void MOJOSHADER_glSetPixelShaderUniformI(unsigned int idx, const int *data,
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   408
                                         unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   410
    const uint maxregs = STATICARRAYLEN(ps_register_file_i) / 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   411
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   412
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   413
        const uint cpy = maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   414
        memcpy(ps_register_file_i + (idx * 4), data, cpy);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   415
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   416
} // MOJOSHADER_glSetPixelShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   422
    const uint maxregs = STATICARRAYLEN(ps_register_file_f) / 4;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   423
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   424
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   425
        uint8 *wptr = ps_register_file_b + idx;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   426
        uint8 *endptr = wptr + maxuint(maxregs - idx, bcount);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   427
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   428
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   429
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
} // MOJOSHADER_glSetPixelShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   431
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   432
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   433
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
                                     int index, unsigned int size,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
                                     MOJOSHADER_attributeType type,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   436
                                     int normalized, unsigned int stride,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   437
                                     const void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   438
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   439
} // MOJOSHADER_glSetVertexAttribute
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   440
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   441
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   442
void MOJOSHADER_glProgramReady(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   444
    int i;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   445
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   446
    if (bound_program == NULL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   447
        return;  // nothing to do.
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   448
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   449
    // !!! FIXME: don't push Uniforms if we know they haven't changed.
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   450
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   451
    // push Uniforms to the program from our register files...
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   452
    for (i = 0; i < bound_program->uniform_count; i++)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   453
    {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   454
        const UniformMap *map = &bound_program->uniforms[i];
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   455
        const MOJOSHADER_uniform *u = map->uniform;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   456
        const MOJOSHADER_uniformType type = u->type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   457
        const MOJOSHADER_shaderType shader_type = map->shader_type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   458
        const int index = u->index;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   459
        const GLint location = map->location;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   460
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   461
        if (shader_type == MOJOSHADER_TYPE_VERTEX)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   462
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   463
            if (type == MOJOSHADER_UNIFORM_FLOAT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   464
                pglUniform4fvARB(location, 1, &vs_register_file_f[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   465
            else if (type == MOJOSHADER_UNIFORM_INT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   466
                pglUniform4ivARB(location, 1, &vs_register_file_i[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   467
            else if (type == MOJOSHADER_UNIFORM_BOOL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   468
                pglUniform1iARB(location, vs_register_file_b[index]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   469
        } // if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   470
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   471
        else if (shader_type == MOJOSHADER_TYPE_PIXEL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   472
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   473
            if (type == MOJOSHADER_UNIFORM_FLOAT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   474
                pglUniform4fvARB(location, 1, &ps_register_file_f[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   475
            else if (type == MOJOSHADER_UNIFORM_INT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   476
                pglUniform4ivARB(location, 1, &ps_register_file_i[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   477
            else if (type == MOJOSHADER_UNIFORM_BOOL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   478
                pglUniform1iARB(location, ps_register_file_b[index]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   479
        } // else if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   480
    } // for
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   481
} // MOJOSHADER_glProgramReady
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   483
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   484
void MOJOSHADER_glDeleteProgram(const MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   485
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
    program_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
} // MOJOSHADER_glDeleteProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
void MOJOSHADER_glDeleteShader(const MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    shader_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
} // MOJOSHADER_glDeleteShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
void MOJOSHADER_glDeinit(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   498
    MOJOSHADER_glBindProgram(NULL);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   499
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   500
    profile = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   501
    malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   502
    free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   503
    malloc_data = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   504
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
    // !!! FIXME: NULL entry points.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
} // MOJOSHADER_glDeinit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   508
// end of mojoshader_opengl.c ...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   509