Prevent type conflicts between Khronos OpenGL headers and PortableGL headers

This commit is contained in:
刘皓 2025-03-15 01:29:12 -04:00
parent 59b4cce600
commit 96e44e285f
No known key found for this signature in database
GPG key ID: 7901753DB465B711
28 changed files with 263 additions and 63 deletions

View file

@ -517,8 +517,8 @@ if is_libretro
cmake.subproject('libsndfile', options: libsndfile_options).dependency('sndfile'),
cmake.subproject('pixman-region', options: pixman_region_options).dependency('pixman-region'),
subproject('stb').get_variable('stb'),
subproject('opengl-registry').get_variable('opengl_registry'),
subproject('portablegl').get_variable('portablegl'),
subproject('opengl-registry').get_variable('opengl_registry'),
subproject('freetype', default_options: freetype_options).get_variable('freetype_dep'),
declare_dependency(
link_with: static_library(

View file

@ -1,6 +1,5 @@
#include <portablegl.h>
#include <stdbool.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,6 +1,5 @@
#include <portablegl.h>
#include <stdbool.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,6 +1,5 @@
#include <portablegl.h>
#include <stdbool.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,4 @@
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#ifdef __cplusplus
extern "C" {

View file

@ -20,7 +20,6 @@
*/
#include <portablegl.h>
#define MKXPZ_NO_GL_TYPES
#include <GLES3/gl3.h>
#include <unordered_map>

View file

@ -3,4 +3,4 @@ url = https://github.com/KhronosGroup/OpenGL-Registry
revision = b53ca669bea4715b6d5fa53c459f47a1fecd7944
depth = 1
patch_directory = opengl-registry
diff_files = opengl-registry-types.patch
diff_files = opengl-registry-portablegl.patch

View file

@ -0,0 +1,67 @@
# Makes the Khronos OpenGL headers use the OpenGL types defined by PortableGL to avoid type conflicts when both the Khronos OpenGL headers and the PortableGL headers are being used.
--- a/api/GLES3/gl3.h
+++ b/api/GLES3/gl3.h
@@ -1,6 +1,6 @@
#ifndef __gles2_gl3_h_
#define __gles2_gl3_h_ 1
-
+#include <portablegl.h>
#ifdef __cplusplus
extern "C" {
#endif
@@ -40,26 +40,26 @@ extern "C" {
#ifndef GL_ES_VERSION_2_0
#define GL_ES_VERSION_2_0 1
#include <KHR/khrplatform.h>
-typedef khronos_int8_t GLbyte;
-typedef khronos_float_t GLclampf;
-typedef khronos_int32_t GLfixed;
-typedef khronos_int16_t GLshort;
-typedef khronos_uint16_t GLushort;
-typedef void GLvoid;
+
+
+
+
+
+
typedef struct __GLsync *GLsync;
-typedef khronos_int64_t GLint64;
-typedef khronos_uint64_t GLuint64;
-typedef unsigned int GLenum;
-typedef unsigned int GLuint;
-typedef char GLchar;
-typedef khronos_float_t GLfloat;
-typedef khronos_ssize_t GLsizeiptr;
-typedef khronos_intptr_t GLintptr;
-typedef unsigned int GLbitfield;
-typedef int GLint;
-typedef unsigned char GLboolean;
-typedef int GLsizei;
-typedef khronos_uint8_t GLubyte;
+
+
+
+
+
+
+
+
+
+
+
+
+
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_COLOR_BUFFER_BIT 0x00004000
@@ -651,7 +651,7 @@ GL_APICALL void GL_APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei
#ifndef GL_ES_VERSION_3_0
#define GL_ES_VERSION_3_0 1
-typedef khronos_uint16_t GLhalf;
+
#define GL_READ_BUFFER 0x0C02
#define GL_UNPACK_ROW_LENGTH 0x0CF2
#define GL_UNPACK_SKIP_ROWS 0x0CF3

View file

@ -1,39 +0,0 @@
# Prevents OpenGL types from being defined in gl3.h if MKXPZ_NO_GL_TYPES is defined.
--- a/api/GLES3/gl3.h
+++ b/api/GLES3/gl3.h
@@ -40,13 +40,16 @@ extern "C" {
#ifndef GL_ES_VERSION_2_0
#define GL_ES_VERSION_2_0 1
#include <KHR/khrplatform.h>
+#ifndef MKXPZ_NO_GL_TYPES
typedef khronos_int8_t GLbyte;
typedef khronos_float_t GLclampf;
typedef khronos_int32_t GLfixed;
typedef khronos_int16_t GLshort;
typedef khronos_uint16_t GLushort;
typedef void GLvoid;
+#endif // MKXPZ_NO_GL_TYPES
typedef struct __GLsync *GLsync;
+#ifndef MKXPZ_NO_GL_TYPES
typedef khronos_int64_t GLint64;
typedef khronos_uint64_t GLuint64;
typedef unsigned int GLenum;
@@ -60,6 +63,7 @@ typedef int GLint;
typedef unsigned char GLboolean;
typedef int GLsizei;
typedef khronos_uint8_t GLubyte;
+#endif // MKXPZ_NO_GL_TYPES
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_COLOR_BUFFER_BIT 0x00004000
@@ -651,7 +655,9 @@ GL_APICALL void GL_APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei
#ifndef GL_ES_VERSION_3_0
#define GL_ES_VERSION_3_0 1
+#ifndef MKXPZ_NO_GL_TYPES
typedef khronos_uint16_t GLhalf;
+#endif // MKXPZ_NO_GL_TYPES
#define GL_READ_BUFFER 0x0C02
#define GL_UNPACK_ROW_LENGTH 0x0CF2
#define GL_UNPACK_SKIP_ROWS 0x0CF3

View file

@ -1,2 +1,2 @@
project('opengl-registry', 'c', meson_version: '>=1.3.0')
opengl_registry = declare_dependency(include_directories: 'api', dependencies: [subproject('egl-registry').get_variable('egl_registry')])
opengl_registry = declare_dependency(include_directories: 'api', dependencies: [subproject('egl-registry').get_variable('egl_registry'), subproject('portablegl').get_variable('portablegl')])

View file

@ -0,0 +1,174 @@
# Renames all the GL_MAX_* macros defined by PortableGL to PGL_MAX_* to avoid conflicts with the Khronos OpenGL headers.
--- a/portablegl.h
+++ b/portablegl.h
@@ -2450,7 +2450,7 @@ enum
#define GL_FALSE 0
#define GL_TRUE 1
-#define GL_STENCIL_BITS 8
+#define PGL_STENCIL_BITS 8
// Just GL_STENCIL_BITS of 1's, not an official GL enum/value
//#define PGL_STENCIL_MASK ((1 << GL_STENCIL_BITS)-1)
@@ -2460,10 +2460,10 @@ enum
// Feel free to change these
#define PGL_MAX_VERTICES 500000
-#define GL_MAX_VERTEX_ATTRIBS 8
-#define GL_MAX_VERTEX_OUTPUT_COMPONENTS (4*GL_MAX_VERTEX_ATTRIBS)
-#define GL_MAX_DRAW_BUFFERS 4
-#define GL_MAX_COLOR_ATTACHMENTS 4
+#define PGL_MAX_VERTEX_ATTRIBS 8
+#define PGL_MAX_VERTEX_OUTPUT_COMPONENTS (4*PGL_MAX_VERTEX_ATTRIBS)
+#define PGL_MAX_DRAW_BUFFERS 4
+#define PGL_MAX_COLOR_ATTACHMENTS 4
//TODO use prefix like GL_SMOOTH? PGL_SMOOTH?
enum { PGL_SMOOTH, PGL_FLAT, PGL_NOPERSPECTIVE };
@@ -2522,7 +2522,7 @@ typedef struct glProgram
frag_func fragment_shader;
void* uniform;
GLsizei vs_output_size;
- GLenum interpolation[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ GLenum interpolation[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
GLboolean fragdepth_or_discard;
@@ -2571,7 +2571,7 @@ void init_glVertex_Attrib(glVertex_Attrib* v);
typedef struct glVertex_Array
{
- glVertex_Attrib vertex_attribs[GL_MAX_VERTEX_ATTRIBS];
+ glVertex_Attrib vertex_attribs[PGL_MAX_VERTEX_ATTRIBS];
//GLuint n_array_bufs;
GLuint element_buffer;
@@ -2893,10 +2893,10 @@ typedef struct glContext
// TODO make some or all of these locals, measure performance
// impact. Would be necessary in the long term if I ever
// parallelize more
- vec4 vertex_attribs_vs[GL_MAX_VERTEX_ATTRIBS];
+ vec4 vertex_attribs_vs[PGL_MAX_VERTEX_ATTRIBS];
Shader_Builtins builtins;
Vertex_Shader_output vs_output;
- float fs_input[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float fs_input[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
GLboolean depth_test;
GLboolean line_smooth;
@@ -3032,7 +3032,7 @@ typedef struct pgl_prog_info
vert_func vs;
frag_func fs;
int vs_out_sz;
- GLenum interp[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ GLenum interp[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
GLboolean uses_fragdepth_or_discard;
} pgl_prog_info;
@@ -5924,8 +5924,8 @@ static void vertex_stage(const GLvoid* indices, GLsizei count, GLsizei instance_
//also initialize the vertex_attrib space
// TODO does creating enabled array actually help perf? At what number
// of GL_MAX_VERTEX_ATTRIBS and vertices does it become a benefit?
- int enabled[GL_MAX_VERTEX_ATTRIBS] = { 0 };
- for (i=0, j=0; i<GL_MAX_VERTEX_ATTRIBS; ++i) {
+ int enabled[PGL_MAX_VERTEX_ATTRIBS] = { 0 };
+ for (i=0, j=0; i<PGL_MAX_VERTEX_ATTRIBS; ++i) {
if (v[i].enabled) {
if (v[i].divisor == 0) {
enabled[j++] = i;
@@ -5979,7 +5979,7 @@ static void vertex_stage(const GLvoid* indices, GLsizei count, GLsizei instance_
//TODO make fs_input static? or a member of glContext?
static void draw_point(glVertex* vert, float poly_offset)
{
- float fs_input[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float fs_input[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
vec3 point = vec4_to_vec3h(vert->screen_space);
point.z += poly_offset; // couldn't this put it outside of [-1,1]?
@@ -6203,8 +6203,8 @@ static void draw_line_clip(glVertex* v1, glVertex* v2)
p1 = v1->clip_space;
p2 = v2->clip_space;
- float v1_out[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
- float v2_out[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float v1_out[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float v2_out[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
vec3 hp1, hp2;
@@ -6901,8 +6901,8 @@ static void draw_triangle_clip(glVertex* v0, glVertex* v1, glVertex* v2, unsigne
float tt;
//quite a bit of stack if there's a lot of clipping ...
- float tmp1_out[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
- float tmp2_out[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float tmp1_out[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float tmp2_out[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
tmp1.vs_out = tmp1_out;
tmp2.vs_out = tmp2_out;
@@ -7136,14 +7136,14 @@ static void draw_triangle_fill(glVertex* v0, glVertex* v1, glVertex* v2, unsigne
Line l20 = make_Line(hp2.x, hp2.y, hp0.x, hp0.y);
float alpha, beta, gamma, tmp, tmp2, z;
- float fs_input[GL_MAX_VERTEX_OUTPUT_COMPONENTS];
- float perspective[GL_MAX_VERTEX_OUTPUT_COMPONENTS*3];
+ float fs_input[PGL_MAX_VERTEX_OUTPUT_COMPONENTS];
+ float perspective[PGL_MAX_VERTEX_OUTPUT_COMPONENTS*3];
float* vs_output = &c->vs_output.output_buf.a[0];
for (int i=0; i<c->vs_output.size; ++i) {
perspective[i] = v0->vs_out[i]/p0.w;
- perspective[GL_MAX_VERTEX_OUTPUT_COMPONENTS + i] = v1->vs_out[i]/p1.w;
- perspective[2*GL_MAX_VERTEX_OUTPUT_COMPONENTS + i] = v2->vs_out[i]/p2.w;
+ perspective[PGL_MAX_VERTEX_OUTPUT_COMPONENTS + i] = v1->vs_out[i]/p1.w;
+ perspective[2*PGL_MAX_VERTEX_OUTPUT_COMPONENTS + i] = v2->vs_out[i]/p2.w;
}
float inv_w0 = 1/p0.w; //is this worth it? faster than just dividing by w down below?
float inv_w1 = 1/p1.w;
@@ -7190,7 +7190,7 @@ static void draw_triangle_fill(glVertex* v0, glVertex* v1, glVertex* v2, unsigne
for (int i=0; i<c->vs_output.size; ++i) {
if (c->vs_output.interpolation[i] == PGL_SMOOTH) {
- tmp = alpha*perspective[i] + beta*perspective[GL_MAX_VERTEX_OUTPUT_COMPONENTS + i] + gamma*perspective[2*GL_MAX_VERTEX_OUTPUT_COMPONENTS + i];
+ tmp = alpha*perspective[i] + beta*perspective[PGL_MAX_VERTEX_OUTPUT_COMPONENTS + i] + gamma*perspective[2*PGL_MAX_VERTEX_OUTPUT_COMPONENTS + i];
fs_input[i] = tmp/tmp2;
@@ -7705,7 +7705,7 @@ void default_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms)
void init_glVertex_Array(glVertex_Array* v)
{
v->deleted = GL_FALSE;
- for (int i=0; i<GL_MAX_VERTEX_ATTRIBS; ++i)
+ for (int i=0; i<PGL_MAX_VERTEX_ATTRIBS; ++i)
init_glVertex_Attrib(&v->vertex_attribs[i]);
}
@@ -8937,7 +8937,7 @@ void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean norm
// but you can easily remove c->cur_vertex_array from the check
// below to enable client arrays for all VAOs and it will
// still work just fine
- if (index >= GL_MAX_VERTEX_ATTRIBS || size < 1 || size > 4 ||
+ if (index >= PGL_MAX_VERTEX_ATTRIBS || size < 1 || size > 4 ||
(c->cur_vertex_array && !c->bound_buffers[GL_ARRAY_BUFFER-GL_ARRAY_BUFFER] && pointer)) {
if (!c->error)
c->error = GL_INVALID_OPERATION;
@@ -8990,7 +8990,7 @@ void glDisableVertexAttribArray(GLuint index)
void glVertexAttribDivisor(GLuint index, GLuint divisor)
{
- if (index >= GL_MAX_VERTEX_ATTRIBS) {
+ if (index >= PGL_MAX_VERTEX_ATTRIBS) {
if (!c->error)
c->error = GL_INVALID_VALUE;
return;
@@ -9694,7 +9694,7 @@ GLuint pglCreateProgram(vert_func vertex_shader, frag_func fragment_shader, GLsi
return 0;
}
- if (n < 0 || n > GL_MAX_VERTEX_OUTPUT_COMPONENTS) {
+ if (n < 0 || n > PGL_MAX_VERTEX_OUTPUT_COMPONENTS) {
if (!c->error)
c->error = GL_INVALID_VALUE;
return 0;

View file

@ -0,0 +1,18 @@
# Changes some of PortableGL's types to more accurately match those of the real OpenGL API.
--- a/portablegl.h
+++ b/portablegl.h
@@ -2054,11 +2054,11 @@ typedef uint64_t GLuint64;
// the pgl macro wrapper)
typedef int32_t GLsizei;
-typedef int32_t GLenum;
+typedef uint32_t GLenum;
typedef int32_t GLbitfield;
typedef intptr_t GLintptr;
-typedef uintptr_t GLsizeiptr;
+typedef ssize_t GLsizeiptr;
typedef void GLvoid;
typedef float GLfloat;

View file

@ -3,4 +3,4 @@ url = https://github.com/rswinkle/PortableGL
revision = 0.98.0
depth = 1
patch_directory = portablegl
diff_files = portablegl-emscripten-symbol-conflict.patch, portablegl-texture.patch
diff_files = portablegl-defines.patch, portablegl-emscripten-symbol-conflict.patch, portablegl-texture.patch, portablegl-types.patch