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