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