mojoshader_opengl.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 13 May 2008 04:29:56 -0400
branchtrunk
changeset 321 d7fb4ccecf0b
parent 313 f5aa15de1690
child 323 b60c88ec8182
permissions -rw-r--r--
Fixed incorrect vertex shader attribute location lookup. We were using a->name instead of a[i].name. Fix from Nicholas Vining (thanks!).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
243
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     1
/**
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     2
 * MojoShader; generate shader programs from bytecode of compiled
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     3
 *  Direct3D shaders.
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     4
 *
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     5
 * Please see the file LICENSE.txt in the source's root directory.
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     6
 *
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     7
 *  This file written by Ryan C. Gordon.
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     8
 */
a0825a0d504a Added comment header to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 242
diff changeset
     9
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
#include <stdio.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include <string.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
#include <stdlib.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
#include <stdarg.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
#include <assert.h>
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
254
13184ff204b9 More Windows patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 253
diff changeset
    16
#ifdef _MSC_VER
13184ff204b9 More Windows patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 253
diff changeset
    17
#define WIN32_LEAN_AND_MEAN 1
13184ff204b9 More Windows patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 253
diff changeset
    18
#include <windows.h>  // GL headers need this for WINGDIAPI definition.
13184ff204b9 More Windows patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 253
diff changeset
    19
#endif
13184ff204b9 More Windows patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 253
diff changeset
    20
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
#include "mojoshader.h"
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    22
#define GL_GLEXT_LEGACY 1
246
897868fdd958 Moved gl*.h into GL directory.
Ryan C. Gordon <icculus@icculus.org>
parents: 245
diff changeset
    23
#include "GL/gl.h"
897868fdd958 Moved gl*.h into GL directory.
Ryan C. Gordon <icculus@icculus.org>
parents: 245
diff changeset
    24
#include "GL/glext.h"
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
// Get basic wankery out of the way here...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    28
typedef unsigned int uint;  // this is a printf() helper. don't use for code.
208
30fe7b63db09 Added STATICARRAYLEN macro to mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 207
diff changeset
    29
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    30
#ifdef _MSC_VER
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    31
#define snprintf _snprintf
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    32
typedef unsigned __int8 uint8;
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    33
typedef unsigned __int32 uint32;
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    34
typedef unsigned __int32 int32;
291
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
    35
// Warning Level 4 considered harmful.  :)
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
    36
#pragma warning(disable: 4100)  // "unreferenced formal parameter"
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
    37
#pragma warning(disable: 4389)  // "signed/unsigned mismatch"
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    38
#else
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    39
#include <stdint.h>
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
typedef uint8_t uint8;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
typedef uint32_t uint32;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
typedef int32_t int32;
250
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    43
#endif
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    44
b75c08291021 Visual C++ fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 249
diff changeset
    45
#define STATICARRAYLEN(x) ( (sizeof ((x))) / (sizeof ((x)[0])) )
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    47
#ifndef SUPPORT_PROFILE_GLSL
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    48
#define SUPPORT_PROFILE_GLSL 1
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    49
#endif
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
    50
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
struct MOJOSHADER_glShader
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
    const MOJOSHADER_parseData *parseData;
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
    54
    GLhandleARB handle;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    58
typedef struct
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    59
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
    60
    MOJOSHADER_shaderType shader_type;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    61
    const MOJOSHADER_uniform *uniform;
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
    62
    GLuint location;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    63
} UniformMap;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    64
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    65
typedef struct
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    66
{
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    67
    MOJOSHADER_shaderType shader_type;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    68
    const MOJOSHADER_sampler *sampler;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    69
    GLuint location;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    70
} SamplerMap;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    71
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    72
typedef struct
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    73
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    74
    const MOJOSHADER_attribute *attribute;
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
    75
    GLuint location;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    76
} AttributeMap;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    77
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
struct MOJOSHADER_glProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    80
    MOJOSHADER_glShader *vertex;
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    81
    MOJOSHADER_glShader *fragment;
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
    82
    GLhandleARB handle;
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
    83
    uint32 constant_count;  // !!! FIXME: misnamed.
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
    84
    GLfloat *constants;  // !!! FIXME: misnamed.
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    85
    uint32 uniform_count;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    86
    UniformMap *uniforms;
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    87
    uint32 sampler_count;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
    88
    SamplerMap *samplers;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    89
    uint32 attribute_count;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    90
    AttributeMap *attributes;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
277
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
    94
#ifndef WINGDIAPI
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
    95
#define WINGDIAPI
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
    96
#endif
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
    97
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
    98
// Entry points in base OpenGL that lack function pointer prototypes...
276
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
    99
typedef WINGDIAPI void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *params);
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   100
typedef WINGDIAPI const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   101
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   102
struct MOJOSHADER_glContext
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   103
{
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   104
    // Allocators...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   105
    MOJOSHADER_malloc malloc_fn;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   106
    MOJOSHADER_free free_fn;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   107
    void *malloc_data;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   108
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   109
    // The constant register files...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   110
    // Man, it kills me how much memory this takes...
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   111
    GLfloat vs_reg_file_f[8192 * 4];
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   112
    GLint vs_reg_file_i[2047 * 4];
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   113
    GLint vs_reg_file_b[2047];
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   114
    GLfloat ps_reg_file_f[8192 * 4];
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   115
    GLint ps_reg_file_i[2047 * 4];
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   116
    GLint ps_reg_file_b[2047];
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   117
    GLuint sampler_reg_file[16];
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   118
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   119
    // GL stuff...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   120
    int opengl_major;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   121
    int opengl_minor;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   122
    MOJOSHADER_glProgram *bound_program;
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   123
    char profile[16];
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   124
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   125
    // Extensions...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   126
    int have_base_opengl;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   127
    int have_GL_ARB_shader_objects;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   128
    int have_GL_ARB_vertex_shader;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   129
    int have_GL_ARB_fragment_shader;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   130
    int have_GL_ARB_shading_language_100;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   131
    int have_GL_NV_half_float;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   132
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   133
    // Entry points...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   134
    PFNGLGETSTRINGPROC glGetString;
276
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   135
    PFNGLGETINTEGERVPROC glGetIntegerv;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   136
    PFNGLDELETEOBJECTARBPROC glDeleteObject;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   137
    PFNGLATTACHOBJECTARBPROC glAttachObject;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   138
    PFNGLCOMPILESHADERARBPROC glCompileShader;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   139
    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObject;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   140
    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObject;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   141
    PFNGLDISABLEVERTEXATTRIBARRAYARBPROC glDisableVertexAttribArray;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   142
    PFNGLENABLEVERTEXATTRIBARRAYARBPROC glEnableVertexAttribArray;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   143
    PFNGLGETATTRIBLOCATIONARBPROC glGetAttribLocation;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   144
    PFNGLGETINFOLOGARBPROC glGetInfoLog;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   145
    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameteriv;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   146
    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocation;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   147
    PFNGLLINKPROGRAMARBPROC glLinkProgram;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   148
    PFNGLSHADERSOURCEARBPROC glShaderSource;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   149
    PFNGLUNIFORM1IARBPROC glUniform1i;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   150
    PFNGLUNIFORM4FVARBPROC glUniform4fv;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   151
    PFNGLUNIFORM4IVARBPROC glUniform4iv;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   152
    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObject;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   153
    PFNGLVERTEXATTRIBPOINTERARBPROC glVertexAttribPointer;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   154
};
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   155
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   156
static MOJOSHADER_glContext *ctx = NULL;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   157
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   159
// Error state...
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   160
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
   161
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   162
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
   163
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   164
    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
   165
} // set_error
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   166
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
// #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
   169
//  to the C runtime's malloc() and free()...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
#if MOJOSHADER_FORCE_ALLOCATOR
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
#define internal_malloc NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
#define internal_free NULL
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
#else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
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
   175
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
   176
#endif
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
static inline void *Malloc(const size_t len)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
{
307
42f6a7ba69e2 Fixes for Visual Studio level 4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 291
diff changeset
   180
    void *retval = ctx->malloc_fn((int) len, ctx->malloc_data);
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   181
    if (retval == NULL)
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   182
        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
   183
    return retval;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
} // Malloc
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
static inline void Free(void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
{
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   188
    if (ptr != NULL)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   189
        ctx->free_fn(ptr, ctx->malloc_data);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
} // Free
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   193
const char *MOJOSHADER_glGetError(void)
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   194
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   195
    return error_buffer;
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   196
} // MOJOSHADER_glGetError
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   197
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   198
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   199
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
   200
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   201
    void *retval = NULL;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   202
    if (lookup != NULL)
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
        retval = lookup(fn);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   205
        if (retval == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   206
        {
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   207
            char arbfn[64];
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   208
            snprintf(arbfn, sizeof (arbfn), "%sARB", fn);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   209
            retval = lookup(arbfn);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   210
        } // if
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   211
    } // if
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   212
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   213
    if (retval == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   214
        *ext = 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   215
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   216
    return retval;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   217
} // loadsym
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   218
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   219
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
   220
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   221
    #define DO_LOOKUP(ext, typ, fn) ctx->fn = (typ) loadsym(lookup, #fn, &ctx->have_##ext)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   222
    DO_LOOKUP(base_opengl, PFNGLGETSTRINGPROC, glGetString);
276
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   223
    DO_LOOKUP(base_opengl, PFNGLGETINTEGERVPROC, glGetIntegerv);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   224
    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
   225
    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
   226
    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
   227
    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
   228
    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
   229
    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
   230
    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
   231
    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
   232
    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
   233
    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
   234
    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
   235
    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
   236
    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
   237
    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
   238
    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
   239
    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
   240
    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
   241
    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
   242
    #undef DO_LOOKUP
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   243
} // lookup_entry_points
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   244
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   245
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   246
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
   247
                            int major, int minor)
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
    if (have == 0)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   250
        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
   251
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   252
    else if (!ctx->have_base_opengl)
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   253
        return 0;  // don't bother checking, we're missing basic functionality.
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   254
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   255
    // See if it's in the spec for this GL implementation's version.
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   256
    if (major >= 0)
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   257
    {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   258
        if ( ((ctx->opengl_major << 16) | (ctx->opengl_minor & 0xFFFF)) >=
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   259
             ((major << 16) | (minor & 0xFFFF)) )
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   260
            return 1;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   261
    } // if
229
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
    // 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
   264
    const char *ptr = strstr(extlist, ext);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   265
    if (ptr == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   266
        return 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
    const char endchar = ptr[strlen(ext)];
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   269
    if ((endchar == '\0') || (endchar == ' '))
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   270
        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
   271
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   272
    return 0;  // just not supported, fail.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   273
} // verify_extension
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   274
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
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
   277
{
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   278
    if (verstr == NULL)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   279
        ctx->opengl_major = ctx->opengl_minor = 0;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   280
    else
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   281
        sscanf(verstr, "%d.%d", &ctx->opengl_major, &ctx->opengl_minor);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   282
} // parse_opengl_version
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   283
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   284
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   285
static void load_extensions(void *(*lookup)(const char *fnname))
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   286
{
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   287
    const char *extlist = NULL;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   288
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   289
    ctx->have_base_opengl = 1;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   290
    ctx->have_GL_ARB_shader_objects = 1;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   291
    ctx->have_GL_ARB_vertex_shader = 1;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   292
    ctx->have_GL_ARB_fragment_shader = 1;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   293
    ctx->have_GL_ARB_shading_language_100 = 1;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   294
    ctx->have_GL_NV_half_float = 1;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   295
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   296
    lookup_entry_points(lookup);
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   297
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   298
    if (!ctx->have_base_opengl)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   299
        set_error("missing basic OpenGL entry points");
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   300
    else
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   301
    {
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   302
        parse_opengl_version((const char *) ctx->glGetString(GL_VERSION));
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   303
        extlist = (const char *) ctx->glGetString(GL_EXTENSIONS);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   304
    } // else
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   305
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   306
    if (extlist == NULL)
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   307
        extlist = "";  // just in case.
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   308
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   309
    #define VERIFY_EXT(ext, major, minor) \
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   310
        ctx->have_##ext = verify_extension(#ext, ctx->have_##ext, extlist, major, minor)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   311
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   312
    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
   313
    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
   314
    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
   315
    VERIFY_EXT(GL_ARB_shading_language_100, 2, 0);
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   316
    VERIFY_EXT(GL_NV_half_float, -1, -1);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   317
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   318
    #undef VERIFY_EXT
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   319
} // load_extensions
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   320
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   321
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   322
static int valid_profile(const char *profile)
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   323
{
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   324
    if (!ctx->have_base_opengl)
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   325
        return 0;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   326
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   327
    #define MUST_HAVE(p, x) \
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   328
        if (!ctx->have_##x) { set_error(#p " profile needs " #x); return 0; }
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   329
291
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
   330
    if (profile == NULL)
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
   331
    {
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
   332
        set_error("NULL profile");
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
   333
        return 0;
2453590bae1b Fix/disable annoying Visual C++ level 4 warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 285
diff changeset
   334
    } // if
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   335
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   336
    #if SUPPORT_PROFILE_GLSL
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   337
    else if (strcmp(profile, MOJOSHADER_PROFILE_GLSL) == 0)
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   338
    {
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   339
        MUST_HAVE(MOJOSHADER_PROFILE_GLSL, GL_ARB_shader_objects);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   340
        MUST_HAVE(MOJOSHADER_PROFILE_GLSL, GL_ARB_vertex_shader);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   341
        MUST_HAVE(MOJOSHADER_PROFILE_GLSL, GL_ARB_fragment_shader);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   342
        MUST_HAVE(MOJOSHADER_PROFILE_GLSL, GL_ARB_shading_language_100);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   343
    } // else if
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   344
    #endif
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   345
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   346
    else
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   347
    {
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   348
        set_error("unknown or unsupported profile");
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   349
        return 0;
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   350
    } // else
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   351
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   352
    #undef MUST_HAVE
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   353
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   354
    return 1;
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   355
} // valid_profile
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   356
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   357
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   358
const char *MOJOSHADER_glBestProfile(void *(*lookup)(const char *fnname))
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   359
{
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   360
    const char *retval = NULL;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   361
    MOJOSHADER_glContext _ctx;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   362
    MOJOSHADER_glContext *current_ctx = ctx;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   363
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   364
    ctx = &_ctx;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   365
    memset(ctx, '\0', sizeof (MOJOSHADER_glContext));
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   366
    load_extensions(lookup);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   367
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   368
    if (ctx->have_base_opengl)
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   369
    {
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   370
        static const char *priority[] = {
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   371
            MOJOSHADER_PROFILE_GLSL,
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   372
        };
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   373
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   374
        int i;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   375
        for (i = 0; i < STATICARRAYLEN(priority); i++)
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   376
        {
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   377
            if (valid_profile(priority[i]))
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   378
            {
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   379
                retval = priority[i];
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   380
                break;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   381
            } // if
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   382
        } // for
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   383
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   384
        if (retval == NULL)
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   385
            set_error("no profiles available");
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   386
    } // if
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   387
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   388
    ctx = current_ctx;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   389
    return retval;
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   390
} // MOJOSHADER_glBestProfile
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   391
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   392
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   393
MOJOSHADER_glContext *MOJOSHADER_glCreateContext(const char *profile,
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   394
                                        void *(*lookup)(const char *fnname),
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   395
                                        MOJOSHADER_malloc m, MOJOSHADER_free f,
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   396
                                        void *d)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
{
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   398
    MOJOSHADER_glContext *retval = NULL;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   399
    MOJOSHADER_glContext *current_ctx = ctx;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   400
    ctx = NULL;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   401
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   402
    if (m == NULL) m = internal_malloc;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   403
    if (f == NULL) f = internal_free;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   404
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   405
    ctx = (MOJOSHADER_glContext *) m(sizeof (MOJOSHADER_glContext), d);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   406
    if (ctx == NULL)
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   407
    {
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   408
        set_error("out of memory");
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   409
        goto init_fail;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   410
    } // if
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   411
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   412
    memset(ctx, '\0', sizeof (MOJOSHADER_glContext));
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   413
    ctx->malloc_fn = m;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   414
    ctx->free_fn = f;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   415
    ctx->malloc_data = d;
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   416
    snprintf(ctx->profile, sizeof (ctx->profile), "%s", profile);
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   417
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   418
    load_extensions(lookup);
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   419
    if (!valid_profile(profile))
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   420
        goto init_fail;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   421
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   422
    MOJOSHADER_glBindProgram(NULL);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   423
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   424
    retval = ctx;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   425
    ctx = current_ctx;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   426
    return retval;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   427
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   428
init_fail:
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   429
    if (ctx != NULL)
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   430
        f(ctx, d);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   431
    ctx = current_ctx;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   432
    return NULL;
238
e98f14da2897 Renamed glInit and glDeinit to glCreateContext and glDestroyContext.
Ryan C. Gordon <icculus@icculus.org>
parents: 237
diff changeset
   433
} // MOJOSHADER_glCreateContext
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   434
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   435
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   436
void MOJOSHADER_glMakeContextCurrent(MOJOSHADER_glContext *_ctx)
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   437
{
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   438
    ctx = _ctx;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   439
} // MOJOSHADER_glMakeContextCurrent
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   440
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   441
276
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   442
int MOJOSHADER_glMaxUniforms(MOJOSHADER_shaderType shader_type)
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   443
{
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   444
    GLenum pname = GL_NONE;
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   445
    GLint val = 0;
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   446
    if (shader_type == MOJOSHADER_TYPE_VERTEX)
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   447
        pname = GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB;
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   448
    else if (shader_type == MOJOSHADER_TYPE_PIXEL)
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   449
        pname = GL_MAX_FRAGMENT_UNIFORM_COMPONENTS_ARB;
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   450
    else
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   451
        return -1;
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   452
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   453
    ctx->glGetIntegerv(pname, &val);
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   454
    return (int) val;
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   455
} // MOJOSHADER_glMaxUniforms
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   456
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   457
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   458
MOJOSHADER_glShader *MOJOSHADER_glCompileShader(const unsigned char *tokenbuf,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   459
                                                const unsigned int bufsize)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   460
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   461
    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
   462
    GLhandleARB shader = 0;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   463
    const MOJOSHADER_parseData *pd = MOJOSHADER_parse(ctx->profile, tokenbuf,
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   464
                                                      bufsize, ctx->malloc_fn,
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   465
                                                      ctx->free_fn,
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   466
                                                      ctx->malloc_data);
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   467
    GLint ok = 0;
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   468
    const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER : GL_VERTEX_SHADER;
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   469
    GLint shaderlen = (GLint) pd->output_len;
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   470
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   471
    if (pd->error != NULL)
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   472
    {
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   473
        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
   474
        goto compile_shader_fail;
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   475
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   476
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   477
    retval = (MOJOSHADER_glShader *) Malloc(sizeof (MOJOSHADER_glShader));
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   478
    if (retval == NULL)
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   479
        goto compile_shader_fail;
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   480
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   481
    shader = ctx->glCreateShaderObject(shader_type);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   482
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   483
    ctx->glShaderSource(shader, 1, (const GLchar **) &pd->output, &shaderlen);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   484
    ctx->glCompileShader(shader);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   485
    ctx->glGetObjectParameteriv(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   486
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   487
    if (!ok)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   488
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   489
        GLsizei len = 0;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   490
        ctx->glGetInfoLog(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
   491
        goto compile_shader_fail;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   492
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   493
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   494
    retval->parseData = pd;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   495
    retval->handle = shader;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   496
    retval->refcount = 1;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   497
    return retval;
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   498
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   499
compile_shader_fail:
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   500
    MOJOSHADER_freeParseData(pd);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   501
    Free(retval);
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   502
    if (shader != 0)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   503
        ctx->glDeleteObject(shader);
207
d9cee469e080 Cleaned up GLSL shader compile code with some gotos.
Ryan C. Gordon <icculus@icculus.org>
parents: 206
diff changeset
   504
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   505
} // MOJOSHADER_glCompileShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   506
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   507
245
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   508
const MOJOSHADER_parseData *MOJOSHADER_glGetShaderParseData(
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   509
                                                MOJOSHADER_glShader *shader)
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   510
{
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   511
    return (shader != NULL) ? shader->parseData : NULL;
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   512
} // MOJOSHADER_glGetShaderParseData
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   513
1f04b805a4f2 Added MOJOSHADER_glGetShaderParseData().
Ryan C. Gordon <icculus@icculus.org>
parents: 243
diff changeset
   514
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   515
static void shader_unref(MOJOSHADER_glShader *shader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   516
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   517
    if (shader != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   518
    {
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   519
        const uint32 refcount = shader->refcount;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   520
        if (refcount > 1)
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   521
            shader->refcount--;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   522
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   523
        {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   524
            ctx->glDeleteObject(shader->handle);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   525
            MOJOSHADER_freeParseData(shader->parseData);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   526
            Free(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   529
} // shader_unref
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
static void program_unref(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   533
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   534
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   535
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   536
        const uint32 refcount = program->refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   537
        if (refcount > 1)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   538
            program->refcount--;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   539
        else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   540
        {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   541
            ctx->glDeleteObject(program->handle);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   542
            shader_unref(program->vertex);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   543
            shader_unref(program->fragment);
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   544
            Free(program->constants);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   545
            Free(program->samplers);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   546
            Free(program->uniforms);
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   547
            Free(program->attributes);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   548
            Free(program);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   549
        } // else
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   550
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   551
} // program_unref
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
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   554
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
   555
                            MOJOSHADER_glShader *shader)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   556
{
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   557
    int i;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   558
    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
   559
    const MOJOSHADER_uniform *u = pd->uniforms;
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   560
    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
   561
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   562
    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
   563
    {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   564
        const GLint loc = ctx->glGetUniformLocation(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
   565
        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
   566
        {
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   567
            UniformMap *map = &program->uniforms[program->uniform_count];
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   568
            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
   569
            map->uniform = &u[i];
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
   570
            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
   571
            program->uniform_count++;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   572
        } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   573
    } // for
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   574
} // lookup_uniforms
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   575
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   576
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   577
static void lookup_samplers(MOJOSHADER_glProgram *program,
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   578
                            MOJOSHADER_glShader *shader)
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   579
{
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   580
    int i;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   581
    const MOJOSHADER_parseData *pd = shader->parseData;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   582
    const MOJOSHADER_sampler *s = pd->samplers;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   583
    const MOJOSHADER_shaderType shader_type = pd->shader_type;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   584
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   585
    for (i = 0; i < pd->sampler_count; i++)
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   586
    {
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   587
        const GLint loc = ctx->glGetUniformLocation(program->handle, s[i].name);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   588
        if (loc != -1)  // maybe the Sampler was optimized out?
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   589
        {
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   590
            SamplerMap *map = &program->samplers[program->sampler_count];
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   591
            map->shader_type = shader_type;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   592
            map->sampler = &s[i];
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   593
            map->location = (GLuint) loc;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   594
            program->sampler_count++;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   595
        } // if
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   596
    } // for
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   597
} // lookup_samplers
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   598
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   599
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   600
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
   601
{
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   602
    int i;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   603
    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
   604
    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
   605
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   606
    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
   607
    {
321
d7fb4ccecf0b Fixed incorrect vertex shader attribute location lookup.
Ryan C. Gordon <icculus@icculus.org>
parents: 313
diff changeset
   608
        const GLint loc = ctx->glGetAttribLocation(program->handle, a[i].name);
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   609
        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
   610
        {
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   611
            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
   612
            map->attribute = &a[i];
215
f2e508f08997 Fixed wrong type for uniform/attribute locations.
Ryan C. Gordon <icculus@icculus.org>
parents: 213
diff changeset
   613
            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
   614
            program->attribute_count++;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   615
        } // if
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   616
    } // for
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   617
} // lookup_attributes
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   618
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   619
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   620
MOJOSHADER_glProgram *MOJOSHADER_glLinkProgram(MOJOSHADER_glShader *vshader,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   621
                                               MOJOSHADER_glShader *pshader)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   622
{
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   623
    if ((vshader == NULL) && (pshader == NULL))
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   624
        return NULL;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   625
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   626
    MOJOSHADER_glProgram *retval = NULL;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   627
    const GLhandleARB program = ctx->glCreateProgramObject();
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   628
    int numregs = 0;
313
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   629
    int consts = 0;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   630
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   631
    if (vshader != NULL) ctx->glAttachObject(program, vshader->handle);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   632
    if (pshader != NULL) ctx->glAttachObject(program, pshader->handle);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   633
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   634
    ctx->glLinkProgram(program);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   635
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   636
    GLint ok = 0;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   637
    ctx->glGetObjectParameteriv(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   638
    if (!ok)
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   639
    {
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   640
        GLsizei len = 0;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   641
        ctx->glGetInfoLog(program, sizeof (error_buffer), &len, (GLchar *) error_buffer);
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   642
        goto link_program_fail;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   643
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   644
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   645
    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
   646
    if (retval == NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   647
        goto link_program_fail;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   648
    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
   649
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   650
    numregs = 0;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   651
    if (vshader != NULL) numregs += vshader->parseData->uniform_count;
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   652
    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
   653
    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
   654
    if (retval->uniforms == NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   655
        goto link_program_fail;
209
2bd25adf2d3a Finished implementing MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 208
diff changeset
   656
    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
   657
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   658
    numregs = 0;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   659
    if (vshader != NULL) numregs += vshader->parseData->sampler_count;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   660
    if (pshader != NULL) numregs += pshader->parseData->sampler_count;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   661
    retval->samplers = (SamplerMap *) Malloc(sizeof (SamplerMap) * numregs);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   662
    if (retval->samplers == NULL)
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   663
        goto link_program_fail;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   664
    memset(retval->samplers, '\0', sizeof (SamplerMap) * numregs);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   665
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   666
    retval->handle = program;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   667
    retval->vertex = vshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   668
    retval->fragment = pshader;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   669
    retval->refcount = 1;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   670
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   671
    if (vshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   672
    {
313
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   673
        if (consts < vshader->parseData->constant_count)
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   674
            consts = vshader->parseData->constant_count;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   675
        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
   676
                                        vshader->parseData->attribute_count);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   677
        if (retval->attributes == NULL)
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   678
            goto link_program_fail;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   679
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   680
        lookup_attributes(retval);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   681
        lookup_uniforms(retval, vshader);
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   682
        lookup_samplers(retval, vshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   683
        vshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   684
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   685
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   686
    if (pshader != NULL)
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   687
    {
313
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   688
        if (consts < pshader->parseData->constant_count)
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   689
            consts = pshader->parseData->constant_count;
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   690
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   691
        lookup_uniforms(retval, pshader);
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   692
        lookup_samplers(retval, vshader);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   693
        pshader->refcount++;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   694
    } // if
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   695
313
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   696
    if (consts > 0)    
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   697
    {
313
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   698
        retval->constants = (GLfloat *) Malloc(sizeof (GLfloat) * consts * 4);
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   699
        if (retval->constants == NULL)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   700
            goto link_program_fail;
313
f5aa15de1690 Shrunk variable name to fit draconian 80-char standards.
Ryan C. Gordon <icculus@icculus.org>
parents: 312
diff changeset
   701
        retval->constant_count = (uint32) consts;
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   702
    } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   703
204
5055ac80916e A little more work on MOJOSHADER_glLinkProgram().
Ryan C. Gordon <icculus@icculus.org>
parents: 203
diff changeset
   704
    return retval;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   705
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   706
link_program_fail:
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   707
    if (retval != NULL)
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   708
    {
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   709
        Free(retval->constants);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   710
        Free(retval->samplers);
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   711
        Free(retval->uniforms);
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   712
        Free(retval->attributes);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   713
        Free(retval);
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   714
    } // if
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   715
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   716
    ctx->glDeleteObject(program);
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
   717
    return NULL;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   718
} // MOJOSHADER_glLinkProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   719
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   720
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   721
void MOJOSHADER_glBindProgram(MOJOSHADER_glProgram *program)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   722
{
210
cb724dd80d4b Whoops, GLSL objects are GLhandleARB, not GLuint.
Ryan C. Gordon <icculus@icculus.org>
parents: 209
diff changeset
   723
    GLhandleARB handle = 0;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   724
    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
   725
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   726
    if (program == ctx->bound_program)
223
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   727
        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
   728
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   729
    // Disable any client-side arrays the current program could have used.
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   730
    // !!! FIXME: don't disable yet...see which ones get reused, and disable
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   731
    // !!! FIXME:  only what we don't need in MOJOSHADER_glProgramReady().
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   732
    if (ctx->bound_program != NULL)
223
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   733
    {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   734
        const int count = ctx->bound_program->attribute_count;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   735
        for (i = 0; i < count; i++)
223
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   736
        {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   737
            const AttributeMap *map = &ctx->bound_program->attributes[i];
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   738
            ctx->glDisableVertexAttribArray(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
   739
        } // if
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   740
    } // for
8df4e9545c87 Disable any enabled client-side arrays when binding a new shader.
Ryan C. Gordon <icculus@icculus.org>
parents: 222
diff changeset
   741
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   742
    if (program != NULL)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   743
    {
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   744
        handle = program->handle;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   745
        program->refcount++;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   746
    } // if
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   747
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   748
    ctx->glUseProgramObject(handle);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   749
    program_unref(ctx->bound_program);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   750
    ctx->bound_program = program;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   751
} // MOJOSHADER_glBindProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   752
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   753
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   754
static inline uint minuint(const uint a, const uint b)
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   755
{
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   756
    return ((a < b) ? a : b);
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   757
} // minuint
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   758
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   759
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   760
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
   761
                                          unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   762
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   763
    const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_f) / 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   764
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   765
    {
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   766
        assert(sizeof (GLfloat) == sizeof (float));
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   767
        const uint cpy = (minuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   768
        memcpy(ctx->vs_reg_file_f + (idx * 4), data, cpy);
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   769
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   770
} // MOJOSHADER_glSetVertexShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   771
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   772
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   773
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
   774
                                          unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   775
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   776
    const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_i) / 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   777
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   778
    {
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   779
        assert(sizeof (GLint) == sizeof (int));
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   780
        const uint cpy = (minuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   781
        memcpy(ctx->vs_reg_file_i + (idx * 4), data, cpy);
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   782
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   783
} // MOJOSHADER_glSetVertexShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   784
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   785
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   786
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
   787
                                          unsigned int bcount)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   788
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   789
    const uint maxregs = STATICARRAYLEN(ctx->vs_reg_file_f) / 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   790
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   791
    {
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   792
        GLint *wptr = ctx->vs_reg_file_b + idx;
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   793
        GLint *endptr = wptr + minuint(maxregs - idx, bcount);
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   794
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   795
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   796
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   797
} // MOJOSHADER_glSetVertexShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   798
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   799
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   800
void MOJOSHADER_glSetPixelShaderUniformF(unsigned int idx, const float *data,
212
76eeb43e054c Whitespace fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 210
diff changeset
   801
                                         unsigned int vec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   802
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   803
    const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_f) / 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   804
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   805
    {
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   806
        assert(sizeof (GLfloat) == sizeof (float));
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   807
        const uint cpy = (minuint(maxregs - idx, vec4n) * sizeof (*data)) * 4;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   808
        memcpy(ctx->ps_reg_file_f + (idx * 4), data, cpy);
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   809
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   810
} // MOJOSHADER_glSetPixelShaderUniformF
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   811
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   812
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   813
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
   814
                                         unsigned int ivec4n)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   815
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   816
    const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_i) / 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   817
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   818
    {
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   819
        assert(sizeof (GLint) == sizeof (int));
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   820
        const uint cpy = (minuint(maxregs - idx, ivec4n) * sizeof (*data)) * 4;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   821
        memcpy(ctx->ps_reg_file_i + (idx * 4), data, cpy);
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   822
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   823
} // MOJOSHADER_glSetPixelShaderUniformI
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   824
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   825
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   826
void MOJOSHADER_glSetPixelShaderUniformB(unsigned int idx, const int *data,
212
76eeb43e054c Whitespace fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 210
diff changeset
   827
                                         unsigned int bcount)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   828
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   829
    const uint maxregs = STATICARRAYLEN(ctx->ps_reg_file_f) / 4;
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   830
    if (idx < maxregs)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   831
    {
249
7f928921766c Cleanups for building as C++ code.
Ryan C. Gordon <icculus@icculus.org>
parents: 246
diff changeset
   832
        GLint *wptr = ctx->ps_reg_file_b + idx;
255
796394130035 Fixed logic bug in uniform setting.
Ryan C. Gordon <icculus@icculus.org>
parents: 254
diff changeset
   833
        GLint *endptr = wptr + minuint(maxregs - idx, bcount);
203
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   834
        while (wptr != endptr)
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   835
            *(wptr++) = *(data++) ? 1 : 0;
ff983e373e37 Implemented uniform array setting in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 202
diff changeset
   836
    } // if
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   837
} // MOJOSHADER_glSetPixelShaderUniformB
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   838
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   839
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   840
void MOJOSHADER_glSetSampler(unsigned int idx, unsigned int unit)
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   841
{
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   842
    const uint maxregs = STATICARRAYLEN(ctx->sampler_reg_file);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   843
    if (idx < maxregs)
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   844
        ctx->sampler_reg_file[idx] = (GLuint) unit;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   845
} // MOJOSHADER_glSetSampler
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   846
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   847
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   848
static inline GLenum opengl_attr_type(const MOJOSHADER_attributeType type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   849
{
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   850
    switch (type)
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   851
    {
241
08f612343cd1 Fixed compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 238
diff changeset
   852
        case MOJOSHADER_ATTRIBUTE_UNKNOWN: return GL_NONE; // oh well.
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   853
        case MOJOSHADER_ATTRIBUTE_BYTE: return GL_BYTE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   854
        case MOJOSHADER_ATTRIBUTE_UBYTE: return GL_UNSIGNED_BYTE;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   855
        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
   856
        case MOJOSHADER_ATTRIBUTE_USHORT: return GL_UNSIGNED_SHORT;
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   857
        case MOJOSHADER_ATTRIBUTE_INT: return GL_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   858
        case MOJOSHADER_ATTRIBUTE_UINT: return GL_UNSIGNED_INT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   859
        case MOJOSHADER_ATTRIBUTE_FLOAT: return GL_FLOAT;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   860
        case MOJOSHADER_ATTRIBUTE_DOUBLE: return GL_DOUBLE;
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   861
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   862
        case MOJOSHADER_ATTRIBUTE_HALF_FLOAT:
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   863
            if (ctx->have_GL_NV_half_float)
236
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   864
                return GL_HALF_FLOAT_NV;
8e2fc535b210 Support for half-float attribute arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 230
diff changeset
   865
            break;
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   866
    } // switch
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   867
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   868
    return GL_NONE;  // oh well. Raises a GL error later.
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   869
} // opengl_attr_type
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   870
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   871
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   872
// !!! FIXME: shouldn't (index) be unsigned?
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   873
void MOJOSHADER_glSetVertexAttribute(MOJOSHADER_usage usage,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   874
                                     int index, unsigned int size,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   875
                                     MOJOSHADER_attributeType type,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   876
                                     int normalized, unsigned int stride,
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   877
                                     const void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   878
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   879
    if ((ctx->bound_program == NULL) || (ctx->bound_program->vertex == NULL))
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   880
        return;
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   881
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   882
    const GLenum gl_type = opengl_attr_type(type);
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   883
    const GLboolean norm = (normalized) ? GL_TRUE : GL_FALSE;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   884
    GLuint gl_index = 0;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   885
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   886
    // We have to map POSITION0 to generic vertex attribute 0; the
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   887
    //  GL_ARB_vertex_shader spec says this is equivalent to using
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   888
    //  glVertexPointer(), but without the limitations of that entry point.
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   889
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   890
    if ((usage != MOJOSHADER_USAGE_POSITION) || (index != 0))
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   891
    {
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   892
        int i;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   893
        const int count = ctx->bound_program->attribute_count;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   894
        for (i = 0; i < count; i++)
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   895
        {
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   896
            const AttributeMap *map = &ctx->bound_program->attributes[i];
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   897
            const MOJOSHADER_attribute *a = map->attribute;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   898
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   899
            // !!! FIXME: is this array guaranteed to be sorted by usage?
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   900
            // !!! FIXME:  if so, we can break if a->usage > usage.
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   901
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   902
            if ((a->usage == usage) && (a->index == index))
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   903
            {
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   904
                gl_index = map->location;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   905
                break;
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   906
            } // if
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   907
        } // for
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   908
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   909
        if (gl_index == 0)
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   910
            return;  // nothing to do, this shader doesn't use this stream.
216
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   911
    } // if
f5f4e5f05951 Implemented MOJOSHADER_glSetVertexAttribute().
Ryan C. Gordon <icculus@icculus.org>
parents: 215
diff changeset
   912
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   913
    ctx->glVertexAttribPointer(gl_index, size, gl_type, norm, stride, ptr);
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   914
    ctx->glEnableVertexAttribArray(gl_index);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   915
} // MOJOSHADER_glSetVertexAttribute
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   916
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   917
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   918
void MOJOSHADER_glProgramReady(void)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   919
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   920
    int i;
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   921
    int count;
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   922
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   923
    if (ctx->bound_program == NULL)
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   924
        return;  // nothing to do.
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   925
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   926
    // !!! FIXME: don't push Uniforms/Samplers if they haven't changed.
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   927
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   928
    // push Uniforms to the program from our register files...
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   929
    count = ctx->bound_program->uniform_count;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   930
    for (i = 0; i < count; i++)
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   931
    {
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   932
        const UniformMap *map = &ctx->bound_program->uniforms[i];
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   933
        const MOJOSHADER_uniform *u = map->uniform;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   934
        const MOJOSHADER_uniformType type = u->type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   935
        const MOJOSHADER_shaderType shader_type = map->shader_type;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   936
        const int index = u->index;
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   937
        const int size = u->array_count;
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   938
        const GLint location = map->location;
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
   939
281
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   940
        // only use arrays for 'c' registers.
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   941
        assert((size == 0) || (type == MOJOSHADER_UNIFORM_FLOAT));
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   942
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   943
        if (size != 0)  // !!! FIXME: this code sucks.
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   944
        {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   945
            // !!! FIXME: calculate this all at link time.
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   946
            if (shader_type == MOJOSHADER_TYPE_VERTEX)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   947
            {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   948
                const MOJOSHADER_constant *c = ctx->bound_program->vertex->parseData->constants;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   949
                int hi = ctx->bound_program->vertex->parseData->constant_count;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   950
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   951
                int j;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   952
                GLfloat *ptr = ctx->bound_program->constants;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   953
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   954
                for (j = 0; j < hi; j++)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   955
                {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   956
                    if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   957
                        continue;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   958
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   959
                    const int idx = c[j].index;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   960
                    if ( (idx >= index) && (idx < (index + size)) )
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   961
                    {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   962
                        memcpy(ptr, &ctx->vs_reg_file_f[idx * 4], 16);  // !!! FIXME: 16
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   963
                        memcpy(&ctx->vs_reg_file_f[idx * 4], &c->value.f, 16);  // !!! FIXME: 16
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   964
                        ptr += 4;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   965
                    } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   966
                } // for
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   967
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   968
                ctx->glUniform4fv(location, size, &ctx->vs_reg_file_f[index * 4]);
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   969
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   970
                ptr = ctx->bound_program->constants;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   971
                for (j = 0; j < hi; j++)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   972
                {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   973
                    if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   974
                        continue;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   975
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   976
                    const int idx = c[j].index;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   977
                    if ( (idx >= index) && (idx < (index + size)) )
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   978
                    {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   979
                        memcpy(&ctx->vs_reg_file_f[idx * 4], ptr, 16);  // !!! FIXME: 16
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   980
                        ptr += 4;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   981
                    } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   982
                } // for
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   983
            } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   984
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   985
            else if (shader_type == MOJOSHADER_TYPE_PIXEL)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   986
            {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   987
                const MOJOSHADER_constant *c = ctx->bound_program->fragment->parseData->constants;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   988
                int hi = ctx->bound_program->fragment->parseData->constant_count;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   989
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   990
                int j;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   991
                GLfloat *ptr = ctx->bound_program->constants;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   992
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   993
                for (j = 0; j < hi; j++)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   994
                {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   995
                    if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   996
                        continue;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   997
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   998
                    const int idx = c[j].index;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
   999
                    if ( (idx >= index) && (idx < (index + size)) )
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1000
                    {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1001
                        memcpy(ptr, &ctx->ps_reg_file_f[idx * 4], 16);  // !!! FIXME: 16
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1002
                        memcpy(&ctx->ps_reg_file_f[idx * 4], &c->value.f, 16);  // !!! FIXME: 16
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1003
                        ptr += 4;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1004
                    } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1005
                } // for
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1006
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1007
                ctx->glUniform4fv(location, size, &ctx->ps_reg_file_f[index * 4]);
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1008
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1009
                ptr = ctx->bound_program->constants;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1010
                for (j = 0; j < hi; j++)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1011
                {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1012
                    if (c[j].type != MOJOSHADER_UNIFORM_FLOAT)
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1013
                        continue;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1014
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1015
                    const int idx = c[j].index;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1016
                    if ( (idx >= index) && (idx < (index + size)) )
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1017
                    {
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1018
                        memcpy(&ctx->ps_reg_file_f[idx * 4], ptr, 16);  // !!! FIXME: 16
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1019
                        ptr += 4;
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1020
                    } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1021
                } // for
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1022
            } // else if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1023
        } // if
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1024
cb0ee573a33d OpenGL glue now handles Uniform arrays.
Ryan C. Gordon <icculus@icculus.org>
parents: 277
diff changeset
  1025
        else if (shader_type == MOJOSHADER_TYPE_VERTEX)
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1026
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1027
            if (type == MOJOSHADER_UNIFORM_FLOAT)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1028
                ctx->glUniform4fv(location, 1, &ctx->vs_reg_file_f[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1029
            else if (type == MOJOSHADER_UNIFORM_INT)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1030
                ctx->glUniform4iv(location, 1, &ctx->vs_reg_file_i[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1031
            else if (type == MOJOSHADER_UNIFORM_BOOL)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1032
                ctx->glUniform1i(location, ctx->vs_reg_file_b[index]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1033
        } // if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1034
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1035
        else if (shader_type == MOJOSHADER_TYPE_PIXEL)
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1036
        {
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1037
            if (type == MOJOSHADER_UNIFORM_FLOAT)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1038
                ctx->glUniform4fv(location, 1, &ctx->ps_reg_file_f[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1039
            else if (type == MOJOSHADER_UNIFORM_INT)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1040
                ctx->glUniform4iv(location, 1, &ctx->ps_reg_file_i[index * 4]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1041
            else if (type == MOJOSHADER_UNIFORM_BOOL)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1042
                ctx->glUniform1i(location, ctx->ps_reg_file_b[index]);
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1043
        } // else if
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
  1044
    } // for
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1045
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1046
    // push Samplers to the program from our register files...
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1047
    count = ctx->bound_program->sampler_count;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1048
    for (i = 0; i < count; i++)
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1049
    {
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1050
        const SamplerMap *map = &ctx->bound_program->samplers[i];
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1051
        const MOJOSHADER_sampler *s = map->sampler;
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1052
        ctx->glUniform1i(map->location, ctx->sampler_reg_file[s->index]);
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
  1053
    } // for
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1054
} // MOJOSHADER_glProgramReady
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1055
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1056
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
  1057
void MOJOSHADER_glDeleteProgram(MOJOSHADER_glProgram *program)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1058
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
  1059
    program_unref(program);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1060
} // MOJOSHADER_glDeleteProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1061
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1062
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
  1063
void MOJOSHADER_glDeleteShader(MOJOSHADER_glShader *shader)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1064
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1065
    shader_unref(shader);
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1066
} // MOJOSHADER_glDeleteShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1067
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1068
238
e98f14da2897 Renamed glInit and glDeinit to glCreateContext and glDestroyContext.
Ryan C. Gordon <icculus@icculus.org>
parents: 237
diff changeset
  1069
void MOJOSHADER_glDestroyContext(MOJOSHADER_glContext *_ctx)
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1070
{
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1071
    MOJOSHADER_glContext *current_ctx = ctx;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1072
    ctx = _ctx;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1073
    MOJOSHADER_glBindProgram(NULL);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
  1074
    lookup_entry_points(NULL);
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1075
    Free(ctx);
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
  1076
    ctx = ((current_ctx == _ctx) ? NULL : current_ctx);
238
e98f14da2897 Renamed glInit and glDeinit to glCreateContext and glDestroyContext.
Ryan C. Gordon <icculus@icculus.org>
parents: 237
diff changeset
  1077
} // MOJOSHADER_glDestroyContext
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1078
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1079
// end of mojoshader_opengl.c ...
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
  1080