mojoshader_opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 Apr 2008 00:44:18 -0400
branchtrunk
changeset 203 ff983e373e37
parent 202 20bdf2d7036e
child 204 5055ac80916e
permissions -rw-r--r--
Implemented uniform array setting in OpenGL glue code.
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
struct MOJOSHADER_glProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
    const MOJOSHADER_glShader *vertex;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
    const MOJOSHADER_glShader *fragment;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    GLuint handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
// Allocators...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
static MOJOSHADER_malloc malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
static MOJOSHADER_free free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
static void *malloc_data = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
// The constant register files...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
// 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
   102
static float vs_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
static int vs_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
static uint8 vs_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
static float ps_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
static int ps_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
static uint8 ps_register_file_b[2047];
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
// GL stuff...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
static MOJOSHADER_glProgram *bound_program = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
// #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
   114
//  to the C runtime's malloc() and free()...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
#if MOJOSHADER_FORCE_ALLOCATOR
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
#define internal_malloc NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
#define internal_free NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
#else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
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
   120
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
   121
#endif
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
static inline void *Malloc(const size_t len)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    return malloc_fn(len, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
} // Malloc
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
static inline void Free(void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    return free_fn(ptr, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
} // Free
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
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
int MOJOSHADER_glInit(const char *_profile,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
                      void *(*lookup)(const char *fnname),
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
                      MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) != 0)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
        profile = MOJOSHADER_PROFILE_GLSL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
        return 0;
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
    // !!! FIXME: lookup glGetString(), check extensions.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
    malloc_fn = (m == NULL) ? internal_malloc : m;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    free_fn = (f == NULL) ? internal_free : f;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
    malloc_data = d;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
    // !!! FIXME: lookup other entry points.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    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
   152
    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
   153
    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
   154
    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
   155
    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
   156
    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
   157
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    MOJOSHADER_glBindProgram(NULL);
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
    return 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
} // MOJOSHADER_glInit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
                                                const unsigned int bufsize)
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_glShader *retval = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
                                                      bufsize, malloc_fn,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
                                                      free_fn, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
    if (pd->error != NULL)
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_freeParseData(pd);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
        return NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
    retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    if (retval == NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
        MOJOSHADER_freeParseData(pd);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        return NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
    
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
    GLint ok = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
    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
   186
    GLint shaderlen = (GLint) pd->output_len;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
    const GLhandleARB shader = pglCreateShaderObjectARB(shader_type);
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
    pglShaderSourceARB(shader, 1, (const GLcharARB **) &pd->output, &shaderlen);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    pglCompileShaderARB(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
    pglGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
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
    if (!ok)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
        GLcharARB err[1024];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        GLsizei len = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        //glGetInfoLogARB(shader, sizeof (err), &len, err);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        //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
   199
        pglDeleteObjectARB(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        MOJOSHADER_freeParseData(pd);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
        Free(retval);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        return NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    retval->parseData = pd;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    retval->handle = shader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
    retval->refcount = 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    return retval;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
} // MOJOSHADER_glCompileShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   211
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
static void shader_unref(MOJOSHADER_glShader *shader)
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
    if (shader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
        else
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
            pglDeleteObjectARB(shader->handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
            MOJOSHADER_freeParseData(shader->parseData);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
            Free(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
} // shader_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
static void program_unref(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   232
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
        else
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
            pglDeleteObjectARB(program->handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
            shader_unref(program->vertex);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
            shader_unref(program->fragment);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
            Free(program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
} // program_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
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
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
                                               MOJOSHADER_glShader *pshader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
    // !!! FIXME: actually link.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
    // !!! FIXME: alloc retval.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
    retval->vertex = vshader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
    retval->fragment = pshader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
    retval->handle = program;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
    retval->refcount = 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
    if (vshader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
        vshader->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
    if (pshader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
        pshader->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
} // MOJOSHADER_glLinkProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    GLuint handle = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        handle = program->handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
        program->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    // !!! FIXME: unbind client-side arrays.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
    pglUseProgramObjectARB(handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
    program_unref(bound_program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    bound_program = program;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
} // MOJOSHADER_glBindProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   283
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
   284
{
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   285
    return ((a > b) ? a : b);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   286
} // maxuint
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   287
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   288
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
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
   290
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   292
    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
   293
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   294
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   295
        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
   296
        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
   297
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
} // MOJOSHADER_glSetVertexShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
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
   302
                                          unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   304
    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
   305
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   306
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   307
        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
   308
        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
   309
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
} // MOJOSHADER_glSetVertexShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
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
   314
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   316
    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
   317
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   318
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   319
        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
   320
        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
   321
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   322
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   323
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
} // MOJOSHADER_glSetVertexShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
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
   328
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   330
    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
   331
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   332
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   333
        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
   334
        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
   335
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
} // MOJOSHADER_glSetPixelShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
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
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
   340
                                         unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   342
    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
   343
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   344
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   345
        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
   346
        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
   347
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
} // MOJOSHADER_glSetPixelShaderUniformI
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
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
   352
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   354
    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
   355
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   356
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   357
        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
   358
        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
   359
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   360
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   361
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
} // MOJOSHADER_glSetPixelShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
                                     int index, unsigned int size,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
                                     MOJOSHADER_attributeType type,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
                                     int normalized, unsigned int stride,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
                                     const void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
} // MOJOSHADER_glSetVertexAttribute
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
void MOJOSHADER_glProgramReady(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   376
} // MOJOSHADER_glProgramReady
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   377
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   378
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
void MOJOSHADER_glDeleteProgram(const MOJOSHADER_glProgram *program)
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
    program_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
} // MOJOSHADER_glDeleteProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
void MOJOSHADER_glDeleteShader(const MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
    shader_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
} // MOJOSHADER_glDeleteShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   390
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
void MOJOSHADER_glDeinit(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
    MOJOSHADER_glBindProgram(NULL);
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
    profile = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
    malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
    free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
    malloc_data = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
    // !!! FIXME: NULL entry points.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
} // MOJOSHADER_glDeinit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
// end of mojoshader_opengl.c ...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404