From c095b420d13fceaaec089cca3f840f0b688d8cb0 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Mon, 22 Jul 2024 01:43:21 +0200 Subject: [PATCH] Move GL3 C code over to C file - put rest in extern "C" codeblock --- gfx/drivers/gl3.c | 189 ++++++++++++++++++ gfx/drivers_shader/shader_gl3.cpp | 312 +++++++----------------------- 2 files changed, 261 insertions(+), 240 deletions(-) diff --git a/gfx/drivers/gl3.c b/gfx/drivers/gl3.c index 888e8e7018..ffb353a9ba 100644 --- a/gfx/drivers/gl3.c +++ b/gfx/drivers/gl3.c @@ -94,6 +94,195 @@ static void gl3_set_viewport(gl3_t *gl, * GL3 COMMON */ +void gl3_framebuffer_copy( + GLuint fb_id, + GLuint quad_program, + GLuint quad_vbo, + GLint flat_ubo_vertex, + struct Size2D size, + GLuint image) +{ + glBindFramebuffer(GL_FRAMEBUFFER, fb_id); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, image); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glViewport(0, 0, size.width, size.height); + glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(quad_program); + if (flat_ubo_vertex >= 0) + { + static float mvp[16] = { + 2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 2.0f, 0.0f, + -1.0f,-1.0f, 0.0f, 1.0f + }; + glUniform4fv(flat_ubo_vertex, 4, mvp); + } + + /* Draw quad */ + glDisable(GL_CULL_FACE); + glDisable(GL_BLEND); + glDisable(GL_DEPTH_TEST); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, quad_vbo); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), + (void *)((uintptr_t)(0))); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), + (void *)((uintptr_t)(2 * sizeof(float)))); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + + glUseProgram(0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void gl3_framebuffer_copy_partial( + GLuint fb_id, + GLuint quad_program, + GLint flat_ubo_vertex, + struct Size2D size, + GLuint image, + float rx, float ry) +{ + GLuint vbo; + const float quad_data[16] = { + 0.0f, 0.0f, 0.0f, 0.0f, + 1.0f, 0.0f, rx, 0.0f, + 0.0f, 1.0f, 0.0f, ry, + 1.0f, 1.0f, rx, ry, + }; + + glBindFramebuffer(GL_FRAMEBUFFER, fb_id); + glActiveTexture(GL_TEXTURE2); + glBindTexture(GL_TEXTURE_2D, image); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glViewport(0, 0, size.width, size.height); + glClear(GL_COLOR_BUFFER_BIT); + + glUseProgram(quad_program); + if (flat_ubo_vertex >= 0) + { + static float mvp[16] = { + 2.0f, 0.0f, 0.0f, 0.0f, + 0.0f, 2.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 2.0f, 0.0f, + -1.0f,-1.0f, 0.0f, 1.0f + }; + glUniform4fv(flat_ubo_vertex, 4, mvp); + } + glDisable(GL_CULL_FACE); + glDisable(GL_BLEND); + glDisable(GL_DEPTH_TEST); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + + /* A bit crude, but heeeey. */ + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + + glBufferData(GL_ARRAY_BUFFER, sizeof(quad_data), quad_data, GL_STREAM_DRAW); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), + (void *)((uintptr_t)(0))); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), + (void *)((uintptr_t)(2 * sizeof(float)))); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glDeleteBuffers(1, &vbo); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glUseProgram(0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +GLuint gl3_compile_shader(GLenum stage, const char *source) +{ + GLint status; + GLuint shader = glCreateShader(stage); + const char *ptr = source; + + glShaderSource(shader, 1, &ptr, NULL); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + + if (!status) + { + GLint length; + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); + if (length > 0) + { + char *info_log = (char*)malloc(length); + + if (info_log) + { + glGetShaderInfoLog(shader, length, &length, info_log); + RARCH_ERR("[GLCore]: Failed to compile shader: %s\n", info_log); + free(info_log); + glDeleteShader(shader); + return 0; + } + } + } + + return shader; +} + +uint32_t gl3_get_cross_compiler_target_version(void) +{ + const char *version = (const char*)glGetString(GL_VERSION); + unsigned major = 0; + unsigned minor = 0; + +#ifdef HAVE_OPENGLES3 + if (!version || sscanf(version, "OpenGL ES %u.%u", &major, &minor) != 2) + return 300; + + if (major == 2 && minor == 0) + return 100; +#else + if (!version || sscanf(version, "%u.%u", &major, &minor) != 2) + return 150; + + if (major == 3) + { + switch (minor) + { + case 2: + return 150; + case 1: + return 140; + case 0: + return 130; + } + } + else if (major == 2) + { + switch (minor) + { + case 1: + return 120; + case 0: + return 110; + } + } +#endif + + return 100 * major + 10 * minor; +} + static void gl3_bind_scratch_vbo(gl3_t *gl, const void *data, size_t size) { if (!gl->scratch_vbos[gl->scratch_vbo_index]) diff --git a/gfx/drivers_shader/shader_gl3.cpp b/gfx/drivers_shader/shader_gl3.cpp index e4eca4f491..2038b66fe2 100644 --- a/gfx/drivers_shader/shader_gl3.cpp +++ b/gfx/drivers_shader/shader_gl3.cpp @@ -58,196 +58,81 @@ static void gl3_build_default_matrix(float *data) data[15] = 1.0f; } -static void gl3_framebuffer_copy( - GLuint fb_id, - GLuint quad_program, - GLuint quad_vbo, - GLint flat_ubo_vertex, - struct Size2D size, - GLuint image) -{ - glBindFramebuffer(GL_FRAMEBUFFER, fb_id); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, image); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glViewport(0, 0, size.width, size.height); - glClear(GL_COLOR_BUFFER_BIT); +extern "C" { - glUseProgram(quad_program); - if (flat_ubo_vertex >= 0) + void gl3_framebuffer_copy( + GLuint fb_id, + GLuint quad_program, + GLuint quad_vbo, + GLint flat_ubo_vertex, + struct Size2D size, + GLuint image); + + void gl3_framebuffer_copy_partial( + GLuint fb_id, + GLuint quad_program, + GLint flat_ubo_vertex, + struct Size2D size, + GLuint image, + float rx, float ry); + + GLuint gl3_compile_shader(GLenum stage, const char *source); + uint32_t gl3_get_cross_compiler_target_version(void); + + static GLenum address_to_gl(glslang_filter_chain_address type) { - static float mvp[16] = { - 2.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 2.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 2.0f, 0.0f, - -1.0f,-1.0f, 0.0f, 1.0f - }; - glUniform4fv(flat_ubo_vertex, 4, mvp); - } - - /* Draw quad */ - glDisable(GL_CULL_FACE); - glDisable(GL_BLEND); - glDisable(GL_DEPTH_TEST); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - glBindBuffer(GL_ARRAY_BUFFER, quad_vbo); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), - (void *)((uintptr_t)(0))); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), - (void *)((uintptr_t)(2 * sizeof(float)))); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - - glUseProgram(0); - glBindTexture(GL_TEXTURE_2D, 0); - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} - -static void gl3_framebuffer_copy_partial( - GLuint fb_id, - GLuint quad_program, - GLint flat_ubo_vertex, - struct Size2D size, - GLuint image, - float rx, float ry) -{ - GLuint vbo; - const float quad_data[16] = { - 0.0f, 0.0f, 0.0f, 0.0f, - 1.0f, 0.0f, rx, 0.0f, - 0.0f, 1.0f, 0.0f, ry, - 1.0f, 1.0f, rx, ry, - }; - - glBindFramebuffer(GL_FRAMEBUFFER, fb_id); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, image); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glViewport(0, 0, size.width, size.height); - glClear(GL_COLOR_BUFFER_BIT); - - glUseProgram(quad_program); - if (flat_ubo_vertex >= 0) - { - static float mvp[16] = { - 2.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 2.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 2.0f, 0.0f, - -1.0f,-1.0f, 0.0f, 1.0f - }; - glUniform4fv(flat_ubo_vertex, 4, mvp); - } - glDisable(GL_CULL_FACE); - glDisable(GL_BLEND); - glDisable(GL_DEPTH_TEST); - glEnableVertexAttribArray(0); - glEnableVertexAttribArray(1); - - /* A bit crude, but heeeey. */ - glGenBuffers(1, &vbo); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - - glBufferData(GL_ARRAY_BUFFER, sizeof(quad_data), quad_data, GL_STREAM_DRAW); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), - (void *)((uintptr_t)(0))); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), - (void *)((uintptr_t)(2 * sizeof(float)))); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glDeleteBuffers(1, &vbo); - glDisableVertexAttribArray(0); - glDisableVertexAttribArray(1); - glUseProgram(0); - glBindTexture(GL_TEXTURE_2D, 0); - glBindFramebuffer(GL_FRAMEBUFFER, 0); -} - -static GLuint gl3_compile_shader(GLenum stage, const char *source) -{ - GLint status; - GLuint shader = glCreateShader(stage); - const char *ptr = source; - - glShaderSource(shader, 1, &ptr, NULL); - glCompileShader(shader); - - glGetShaderiv(shader, GL_COMPILE_STATUS, &status); - - if (!status) - { - GLint length; - glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); - if (length > 0) + switch (type) { - char *info_log = (char*)malloc(length); - - if (info_log) - { - glGetShaderInfoLog(shader, length, &length, info_log); - RARCH_ERR("[GLCore]: Failed to compile shader: %s\n", info_log); - free(info_log); - glDeleteShader(shader); - return 0; - } - } - } - - return shader; -} - -static uint32_t gl3_get_cross_compiler_target_version(void) -{ - const char *version = (const char*)glGetString(GL_VERSION); - unsigned major = 0; - unsigned minor = 0; - #ifdef HAVE_OPENGLES3 - if (!version || sscanf(version, "OpenGL ES %u.%u", &major, &minor) != 2) - return 300; - - if (major == 2 && minor == 0) - return 100; -#else - if (!version || sscanf(version, "%u.%u", &major, &minor) != 2) - return 150; - - if (major == 3) - { - switch (minor) - { - case 2: - return 150; - case 1: - return 140; - case 0: - return 130; - } - } - else if (major == 2) - { - switch (minor) - { - case 1: - return 120; - case 0: - return 110; - } - } + case GLSLANG_FILTER_CHAIN_ADDRESS_CLAMP_TO_BORDER: +#if 0 + RARCH_WARN("[GLCore]: No CLAMP_TO_BORDER in GLES3. Falling back to edge clamp.\n"); #endif + return GL_CLAMP_TO_EDGE; +#else + case GLSLANG_FILTER_CHAIN_ADDRESS_CLAMP_TO_BORDER: + return GL_CLAMP_TO_BORDER; +#endif + case GLSLANG_FILTER_CHAIN_ADDRESS_REPEAT: + return GL_REPEAT; + case GLSLANG_FILTER_CHAIN_ADDRESS_MIRRORED_REPEAT: + return GL_MIRRORED_REPEAT; + case GLSLANG_FILTER_CHAIN_ADDRESS_CLAMP_TO_EDGE: + default: + break; + } - return 100 * major + 10 * minor; + return GL_CLAMP_TO_EDGE; + } + + static GLenum convert_filter_to_mag_gl(glslang_filter_chain_filter filter) + { + switch (filter) + { + case GLSLANG_FILTER_CHAIN_LINEAR: + return GL_LINEAR; + case GLSLANG_FILTER_CHAIN_NEAREST: + default: + break; + } + + return GL_NEAREST; + } + + static GLenum convert_filter_to_min_gl(glslang_filter_chain_filter filter, glslang_filter_chain_filter mipfilter) + { + if ( (filter == GLSLANG_FILTER_CHAIN_LINEAR) + && (mipfilter == GLSLANG_FILTER_CHAIN_LINEAR) + ) + return GL_LINEAR_MIPMAP_LINEAR; + else if (filter == GLSLANG_FILTER_CHAIN_LINEAR) + return GL_LINEAR_MIPMAP_NEAREST; + else if (mipfilter == GLSLANG_FILTER_CHAIN_LINEAR) + return GL_NEAREST_MIPMAP_LINEAR; + return GL_NEAREST_MIPMAP_NEAREST; + } } - GLuint gl3_cross_compile_program( const uint32_t *vertex, size_t vertex_size, const uint32_t *fragment, size_t fragment_size, @@ -493,59 +378,6 @@ struct Texture glslang_filter_chain_address address; }; -static GLenum address_to_gl(glslang_filter_chain_address type) -{ - switch (type) - { -#ifdef HAVE_OPENGLES3 - case GLSLANG_FILTER_CHAIN_ADDRESS_CLAMP_TO_BORDER: -#if 0 - RARCH_WARN("[GLCore]: No CLAMP_TO_BORDER in GLES3. Falling back to edge clamp.\n"); -#endif - return GL_CLAMP_TO_EDGE; -#else - case GLSLANG_FILTER_CHAIN_ADDRESS_CLAMP_TO_BORDER: - return GL_CLAMP_TO_BORDER; -#endif - case GLSLANG_FILTER_CHAIN_ADDRESS_REPEAT: - return GL_REPEAT; - case GLSLANG_FILTER_CHAIN_ADDRESS_MIRRORED_REPEAT: - return GL_MIRRORED_REPEAT; - case GLSLANG_FILTER_CHAIN_ADDRESS_CLAMP_TO_EDGE: - default: - break; - } - - return GL_CLAMP_TO_EDGE; -} - -static GLenum convert_filter_to_mag_gl(glslang_filter_chain_filter filter) -{ - switch (filter) - { - case GLSLANG_FILTER_CHAIN_LINEAR: - return GL_LINEAR; - case GLSLANG_FILTER_CHAIN_NEAREST: - default: - break; - } - - return GL_NEAREST; -} - -static GLenum convert_filter_to_min_gl(glslang_filter_chain_filter filter, glslang_filter_chain_filter mipfilter) -{ - if ( (filter == GLSLANG_FILTER_CHAIN_LINEAR) - && (mipfilter == GLSLANG_FILTER_CHAIN_LINEAR) - ) - return GL_LINEAR_MIPMAP_LINEAR; - else if (filter == GLSLANG_FILTER_CHAIN_LINEAR) - return GL_LINEAR_MIPMAP_NEAREST; - else if (mipfilter == GLSLANG_FILTER_CHAIN_LINEAR) - return GL_NEAREST_MIPMAP_LINEAR; - return GL_NEAREST_MIPMAP_NEAREST; -} - static GLenum convert_glslang_format(glslang_format fmt) { #undef FMT @@ -2619,11 +2451,11 @@ gl3_filter_chain_t *gl3_filter_chain_create_from_preset( { /* Allow duplicate #pragma parameter, but * only if they are exactly the same. */ - if (meta_param.desc != itr->desc || - meta_param.initial != itr->initial || - meta_param.minimum != itr->minimum || - meta_param.maximum != itr->maximum || - meta_param.step != itr->step) + if ( meta_param.desc != itr->desc + || meta_param.initial != itr->initial + || meta_param.minimum != itr->minimum + || meta_param.maximum != itr->maximum + || meta_param.step != itr->step) { RARCH_ERR("[GLCore]: Duplicate parameters found for \"%s\", but arguments do not match.\n", itr->id);