mojoshader_opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 Apr 2008 05:04:38 -0400
branchtrunk
changeset 220 df5ea69833d5
parent 219 61167fc96790
child 221 328ac819b965
permissions -rw-r--r--
Fixes to Malloc() and Free() in mojoshader_opengl.c ...
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
#include <stdio.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <string.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <stdlib.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <stdint.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include <stdarg.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include <assert.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include "mojoshader.h"
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
// Get basic wankery out of the way here...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
208
30fe7b63db09 Added STATICARRAYLEN macro to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 207
diff changeset
    12
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
30fe7b63db09 Added STATICARRAYLEN macro to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 207
diff changeset
    13
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
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
    15
typedef uint8_t uint8;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
typedef uint32_t uint32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
typedef int32_t int32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
struct MOJOSHADER_glShader
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
    const MOJOSHADER_parseData *parseData;
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
    22
    GLhandleARB handle;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
    uint32 refcount;
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
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    26
typedef struct
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    27
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
    28
    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
    29
    MOJOSHADER_uniform *uniform;
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
    30
    GLuint location;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    31
} UniformMap;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    32
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    33
typedef struct
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    34
{
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    35
    MOJOSHADER_attribute *attribute;
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
    36
    GLuint location;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    37
} AttributeMap;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    38
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
struct MOJOSHADER_glProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    const MOJOSHADER_glShader *vertex;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    const MOJOSHADER_glShader *fragment;
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
    43
    GLhandleARB handle;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    44
    uint32 uniform_count;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    45
    UniformMap uniforms;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    46
    uint32 attribute_count;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    47
    AttributeMap attributes;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
// Allocators...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
static MOJOSHADER_malloc malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
static MOJOSHADER_free free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
static void *malloc_data = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
// The constant register files...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
// 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
    58
static float vs_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
static int vs_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
static uint8 vs_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
static float ps_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
static int ps_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
static uint8 ps_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
// GL stuff...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
static MOJOSHADER_glProgram *bound_program = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    68
// Error state...
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    69
static char error_buffer[1024] = { '\0' };
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    70
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    71
static void set_error(const char *str)
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    72
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    73
    snprintf(error_buffer, sizeof (error_buffer), "%s", str);
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    74
} // set_error
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
    75
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
// #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
    78
//  to the C runtime's malloc() and free()...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
#if MOJOSHADER_FORCE_ALLOCATOR
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
#define internal_malloc NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
#define internal_free NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
#else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
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
    84
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
    85
#endif
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
static inline void *Malloc(const size_t len)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
{
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
    89
    void *retval = malloc_fn(len, malloc_data);
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
    90
    if (retval == NULL)
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
    91
        set_error("out of memory");
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
    92
    return retval;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
} // Malloc
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
static inline void Free(void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
{
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
    97
    if (ptr != NULL)
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
    98
        free_fn(ptr, malloc_data);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
} // Free
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   102
const char *MOJOSHADER_glGetError(void)
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   103
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   104
    return error_buffer;
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   105
} // MOJOSHADER_glGetError
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   106
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   107
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
int MOJOSHADER_glInit(const char *_profile,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
                      void *(*lookup)(const char *fnname),
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
                      MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
{
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   112
    error_buffer[0] = '\0';
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   113
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
    if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) != 0)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
        profile = MOJOSHADER_PROFILE_GLSL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
    else
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   117
    {
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   118
        set_error("unknown profile");
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        return 0;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   120
    } // else
202
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
    // !!! FIXME: lookup glGetString(), check extensions.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
    malloc_fn = (m == NULL) ? internal_malloc : m;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
    free_fn = (f == NULL) ? internal_free : f;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
    malloc_data = d;
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
    // !!! FIXME: lookup other entry points.
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
    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
   131
    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
   132
    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
   133
    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
   134
    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
   135
    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
   136
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
    MOJOSHADER_glBindProgram(NULL);
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 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
} // MOJOSHADER_glInit
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
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
                                                const unsigned int bufsize)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
    MOJOSHADER_glShader *retval = NULL;
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   147
    GLhandleARB shader = 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
                                                      bufsize, malloc_fn,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
                                                      free_fn, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
    if (pd->error != NULL)
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   152
    {
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   153
        set_error(pd->error);
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   154
        goto compile_shader_fail;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   155
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
    retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
    if (retval == NULL)
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   159
        goto compile_shader_fail;
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   160
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    GLint ok = 0;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    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
   163
    GLint shaderlen = (GLint) pd->output_len;
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   164
    shader = pglCreateShaderObjectARB(shader_type);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
    pglShaderSourceARB(shader, 1, (const GLcharARB **) &pd->output, &shaderlen);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    pglCompileShaderARB(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    pglGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    if (!ok)
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
        GLsizei len = 0;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   173
        pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   174
                         (GLcharARB *) error_buffer);
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   175
        goto compile_shader_fail;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
    retval->parseData = pd;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
    retval->handle = shader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
    retval->refcount = 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
    return retval;
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   182
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   183
compile_shader_fail:
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   184
    MOJOSHADER_freeParseData(pd);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   185
    Free(retval);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   186
    if (shader != 0)
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   187
        pglDeleteObjectARB(shader);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   188
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
} // MOJOSHADER_glCompileShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
static void shader_unref(MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    if (shader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
            pglDeleteObjectARB(shader->handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
            MOJOSHADER_freeParseData(shader->parseData);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
            Free(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
} // shader_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   209
static void program_unref(MOJOSHADER_glProgram *program)
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
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
        {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
            pglDeleteObjectARB(program->handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
            shader_unref(program->vertex);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
            shader_unref(program->fragment);
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   221
            Free(program->attributes);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   222
            Free(program->uniforms);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
            Free(program);
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
} // program_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
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   229
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
   230
                            MOJOSHADER_glShader *shader)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   231
{
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   232
    int i;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   233
    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
   234
    const MOJOSHADER_uniforms *u = pd->uniforms;
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   235
    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
   236
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   237
    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
   238
    {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   239
        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
   240
        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
   241
        {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   242
            UniformMap *map = &program->uniforms[program->uniform_count];
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   243
            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
   244
            map->uniform = &u[i];
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
   245
            map->location = (GLuint) loc;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   246
            program->uniform_count++;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   247
        } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   248
    } // for
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   249
} // lookup_uniforms
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   250
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   251
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   252
static void lookup_attributes(MOJOSHADER_glProgram *program)
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   253
{
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   254
    int i;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   255
    const MOJOSHADER_parseData *pd = program->vertex->parseData;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   256
    const MOJOSHADER_attributes *a = pd->attributes;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   257
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   258
    for (i = 0; i < pd->attribute_count; i++)
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   259
    {
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   260
        const GLint loc = pglGetAttribLocationARB(program->handle, a->name);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   261
        if (loc != -1)  // maybe the Attribute was optimized out?
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   262
        {
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   263
            AttributeMap *map = &program->attributes[program->attribute_count];
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   264
            map->attribute = &a[i];
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
   265
            map->location = (GLuint) loc;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   266
            program->attribute_count++;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   267
        } // if
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   268
    } // for
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   269
} // lookup_attributes
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   270
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   271
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
                                               MOJOSHADER_glShader *pshader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
{
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   275
    if ((vshader == NULL) && (pshader == NULL))
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   276
        return NULL;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   277
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   278
    MOJOSHADER_glProgram *retval = NULL;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   279
    const GLhandleARB program = pglCreateProgramObjectARB();
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   280
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   281
    if (vshader != NULL) pglAttachObjectARB(program, vshader->handle);
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   282
    if (pshader != NULL) pglAttachObjectARB(program, pshader->handle);
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   283
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   284
    pglLinkProgramARB(program);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   286
    GLint ok = 0;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   287
    pglGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   288
    if (!ok)
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   289
    {
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   290
        GLsizei len = 0;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   291
        pglGetInfoLogARB(shader, sizeof (error_buffer), &len,
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   292
                         (GLcharARB *) error_buffer);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   293
        goto link_program_fail;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   294
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   296
    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
   297
    if (retval == NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   298
        goto link_program_fail;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   299
    memset(retval, '\0', sizeof (MOJOSHADER_glProgram));
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   300
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   301
    int numregs = 0;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   302
    if (vshader != NULL) numregs += vshader->parseData->uniform_count;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   303
    if (pshader != NULL) numregs += pshader->parseData->uniform_count;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   304
    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
   305
    if (retval->uniforms == NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   306
        goto link_program_fail;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   307
    memset(retval->uniforms, '\0', sizeof (UniformMap) * numregs);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   308
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   309
    retval->handle = program;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   310
    retval->vertex = vshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   311
    retval->fragment = pshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   312
    retval->refcount = 1;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    if (vshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   315
    {
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   316
        retval->attributes = (AttributeMap *) Malloc(sizeof (AttributeMap) *
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   317
                                        vshader->parseData->attribute_count);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   318
        if (retval->attributes == NULL)
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   319
            goto link_program_fail;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   320
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   321
        lookup_attributes(retval);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   322
        lookup_uniforms(retval, vshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
        vshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   324
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   325
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
    if (pshader != NULL)
205
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
        lookup_uniforms(retval, pshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
        pshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   330
    } // if
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   331
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   332
    return retval;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   333
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   334
link_program_fail:
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   335
    if (retval != NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   336
    {
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   337
        Free(retval->uniforms);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   338
        Free(retval->attributes);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   339
        Free(retval);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   340
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   341
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   342
    pglDeleteObjectARB(program);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   343
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
} // MOJOSHADER_glLinkProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
{
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
   349
    GLhandleARB handle = 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   352
        handle = program->handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
        program->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   355
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    // !!! FIXME: unbind client-side arrays.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   357
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    pglUseProgramObjectARB(handle);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    program_unref(bound_program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
    bound_program = program;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
} // MOJOSHADER_glBindProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   363
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   364
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
   365
{
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   366
    return ((a > b) ? a : b);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   367
} // maxuint
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   368
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   369
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
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
   371
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   373
    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
   374
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   375
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   376
        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
   377
        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
   378
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   379
} // MOJOSHADER_glSetVertexShaderUniformF
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   382
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
   383
                                          unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   385
    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
   386
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   387
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   388
        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
   389
        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
   390
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
} // MOJOSHADER_glSetVertexShaderUniformI
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
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
   395
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   397
    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
   398
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   399
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   400
        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
   401
        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
   402
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   403
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   404
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
} // MOJOSHADER_glSetVertexShaderUniformB
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
212
76eeb43e054c Whitespace fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 210
diff changeset
   409
                                         unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   411
    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
   412
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   413
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   414
        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
   415
        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
   416
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
} // MOJOSHADER_glSetPixelShaderUniformF
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
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
   421
                                         unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   423
    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
   424
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   425
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   426
        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
   427
        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
   428
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   429
} // MOJOSHADER_glSetPixelShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   430
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
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
212
76eeb43e054c Whitespace fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 210
diff changeset
   433
                                         unsigned int bcount)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   435
    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
   436
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   437
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   438
        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
   439
        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
   440
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   441
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   442
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   443
} // MOJOSHADER_glSetPixelShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   444
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   445
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   446
static inline GLenum opengl_posattr_type(const MOJOSHADER_attributeType type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   447
{
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   448
    // we don't ever use the glVertexPointer() stream, so we just need to
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   449
    //  make the data types reasonably match, so the GL doesn't overrun
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   450
    //  the buffer when dereferencing it.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   451
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   452
    switch (type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   453
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   454
        case MOJOSHADER_ATTRIBUTE_BYTE: return GL_NONE;  // oh well.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   455
        case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_NONE;  // oh well.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   456
        case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   457
        case MOJOSHADER_ATTRIBUTE_USHORT: return GL_SHORT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   458
        case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   459
        case MOJOSHADER_ATTRIBUTE_UINT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   460
        case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   461
        case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   462
    } // switch
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   463
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   464
    return GL_NONE;  // oh well. Raises a GL error later.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   465
} // opengl_posattr_type
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   466
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   467
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   468
static inline GLenum opengl_attr_type(const MOJOSHADER_attributeType type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   469
{
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   470
    switch (type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   471
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   472
        case MOJOSHADER_ATTRIBUTE_BYTE: return GL_BYTE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   473
        case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_UNSIGNED_BYTE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   474
        case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   475
        case MOJOSHADER_ATTRIBUTE_USHORT: return GL_USHORT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   476
        case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   477
        case MOJOSHADER_ATTRIBUTE_UINT: return GL_UNSIGNED_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   478
        case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   479
        case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   480
    } // switch
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   481
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   482
    return GL_NONE;  // oh well. Raises a GL error later.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   483
} // opengl_attr_type
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   484
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   485
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
                                     int index, unsigned int size,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
                                     MOJOSHADER_attributeType type,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
                                     int normalized, unsigned int stride,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   490
                                     const void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   491
{
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   492
    if (bound_program == NULL)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   493
        return;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   494
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   495
    // Since glVertexPointer() lacks the flexibility that we can get from
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   496
    //  glVertexAttribPointer(), we set POSITION0 to a generic vertex
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   497
    //  attribute, and the shaders we generate know to look there instead of
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   498
    //  GLSL's gl_Position global variable. But to keep the GL handy, we
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   499
    //  also set the best possible equivalent with glVertexPointer(). Messy.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   500
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   501
    if ((usage == MOJOSHADER_USAGE_POSITION) && (index == 0))
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   502
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   503
        // !!! FIXME: fails if size==1.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   504
        pglVertexPointer(size, opengl_posattr_type(type), stride, ptr);
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   505
    } // if
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   506
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   507
    int i;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   508
    GLuint gl_index = 0;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   509
    for (i = 0; i < bound_program->attribute_count; i++)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   510
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   511
        const AttributeMap *map = &bound_program->attributes[i];
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   512
        const MOJOSHADER_attribute *a = map->attribute;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   513
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   514
        // !!! FIXME: is this array guaranteed to be sorted by usage?
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   515
        // !!! FIXME:  if so, we can break out of the loop if a->usage > usage.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   516
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   517
        if ((a->usage == usage) && (a->index == index))
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   518
        {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   519
            gl_index = map->location;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   520
            break;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   521
        } // if
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   522
    } // for
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   523
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   524
    if (gl_index != 0)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   525
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   526
        const GLenum gl_type = opengl_attr_type(type);
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   527
        const GLboolean gl_norm = (normalized) ? GL_TRUE : GL_FALSE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   528
        pglVertexAttribPointer(gl_index, size, gl_type, gl_norm, stride, ptr);
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   529
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   530
} // MOJOSHADER_glSetVertexAttribute
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   531
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   532
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
void MOJOSHADER_glProgramReady(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   535
    int i;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   536
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   537
    if (bound_program == NULL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   538
        return;  // nothing to do.
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   539
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   540
    // !!! 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
   541
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   542
    // push Uniforms to the program from our register files...
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   543
    for (i = 0; i < bound_program->uniform_count; i++)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   544
    {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   545
        const UniformMap *map = &bound_program->uniforms[i];
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   546
        const MOJOSHADER_uniform *u = map->uniform;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   547
        const MOJOSHADER_uniformType type = u->type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   548
        const MOJOSHADER_shaderType shader_type = map->shader_type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   549
        const int index = u->index;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   550
        const GLint location = map->location;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   551
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   552
        if (shader_type == MOJOSHADER_TYPE_VERTEX)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   553
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   554
            if (type == MOJOSHADER_UNIFORM_FLOAT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   555
                pglUniform4fvARB(location, 1, &vs_register_file_f[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   556
            else if (type == MOJOSHADER_UNIFORM_INT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   557
                pglUniform4ivARB(location, 1, &vs_register_file_i[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   558
            else if (type == MOJOSHADER_UNIFORM_BOOL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   559
                pglUniform1iARB(location, vs_register_file_b[index]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   560
        } // if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   561
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   562
        else if (shader_type == MOJOSHADER_TYPE_PIXEL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   563
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   564
            if (type == MOJOSHADER_UNIFORM_FLOAT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   565
                pglUniform4fvARB(location, 1, &ps_register_file_f[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   566
            else if (type == MOJOSHADER_UNIFORM_INT)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   567
                pglUniform4ivARB(location, 1, &ps_register_file_i[index * 4]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   568
            else if (type == MOJOSHADER_UNIFORM_BOOL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   569
                pglUniform1iARB(location, ps_register_file_b[index]);
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   570
        } // else if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   571
    } // for
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   572
} // MOJOSHADER_glProgramReady
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   573
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   574
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   575
void MOJOSHADER_glDeleteProgram(const MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   576
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
    program_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   578
} // MOJOSHADER_glDeleteProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   580
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   581
void MOJOSHADER_glDeleteShader(const MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   582
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   583
    shader_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   584
} // MOJOSHADER_glDeleteShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   585
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
void MOJOSHADER_glDeinit(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   588
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   589
    MOJOSHADER_glBindProgram(NULL);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   590
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
    profile = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   592
    malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   593
    free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   594
    malloc_data = NULL;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   595
    error_buffer[0] = '\0';
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   596
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   597
    // !!! FIXME: NULL entry points.
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
} // MOJOSHADER_glDeinit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   599
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   600
// end of mojoshader_opengl.c ...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601