mojoshader_opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 28 Apr 2008 04:49:02 -0400
branchtrunk
changeset 236 8e2fc535b210
parent 230 396aa372c7d8
child 237 09f35dfc1d7e
permissions -rw-r--r--
Support for half-float attribute arrays.
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"
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
     9
#define GL_GLEXT_LEGACY 1
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    10
#include "gl.h"
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    11
#include "glext.h"
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
// Get basic wankery out of the way here...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
208
30fe7b63db09 Added STATICARRAYLEN macro to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 207
diff changeset
    15
#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
    16
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
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
    18
typedef uint8_t uint8;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
typedef uint32_t uint32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
typedef int32_t int32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
struct MOJOSHADER_glShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
    const MOJOSHADER_parseData *parseData;
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
    25
    GLhandleARB handle;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    29
typedef struct
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    30
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
    31
    MOJOSHADER_shaderType shader_type;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    32
    const MOJOSHADER_uniform *uniform;
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
    33
    GLuint location;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    34
} UniformMap;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    35
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    36
typedef struct
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    37
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    38
    const MOJOSHADER_attribute *attribute;
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
    39
    GLuint location;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    40
} AttributeMap;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    41
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
struct MOJOSHADER_glProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    44
    MOJOSHADER_glShader *vertex;
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    45
    MOJOSHADER_glShader *fragment;
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
    46
    GLhandleARB handle;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    47
    uint32 uniform_count;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    48
    UniformMap *uniforms;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    49
    uint32 attribute_count;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    50
    AttributeMap *attributes;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
// Allocators...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
static MOJOSHADER_malloc malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
static MOJOSHADER_free free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
static void *malloc_data = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
// The constant register files...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
// 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
    61
static float vs_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
static int vs_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
static uint8 vs_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
static float ps_register_file_f[8192 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
static int ps_register_file_i[2047 * 4];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
static uint8 ps_register_file_b[2047];
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
// GL stuff...
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    69
static int opengl_major = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    70
static int opengl_minor = 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
static MOJOSHADER_glProgram *bound_program = NULL;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    72
static const char *profile = NULL;
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    73
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    74
// Extensions...
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    75
static int have_base_opengl = 0;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    76
static int have_GL_ARB_shader_objects = 0;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    77
static int have_GL_ARB_vertex_shader = 0;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    78
static int have_GL_ARB_fragment_shader = 0;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    79
static int have_GL_ARB_shading_language_100 = 0;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    80
static int have_GL_NV_half_float = 0;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
    81
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    82
// Entry points...
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    83
typedef WINGDIAPI const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    84
typedef WINGDIAPI void (APIENTRYP PFNGLDISABLECLIENTSTATEPROC) (GLenum array);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    85
typedef WINGDIAPI void (APIENTRYP PFNGLENABLECLIENTSTATEPROC) (GLenum array);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    86
typedef WINGDIAPI void (APIENTRYP PFNGLVERTEXPOINTERPROC) (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    87
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    88
static PFNGLGETSTRINGPROC pglGetString = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    89
static PFNGLDELETEOBJECTARBPROC pglDeleteObject = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    90
static PFNGLATTACHOBJECTARBPROC pglAttachObject = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    91
static PFNGLCOMPILESHADERARBPROC pglCompileShader = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    92
static PFNGLCREATEPROGRAMOBJECTARBPROC pglCreateProgramObject = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    93
static PFNGLCREATESHADEROBJECTARBPROC pglCreateShaderObject = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    94
static PFNGLDISABLECLIENTSTATEPROC pglDisableClientState = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    95
static PFNGLDISABLEVERTEXATTRIBARRAYARBPROC pglDisableVertexAttribArray = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    96
static PFNGLENABLECLIENTSTATEPROC pglEnableClientState = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    97
static PFNGLENABLEVERTEXATTRIBARRAYARBPROC pglEnableVertexAttribArray = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    98
static PFNGLGETATTRIBLOCATIONARBPROC pglGetAttribLocation = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
    99
static PFNGLGETINFOLOGARBPROC pglGetInfoLog = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   100
static PFNGLGETOBJECTPARAMETERIVARBPROC pglGetObjectParameteriv = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   101
static PFNGLGETUNIFORMLOCATIONARBPROC pglGetUniformLocation = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   102
static PFNGLLINKPROGRAMARBPROC pglLinkProgram = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   103
static PFNGLSHADERSOURCEARBPROC pglShaderSource = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   104
static PFNGLUNIFORM1IARBPROC pglUniform1i = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   105
static PFNGLUNIFORM4FVARBPROC pglUniform4fv = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   106
static PFNGLUNIFORM4IVARBPROC pglUniform4iv = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   107
static PFNGLUSEPROGRAMOBJECTARBPROC pglUseProgramObject = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   108
static PFNGLVERTEXATTRIBPOINTERARBPROC pglVertexAttribPointer = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   109
static PFNGLVERTEXPOINTERPROC pglVertexPointer = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   110
202
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 state...
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   113
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
   114
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   115
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
   116
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   117
    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
   118
} // set_error
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   119
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
// #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
   122
//  to the C runtime's malloc() and free()...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
#if MOJOSHADER_FORCE_ALLOCATOR
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
#define internal_malloc NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
#define internal_free NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
#else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
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
   128
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
   129
#endif
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
static inline void *Malloc(const size_t len)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
{
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   133
    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
   134
    if (retval == NULL)
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   135
        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
   136
    return retval;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
} // Malloc
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
static inline void Free(void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
{
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   141
    if (ptr != NULL)
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   142
        free_fn(ptr, malloc_data);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
} // Free
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   146
const char *MOJOSHADER_glGetError(void)
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   147
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   148
    return error_buffer;
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   149
} // MOJOSHADER_glGetError
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   150
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   151
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   152
static void *loadsym(void *(*lookup)(const char *fn), const char *fn, int *ext)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   153
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   154
    void *retval = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   155
    if (lookup != NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   156
    {
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   157
        retval = lookup(fn);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   158
        if (retval == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   159
        {
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   160
            char arbfn[64];
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   161
            snprintf(arbfn, sizeof (arbfn), "%sARB", fn);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   162
            retval = lookup(arbfn);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   163
        } // if
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   164
    } // if
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   165
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   166
    if (retval == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   167
        *ext = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   168
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   169
    return retval;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   170
} // loadsym
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   171
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   172
static void lookup_entry_points(void *(*lookup)(const char *fnname))
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   173
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   174
    #define DO_LOOKUP(ext, typ, fn) p##fn = (typ) loadsym(lookup, #fn, &have_##ext)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   175
    DO_LOOKUP(base_opengl, PFNGLGETSTRINGPROC, glGetString);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   176
    DO_LOOKUP(base_opengl, PFNGLDISABLECLIENTSTATEPROC, glDisableClientState);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   177
    DO_LOOKUP(base_opengl, PFNGLENABLECLIENTSTATEPROC, glEnableClientState);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   178
    DO_LOOKUP(base_opengl, PFNGLVERTEXPOINTERPROC, glVertexPointer);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   179
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLDELETEOBJECTARBPROC, glDeleteObject);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   180
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLATTACHOBJECTARBPROC, glAttachObject);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   181
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLCOMPILESHADERARBPROC, glCompileShader);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   182
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLCREATEPROGRAMOBJECTARBPROC, glCreateProgramObject);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   183
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLCREATESHADEROBJECTARBPROC, glCreateShaderObject);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   184
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLGETINFOLOGARBPROC, glGetInfoLog);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   185
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLGETOBJECTPARAMETERIVARBPROC, glGetObjectParameteriv);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   186
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLGETUNIFORMLOCATIONARBPROC, glGetUniformLocation);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   187
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLLINKPROGRAMARBPROC, glLinkProgram);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   188
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLSHADERSOURCEARBPROC, glShaderSource);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   189
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM1IARBPROC, glUniform1i);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   190
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM4FVARBPROC, glUniform4fv);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   191
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLUNIFORM4IVARBPROC, glUniform4iv);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   192
    DO_LOOKUP(GL_ARB_shader_objects, PFNGLUSEPROGRAMOBJECTARBPROC, glUseProgramObject);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   193
    DO_LOOKUP(GL_ARB_vertex_shader, PFNGLDISABLEVERTEXATTRIBARRAYARBPROC, glDisableVertexAttribArray);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   194
    DO_LOOKUP(GL_ARB_vertex_shader, PFNGLENABLEVERTEXATTRIBARRAYARBPROC, glEnableVertexAttribArray);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   195
    DO_LOOKUP(GL_ARB_vertex_shader, PFNGLGETATTRIBLOCATIONARBPROC, glGetAttribLocation);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   196
    DO_LOOKUP(GL_ARB_vertex_shader, PFNGLVERTEXATTRIBPOINTERARBPROC, glVertexAttribPointer);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   197
    #undef DO_LOOKUP
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   198
} // lookup_entry_points
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   199
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   200
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   201
static int verify_extension(const char *ext, int have, const char *extlist,
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   202
                            int major, int minor)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   203
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   204
    if (have == 0)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   205
        return 0;  // don't bother checking, we're missing an entry point.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   206
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   207
    // See if it's in the spec for this GL implementation's version.
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   208
    if (major >= 0)
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   209
    {
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   210
        if ( ((opengl_major << 16) | (opengl_minor & 0xFFFF)) >=
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   211
             ((major << 16) | (minor & 0xFFFF)) )
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   212
            return 1;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   213
    } // if
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   214
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   215
    // Not available in the GL version, check the extension list.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   216
    const char *ptr = strstr(extlist, ext);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   217
    if (ptr == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   218
        return 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   219
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   220
    const char endchar = ptr[strlen(ext)];
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   221
    if ((endchar == '\0') || (endchar == ' '))
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   222
        return 1;  // extension is in the list.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   223
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   224
    return 0;  // just not supported, fail.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   225
} // verify_extension
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   226
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   227
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   228
static void parse_opengl_version(const char *verstr)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   229
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   230
    if (verstr == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   231
        opengl_major = opengl_minor = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   232
    else
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   233
        sscanf(verstr, "%d.%d", &opengl_major, &opengl_minor);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   234
} // parse_opengl_version
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   235
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   236
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   237
static int check_extensions(void *(*lookup)(const char *fnname))
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   238
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   239
    have_base_opengl = 1;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   240
    have_GL_ARB_shader_objects = 1;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   241
    have_GL_ARB_vertex_shader = 1;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   242
    have_GL_ARB_fragment_shader = 1;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   243
    have_GL_ARB_shading_language_100 = 1;
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   244
    have_GL_NV_half_float = 1;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   245
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   246
    lookup_entry_points(lookup);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   247
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   248
    if (!have_base_opengl)   // a function we should definitely have is MIA?
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   249
    {
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   250
        set_error("missing basic OpenGL entry points");
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   251
        return 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   252
    } // if
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   253
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   254
    parse_opengl_version((const char *) pglGetString(GL_VERSION));
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   255
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   256
    const char *extlist = (const char *) pglGetString(GL_EXTENSIONS);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   257
    if (extlist == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   258
        extlist = "";  // just in case.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   259
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   260
    #define VERIFY_EXT(ext, major, minor) \
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   261
        have_##ext = verify_extension(#ext, have_##ext, extlist, major, minor)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   262
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   263
    VERIFY_EXT(GL_ARB_shader_objects, 2, 0);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   264
    VERIFY_EXT(GL_ARB_vertex_shader, 2, 0);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   265
    VERIFY_EXT(GL_ARB_fragment_shader, 2, 0);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   266
    VERIFY_EXT(GL_ARB_shading_language_100, 2, 0);
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   267
    VERIFY_EXT(GL_NV_half_float, -1, -1);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   268
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   269
    #undef VERIFY_EXT
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   270
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   271
    #define REQUIRE_GL_EXTENSION(x) \
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   272
        if (!have_##x) { set_error("This profile requires " #x); return 0; }
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   273
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   274
    if (strcmp(profile, MOJOSHADER_PROFILE_GLSL) == 0)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   275
    {
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   276
        REQUIRE_GL_EXTENSION(GL_ARB_shader_objects);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   277
        REQUIRE_GL_EXTENSION(GL_ARB_vertex_shader);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   278
        REQUIRE_GL_EXTENSION(GL_ARB_fragment_shader);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   279
        REQUIRE_GL_EXTENSION(GL_ARB_shading_language_100);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   280
    } // if
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   281
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   282
    else
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   283
    {
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   284
        set_error("unknown profile");
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   285
        return 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   286
    } // else
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   287
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   288
    #undef REQUIRE_GL_EXTENSION
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   289
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   290
    return 1;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   291
} // check_extensions
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   292
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   293
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
int MOJOSHADER_glInit(const char *_profile,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
                      void *(*lookup)(const char *fnname),
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
                      MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
{
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   298
    error_buffer[0] = '\0';
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   299
230
396aa372c7d8 Fixed logic error.
Ryan C. Gordon <icculus@icculus.org>
parents: 229
diff changeset
   300
    if (strcmp(_profile, MOJOSHADER_PROFILE_GLSL) == 0)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
        profile = MOJOSHADER_PROFILE_GLSL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
    else
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   303
    {
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   304
        set_error("unknown profile");
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   305
        goto init_fail;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   306
    } // else
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   308
    if (!check_extensions(lookup))
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   309
        goto init_fail;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
    malloc_fn = (m == NULL) ? internal_malloc : m;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
    free_fn = (f == NULL) ? internal_free : f;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
    malloc_data = d;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    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
   316
    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
   317
    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
   318
    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
   319
    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
   320
    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
   321
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
    MOJOSHADER_glBindProgram(NULL);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
    return 1;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   325
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   326
init_fail:
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   327
    opengl_major = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   328
    opengl_minor = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   329
    profile = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   330
    malloc_fn = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   331
    free_fn = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   332
    malloc_data = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   333
    lookup_entry_points(NULL);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   334
    return 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
} // MOJOSHADER_glInit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
                                                const unsigned int bufsize)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
    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
   342
    GLhandleARB shader = 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
                                                      bufsize, malloc_fn,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
                                                      free_fn, malloc_data);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   346
    if (pd->error != NULL)
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   347
    {
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   348
        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
   349
        goto compile_shader_fail;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   350
    } // if
202
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
    retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   353
    if (retval == NULL)
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   354
        goto compile_shader_fail;
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   355
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
    GLint ok = 0;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   357
    const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER : GL_VERTEX_SHADER;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   358
    GLint shaderlen = (GLint) pd->output_len;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   359
    shader = pglCreateShaderObject(shader_type);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   360
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   361
    pglShaderSource(shader, 1, (const GLchar **) &pd->output, &shaderlen);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   362
    pglCompileShader(shader);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   363
    pglGetObjectParameteriv(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    if (!ok)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   366
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
        GLsizei len = 0;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   368
        pglGetInfoLog(shader, sizeof (error_buffer), &len, (GLchar *) error_buffer);
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   369
        goto compile_shader_fail;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   370
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   371
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   372
    retval->parseData = pd;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   373
    retval->handle = shader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   374
    retval->refcount = 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   375
    return retval;
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   376
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   377
compile_shader_fail:
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   378
    MOJOSHADER_freeParseData(pd);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   379
    Free(retval);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   380
    if (shader != 0)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   381
        pglDeleteObject(shader);
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   382
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   383
} // MOJOSHADER_glCompileShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
static void shader_unref(MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   387
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   388
    if (shader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   389
    {
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   390
        const uint32 refcount = shader->refcount;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
        if (refcount > 1)
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   392
            shader->refcount--;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
        {
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   395
            pglDeleteObject(shader->handle);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   396
            MOJOSHADER_freeParseData(shader->parseData);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
            Free(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   400
} // shader_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   401
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   402
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   403
static void program_unref(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   404
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   405
    if (program != NULL)
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
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   408
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   409
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   410
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   411
        {
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   412
            pglDeleteObject(program->handle);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   413
            shader_unref(program->vertex);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   414
            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
   415
            Free(program->attributes);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   416
            Free(program->uniforms);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   417
            Free(program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   418
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   419
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   420
} // program_unref
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   423
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
   424
                            MOJOSHADER_glShader *shader)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   425
{
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   426
    int i;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   427
    const MOJOSHADER_parseData *pd = shader->parseData;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   428
    const MOJOSHADER_uniform *u = pd->uniforms;
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   429
    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
   430
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   431
    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
   432
    {
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   433
        const GLint loc = pglGetUniformLocation(program->handle, u[i].name);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   434
        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
   435
        {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   436
            UniformMap *map = &program->uniforms[program->uniform_count];
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   437
            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
   438
            map->uniform = &u[i];
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
   439
            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
   440
            program->uniform_count++;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   441
        } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   442
    } // for
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   443
} // lookup_uniforms
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   444
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   445
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   446
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
   447
{
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   448
    int i;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   449
    const MOJOSHADER_parseData *pd = program->vertex->parseData;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   450
    const MOJOSHADER_attribute *a = pd->attributes;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   451
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   452
    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
   453
    {
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   454
        const GLint loc = pglGetAttribLocation(program->handle, a->name);
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   455
        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
   456
        {
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   457
            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
   458
            map->attribute = &a[i];
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
   459
            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
   460
            program->attribute_count++;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   461
        } // if
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   462
    } // for
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   463
} // lookup_attributes
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   464
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   465
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   466
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   467
                                               MOJOSHADER_glShader *pshader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   468
{
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   469
    if ((vshader == NULL) && (pshader == NULL))
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   470
        return NULL;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   471
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   472
    MOJOSHADER_glProgram *retval = NULL;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   473
    const GLhandleARB program = pglCreateProgramObject();
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   474
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   475
    if (vshader != NULL) pglAttachObject(program, vshader->handle);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   476
    if (pshader != NULL) pglAttachObject(program, pshader->handle);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   477
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   478
    pglLinkProgram(program);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   479
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   480
    GLint ok = 0;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   481
    pglGetObjectParameteriv(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   482
    if (!ok)
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   483
    {
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   484
        GLsizei len = 0;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   485
        pglGetInfoLog(program, sizeof (error_buffer), &len, (GLchar *) error_buffer);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   486
        goto link_program_fail;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   487
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   489
    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
   490
    if (retval == NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   491
        goto link_program_fail;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   492
    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
   493
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   494
    int numregs = 0;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   495
    if (vshader != NULL) numregs += vshader->parseData->uniform_count;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   496
    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
   497
    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
   498
    if (retval->uniforms == NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   499
        goto link_program_fail;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   500
    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
   501
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   502
    retval->handle = program;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   503
    retval->vertex = vshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   504
    retval->fragment = pshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   505
    retval->refcount = 1;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
    if (vshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   508
    {
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   509
        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
   510
                                        vshader->parseData->attribute_count);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   511
        if (retval->attributes == NULL)
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   512
            goto link_program_fail;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   513
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   514
        lookup_attributes(retval);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   515
        lookup_uniforms(retval, vshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
        vshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   517
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   518
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   519
    if (pshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   520
    {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   521
        lookup_uniforms(retval, pshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
        pshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   523
    } // if
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   524
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   525
    return retval;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   526
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   527
link_program_fail:
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   528
    if (retval != NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   529
    {
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   530
        Free(retval->uniforms);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   531
        Free(retval->attributes);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   532
        Free(retval);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   533
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   534
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   535
    pglDeleteObject(program);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   536
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
} // MOJOSHADER_glLinkProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   541
{
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
   542
    GLhandleARB handle = 0;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   543
    int i;
223
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   544
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   545
    if (program == bound_program)
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   546
        return;  // nothing to do.
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   547
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   548
    // Disable any client-side arrays the current program could have used.
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   549
    if (bound_program != NULL)
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   550
    {
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   551
        pglDisableClientState(GL_VERTEX_ARRAY);
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   552
        for (i = 0; i < bound_program->attribute_count; i++)
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   553
        {
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   554
            const AttributeMap *map = &bound_program->attributes[i];
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   555
            pglDisableVertexAttribArray(map->location);
223
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   556
        } // if
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   557
    } // for
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   558
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   559
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   560
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   561
        handle = program->handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   562
        program->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   563
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   564
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   565
    pglUseProgramObject(handle);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   566
    program_unref(bound_program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   567
    bound_program = program;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   568
} // MOJOSHADER_glBindProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   569
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   570
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   571
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
   572
{
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   573
    return ((a > b) ? a : b);
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   574
} // maxuint
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   575
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   576
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   577
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
   578
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   579
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   580
    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
   581
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   582
    {
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   583
        const uint cpy = (maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   584
        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
   585
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   586
} // MOJOSHADER_glSetVertexShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   587
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
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
   590
                                          unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   591
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   592
    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
   593
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   594
    {
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   595
        const uint cpy = (maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   596
        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
   597
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   598
} // MOJOSHADER_glSetVertexShaderUniformI
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
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   601
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
   602
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   603
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   604
    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
   605
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   606
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   607
        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
   608
        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
   609
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   610
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   611
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   612
} // MOJOSHADER_glSetVertexShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   613
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   614
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   615
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
212
76eeb43e054c Whitespace fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 210
diff changeset
   616
                                         unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   617
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   618
    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
   619
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   620
    {
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   621
        const uint cpy = (maxuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   622
        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
   623
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   624
} // MOJOSHADER_glSetPixelShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   625
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   626
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   627
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
   628
                                         unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   629
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   630
    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
   631
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   632
    {
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   633
        const uint cpy = (maxuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   634
        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
   635
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   636
} // MOJOSHADER_glSetPixelShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   637
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   638
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   639
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
212
76eeb43e054c Whitespace fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 210
diff changeset
   640
                                         unsigned int bcount)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   641
{
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   642
    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
   643
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   644
    {
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   645
        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
   646
        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
   647
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   648
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   649
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   650
} // MOJOSHADER_glSetPixelShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   651
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   652
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   653
static inline GLenum opengl_posattr_type(const MOJOSHADER_attributeType type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   654
{
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   655
    // 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
   656
    //  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
   657
    //  the buffer when dereferencing it.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   658
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   659
    switch (type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   660
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   661
        case MOJOSHADER_ATTRIBUTE_BYTE: return GL_NONE;  // oh well.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   662
        case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_NONE;  // oh well.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   663
        case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   664
        case MOJOSHADER_ATTRIBUTE_USHORT: return GL_SHORT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   665
        case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   666
        case MOJOSHADER_ATTRIBUTE_UINT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   667
        case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   668
        case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   669
        case MOJOSHADER_ATTRIBUTE_HALF_FLOAT: return GL_SHORT;
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   670
    } // switch
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   671
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   672
    return GL_NONE;  // oh well. Raises a GL error later.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   673
} // opengl_posattr_type
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   674
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   675
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   676
static inline GLenum opengl_attr_type(const MOJOSHADER_attributeType type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   677
{
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   678
    switch (type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   679
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   680
        case MOJOSHADER_ATTRIBUTE_BYTE: return GL_BYTE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   681
        case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_UNSIGNED_BYTE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   682
        case MOJOSHADER_ATTRIBUTE_SHORT: return GL_SHORT;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   683
        case MOJOSHADER_ATTRIBUTE_USHORT: return GL_UNSIGNED_SHORT;
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   684
        case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   685
        case MOJOSHADER_ATTRIBUTE_UINT: return GL_UNSIGNED_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   686
        case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   687
        case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   688
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   689
        case MOJOSHADER_ATTRIBUTE_HALF_FLOAT:
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   690
            if (have_GL_NV_half_float)
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   691
                return GL_HALF_FLOAT_NV;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   692
            break;
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   693
    } // switch
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   694
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   695
    return GL_NONE;  // oh well. Raises a GL error later.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   696
} // opengl_attr_type
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   697
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   698
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   699
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   700
                                     int index, unsigned int size,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   701
                                     MOJOSHADER_attributeType type,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   702
                                     int normalized, unsigned int stride,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   703
                                     const void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   704
{
221
328ac819b965 Don't update vertex arrays if bound program has no vertex shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 220
diff changeset
   705
    if ((bound_program == NULL) || (bound_program->vertex == NULL))
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   706
        return;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   707
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   708
    // Since glVertexPointer() lacks the flexibility that we can get from
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   709
    //  glVertexAttribPointer(), we set POSITION0 to a generic vertex
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   710
    //  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
   711
    //  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
   712
    //  also set the best possible equivalent with glVertexPointer(). Messy.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   713
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   714
    if ((usage == MOJOSHADER_USAGE_POSITION) && (index == 0))
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   715
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   716
        // !!! FIXME: fails if size==1.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   717
        pglVertexPointer(size, opengl_posattr_type(type), stride, ptr);
222
ed4421aa7e00 Enable client-side arrays as necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
   718
        pglEnableClientState(GL_VERTEX_ARRAY);
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   719
    } // if
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   720
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   721
    int i;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   722
    GLuint gl_index = 0;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   723
    for (i = 0; i < bound_program->attribute_count; i++)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   724
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   725
        const AttributeMap *map = &bound_program->attributes[i];
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   726
        const MOJOSHADER_attribute *a = map->attribute;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   727
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   728
        // !!! FIXME: is this array guaranteed to be sorted by usage?
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   729
        // !!! 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
   730
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   731
        if ((a->usage == usage) && (a->index == index))
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   732
        {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   733
            gl_index = map->location;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   734
            break;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   735
        } // if
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   736
    } // for
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   737
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   738
    if (gl_index != 0)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   739
    {
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   740
        const GLenum gl_type = opengl_attr_type(type);
222
ed4421aa7e00 Enable client-side arrays as necessary.
Ryan C. Gordon <icculus@icculus.org>
parents: 221
diff changeset
   741
        const GLboolean norm = (normalized) ? GL_TRUE : GL_FALSE;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   742
        pglVertexAttribPointer(gl_index, size, gl_type, norm, stride, ptr);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   743
        pglEnableVertexAttribArray(gl_index);
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   744
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
} // MOJOSHADER_glSetVertexAttribute
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   748
void MOJOSHADER_glProgramReady(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   749
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   750
    int i;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   751
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   752
    if (bound_program == NULL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   753
        return;  // nothing to do.
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   754
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   755
    // !!! 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
   756
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   757
    // push Uniforms to the program from our register files...
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   758
    for (i = 0; i < bound_program->uniform_count; i++)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   759
    {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   760
        const UniformMap *map = &bound_program->uniforms[i];
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   761
        const MOJOSHADER_uniform *u = map->uniform;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   762
        const MOJOSHADER_uniformType type = u->type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   763
        const MOJOSHADER_shaderType shader_type = map->shader_type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   764
        const int index = u->index;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   765
        const GLint location = map->location;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   766
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   767
        if (shader_type == MOJOSHADER_TYPE_VERTEX)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   768
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   769
            if (type == MOJOSHADER_UNIFORM_FLOAT)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   770
                pglUniform4fv(location, 1, &vs_register_file_f[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   771
            else if (type == MOJOSHADER_UNIFORM_INT)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   772
                pglUniform4iv(location, 1, &vs_register_file_i[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   773
            else if (type == MOJOSHADER_UNIFORM_BOOL)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   774
                pglUniform1i(location, vs_register_file_b[index]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   775
        } // if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   776
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   777
        else if (shader_type == MOJOSHADER_TYPE_PIXEL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   778
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   779
            if (type == MOJOSHADER_UNIFORM_FLOAT)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   780
                pglUniform4fv(location, 1, &ps_register_file_f[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   781
            else if (type == MOJOSHADER_UNIFORM_INT)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   782
                pglUniform4iv(location, 1, &ps_register_file_i[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   783
            else if (type == MOJOSHADER_UNIFORM_BOOL)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   784
                pglUniform1i(location, ps_register_file_b[index]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   785
        } // else if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   786
    } // for
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   787
} // MOJOSHADER_glProgramReady
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   788
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   789
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   790
void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   791
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   792
    program_unref(program);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   793
} // MOJOSHADER_glDeleteProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   794
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   795
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   796
void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   797
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
    shader_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   799
} // MOJOSHADER_glDeleteShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   801
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
void MOJOSHADER_glDeinit(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   803
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   804
    MOJOSHADER_glBindProgram(NULL);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   805
    lookup_entry_points(NULL);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   806
    profile = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   807
    malloc_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   808
    free_fn = NULL;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   809
    malloc_data = NULL;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   810
    error_buffer[0] = '\0';
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   811
    opengl_major = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   812
    opengl_minor = 0;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
} // MOJOSHADER_glDeinit
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   814
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
// end of mojoshader_opengl.c ...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   816