From e506dbb1bb15418668f81f9cb5420f1afbf54f62 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Thu, 30 Jun 2022 19:53:32 -0400 Subject: [PATCH] GL Fixes --- media-layer/gles/CMakeLists.txt | 11 +- .../gles/src/compatibility-layer/buffer.cpp | 34 +++ .../gles/src/compatibility-layer/draw.c | 2 +- .../gles/src/compatibility-layer/matrix.c | 1 - .../src/compatibility-layer/passthrough.c | 10 +- .../gles/src/compatibility-layer/state.c | 2 +- media-layer/gles/src/passthrough.c | 204 ++++++++++++++++++ .../{compatibility-layer => }/passthrough.h | 2 +- media-layer/proxy/src/GLESv1_CM.c | 27 ++- 9 files changed, 272 insertions(+), 21 deletions(-) create mode 100644 media-layer/gles/src/compatibility-layer/buffer.cpp create mode 100644 media-layer/gles/src/passthrough.c rename media-layer/gles/src/{compatibility-layer => }/passthrough.h (87%) diff --git a/media-layer/gles/CMakeLists.txt b/media-layer/gles/CMakeLists.txt index 83eb6211d..b8741fe3f 100644 --- a/media-layer/gles/CMakeLists.txt +++ b/media-layer/gles/CMakeLists.txt @@ -1,7 +1,7 @@ project(media-layer-stubs) # Stubs Only Needed For ARM -if(MCPI_USE_GLES1_COMPATIBILITY_LAYER AND BUILD_NATIVE_COMPONENTS AND NOT MCPI_HEADLESS_MODE) +if(BUILD_NATIVE_COMPONENTS AND NOT MCPI_HEADLESS_MODE) # GLESv1_CM Compatibility Layer set(GLES1_LINK_MODE "SHARED") if(MCPI_USE_MEDIA_LAYER_PROXY) @@ -9,10 +9,15 @@ if(MCPI_USE_GLES1_COMPATIBILITY_LAYER AND BUILD_NATIVE_COMPONENTS AND NOT MCPI_H # (This is so it doesn't interfere with the Media Layer Proxy Server's libGLESv1_CM.so.1 symlink.) set(GLES1_LINK_MODE "OBJECT") endif() - add_library(GLESv1_CM "${GLES1_LINK_MODE}" src/compatibility-layer/state.c src/compatibility-layer/passthrough.c src/compatibility-layer/matrix.c src/compatibility-layer/draw.c) + if(MCPI_USE_GLES1_COMPATIBILITY_LAYER) + set(GLES_SRC src/compatibility-layer/state.c src/compatibility-layer/passthrough.c src/compatibility-layer/matrix.c src/compatibility-layer/draw.c src/compatibility-layer/buffer.cpp) + else() + set(GLES_SRC src/passthrough.c) + endif() + add_library(GLESv1_CM "${GLES1_LINK_MODE}" ${GLES_SRC}) target_link_libraries(GLESv1_CM PRIVATE glfw PUBLIC reborn-util PRIVATE dl PRIVATE m) # Install - if(NOT MCPI_USE_MEDIA_LAYER_PROXY) + if(GLES1_LINK_MODE STREQUAL "SHARED") install(TARGETS GLESv1_CM DESTINATION "${MCPI_LIB_DIR}") endif() else() diff --git a/media-layer/gles/src/compatibility-layer/buffer.cpp b/media-layer/gles/src/compatibility-layer/buffer.cpp new file mode 100644 index 000000000..dd47cb6cd --- /dev/null +++ b/media-layer/gles/src/compatibility-layer/buffer.cpp @@ -0,0 +1,34 @@ +#include + +#include + +#include "../passthrough.h" + +// Store Buffers +static std::unordered_map buffers_map; +// Get Buffer +GL_FUNC(glGenBuffers, void, (GLsizei n, GLuint *buffers)); +static GLuint get_real_buffer(GLuint fake_buffer) { + if (buffers_map.count(fake_buffer) > 0) { + return buffers_map[fake_buffer]; + } else { + GLuint new_buffer; + real_glGenBuffers()(1, &new_buffer); + buffers_map[fake_buffer] = new_buffer; + return get_real_buffer(fake_buffer); + } +} + +// Convert Fake Buffers To Real Buffers When Calling GL +GL_FUNC(glBindBuffer, void, (GLenum target, GLuint buffer)); +void glBindBuffer(GLenum target, GLuint buffer) { + real_glBindBuffer()(target, get_real_buffer(buffer)); +} +GL_FUNC(glDeleteBuffers, void, (GLsizei n, const GLuint *buffers)); +void glDeleteBuffers(GLsizei n, const GLuint *buffers) { + for (int i = 0; i < n; i++) { + if (buffers_map.count(buffers[i]) > 0) { + real_glDeleteBuffers()(1, &buffers_map[i]); + } + } +} diff --git a/media-layer/gles/src/compatibility-layer/draw.c b/media-layer/gles/src/compatibility-layer/draw.c index f0c21447a..fb4ca1a52 100644 --- a/media-layer/gles/src/compatibility-layer/draw.c +++ b/media-layer/gles/src/compatibility-layer/draw.c @@ -1,5 +1,5 @@ #include "state.h" -#include "passthrough.h" +#include "../passthrough.h" #include diff --git a/media-layer/gles/src/compatibility-layer/matrix.c b/media-layer/gles/src/compatibility-layer/matrix.c index 22610d7cf..33d43bae4 100644 --- a/media-layer/gles/src/compatibility-layer/matrix.c +++ b/media-layer/gles/src/compatibility-layer/matrix.c @@ -4,7 +4,6 @@ #include #include "state.h" -#include "passthrough.h" // Matrix Common static void matrix_copy(matrix_t *src, matrix_t *dst) { diff --git a/media-layer/gles/src/compatibility-layer/passthrough.c b/media-layer/gles/src/compatibility-layer/passthrough.c index 31a9a3403..e41898dff 100644 --- a/media-layer/gles/src/compatibility-layer/passthrough.c +++ b/media-layer/gles/src/compatibility-layer/passthrough.c @@ -1,6 +1,6 @@ #include -#include "passthrough.h" +#include "../passthrough.h" // Simple v1.1 -> v2.0 Passthrough Functions GL_FUNC(glLineWidth, void, (GLfloat width)); @@ -43,10 +43,6 @@ GL_FUNC(glDepthFunc, void, (GLenum func)); void glDepthFunc(GLenum func) { real_glDepthFunc()(func); } -GL_FUNC(glBindBuffer, void, (GLenum target, GLuint buffer)); -void glBindBuffer(GLenum target, GLuint buffer) { - real_glBindBuffer()(target, buffer); -} GL_FUNC(glClearColor, void, (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)); void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) { real_glClearColor()(red, green, blue, alpha); @@ -61,10 +57,6 @@ void glHint(GLenum target, GLenum mode) { real_glHint()(target, mode); } } -GL_FUNC(glDeleteBuffers, void, (GLsizei n, const GLuint *buffers)); -void glDeleteBuffers(GLsizei n, const GLuint *buffers) { - real_glDeleteBuffers()(n, buffers); -} GL_FUNC(glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)); void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { real_glColorMask()(red, green, blue, alpha); diff --git a/media-layer/gles/src/compatibility-layer/state.c b/media-layer/gles/src/compatibility-layer/state.c index 26caad08b..f84dc21d9 100644 --- a/media-layer/gles/src/compatibility-layer/state.c +++ b/media-layer/gles/src/compatibility-layer/state.c @@ -1,7 +1,7 @@ #include #include "state.h" -#include "passthrough.h" +#include "../passthrough.h" // GL State gl_state_t gl_state = { diff --git a/media-layer/gles/src/passthrough.c b/media-layer/gles/src/passthrough.c new file mode 100644 index 000000000..8a09d4918 --- /dev/null +++ b/media-layer/gles/src/passthrough.c @@ -0,0 +1,204 @@ +#include + +#include "passthrough.h" + +GL_FUNC(glFogfv, void, (GLenum pname, const GLfloat *params)); +void glFogfv(GLenum pname, const GLfloat *params) { + real_glFogfv()(pname, params); +} +GL_FUNC(glVertexPointer, void, (GLint size, GLenum type, GLsizei stride, const void *pointer)); +void glVertexPointer(GLint size, GLenum type, GLsizei stride, const void *pointer) { + real_glVertexPointer()(size, type, stride, pointer); +} +GL_FUNC(glLineWidth, void, (GLfloat width)); +void glLineWidth(GLfloat width) { + real_glLineWidth()(width); +} +GL_FUNC(glBlendFunc, void, (GLenum sfactor, GLenum dfactor)); +void glBlendFunc(GLenum sfactor, GLenum dfactor) { + real_glBlendFunc()(sfactor, dfactor); +} +GL_FUNC(glDrawArrays, void, (GLenum mode, GLint first, GLsizei count)); +void glDrawArrays(GLenum mode, GLint first, GLsizei count) { + real_glDrawArrays()(mode, first, count); +} +GL_FUNC(glColor4f, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)); +void glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { + real_glColor4f()(red, green, blue, alpha); +} +GL_FUNC(glClear, void, (GLbitfield mask)); +void glClear(GLbitfield mask) { + real_glClear()(mask); +} +GL_FUNC(glBufferData, void, (GLenum target, GLsizeiptr size, const void *data, GLenum usage)); +void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage) { + real_glBufferData()(target, size, data, usage); +} +GL_FUNC(glFogx, void, (GLenum pname, GLfixed param)); +void glFogx(GLenum pname, GLfixed param) { + real_glFogx()(pname, param); +} +GL_FUNC(glFogf, void, (GLenum pname, GLfloat param)); +void glFogf(GLenum pname, GLfloat param) { + real_glFogf()(pname, param); +} +GL_FUNC(glMatrixMode, void, (GLenum mode)); +void glMatrixMode(GLenum mode) { + real_glMatrixMode()(mode); +} +GL_FUNC(glColorPointer, void, (GLint size, GLenum type, GLsizei stride, const void *pointer)); +void glColorPointer(GLint size, GLenum type, GLsizei stride, const void *pointer) { + real_glColorPointer()(size, type, stride, pointer); +} +GL_FUNC(glScissor, void, (GLint x, GLint y, GLsizei width, GLsizei height)); +void glScissor(GLint x, GLint y, GLsizei width, GLsizei height) { + real_glScissor()(x, y, width, height); +} +GL_FUNC(glTexParameteri, void, (GLenum target, GLenum pname, GLint param)); +void glTexParameteri(GLenum target, GLenum pname, GLint param) { + real_glTexParameteri()(target, pname, param); +} +GL_FUNC(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels)); +void glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels) { + real_glTexImage2D()(target, level, internalformat, width, height, border, format, type, pixels); +} +GL_FUNC(glEnable, void, (GLenum cap)); +void glEnable(GLenum cap) { + real_glEnable()(cap); +} +GL_FUNC(glEnableClientState, void, (GLenum array)); +void glEnableClientState(GLenum array) { + real_glEnableClientState()(array); +} +GL_FUNC(glPolygonOffset, void, (GLfloat factor, GLfloat units)); +void glPolygonOffset(GLfloat factor, GLfloat units) { + real_glPolygonOffset()(factor, units); +} +GL_FUNC(glDisableClientState, void, (GLenum array)); +void glDisableClientState(GLenum array) { + real_glDisableClientState()(array); +} +GL_FUNC(glDepthRangef, void, (GLclampf near, GLclampf far)); +void glDepthRangef(GLclampf near, GLclampf far) { + real_glDepthRangef()(near, far); +} +GL_FUNC(glDepthFunc, void, (GLenum func)); +void glDepthFunc(GLenum func) { + real_glDepthFunc()(func); +} +GL_FUNC(glBindBuffer, void, (GLenum target, GLuint buffer)); +void glBindBuffer(GLenum target, GLuint buffer) { + real_glBindBuffer()(target, buffer); +} +GL_FUNC(glClearColor, void, (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha)); +void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) { + real_glClearColor()(red, green, blue, alpha); +} +GL_FUNC(glPopMatrix, void, ()); +void glPopMatrix() { + real_glPopMatrix()(); +} +GL_FUNC(glLoadIdentity, void, ()); +void glLoadIdentity() { + real_glLoadIdentity()(); +} +GL_FUNC(glScalef, void, (GLfloat x, GLfloat y, GLfloat z)); +void glScalef(GLfloat x, GLfloat y, GLfloat z) { + real_glScalef()(x, y, z); +} +GL_FUNC(glPushMatrix, void, ()); +void glPushMatrix() { + real_glPushMatrix()(); +} +GL_FUNC(glDepthMask, void, (GLboolean flag)); +void glDepthMask(GLboolean flag) { + real_glDepthMask()(flag); +} +GL_FUNC(glHint, void, (GLenum target, GLenum mode)); +void glHint(GLenum target, GLenum mode) { + real_glHint()(target, mode); +} +GL_FUNC(glMultMatrixf, void, (const GLfloat *m)); +void glMultMatrixf(const GLfloat *m) { + real_glMultMatrixf()(m); +} +GL_FUNC(glTexCoordPointer, void, (GLint size, GLenum type, GLsizei stride, const void *pointer)); +void glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const void *pointer) { + real_glTexCoordPointer()(size, type, stride, pointer); +} +GL_FUNC(glDeleteBuffers, void, (GLsizei n, const GLuint *buffers)); +void glDeleteBuffers(GLsizei n, const GLuint *buffers) { + real_glDeleteBuffers()(n, buffers); +} +GL_FUNC(glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)); +void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) { + real_glColorMask()(red, green, blue, alpha); +} +GL_FUNC(glTexSubImage2D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels)); +void glTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels) { + real_glTexSubImage2D()(target, level, xoffset, yoffset, width, height, format, type, pixels); +} +GL_FUNC(glGenTextures, void, (GLsizei n, GLuint *textures)); +void glGenTextures(GLsizei n, GLuint *textures) { + real_glGenTextures()(n, textures); +} +GL_FUNC(glDeleteTextures, void, (GLsizei n, const GLuint *textures)); +void glDeleteTextures(GLsizei n, const GLuint *textures) { + real_glDeleteTextures()(n, textures); +} +GL_FUNC(glAlphaFunc, void, (GLenum func, GLclampf ref)); +void glAlphaFunc(GLenum func, GLclampf ref) { + real_glAlphaFunc()(func, ref); +} +GL_FUNC(glGetFloatv, void, (GLenum pname, GLfloat *params)); +void glGetFloatv(GLenum pname, GLfloat *params) { + real_glGetFloatv()(pname, params); +} +GL_FUNC(glBindTexture, void, (GLenum target, GLuint texture)); +void glBindTexture(GLenum target, GLuint texture) { + real_glBindTexture()(target, texture); +} +GL_FUNC(glTranslatef, void, (GLfloat x, GLfloat y, GLfloat z)); +void glTranslatef(GLfloat x, GLfloat y, GLfloat z) { + real_glTranslatef()(x, y, z); +} +GL_FUNC(glShadeModel, void, (GLenum mode)); +void glShadeModel(GLenum mode) { + real_glShadeModel()(mode); +} +GL_FUNC(glOrthof, void, (GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far)); +void glOrthof(GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far) { + real_glOrthof()(left, right, bottom, top, near, far); +} +GL_FUNC(glDisable, void, (GLenum cap)); +void glDisable(GLenum cap) { + real_glDisable()(cap); +} +GL_FUNC(glCullFace, void, (GLenum mode)); +void glCullFace(GLenum mode) { + real_glCullFace()(mode); +} +GL_FUNC(glRotatef, void, (GLfloat angle, GLfloat x, GLfloat y, GLfloat z)); +void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { + real_glRotatef()(angle, x, y, z); +} +GL_FUNC(glViewport, void, (GLint x, GLint y, GLsizei width, GLsizei height)); +void glViewport(GLint x, GLint y, GLsizei width, GLsizei height) { + real_glViewport()(x, y, width, height); +} +GL_FUNC(glNormal3f, void, (GLfloat nx, GLfloat ny, GLfloat nz)); +void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz) { + real_glNormal3f()(nx, ny, nz); +} +GL_FUNC(glIsEnabled, GLboolean, (GLenum cap)); +GLboolean glIsEnabled(GLenum cap) { + return real_glIsEnabled()(cap); +} +GL_FUNC(glGetIntegerv, void, (GLenum pname, GLint *data)); +void glGetIntegerv(GLenum pname, GLint *data) { + real_glGetIntegerv()(pname, data); +} +GL_FUNC(glReadPixels, void, (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data)); +void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void *data) { + real_glReadPixels()(x, y, width, height, format, type, data); +} diff --git a/media-layer/gles/src/compatibility-layer/passthrough.h b/media-layer/gles/src/passthrough.h similarity index 87% rename from media-layer/gles/src/compatibility-layer/passthrough.h rename to media-layer/gles/src/passthrough.h index 78b732cb0..6089af132 100644 --- a/media-layer/gles/src/compatibility-layer/passthrough.h +++ b/media-layer/gles/src/passthrough.h @@ -7,7 +7,7 @@ #define GL_FUNC(name, return_type, args) \ typedef return_type (*real_##name##_t)args; \ \ - __attribute__((__unused__)) static real_##name##_t real_##name() { \ + static real_##name##_t real_##name() { \ static real_##name##_t func = NULL; \ if (!func) { \ func = (real_##name##_t) glfwGetProcAddress(#name); \ diff --git a/media-layer/proxy/src/GLESv1_CM.c b/media-layer/proxy/src/GLESv1_CM.c index d0a95136f..40fef92b0 100644 --- a/media-layer/proxy/src/GLESv1_CM.c +++ b/media-layer/proxy/src/GLESv1_CM.c @@ -38,17 +38,17 @@ CALL(11, glFogfv, void, (GLenum pname, const GLfloat *params)) { // 'pointer' Is Only Supported As An Integer, Not As An Actual Pointer #if defined(MEDIA_LAYER_PROXY_SERVER) #define CALL_GL_POINTER(unique_id, name) \ + static int is_set_##name = 0; \ CALL(unique_id, name, void, (GLint size, GLenum type, GLsizei stride, const void *pointer)) { \ /* Check */ \ - static int last_set = 0; \ static GLint last_size; \ static GLenum last_type; \ static GLsizei last_stride; \ static const void *last_pointer; \ - if (last_set && last_size == size && last_type == type && last_stride == stride && last_pointer == pointer) { \ + if (is_set_##name && last_size == size && last_type == type && last_stride == stride && last_pointer == pointer) { \ return; \ } else { \ - last_set = 1; \ + is_set_##name = 1; \ last_size = size; \ last_type = type; \ last_stride = stride; \ @@ -524,6 +524,8 @@ CALL(28, glPolygonOffset, void, (GLfloat factor, GLfloat units)) { #endif } +CALL_GL_POINTER(41, glTexCoordPointer) + #if defined(MEDIA_LAYER_PROXY_SERVER) void glDisableClientState(GLenum array) { // Set @@ -532,6 +534,23 @@ void glDisableClientState(GLenum array) { return; } else { *enabled = 0; + // Not needed when using compatibility layer +#ifndef MCPI_USE_GLES1_COMPATIBILITY_LAYER + switch (array) { + case GL_VERTEX_ARRAY: { + is_set_glVertexPointer = 0; + break; + } + case GL_COLOR_ARRAY: { + is_set_glColorPointer = 0; + break; + } + case GL_TEXTURE_COORD_ARRAY: { + is_set_glTexCoordPointer = 0; + break; + } + } +#endif } } #endif @@ -720,8 +739,6 @@ CALL(40, glMultMatrixf, void, (const GLfloat *m)) { #endif } -CALL_GL_POINTER(41, glTexCoordPointer) - CALL(42, glDeleteBuffers, void, (GLsizei n, const GLuint *buffers)) { #if defined(MEDIA_LAYER_PROXY_SERVER) // Lock Proxy