mojoshader_opengl.c
author Ethan Lee <flibitijibibo@flibitijibibo.com>
Mon, 27 Jan 2020 09:27:23 -0500
changeset 1232 eec14d60f416
parent 1225 50b8dd7e0b1a
child 1237 da180fe5b4b7
permissions -rw-r--r--
Better value for max vertex_attrib_loc
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
1195
f4ef8606c68d Apply some MSC_VER special cases to WIN32 as well (thanks Vincent!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1192
diff changeset
    16
#ifdef _WIN32
254
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
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    21
#if (defined(__APPLE__) && defined(__MACH__))
1192
706e4d246ca1 iOS support (thanks Caleb!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1188
diff changeset
    22
#include "TargetConditionals.h"
706e4d246ca1 iOS support (thanks Caleb!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1188
diff changeset
    23
#if !TARGET_OS_IPHONE && !TARGET_OS_TV
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    24
#define PLATFORM_MACOSX 1
1192
706e4d246ca1 iOS support (thanks Caleb!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1188
diff changeset
    25
#endif /* !TARGET_OS_IPHONE && !TARGET_OS_TV */
706e4d246ca1 iOS support (thanks Caleb!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1188
diff changeset
    26
#endif /* (defined(__APPLE__) && defined(__MACH__)) */
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    27
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    28
#if PLATFORM_MACOSX
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    29
#include <Carbon/Carbon.h>
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    30
#endif
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
    31
464
eba4cf79437f Moved some common stuff to mojoshader_internal.h ...
Ryan C. Gordon <icculus@icculus.org>
parents: 463
diff changeset
    32
#define __MOJOSHADER_INTERNAL__ 1
eba4cf79437f Moved some common stuff to mojoshader_internal.h ...
Ryan C. Gordon <icculus@icculus.org>
parents: 463
diff changeset
    33
#include "mojoshader_internal.h"
eba4cf79437f Moved some common stuff to mojoshader_internal.h ...
Ryan C. Gordon <icculus@icculus.org>
parents: 463
diff changeset
    34
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    35
#define GL_GLEXT_LEGACY 1
246
897868fdd958 Moved gl*.h into GL directory.
Ryan C. Gordon <icculus@icculus.org>
parents: 245
diff changeset
    36
#include "GL/gl.h"
897868fdd958 Moved gl*.h into GL directory.
Ryan C. Gordon <icculus@icculus.org>
parents: 245
diff changeset
    37
#include "GL/glext.h"
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
    39
#if SUPPORT_PROFILE_GLSPIRV
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    40
#include "spirv/spirv.h"
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    41
#endif
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    42
1073
6eccf031c7e6 Cleaned up half-float stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 1072
diff changeset
    43
#ifndef GL_HALF_FLOAT_NV
6eccf031c7e6 Cleaned up half-float stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 1072
diff changeset
    44
#define GL_HALF_FLOAT_NV 0x140B
6eccf031c7e6 Cleaned up half-float stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 1072
diff changeset
    45
#endif
6eccf031c7e6 Cleaned up half-float stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 1072
diff changeset
    46
6eccf031c7e6 Cleaned up half-float stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 1072
diff changeset
    47
#ifndef GL_HALF_FLOAT_ARB
6eccf031c7e6 Cleaned up half-float stuff.
Ryan C. Gordon <icculus@icculus.org>
parents: 1072
diff changeset
    48
#define GL_HALF_FLOAT_ARB 0x140B
449
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    49
#endif
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    50
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    51
#ifndef GL_HALF_FLOAT_OES
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    52
#define GL_HALF_FLOAT_OES 0x8D61
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    53
#endif
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    54
1056
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
    55
// this happens to be the same value for ARB1 and GLSL.
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
    56
#ifndef GL_PROGRAM_POINT_SIZE
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
    57
#define GL_PROGRAM_POINT_SIZE 0x8642
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
    58
#endif
449
5a978f37e750 Added support for GL_ARB_half_float_vertex and GL_OES_vertex_half_float.
Ryan C. Gordon <icculus@icculus.org>
parents: 448
diff changeset
    59
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    60
// FIXME: ARB_gl_spirv in glext.h? -flibit
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    61
#ifndef GL_ARB_gl_spirv
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    62
#define GL_ARB_gl_spirv 1
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    63
#define GL_SHADER_BINARY_FORMAT_SPIR_V_ARB 0x9551
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    64
typedef void (APIENTRYP PFNGLSPECIALIZESHADERARBPROC) (
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    65
    GLuint shader,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    66
    const GLchar* pEntryPoint,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    67
    GLuint numSpecializationConstants,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    68
    const GLuint* pConstantIndex,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    69
    const GLuint* pConstantValue
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    70
);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    71
#endif
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
    72
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
struct MOJOSHADER_glShader
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
{
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    const MOJOSHADER_parseData *parseData;
323
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
    76
    GLuint handle;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    uint32 refcount;
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    80
typedef struct
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    81
{
206
7027d889acdd Implemented MOJOSHADER_glProgramReady().
Ryan C. Gordon <icculus@icculus.org>
parents: 205
diff changeset
    82
    MOJOSHADER_shaderType shader_type;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    83
    const MOJOSHADER_uniform *uniform;
798
5dd67cc04cf9 Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 797
diff changeset
    84
    GLint location;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    85
} UniformMap;
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    86
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    87
typedef struct
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    88
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    89
    const MOJOSHADER_attribute *attribute;
798
5dd67cc04cf9 Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 797
diff changeset
    90
    GLint location;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    91
} AttributeMap;
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
    92
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
struct MOJOSHADER_glProgram
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
{
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    95
    MOJOSHADER_glShader *vertex;
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
    96
    MOJOSHADER_glShader *fragment;
323
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
    97
    GLuint handle;
774
9ac0e5ad0205 Don't push uniforms if they've definitely not changed.
Ryan C. Gordon <icculus@icculus.org>
parents: 773
diff changeset
    98
    uint32 generation;
205
8583f89985df Cache uniform locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 204
diff changeset
    99
    uint32 uniform_count;
1090
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   100
    uint32 texbem_count;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   101
    UniformMap *uniforms;
213
73935330a973 Cache attribute locations for GLSL programs at link time.
Ryan C. Gordon <icculus@icculus.org>
parents: 212
diff changeset
   102
    uint32 attribute_count;
226
4a2b3d0b535f Patched mojoshader_opengl.c to compile, mostly.
Ryan C. Gordon <icculus@icculus.org>
parents: 223
diff changeset
   103
    AttributeMap *attributes;
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   104
    size_t vs_uniforms_float4_count;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   105
    GLfloat *vs_uniforms_float4;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   106
    size_t vs_uniforms_int4_count;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   107
    GLint *vs_uniforms_int4;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   108
    size_t vs_uniforms_bool_count;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   109
    GLint *vs_uniforms_bool;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   110
    size_t ps_uniforms_float4_count;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   111
    GLfloat *ps_uniforms_float4;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   112
    size_t ps_uniforms_int4_count;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   113
    GLint *ps_uniforms_int4;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   114
    size_t ps_uniforms_bool_count;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   115
    GLint *ps_uniforms_bool;
1043
6227066350b4 Preshader input registers are separate from the actual shader constant file!
Ryan C. Gordon <icculus@icculus.org>
parents: 1040
diff changeset
   116
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
    uint32 refcount;
1056
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
   118
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
   119
    int uses_pointsize;
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
   120
1232
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   121
    // According to MSDN...
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   122
    //
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   123
    // n is an optional integer between 0 and the number of resources supported.
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   124
    //  For example, POSITION0, TEXCOOR1, etc.
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   125
    //
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   126
    // The input registers consist of 16 four-component floating-point vectors,
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   127
    //   designated as v0 through v15.
eec14d60f416 Better value for max vertex_attrib_loc
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1225
diff changeset
   128
    GLint vertex_attrib_loc[MOJOSHADER_USAGE_TOTAL][16];
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   129
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   130
    // GLSL uses these...location of uniform arrays.
797
58d15db2881e GLSL Uniform locations are signed ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 791
diff changeset
   131
    GLint vs_float4_loc;
58d15db2881e GLSL Uniform locations are signed ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 791
diff changeset
   132
    GLint vs_int4_loc;
58d15db2881e GLSL Uniform locations are signed ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 791
diff changeset
   133
    GLint vs_bool_loc;
58d15db2881e GLSL Uniform locations are signed ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 791
diff changeset
   134
    GLint ps_float4_loc;
58d15db2881e GLSL Uniform locations are signed ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 791
diff changeset
   135
    GLint ps_int4_loc;
58d15db2881e GLSL Uniform locations are signed ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 791
diff changeset
   136
    GLint ps_bool_loc;
1210
c586d4590241 Replace glProgramViewportFlip with glProgramViewportInfo
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1207
diff changeset
   137
c586d4590241 Replace glProgramViewportFlip with glProgramViewportInfo
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1207
diff changeset
   138
    // Numerous fixes for coordinate system mismatches
c586d4590241 Replace glProgramViewportFlip with glProgramViewportInfo
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1207
diff changeset
   139
    GLint ps_vpos_flip_loc;
c586d4590241 Replace glProgramViewportFlip with glProgramViewportInfo
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1207
diff changeset
   140
    int current_vpos_flip[2];
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   141
#ifdef MOJOSHADER_FLIP_RENDERTARGET
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   142
    GLint vs_flip_loc;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   143
    int current_flip;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   144
#endif
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
};
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
277
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
   147
#ifndef WINGDIAPI
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
   148
#define WINGDIAPI
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
   149
#endif
49a6571cac6d Patched to compile on older (?) GL headers.
Ryan C. Gordon <icculus@icculus.org>
parents: 276
diff changeset
   150
242
c3c5693ba179 Fixed vertex attribute aliasing in OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 241
diff changeset
   151
// 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
   152
typedef WINGDIAPI void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *params);
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   153
typedef WINGDIAPI const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
333
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   154
typedef WINGDIAPI GLenum (APIENTRYP PFNGLGETERRORPROC) (void);
358
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   155
typedef WINGDIAPI void (APIENTRYP PFNGLENABLEPROC) (GLenum cap);
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   156
typedef WINGDIAPI void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap);
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   157
773
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   158
// Max entries for each register file type...
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   159
#define MAX_REG_FILE_F 8192
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   160
#define MAX_REG_FILE_I 2047
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   161
#define MAX_REG_FILE_B 2047
1090
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   162
#define MAX_TEXBEMS 3  // ps_1_1 allows 4 texture stages, texbem can't use t0.
773
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   163
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   164
struct MOJOSHADER_glContext
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   165
{
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   166
    // Allocators...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   167
    MOJOSHADER_malloc malloc_fn;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   168
    MOJOSHADER_free free_fn;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   169
    void *malloc_data;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   170
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   171
    // The constant register files...
806
facbf9d3f1ce Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 798
diff changeset
   172
    // !!! FIXME: Man, it kills me how much memory this takes...
facbf9d3f1ce Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 798
diff changeset
   173
    // !!! FIXME:  ... make this dynamically allocated on demand.
773
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   174
    GLfloat vs_reg_file_f[MAX_REG_FILE_F * 4];
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   175
    GLint vs_reg_file_i[MAX_REG_FILE_I * 4];
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   176
    uint8 vs_reg_file_b[MAX_REG_FILE_B];
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   177
    GLfloat ps_reg_file_f[MAX_REG_FILE_F * 4];
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   178
    GLint ps_reg_file_i[MAX_REG_FILE_I * 4];
3e3638be6e2b Replaced some constants with #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 772
diff changeset
   179
    uint8 ps_reg_file_b[MAX_REG_FILE_B];
284
ea52f9707795 Sampler loading support in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 281
diff changeset
   180
    GLuint sampler_reg_file[16];
1090
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
   181
    GLfloat texbem_state[MAX_TEXBEMS * 6];
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   182
774
9ac0e5ad0205 Don't push uniforms if they've definitely not changed.
Ryan C. Gordon <icculus@icculus.org>
parents: 773
diff changeset
   183
    // This increments every time we change the register files.
9ac0e5ad0205 Don't push uniforms if they've definitely not changed.
Ryan C. Gordon <icculus@icculus.org>
parents: 773
diff changeset
   184
    uint32 generation;
9ac0e5ad0205 Don't push uniforms if they've definitely not changed.
Ryan C. Gordon <icculus@icculus.org>
parents: 773
diff changeset
   185
1066
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   186
    // This keeps track of implicitly linked programs.
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   187
    HashTable *linker_cache;
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   188
775
2c93dcb14687 Only enable/disable vertex arrays when forced to.
Ryan C. Gordon <icculus@icculus.org>
parents: 774
diff changeset
   189
    // This tells us which vertex attribute arrays we have enabled.
798
5dd67cc04cf9 Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 797
diff changeset
   190
    GLint max_attrs;
775
2c93dcb14687 Only enable/disable vertex arrays when forced to.
Ryan C. Gordon <icculus@icculus.org>
parents: 774
diff changeset
   191
    uint8 want_attr[32];
2c93dcb14687 Only enable/disable vertex arrays when forced to.
Ryan C. Gordon <icculus@icculus.org>
parents: 774
diff changeset
   192
    uint8 have_attr[32];
2c93dcb14687 Only enable/disable vertex arrays when forced to.
Ryan C. Gordon <icculus@icculus.org>
parents: 774
diff changeset
   193
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   194
    // This shadows vertex attribute and divisor states.
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   195
    GLuint attr_divisor[32];
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   196
1056
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
   197
    // rarely used, so we don't touch when we don't have to.
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
   198
    int pointsize_enabled;
73c697a7e950 Toggle pointsize support as necessary in the OpenGL bindings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1043
diff changeset
   199
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   200
    // GL stuff...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   201
    int opengl_major;
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   202
    int opengl_minor;
1070
32eb4fe78351 Reworked GLSL extension and version detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 1069
diff changeset
   203
    int glsl_major;
32eb4fe78351 Reworked GLSL extension and version detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 1069
diff changeset
   204
    int glsl_minor;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   205
    MOJOSHADER_glProgram *bound_program;
262
0f1531ac2578 Implemented MOJOSHADER_glBestProfile().
Ryan C. Gordon <icculus@icculus.org>
parents: 255
diff changeset
   206
    char profile[16];
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   207
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   208
#ifdef MOJOSHADER_XNA4_VERTEX_TEXTURES
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   209
    // Vertex texture sampler offset...
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   210
    int vertex_sampler_offset;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   211
#endif
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   212
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   213
    // Extensions...
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   214
    int have_core_opengl;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   215
    int have_opengl_2;  // different entry points than ARB extensions.
1117
f6712bf72c19 Allow looking up OpenGL extensions in the way appropriate for GL3+.
Ryan C. Gordon <icculus@icculus.org>
parents: 1111
diff changeset
   216
    int have_opengl_3;  // different extension query.
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   217
    int have_opengl_es; // different extension requirements
463
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   218
    int have_GL_ARB_vertex_program;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   219
    int have_GL_ARB_fragment_program;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   220
    int have_GL_NV_vertex_program2_option;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   221
    int have_GL_NV_fragment_program2;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   222
    int have_GL_NV_vertex_program3;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   223
    int have_GL_NV_gpu_program4;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   224
    int have_GL_ARB_shader_objects;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   225
    int have_GL_ARB_vertex_shader;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   226
    int have_GL_ARB_fragment_shader;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   227
    int have_GL_ARB_shading_language_100;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   228
    int have_GL_NV_half_float;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   229
    int have_GL_ARB_half_float_vertex;
6f3a82d7e3d2 Removed bitfields for full ints.
Ryan C. Gordon <icculus@icculus.org>
parents: 460
diff changeset
   230
    int have_GL_OES_vertex_half_float;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   231
    int have_GL_ARB_instanced_arrays;
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   232
    int have_GL_ARB_ES2_compatibility;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   233
    int have_GL_ARB_gl_spirv;
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   234
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   235
    // Entry points...
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   236
    PFNGLGETSTRINGPROC glGetString;
1117
f6712bf72c19 Allow looking up OpenGL extensions in the way appropriate for GL3+.
Ryan C. Gordon <icculus@icculus.org>
parents: 1111
diff changeset
   237
    PFNGLGETSTRINGIPROC glGetStringi;
333
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   238
    PFNGLGETERRORPROC glGetError;
276
1d7437469c94 Added MOJOSHADER_glMaxUniforms().
Ryan C. Gordon <icculus@icculus.org>
parents: 262
diff changeset
   239
    PFNGLGETINTEGERVPROC glGetIntegerv;
358
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   240
    PFNGLENABLEPROC glEnable;
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   241
    PFNGLDISABLEPROC glDisable;
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   242
    PFNGLDELETESHADERPROC glDeleteShader;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   243
    PFNGLDELETEPROGRAMPROC glDeleteProgram;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   244
    PFNGLATTACHSHADERPROC glAttachShader;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   245
    PFNGLCOMPILESHADERPROC glCompileShader;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   246
    PFNGLCREATESHADERPROC glCreateShader;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   247
    PFNGLCREATEPROGRAMPROC glCreateProgram;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   248
    PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   249
    PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   250
    PFNGLGETATTRIBLOCATIONPROC glGetAttribLocation;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   251
    PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
1125
3de60f597ebd Don't use the extension entry point glGetInfoLogARB() in the core GL2 path.
Ryan C. Gordon <icculus@icculus.org>
parents: 1124
diff changeset
   252
    PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   253
    PFNGLGETSHADERIVPROC glGetShaderiv;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   254
    PFNGLGETPROGRAMIVPROC glGetProgramiv;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   255
    PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   256
    PFNGLLINKPROGRAMPROC glLinkProgram;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   257
    PFNGLSHADERSOURCEPROC glShaderSource;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   258
    PFNGLUNIFORM1IPROC glUniform1i;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   259
    PFNGLUNIFORM1IVPROC glUniform1iv;
1210
c586d4590241 Replace glProgramViewportFlip with glProgramViewportInfo
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1207
diff changeset
   260
    PFNGLUNIFORM2FPROC glUniform2f;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   261
#ifdef MOJOSHADER_FLIP_RENDERTARGET
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   262
    PFNGLUNIFORM1FPROC glUniform1f;
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   263
#endif
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   264
    PFNGLUNIFORM4FVPROC glUniform4fv;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   265
    PFNGLUNIFORM4IVPROC glUniform4iv;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   266
    PFNGLUSEPROGRAMPROC glUseProgram;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   267
    PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   268
    PFNGLDELETEOBJECTARBPROC glDeleteObjectARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   269
    PFNGLATTACHOBJECTARBPROC glAttachObjectARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   270
    PFNGLCOMPILESHADERARBPROC glCompileShaderARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   271
    PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   272
    PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   273
    PFNGLDISABLEVERTEXATTRIBARRAYARBPROC glDisableVertexAttribArrayARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   274
    PFNGLENABLEVERTEXATTRIBARRAYARBPROC glEnableVertexAttribArrayARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   275
    PFNGLGETATTRIBLOCATIONARBPROC glGetAttribLocationARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   276
    PFNGLGETINFOLOGARBPROC glGetInfoLogARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   277
    PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   278
    PFNGLGETUNIFORMLOCATIONARBPROC glGetUniformLocationARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   279
    PFNGLLINKPROGRAMARBPROC glLinkProgramARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   280
    PFNGLSHADERSOURCEARBPROC glShaderSourceARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   281
    PFNGLUNIFORM1IARBPROC glUniform1iARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   282
    PFNGLUNIFORM1IVARBPROC glUniform1ivARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   283
    PFNGLUNIFORM4FVARBPROC glUniform4fvARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   284
    PFNGLUNIFORM4IVARBPROC glUniform4ivARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   285
    PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   286
    PFNGLVERTEXATTRIBPOINTERARBPROC glVertexAttribPointerARB;
333
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   287
    PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
395
d2307cc5d6e7 Use local parameters, not environment params, in the arb1/nv2 profiles.
Ryan C. Gordon <icculus@icculus.org>
parents: 394
diff changeset
   288
    PFNGLPROGRAMLOCALPARAMETER4FVARBPROC glProgramLocalParameter4fvARB;
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 424
diff changeset
   289
    PFNGLPROGRAMLOCALPARAMETERI4IVNVPROC glProgramLocalParameterI4ivNV;
333
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   290
    PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB;
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   291
    PFNGLGENPROGRAMSARBPROC glGenProgramsARB;
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   292
    PFNGLBINDPROGRAMARBPROC glBindProgramARB;
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   293
    PFNGLPROGRAMSTRINGARBPROC glProgramStringARB;
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   294
    PFNGLVERTEXATTRIBDIVISORARBPROC glVertexAttribDivisorARB;
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   295
    PFNGLSHADERBINARYPROC glShaderBinary;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   296
    PFNGLSPECIALIZESHADERARBPROC glSpecializeShaderARB;
323
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
   297
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
   298
    // interface for profile-specific things.
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
   299
    int (*profileMaxUniforms)(MOJOSHADER_shaderType shader_type);
333
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   300
    int (*profileCompileShader)(const MOJOSHADER_parseData *pd, GLuint *s);
323
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
   301
    void (*profileDeleteShader)(const GLuint shader);
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
   302
    void (*profileDeleteProgram)(const GLuint program);
347
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 340
diff changeset
   303
    GLint (*profileGetAttribLocation)(MOJOSHADER_glProgram *program, int idx);
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   304
    GLint (*profileGetUniformLocation)(MOJOSHADER_glProgram *program, MOJOSHADER_glShader *shader, int idx);
347
f8d9d0ae6ba8 Bunch more work (ARB1 profile, OpenGL glue, and general fixes).
Ryan C. Gordon <icculus@icculus.org>
parents: 340
diff changeset
   305
    GLint (*profileGetSamplerLocation)(MOJOSHADER_glProgram *, MOJOSHADER_glShader *, int);
333
2b36a1d72fa1 Wrote most of the arb1 OpenGL glue code.
Ryan C. Gordon <icculus@icculus.org>
parents: 323
diff changeset
   306
    GLuint (*profileLinkProgram)(MOJOSHADER_glShader *, MOJOSHADER_glShader *);
765
076f3bd42329 Moved profileInitProgram to profileFinalInitProgram.
Ryan C. Gordon <icculus@icculus.org>
parents: 764
diff changeset
   307
    void (*profileFinalInitProgram)(MOJOSHADER_glProgram *program);
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   308
    void (*profileUseProgram)(MOJOSHADER_glProgram *program);
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   309
    void (*profilePushConstantArray)(MOJOSHADER_glProgram *, const MOJOSHADER_uniform *, const GLfloat *);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   310
    void (*profilePushUniforms)(void);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   311
    void (*profilePushSampler)(GLint loc, GLuint sampler);
760
6dc8d2cafc58 "Must Push" seemed more correct than "Must Load".
Ryan C. Gordon <icculus@icculus.org>
parents: 759
diff changeset
   312
    int (*profileMustPushConstantArrays)(void);
6dc8d2cafc58 "Must Push" seemed more correct than "Must Load".
Ryan C. Gordon <icculus@icculus.org>
parents: 759
diff changeset
   313
    int (*profileMustPushSamplers)(void);
1197
4883fc7d3751 Skip GL_PROGRAM_POINT_SIZE for ES contexts
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1196
diff changeset
   314
    void (*profileToggleProgramPointSize)(int enable);
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   315
};
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   316
323
b60c88ec8182 Initial work on ARB1 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 321
diff changeset
   317
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   318
static MOJOSHADER_glContext *ctx = NULL;
229
67a3d459d865 OpenGL extension lookup and entry point loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 226
diff changeset
   319
219
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   320
// Error state...
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   321
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
   322
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   323
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
   324
{
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   325
    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
   326
} // set_error
61167fc96790 Added MOJOSHADER_glGetError() function, filled in error state.
Ryan C. Gordon <icculus@icculus.org>
parents: 216
diff changeset
   327
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   328
#if PLATFORM_MACOSX
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   329
static inline int macosx_version_atleast(int x, int y, int z)
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   330
{
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   331
    static int checked = 0;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   332
    static int combined = 0;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   333
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   334
    if (!checked)
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   335
    {
1133
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   336
        SInt32 ver = 0;
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   337
        SInt32 major = 0;
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   338
        SInt32 minor = 0;
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   339
        SInt32 patch = 0;
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   340
        int convert = 0;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   341
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   342
        if (Gestalt(gestaltSystemVersion, &ver) != noErr)
1133
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   343
        {
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   344
            ver = 0x1000;  // oh well.
1133
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   345
            convert = 1;  // split (ver) into (major),(minor),(patch).
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   346
        }
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   347
        else if (ver < 0x1030)
1133
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   348
        {
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   349
            convert = 1;  // split (ver) into (major),(minor),(patch).
1133
eb1eb43987b3 Static analysis fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   350
        }
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   351
        else
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   352
        {
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   353
            // presumably this won't fail. But if it does, we'll just use the
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   354
            //  original version value. This might cut the value--10.12.11 will
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   355
            //  come out to 10.9.9, for example--but it's better than nothing.
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   356
            if (Gestalt(gestaltSystemVersionMajor, &major) != noErr)
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   357
                convert = 1;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   358
            else if (Gestalt(gestaltSystemVersionMinor, &minor) != noErr)
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   359
                convert = 1;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   360
            else if (Gestalt(gestaltSystemVersionBugFix, &patch) != noErr)
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   361
                convert = 1;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   362
        } // else
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   363
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   364
        if (convert)
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   365
        {
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   366
            major = ((ver & 0xFF00) >> 8);
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   367
            major = (((major / 16) * 10) + (major % 16));
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   368
            minor = ((ver & 0xF0) >> 4);
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   369
            patch = (ver & 0xF);
533
177b9242d08a Fixed a comment.
Ryan C. Gordon <icculus@icculus.org>
parents: 501
diff changeset
   370
        } // if
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   371
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   372
        combined = (major << 16) | (minor << 8) | patch;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   373
        checked = 1;
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   374
    } // if
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   375
460
54c614f0fcb0 Fixed OS X detection code.
Ryan C. Gordon <icculus@icculus.org>
parents: 459
diff changeset
   376
    return (combined >= ((x << 16) | (y << 8) | z));
457
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   377
} // macosx_version_atleast
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   378
#endif
96552d8041bf Forbid GLSL on Mac OS X Tiger systems.
Ryan C. Gordon <icculus@icculus.org>
parents: 450
diff changeset
   379
1066
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   380
static inline void out_of_memory(void)
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   381
{
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   382
    set_error("out of memory");
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   383
} // out_of_memory
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   384
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   385
static inline void *Malloc(const size_t len)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   386
{
1188
25000edc0176 Move zeromalloc trickery to internal malloc/free functions
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1182
diff changeset
   387
    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
   388
    if (retval == NULL)
1066
ff14db741834 Added MOJOSHADER_glBindShaders().
Ryan C. Gordon <icculus@icculus.org>
parents: 1063
diff changeset
   389
        out_of_memory();
220
df5ea69833d5 Fixes to Malloc() and Free() in mojoshader_opengl.c ...
Ryan C. Gordon <icculus@icculus.org>
parents: 219
diff changeset
   390
    return retval;
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   391
} // Malloc
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   392
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   393
static inline void Free(void *ptr)
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   394
{
1188
25000edc0176 Move zeromalloc trickery to internal malloc/free functions
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1182
diff changeset
   395
    if (ptr != NULL)
237
09f35dfc1d7e OpenGL glue now allows for multiple contexts.
Ryan C. Gordon <icculus@icculus.org>
parents: 236
diff changeset
   396
        ctx->free_fn(ptr, ctx->malloc_data);
202
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   397
} // Free
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   398
20bdf2d7036e Initial add of mojoshader_opengl.c
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   399
358
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   400
static inline void toggle_gl_state(GLenum state, int val)
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   401
{
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   402
    if (val)
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   403
        ctx->glEnable(state);
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   404
    else
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   405
        ctx->glDisable(state);
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   406
} // toggle_gl_state
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   407
f63e18cf642f Fixed MOJOSHADER_glBindProgram() for arb1 profile (thanks, Nicholas!).
Ryan C. Gordon <icculus@icculus.org>
parents: 355
diff changeset
   408
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   409
// profile-specific implementations...
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   410
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
   411
#if SUPPORT_PROFILE_GLSL || SUPPORT_PROFILE_GLSPIRV
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   412
static inline GLenum glsl_shader_type(const MOJOSHADER_shaderType t)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   413
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   414
    // these enums match between core 2.0 and the ARB extensions.
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   415
    if (t == MOJOSHADER_TYPE_VERTEX)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   416
        return GL_VERTEX_SHADER;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   417
    else if (t == MOJOSHADER_TYPE_PIXEL)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   418
        return GL_FRAGMENT_SHADER;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   419
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   420
    // !!! FIXME: geometry shaders?
1207
c4a393403859 Assert of unknown GLSL shader type (thanks Kate!)
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1197
diff changeset
   421
    assert(0 && "Unknown GLSL shader type!");
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   422
    return GL_NONE;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   423
} // glsl_shader_type
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   424
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   425
760
6dc8d2cafc58 "Must Push" seemed more correct than "Must Load".
Ryan C. Gordon <icculus@icculus.org>
parents: 759
diff changeset
   426
static int impl_GLSL_MustPushConstantArrays(void) { return 1; }
6dc8d2cafc58 "Must Push" seemed more correct than "Must Load".
Ryan C. Gordon <icculus@icculus.org>
parents: 759
diff changeset
   427
static int impl_GLSL_MustPushSamplers(void) { return 1; }
438
73492129c1af Expose true constant arrays in parseData, load them at link time for GLSL.
Ryan C. Gordon <icculus@icculus.org>
parents: 436
diff changeset
   428
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   429
static int impl_GLSL_MaxUniforms(MOJOSHADER_shaderType shader_type)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   430
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   431
    // these enums match between core 2.0 and the ARB extensions.
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   432
    GLenum pname = GL_NONE;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   433
    GLint val = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   434
    if (shader_type == MOJOSHADER_TYPE_VERTEX)
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   435
        pname = GL_MAX_VERTEX_UNIFORM_COMPONENTS;
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   436
    else if (shader_type == MOJOSHADER_TYPE_PIXEL)
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   437
        pname = GL_MAX_FRAGMENT_UNIFORM_COMPONENTS;
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   438
    else
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   439
        return -1;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   440
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   441
    ctx->glGetIntegerv(pname, &val);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   442
    return (int) val;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   443
} // impl_GLSL_MaxUniforms
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   444
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
   445
#if SUPPORT_PROFILE_GLSPIRV
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   446
static const SpirvPatchTable* spv_getPatchTable(MOJOSHADER_glShader *shader)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   447
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   448
    const MOJOSHADER_parseData *pd = shader->parseData;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   449
    size_t table_offset = pd->output_len - sizeof(SpirvPatchTable);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   450
    return (const SpirvPatchTable *) (pd->output + table_offset);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   451
} // spv_getPatchTable
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   452
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   453
static int spv_CompileShader(const MOJOSHADER_parseData *pd, int32 base_location, GLuint *s, int32 patch_pcoord)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   454
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   455
    GLint ok = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   456
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   457
    GLsizei data_len = pd->output_len - sizeof(SpirvPatchTable);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   458
    const GLvoid* data = pd->output;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   459
    uint32 *patched_data = NULL;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   460
    if (base_location || patch_pcoord)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   461
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   462
        size_t i, max;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   463
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   464
        patched_data = (uint32 *) Malloc(data_len);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   465
        memcpy(patched_data, data, data_len);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   466
        const SpirvPatchTable *table = (const SpirvPatchTable *) &pd->output[data_len];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   467
        if (table->vpflip.offset)      patched_data[table->vpflip.offset]      += base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   468
        if (table->array_vec4.offset)  patched_data[table->array_vec4.offset]  += base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   469
        if (table->array_ivec4.offset) patched_data[table->array_ivec4.offset] += base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   470
        if (table->array_bool.offset)  patched_data[table->array_bool.offset]  += base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   471
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   472
        for (i = 0, max = STATICARRAYLEN(table->samplers); i < max; i++)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   473
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   474
            SpirvPatchEntry entry = table->samplers[i];
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   475
            if (entry.offset)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   476
                patched_data[entry.offset] += base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   477
        } // for
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   478
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   479
        if (patch_pcoord && table->ps_texcoord0_offset)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   480
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   481
            // Subtract 3 to get from Location value offset to start of op.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   482
            uint32 op_base = table->ps_texcoord0_offset - 3;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   483
            assert(patched_data[op_base+0] == (SpvOpDecorate | (4 << 16)));
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   484
            assert(patched_data[op_base+2] == SpvDecorationLocation);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   485
            patched_data[op_base+2] = SpvDecorationBuiltIn;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   486
            patched_data[op_base+3] = SpvBuiltInPointCoord;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   487
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   488
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   489
        data = patched_data;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   490
    } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   491
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   492
    const GLuint shader = ctx->glCreateShader(glsl_shader_type(pd->shader_type));
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   493
    ctx->glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, data, data_len);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   494
    ctx->glSpecializeShaderARB(shader, pd->mainfn, 0, NULL, NULL); // FIXME: Spec Constants? -flibit
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   495
    ctx->glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   496
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   497
    if (patched_data)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   498
        Free(patched_data);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   499
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   500
    if (!ok)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   501
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   502
        GLsizei len = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   503
        ctx->glGetShaderInfoLog(shader, sizeof(error_buffer), &len,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   504
                             (GLchar *) error_buffer);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   505
        ctx->glDeleteShader(shader);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   506
        *s = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   507
        return 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   508
    } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   509
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   510
    *s = shader;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   511
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   512
    return 1;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   513
} // spv_CompileShader
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   514
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   515
static int impl_SPIRV_CompileShader(const MOJOSHADER_parseData *pd, GLuint *s)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   516
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   517
    // Compilation postponed until linking, but generate dummy shader id so hash table lookups work.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   518
    *s = ctx->glCreateShader(glsl_shader_type(pd->shader_type));
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   519
    return 1;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   520
} // impl_SPIRV_CompileShader
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   521
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   522
static GLuint impl_SPIRV_LinkProgram(MOJOSHADER_glShader *vshader,
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   523
                                     MOJOSHADER_glShader *pshader)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   524
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   525
    GLint ok = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   526
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   527
    // Shader compilation postponed until linking due to uniform locations being global in program.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   528
    // To avoid overlap between VS and PS, we need to know about other shader stages to assign final
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   529
    // uniform locations before compilation.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   530
    GLuint vs_handle = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   531
    int32 base_location = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   532
    int32 patch_pcoord = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   533
    if (vshader)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   534
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   535
        if (!spv_CompileShader(vshader->parseData, base_location, &vs_handle, patch_pcoord))
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   536
            return 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   537
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   538
        const SpirvPatchTable* patch_table = spv_getPatchTable(vshader);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   539
        base_location += patch_table->location_count;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   540
        patch_pcoord = patch_table->vs_has_psize;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   541
    } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   542
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   543
    GLuint ps_handle = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   544
    if (pshader)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   545
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   546
        if (!spv_CompileShader(pshader->parseData, base_location, &ps_handle, patch_pcoord))
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   547
            return 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   548
    } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   549
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   550
    if (ctx->have_opengl_2)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   551
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   552
        const GLuint program = ctx->glCreateProgram();
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   553
        if (vs_handle)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   554
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   555
            ctx->glAttachShader(program, vs_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   556
            ctx->glDeleteShader(vs_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   557
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   558
        if (ps_handle)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   559
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   560
            ctx->glAttachShader(program, ps_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   561
            ctx->glDeleteShader(ps_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   562
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   563
        ctx->glLinkProgram(program);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   564
        ctx->glGetProgramiv(program, GL_LINK_STATUS, &ok);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   565
        if (!ok)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   566
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   567
            GLsizei len = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   568
            ctx->glGetProgramInfoLog(program, sizeof (error_buffer),
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   569
                                     &len, (GLchar *) error_buffer);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   570
            ctx->glDeleteProgram(program);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   571
            return 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   572
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   573
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   574
        return program;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   575
    } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   576
    else
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   577
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   578
        const GLhandleARB program = ctx->glCreateProgramObjectARB();
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   579
        assert(sizeof(program) == sizeof(GLuint));  // not always true on OS X!
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   580
        if (vs_handle)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   581
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   582
            ctx->glAttachObjectARB(program, (GLhandleARB) vs_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   583
            ctx->glDeleteObjectARB((GLhandleARB) vs_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   584
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   585
        if (ps_handle)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   586
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   587
            ctx->glAttachObjectARB(program, (GLhandleARB) ps_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   588
            ctx->glDeleteObjectARB((GLhandleARB) ps_handle);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   589
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   590
        ctx->glLinkProgramARB(program);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   591
        ctx->glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   592
        if (!ok)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   593
        {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   594
            GLsizei len = 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   595
            ctx->glGetInfoLogARB(program, sizeof (error_buffer),
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   596
                                 &len, (GLcharARB *) error_buffer);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   597
            ctx->glDeleteObjectARB(program);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   598
            return 0;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   599
        } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   600
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   601
        return (GLuint) program;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   602
    } // else
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   603
} // impl_SPIRV_LinkProgram
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   604
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   605
static void impl_SPIRV_DeleteShader(const GLuint shader)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   606
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   607
    ctx->glDeleteShader(shader);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   608
} // impl_SPIRV_DeleteShader
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   609
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   610
static void impl_SPIRV_DeleteProgram(const GLuint program)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   611
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   612
    if (ctx->have_opengl_2)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   613
        ctx->glDeleteProgram(program);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   614
    else
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   615
        ctx->glDeleteObjectARB((GLhandleARB) program);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   616
} // impl_SPIRV_DeleteProgram
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   617
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   618
static GLint impl_SPIRV_GetAttribLocation(MOJOSHADER_glProgram *program, int idx)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   619
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   620
    return idx;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   621
} // impl_SPIRV_GetAttribLocation
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   622
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   623
static GLint impl_SPIRV_GetUniformLocation(MOJOSHADER_glProgram *program, MOJOSHADER_glShader *shader, int idx)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   624
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   625
    return 0;  // no-op, we push this as one big-ass array now.
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   626
} // impl_SPIRV_GetUniformLocation
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   627
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   628
static GLint impl_SPIRV_GetSamplerLocation(MOJOSHADER_glProgram *program, MOJOSHADER_glShader *shader, int idx)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   629
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   630
    const SpirvPatchTable *table = spv_getPatchTable(shader);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   631
    GLint location = table->samplers[idx].location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   632
    if (location == -1)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   633
        return location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   634
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   635
    assert(location >= 0);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   636
    if (shader->parseData->shader_type == MOJOSHADER_TYPE_PIXEL)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   637
        location += spv_getPatchTable(program->vertex)->location_count;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   638
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   639
    return location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   640
} // impl_SPIRV_GetSamplerLocation
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   641
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   642
static void impl_SPIRV_FinalInitProgram(MOJOSHADER_glProgram *program)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   643
{
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   644
    const SpirvPatchTable *vs_table = spv_getPatchTable(program->vertex);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   645
    const SpirvPatchTable *ps_table = spv_getPatchTable(program->fragment);
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   646
    program->vs_float4_loc = vs_table->array_vec4.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   647
    program->vs_int4_loc   = vs_table->array_ivec4.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   648
    program->vs_bool_loc   = vs_table->array_bool.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   649
    program->ps_float4_loc = ps_table->array_vec4.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   650
    program->ps_int4_loc   = ps_table->array_ivec4.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   651
    program->ps_bool_loc   = ps_table->array_bool.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   652
    program->ps_vpos_flip_loc = ps_table->vpflip.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   653
#ifdef MOJOSHADER_FLIP_RENDERTARGET
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   654
    program->vs_flip_loc   = vs_table->vpflip.location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   655
#endif
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   656
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   657
    int32 ps_base_location = vs_table->location_count;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   658
    if (ps_base_location)
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   659
    {
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   660
        if (program->ps_float4_loc != -1) program->ps_float4_loc += ps_base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   661
        if (program->ps_int4_loc   != -1) program->ps_int4_loc   += ps_base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   662
        if (program->ps_bool_loc   != -1) program->ps_bool_loc   += ps_base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   663
        if (program->ps_vpos_flip_loc != -1) program->ps_vpos_flip_loc += ps_base_location;
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   664
    } // if
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   665
} // impl_SPIRV_FinalInitProgram
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
   666
#endif // SUPPORT_PROFILE_GLSPIRV
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   667
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   668
#if SUPPORT_PROFILE_GLSL
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   669
static int impl_GLSL_CompileShader(const MOJOSHADER_parseData *pd, GLuint *s)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   670
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   671
    GLint ok = 0;
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   672
    const GLint codelen = (GLint) pd->output_len;
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   673
    const GLenum shader_type = glsl_shader_type(pd->shader_type);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   674
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   675
    if (ctx->have_opengl_2)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   676
    {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   677
        const GLuint shader = ctx->glCreateShader(shader_type);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   678
        ctx->glShaderSource(shader, 1, (const GLchar**) &pd->output, &codelen);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   679
        ctx->glCompileShader(shader);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   680
        ctx->glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   681
        if (!ok)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   682
        {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   683
            GLsizei len = 0;
1125
3de60f597ebd Don't use the extension entry point glGetInfoLogARB() in the core GL2 path.
Ryan C. Gordon <icculus@icculus.org>
parents: 1124
diff changeset
   684
            ctx->glGetShaderInfoLog(shader, sizeof (error_buffer), &len,
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   685
                                 (GLchar *) error_buffer);
1124
464fc9101a7e Leak fix: delete GLSL shader objects if we fail to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 1118
diff changeset
   686
            ctx->glDeleteShader(shader);
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   687
            *s = 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   688
            return 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   689
        } // if
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   690
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   691
        *s = shader;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   692
    } // if
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   693
    else
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   694
    {
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   695
        const GLhandleARB shader = ctx->glCreateShaderObjectARB(shader_type);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   696
        assert(sizeof (shader) == sizeof (*s));  // not always true on OS X!
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   697
        ctx->glShaderSourceARB(shader, 1,
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   698
                              (const GLcharARB **) &pd->output, &codelen);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   699
        ctx->glCompileShaderARB(shader);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   700
        ctx->glGetObjectParameterivARB(shader,GL_OBJECT_COMPILE_STATUS_ARB,&ok);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   701
        if (!ok)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   702
        {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   703
            GLsizei len = 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   704
            ctx->glGetInfoLogARB(shader, sizeof (error_buffer), &len,
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   705
                                 (GLcharARB *) error_buffer);
1124
464fc9101a7e Leak fix: delete GLSL shader objects if we fail to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 1118
diff changeset
   706
            ctx->glDeleteObjectARB(shader);
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   707
            *s = 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   708
            return 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   709
        } // if
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   710
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   711
        *s = (GLuint) shader;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   712
    } // else
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   713
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   714
    return 1;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   715
} // impl_GLSL_CompileShader
1224
21cd84f1aa0a Add support for emitting SPIR-V shaders.
Martin Krošlák <kroslakma@gmail.com>
parents: 1223
diff changeset
   716
#endif // SUPPORT_PROFILE_GLSL
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   717
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   718
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   719
static void impl_GLSL_DeleteShader(const GLuint shader)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   720
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   721
    if (ctx->have_opengl_2)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   722
        ctx->glDeleteShader(shader);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   723
    else
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   724
        ctx->glDeleteObjectARB((GLhandleARB) shader);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   725
} // impl_GLSL_DeleteShader
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   726
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   727
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   728
static void impl_GLSL_DeleteProgram(const GLuint program)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   729
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   730
    if (ctx->have_opengl_2)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   731
        ctx->glDeleteProgram(program);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   732
    else
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   733
        ctx->glDeleteObjectARB((GLhandleARB) program);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   734
} // impl_GLSL_DeleteProgram
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   735
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   736
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   737
static GLint impl_GLSL_GetUniformLocation(MOJOSHADER_glProgram *program,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   738
                                          MOJOSHADER_glShader *shader, int idx)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   739
{
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   740
    return 0;  // no-op, we push this as one big-ass array now.
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   741
} // impl_GLSL_GetUniformLocation
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   742
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   743
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   744
static inline GLint glsl_uniform_loc(MOJOSHADER_glProgram *program,
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   745
                                          const char *name)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   746
{
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   747
    return ctx->have_opengl_2 ?
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   748
        ctx->glGetUniformLocation(program->handle, name) :
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   749
        ctx->glGetUniformLocationARB((GLhandleARB) program->handle, name);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   750
} // glsl_uniform_loc
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   751
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   752
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   753
static GLint impl_GLSL_GetSamplerLocation(MOJOSHADER_glProgram *program,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   754
                                          MOJOSHADER_glShader *shader, int idx)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   755
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   756
    return glsl_uniform_loc(program, shader->parseData->samplers[idx].name);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   757
} // impl_GLSL_GetSamplerLocation
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   758
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   759
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   760
static GLint impl_GLSL_GetAttribLocation(MOJOSHADER_glProgram *program, int idx)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   761
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   762
    const MOJOSHADER_parseData *pd = program->vertex->parseData;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   763
    const MOJOSHADER_attribute *a = pd->attributes;
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   764
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   765
    if (ctx->have_opengl_2)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   766
    {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   767
        return ctx->glGetAttribLocation(program->handle,
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   768
                                        (const GLchar *) a[idx].name);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   769
    } // if
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   770
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   771
    return ctx->glGetAttribLocationARB((GLhandleARB) program->handle,
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   772
                                        (const GLcharARB *) a[idx].name);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   773
} // impl_GLSL_GetAttribLocation
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   774
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   775
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   776
static GLuint impl_GLSL_LinkProgram(MOJOSHADER_glShader *vshader,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   777
                                    MOJOSHADER_glShader *pshader)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   778
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   779
    GLint ok = 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   780
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   781
    if (ctx->have_opengl_2)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   782
    {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   783
        const GLuint program = ctx->glCreateProgram();
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   784
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   785
        if (vshader != NULL) ctx->glAttachShader(program, vshader->handle);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   786
        if (pshader != NULL) ctx->glAttachShader(program, pshader->handle);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   787
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   788
        ctx->glLinkProgram(program);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   789
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   790
        ctx->glGetProgramiv(program, GL_LINK_STATUS, &ok);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   791
        if (!ok)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   792
        {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   793
            GLsizei len = 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   794
            ctx->glGetProgramInfoLog(program, sizeof (error_buffer),
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   795
                                     &len, (GLchar *) error_buffer);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   796
            ctx->glDeleteProgram(program);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   797
            return 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   798
        } // if
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   799
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   800
        return program;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   801
    } // if
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   802
    else
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   803
    {
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   804
        const GLhandleARB program = ctx->glCreateProgramObjectARB();
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   805
        assert(sizeof(program) == sizeof(GLuint));  // not always true on OS X!
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   806
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   807
        if (vshader != NULL)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   808
            ctx->glAttachObjectARB(program, (GLhandleARB) vshader->handle);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   809
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   810
        if (pshader != NULL)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   811
            ctx->glAttachObjectARB(program, (GLhandleARB) pshader->handle);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   812
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   813
        ctx->glLinkProgramARB(program);
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   814
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   815
        ctx->glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   816
        if (!ok)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   817
        {
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   818
            GLsizei len = 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   819
            ctx->glGetInfoLogARB(program, sizeof (error_buffer),
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   820
                                 &len, (GLcharARB *) error_buffer);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   821
            ctx->glDeleteObjectARB(program);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   822
            return 0;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   823
        } // if
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   824
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   825
        return (GLuint) program;
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   826
    } // else
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   827
} // impl_GLSL_LinkProgram
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   828
765
076f3bd42329 Moved profileInitProgram to profileFinalInitProgram.
Ryan C. Gordon <icculus@icculus.org>
parents: 764
diff changeset
   829
static void impl_GLSL_FinalInitProgram(MOJOSHADER_glProgram *program)
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   830
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   831
    program->vs_float4_loc = glsl_uniform_loc(program, "vs_uniforms_vec4");
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   832
    program->vs_int4_loc = glsl_uniform_loc(program, "vs_uniforms_ivec4");
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   833
    program->vs_bool_loc = glsl_uniform_loc(program, "vs_uniforms_bool");
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   834
    program->ps_float4_loc = glsl_uniform_loc(program, "ps_uniforms_vec4");
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   835
    program->ps_int4_loc = glsl_uniform_loc(program, "ps_uniforms_ivec4");
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   836
    program->ps_bool_loc = glsl_uniform_loc(program, "ps_uniforms_bool");
1210
c586d4590241 Replace glProgramViewportFlip with glProgramViewportInfo
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1207
diff changeset
   837
    program->ps_vpos_flip_loc = glsl_uniform_loc(program, "vposFlip");
1150
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   838
#ifdef MOJOSHADER_FLIP_RENDERTARGET
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   839
    program->vs_flip_loc = glsl_uniform_loc(program, "vpFlip");
02c0f0afb39a - Add ability to build MojoShader as a shared library
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1149
diff changeset
   840
#endif
765
076f3bd42329 Moved profileInitProgram to profileFinalInitProgram.
Ryan C. Gordon <icculus@icculus.org>
parents: 764
diff changeset
   841
} // impl_GLSL_FinalInitProgram
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   842
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   843
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   844
static void impl_GLSL_UseProgram(MOJOSHADER_glProgram *program)
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   845
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   846
    if (ctx->have_opengl_2)
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   847
        ctx->glUseProgram(program ? program->handle : 0);
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   848
    else
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   849
        ctx->glUseProgramObjectARB((GLhandleARB) (program ? program->handle : 0));
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   850
} // impl_GLSL_UseProgram
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   851
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   852
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   853
static void impl_GLSL_PushConstantArray(MOJOSHADER_glProgram *program,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   854
                                        const MOJOSHADER_uniform *u,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   855
                                        const GLfloat *f)
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   856
{
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
   857
    const GLint loc = glsl_uniform_loc(program, u->name);
798
5dd67cc04cf9 Bunch of small tweaks to make this compile as C++ code without errors/warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 797
diff changeset
   858
    if (loc >= 0)   // not optimized out?
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   859
        ctx->glUniform4fv(loc, u->array_count, f);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   860
} // impl_GLSL_PushConstantArray
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   861
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   862
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   863
static void impl_GLSL_PushUniforms(void)
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   864
{
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   865
    const MOJOSHADER_glProgram *program = ctx->bound_program;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   866
768
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
   867
    assert(program->uniform_count > 0);  // don't call with nothing to do!
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
   868
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   869
    if (program->vs_float4_loc != -1)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   870
    {
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   871
        ctx->glUniform4fv(program->vs_float4_loc,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   872
                          program->vs_uniforms_float4_count,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   873
                          program->vs_uniforms_float4);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   874
    } // if
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   875
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   876
    if (program->vs_int4_loc != -1)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   877
    {
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   878
        ctx->glUniform4iv(program->vs_int4_loc,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   879
                          program->vs_uniforms_int4_count,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   880
                          program->vs_uniforms_int4);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   881
    } // if
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   882
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   883
    if (program->vs_bool_loc != -1)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   884
    {
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   885
        ctx->glUniform1iv(program->vs_bool_loc,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   886
                          program->vs_uniforms_bool_count,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   887
                          program->vs_uniforms_bool);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   888
    } // if
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   889
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   890
    if (program->ps_float4_loc != -1)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   891
    {
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   892
        ctx->glUniform4fv(program->ps_float4_loc,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   893
                          program->ps_uniforms_float4_count,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   894
                          program->ps_uniforms_float4);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   895
    } // if
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   896
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   897
    if (program->ps_int4_loc != -1)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   898
    {
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   899
        ctx->glUniform4iv(program->ps_int4_loc,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   900
                          program->ps_uniforms_int4_count,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   901
                          program->ps_uniforms_int4);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   902
    } // if
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   903
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   904
    if (program->ps_bool_loc != -1)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   905
    {
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   906
        ctx->glUniform1iv(program->ps_bool_loc,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   907
                          program->ps_uniforms_bool_count,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   908
                          program->ps_uniforms_bool);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   909
    } // if
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   910
} // impl_GLSL_PushUniforms
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   911
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   912
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   913
static void impl_GLSL_PushSampler(GLint loc, GLuint sampler)
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   914
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   915
    ctx->glUniform1i(loc, sampler);
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   916
} // impl_GLSL_PushSampler
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   917
1225
50b8dd7e0b1a Add GLSPIRV profile, to allow for both GL- and VK-friendly SPIR-V output
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1224
diff changeset
   918
#endif // SUPPORT_PROFILE_GLSL || SUPPORT_PROFILE_GLSPIRV
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   919
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   920
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   921
#if SUPPORT_PROFILE_ARB1
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   922
static inline GLenum arb1_shader_type(const MOJOSHADER_shaderType t)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   923
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   924
    if (t == MOJOSHADER_TYPE_VERTEX)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   925
        return GL_VERTEX_PROGRAM_ARB;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   926
    else if (t == MOJOSHADER_TYPE_PIXEL)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   927
        return GL_FRAGMENT_PROGRAM_ARB;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   928
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   929
    // !!! FIXME: geometry shaders?
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   930
    return GL_NONE;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   931
} // arb1_shader_type
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   932
760
6dc8d2cafc58 "Must Push" seemed more correct than "Must Load".
Ryan C. Gordon <icculus@icculus.org>
parents: 759
diff changeset
   933
static int impl_ARB1_MustPushConstantArrays(void) { return 0; }
6dc8d2cafc58 "Must Push" seemed more correct than "Must Load".
Ryan C. Gordon <icculus@icculus.org>
parents: 759
diff changeset
   934
static int impl_ARB1_MustPushSamplers(void) { return 0; }
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   935
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   936
static int impl_ARB1_MaxUniforms(MOJOSHADER_shaderType shader_type)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   937
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   938
    GLint retval = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   939
    const GLenum program_type = arb1_shader_type(shader_type);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   940
    if (program_type == GL_NONE)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   941
        return -1;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   942
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   943
    ctx->glGetProgramivARB(program_type, GL_MAX_PROGRAM_PARAMETERS_ARB, &retval);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   944
    return (int) retval;  // !!! FIXME: times four?
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   945
} // impl_ARB1_MaxUniforms
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   946
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   947
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   948
static int impl_ARB1_CompileShader(const MOJOSHADER_parseData *pd, GLuint *s)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   949
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   950
    GLint shaderlen = (GLint) pd->output_len;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   951
    const GLenum shader_type = arb1_shader_type(pd->shader_type);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   952
    GLuint shader = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   953
    ctx->glGenProgramsARB(1, &shader);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   954
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   955
    ctx->glGetError();  // flush any existing error state.
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   956
    ctx->glBindProgramARB(shader_type, shader);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   957
    ctx->glProgramStringARB(shader_type, GL_PROGRAM_FORMAT_ASCII_ARB,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   958
                                shaderlen, pd->output);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   959
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   960
    if (ctx->glGetError() == GL_INVALID_OPERATION)
1223
32ab7e4fbdc6 Various style/redundancy fixes found during SPIR-V work
Martin Krošlák <kroslakma@gmail.com>
parents: 1210
diff changeset
   961
    {
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   962
        GLint pos = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   963
        ctx->glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &pos);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   964
        const GLubyte *errstr = ctx->glGetString(GL_PROGRAM_ERROR_STRING_ARB);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   965
        snprintf(error_buffer, sizeof (error_buffer),
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   966
                  "ARB1 compile error at position %d: %s",
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   967
                  (int) pos, (const char *) errstr);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   968
        ctx->glBindProgramARB(shader_type, 0);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   969
        ctx->glDeleteProgramsARB(1, &shader);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   970
        *s = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   971
        return 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   972
    } // if
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   973
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   974
    *s = shader;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   975
    return 1;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   976
} // impl_ARB1_CompileShader
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   977
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   978
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   979
static void impl_ARB1_DeleteShader(const GLuint _shader)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   980
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   981
    GLuint shader = _shader;  // const removal.
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   982
    ctx->glDeleteProgramsARB(1, &shader);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   983
} // impl_ARB1_DeleteShader
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   984
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   985
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   986
static void impl_ARB1_DeleteProgram(const GLuint program)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   987
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   988
    // no-op. ARB1 doesn't have real linked programs.
1173
4b2f745c643b GLSL: Use varyings when usage_str is NULL for vertex/pixel shaders
Ethan Lee <flibitijibibo@flibitijibibo.com>
parents: 1156
diff changeset
   989
} // impl_ARB1_DeleteProgram
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   990
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   991
static GLint impl_ARB1_GetUniformLocation(MOJOSHADER_glProgram *program,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   992
                                          MOJOSHADER_glShader *shader, int idx)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   993
{
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
   994
    return 0;  // no-op, we push this as one big-ass array now.
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   995
} // impl_ARB1_GetUniformLocation
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   996
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   997
static GLint impl_ARB1_GetSamplerLocation(MOJOSHADER_glProgram *program,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   998
                                          MOJOSHADER_glShader *shader, int idx)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
   999
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1000
    return shader->parseData->samplers[idx].index;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1001
} // impl_ARB1_GetSamplerLocation
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1002
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1003
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1004
static GLint impl_ARB1_GetAttribLocation(MOJOSHADER_glProgram *program, int idx)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1005
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1006
    return idx;  // map to vertex arrays in the same order as the parseData.
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1007
} // impl_ARB1_GetAttribLocation
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1008
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1009
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1010
static GLuint impl_ARB1_LinkProgram(MOJOSHADER_glShader *vshader,
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1011
                                    MOJOSHADER_glShader *pshader)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1012
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1013
    // there is no formal linking in ARB1...just return a unique value.
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1014
    static GLuint retval = 1;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1015
    return retval++;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1016
} // impl_ARB1_LinkProgram
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1017
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1018
765
076f3bd42329 Moved profileInitProgram to profileFinalInitProgram.
Ryan C. Gordon <icculus@icculus.org>
parents: 764
diff changeset
  1019
static void impl_ARB1_FinalInitProgram(MOJOSHADER_glProgram *program)
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1020
{
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1021
    // no-op.
765
076f3bd42329 Moved profileInitProgram to profileFinalInitProgram.
Ryan C. Gordon <icculus@icculus.org>
parents: 764
diff changeset
  1022
} // impl_ARB1_FinalInitProgram
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1023
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1024
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
  1025
static void impl_ARB1_UseProgram(MOJOSHADER_glProgram *program)
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1026
{
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1027
    GLuint vhandle = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1028
    GLuint phandle = 0;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1029
    if (program != NULL)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1030
    {
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1031
        if (program->vertex != NULL)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1032
            vhandle = program->vertex->handle;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1033
        if (program->fragment != NULL)
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1034
            phandle = program->fragment->handle;
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1035
    } // if
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1036
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1037
    toggle_gl_state(GL_VERTEX_PROGRAM_ARB, vhandle != 0);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1038
    toggle_gl_state(GL_FRAGMENT_PROGRAM_ARB, phandle != 0);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1039
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1040
    ctx->glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vhandle);
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1041
    ctx->glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, phandle);
1072
3400f252c4e0 Fixed up calling GL2 vs ARB extension entry points.
Ryan C. Gordon <icculus@icculus.org>
parents: 1071
diff changeset
  1042
} // impl_ARB1_UseProgram
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1043
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1044
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1045
static void impl_ARB1_PushConstantArray(MOJOSHADER_glProgram *program,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1046
                                        const MOJOSHADER_uniform *u,
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1047
                                        const GLfloat *f)
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1048
{
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1049
    // no-op. Constant arrays are defined in source code for arb1.
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1050
} // impl_ARB1_PushConstantArray
411
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1051
01bc1352c047 Rearranged profile-specific functions in OpenGL glue.
Ryan C. Gordon <icculus@icculus.org>
parents: 410
diff changeset
  1052
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1053
static void impl_ARB1_PushUniforms(void)
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 424
diff changeset
  1054
{
768
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1055
    // vertex shader uniforms come first in program->uniforms array.
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1056
    MOJOSHADER_shaderType shader_type = MOJOSHADER_TYPE_VERTEX;
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1057
    GLenum arb_shader_type = arb1_shader_type(shader_type);
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1058
    const MOJOSHADER_glProgram *program = ctx->bound_program;
768
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1059
    const uint32 count = program->uniform_count;
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1060
    const GLfloat *srcf = program->vs_uniforms_float4;
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1061
    const GLint *srci = program->vs_uniforms_int4;
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1062
    const GLint *srcb = program->vs_uniforms_bool;
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1063
    GLint loc = 0;
1090
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
  1064
    GLint texbem_loc = 0;
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1065
    uint32 i;
431
0d0cbe10db02 First shot at nv4 profile.
Ryan C. Gordon <icculus@icculus.org>
parents: 424
diff changeset
  1066
768
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1067
    assert(count > 0);  // shouldn't call this with nothing to do!
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1068
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1069
    for (i = 0; i < count; i++)
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1070
    {
767
806ba18f23aa Cleanups and fixes in ARB1 uniform pushing.
Ryan C. Gordon <icculus@icculus.org>
parents: 766
diff changeset
  1071
        UniformMap *map = &program->uniforms[i];
770
b5e545408b02 Fixed incorrect variable name.
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
  1072
        const MOJOSHADER_shaderType uniform_shader_type = map->shader_type;
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1073
        const MOJOSHADER_uniform *u = map->uniform;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1074
        const MOJOSHADER_uniformType type = u->type;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1075
        const int size = u->array_count ? u->array_count : 1;
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1076
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1077
        assert(!u->constant);
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1078
768
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1079
        // Did we switch from vertex to pixel (to geometry, etc)?
770
b5e545408b02 Fixed incorrect variable name.
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
  1080
        if (shader_type != uniform_shader_type)
763
ef6489e7e263 Serious OpenGL glue surgery to push uniforms as one big array.
Ryan C. Gordon <icculus@icculus.org>
parents: 761
diff changeset
  1081
        {
1090
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
  1082
            if (shader_type == MOJOSHADER_TYPE_PIXEL)
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
  1083
                texbem_loc = loc;
636ffcd3f14a First shot at GLSL/ARB1 support for TEXBEM and TEXBEML opcodes.
Ryan C. Gordon <icculus@icculus.org>
parents: 1073
diff changeset
  1084
768
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1085
            // we start with vertex, move to pixel, then to geometry, etc.
9ba81386bd4d Optimize/cleanup in uniform array iteration work.
Ryan C. Gordon <icculus@icculus.org>
parents: 767
diff changeset
  1086
            //  The array should always be sorted as such.
770
b5e545408b02 Fixed incorrect variable name.
Ryan C. Go