From e9ffd5c6a65f1843460dab60e646e108ebc31caa Mon Sep 17 00:00:00 2001 From: schellingb <14200249+schellingb@users.noreply.github.com> Date: Tue, 23 Jul 2024 01:22:31 +0900 Subject: [PATCH] Generate compatible GLSL headers at runtime depending on the hw renderer type and version --- dosbox_pure_libretro.cpp | 33 +++++++++++++++----- include/dbp_opengl.h | 13 -------- src/hardware/voodoo.cpp | 67 +++++++++++++++++++--------------------- 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/dosbox_pure_libretro.cpp b/dosbox_pure_libretro.cpp index d22d793d..bd7fa44c 100644 --- a/dosbox_pure_libretro.cpp +++ b/dosbox_pure_libretro.cpp @@ -3611,10 +3611,9 @@ bool retro_load_game(const struct retro_game_info *info) //#4 //GFX_ShowMsg("[DBP:GL] GL Point Size Range: %f ~ %f", pointsizes[0], pointsizes[1]); static const char* vertex_shader_src = - DBP_OPENGL_HEADER - "attribute vec2 a_position;" - "attribute vec2 a_texcoord;" - "varying vec2 v_texcoord;" + "in vec2 a_position;" + "in vec2 a_texcoord;" + "out vec2 v_texcoord;" "void main()" "{" "v_texcoord = a_texcoord;" @@ -3622,12 +3621,11 @@ bool retro_load_game(const struct retro_game_info *info) //#4 "}"; static const char* fragment_shader_src = - DBP_OPENGL_HEADER "uniform sampler2D u_texture;" - "varying vec2 v_texcoord;" + "in vec2 v_texcoord;" "void main()" "{" - "gl_FragColor = texture2D(u_texture, v_texcoord).bgra;" + "fragColor = texture(u_texture, v_texcoord).bgra;" "}"; static const char* bind_attrs[] = { "a_position", "a_texcoord" }; @@ -4344,6 +4342,27 @@ static unsigned CreateShaderOfType(int type, int count, const char** shader_src) unsigned DBP_Build_GL_Program(int vertex_shader_srcs_count, const char** vertex_shader_srcs, int fragment_shader_srcs_count, const char** fragment_shader_srcs, int bind_attribs_count, const char** bind_attribs) { + const char *tmpvsrcs[2], *tmpfsrcs[2]; + if (vertex_shader_srcs_count == 1) { tmpvsrcs[0] = NULL; tmpvsrcs[1] = *vertex_shader_srcs; vertex_shader_srcs_count = 2; vertex_shader_srcs = tmpvsrcs; } + if (fragment_shader_srcs_count == 1) { tmpfsrcs[0] = NULL; tmpfsrcs[1] = *fragment_shader_srcs; fragment_shader_srcs_count = 2; fragment_shader_srcs = tmpfsrcs; } + DBP_ASSERT(vertex_shader_srcs[0] == NULL && fragment_shader_srcs[0] == NULL); // need slot for header + + if (dbp_hw_render.context_type == RETRO_HW_CONTEXT_OPENGLES2 || dbp_hw_render.context_type == RETRO_HW_CONTEXT_OPENGLES3 || dbp_hw_render.context_type == RETRO_HW_CONTEXT_OPENGLES_VERSION) + { + vertex_shader_srcs[0] = "#define in attribute\n#define out varying\nprecision highp float;"; + fragment_shader_srcs[0] = "#define in varying\n#define texture texture2D\n#define fragColor gl_FragColor\nprecision highp float;"; + } + else if (((dbp_hw_render.version_major << 16) | dbp_hw_render.version_minor) < 0x30001) + { + vertex_shader_srcs[0] = "#define in attribute\n#define out varying\n"; + fragment_shader_srcs[0] = "#define in varying\n#define texture texture2D\n#define fragColor gl_FragColor\n"; + } + else + { + vertex_shader_srcs[0] = "#version 140\n"; + fragment_shader_srcs[0] = "#version 140\nout vec4 fragColor;"; + } + unsigned vert = CreateShaderOfType(MYGL_VERTEX_SHADER, vertex_shader_srcs_count, vertex_shader_srcs); unsigned frag = CreateShaderOfType(MYGL_FRAGMENT_SHADER, fragment_shader_srcs_count, fragment_shader_srcs); unsigned prog = myglCreateProgram(); diff --git a/include/dbp_opengl.h b/include/dbp_opengl.h index 6e972893..7e8a8b4f 100644 --- a/include/dbp_opengl.h +++ b/include/dbp_opengl.h @@ -151,19 +151,6 @@ MYGL_FOR_EACH_PROC(MYGL_MAKEFUNCEXT) #define MYGL_MAKEFUNCPTR(REQUIRE, RET, NAME, ARGS) RET (RETRO_CALLCONV* mygl##NAME)ARGS; #define MYGL_MAKEPROCARRENTRY(REQUIRE, RET, NAME, ARGS) { (retro_proc_address_t&)mygl##NAME , "gl" #NAME, REQUIRE }, -#if defined(__APPLE__) || defined(__MACH__) -#include "TargetConditionals.h" -#if (!defined(TARGET_OS_IPHONE) || !TARGET_OS_IPHONE) && (!defined(TARGET_OS_SIMULATOR) || !TARGET_OS_SIMULATOR) -#define DBP_IS_MAC_OS_DESKTOP -#endif -#endif - -#if (defined(__ARM_NEON__) || defined(IOS) || defined(ANDROID) || defined(__aarch64__) || defined(_M_ARM64) || defined(__arm__) || defined(_M_ARM)) && !defined(DBP_IS_MAC_OS_DESKTOP) -#define DBP_OPENGL_HEADER "precision highp float;" -#else -#define DBP_OPENGL_HEADER "#version 110\n" -#endif - unsigned DBP_Build_GL_Program(int vertex_shader_srcs_count, const char** vertex_shader_srcs, int fragment_shader_srcs_count, const char** fragment_shader_srcs, int bind_attribs_count, const char** bind_attribs); bool voodoo_ogl_is_active(); diff --git a/src/hardware/voodoo.cpp b/src/hardware/voodoo.cpp index 5322131e..2d38d526 100644 --- a/src/hardware/voodoo.cpp +++ b/src/hardware/voodoo.cpp @@ -2921,10 +2921,9 @@ static bool vogl_active; static const char* ogl_display_bind_attrs[] = { "a_position", "a_texcoord" }; static const char* ogl_display_vertex_shader_src = - DBP_OPENGL_HEADER - "attribute vec3 a_position;" - "attribute vec2 a_texcoord;" - "varying vec2 v_texcoord;" + "in vec3 a_position;" + "in vec2 a_texcoord;" + "out vec2 v_texcoord;" "void main()" "{" "v_texcoord = a_texcoord;" @@ -2932,24 +2931,22 @@ static const char* ogl_display_vertex_shader_src = "}"; static const char* ogl_display_fragment_shader_src = - DBP_OPENGL_HEADER "uniform vec3 clut_exp, clut_fac;" "uniform sampler2D u_texture;" - "varying vec2 v_texcoord;" + "in vec2 v_texcoord;" "void main()" "{" - //"gl_FragColor = texture2D(u_texture, v_texcoord);" - "gl_FragColor = vec4(pow(texture2D(u_texture, v_texcoord).rgb, clut_exp) * clut_fac, 1.0);" + //"fragColor = texture(u_texture, v_texcoord);" + "fragColor = vec4(pow(texture(u_texture, v_texcoord).rgb, clut_exp) * clut_fac, 1.0);" "}"; static const char* ogl_drawdepth_fragment_shader_src = // encode 0.0 to 1.0 with 16 bits of accuracy - DBP_OPENGL_HEADER "uniform sampler2D u_texture;" - "varying vec2 v_texcoord;" + "in vec2 v_texcoord;" "void main()" "{" - "float d = texture2D(u_texture, v_texcoord).r * 65535.0, m = mod(d, 256.0);" - "gl_FragColor = vec4((d - m) * 0.000015318627450980392156862745098039, m * 0.003921568627450980392156862745098, 0.0, 0.0);" + "float d = texture(u_texture, v_texcoord).r * 65535.0, m = mod(d, 256.0);" + "fragColor = vec4((d - m) * 0.000015318627450980392156862745098039, m * 0.003921568627450980392156862745098, 0.0, 0.0);" "}"; enum ogl_readback_mode : UINT8 { OGL_READBACK_MODE_COLOR0, OGL_READBACK_MODE_COLOR1, OGL_READBACK_MODE_COLOR2, OGL_READBACK_MODE_DEPTH, _OGL_READBACK_MODE_COUNT }; @@ -3652,22 +3649,20 @@ void voodoo_ogl_mainthread() // called while emulation thread is sleeping ogl_program* prog = &vogl->programs.AddOne(); prog->eff = eff; - int vshadernum = 0, fshadernum = 0; + int vshadernum = 1, fshadernum = 1; const char *vshadersrcs[16], *fshadersrcs[72]; // more than max possible - - addshdr(v, DBP_OPENGL_HEADER); - addshdr(f, DBP_OPENGL_HEADER); + vshadersrcs[0] = fshadersrcs[0] = NULL; // leave space for header addshdr(v, - "attribute vec3 a_position;" nl - "attribute vec4 a_color;" nl - "attribute vec3 a_foglodblend;" nl - "attribute vec3 a_texcoord0;" nl - "attribute vec3 a_texcoord1;"); - condshdr(v, usevcolor, "varying vec4 v_color;"); - condshdr(v, uset[0], "varying vec3 v_texcoord0;"); - condshdr(v, uset[1], "varying vec3 v_texcoord1;"); - condshdr(v, usefoglodblend, "varying vec3 v_foglodblend;"); + "in vec3 a_position;" nl + "in vec4 a_color;" nl + "in vec3 a_foglodblend;" nl + "in vec3 a_texcoord0;" nl + "in vec3 a_texcoord1;"); + condshdr(v, usevcolor, "out vec4 v_color;"); + condshdr(v, uset[0], "out vec3 v_texcoord0;"); + condshdr(v, uset[1], "out vec3 v_texcoord1;"); + condshdr(v, usefoglodblend, "out vec3 v_foglodblend;"); addshdr(v, "uniform vec3 view;" nl "void main()" nl @@ -3687,10 +3682,10 @@ void voodoo_ogl_mainthread() // called while emulation thread is sleeping //--------------------------------------------------------------------------------------------- - condshdr(f, usevcolor, "varying vec4 v_color;"); - condshdr(f, uset[0], "varying vec3 v_texcoord0;"); - condshdr(f, uset[1], "varying vec3 v_texcoord1;"); - condshdr(f, usefoglodblend, "varying vec3 v_foglodblend;"); + condshdr(f, usevcolor, "in vec4 v_color;"); + condshdr(f, uset[0], "in vec3 v_texcoord0;"); + condshdr(f, uset[1], "in vec3 v_texcoord1;"); + condshdr(f, usefoglodblend, "in vec3 v_foglodblend;"); condshdr(f, uset[0], "uniform sampler2D tex0;"); condshdr(f, uset[1], "uniform sampler2D tex1;"); addshdr(f, @@ -3702,7 +3697,7 @@ void voodoo_ogl_mainthread() // called while emulation thread is sleeping "uniform vec4 fogcolor_alpharef;" nl "void main()" nl "{" nl - //"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); return;" nl // debug draw red + //"fragColor = vec4(1.0, 0.0, 0.0, 1.0); return;" nl // debug draw red "vec4 texel = vec4(1.0);" nl "vec4 clocal = vec4(1.0);" nl "vec4 cother = vec4(0.0);" nl @@ -3710,7 +3705,7 @@ void voodoo_ogl_mainthread() // called while emulation thread is sleeping if (uset[1]) { - addshdr(f, "clocal = texture2D(tex1, v_texcoord1.xy / v_texcoord1.z).bgra;"); + addshdr(f, "clocal = texture(tex1, v_texcoord1.xy / v_texcoord1.z).bgra;"); //addshdr(f, "clocal = vec4(v_texcoord1.x/v_texcoord1.z, v_texcoord1.y/v_texcoord1.z, 0.0f, 1.0f);"); //addshdr(f, "clocal = vec4(0.5f, 0.5f, 0.5f, 0.5f);"); Local::MakeTexShader(fshadernum, fshadersrcs, 1, eff.tex_mode[1]); @@ -3719,12 +3714,12 @@ void voodoo_ogl_mainthread() // called while emulation thread is sleeping if (uset[0]) { - addshdr(f, "clocal = texture2D(tex0, v_texcoord0.xy/v_texcoord0.z).bgra;"); + addshdr(f, "clocal = texture(tex0, v_texcoord0.xy/v_texcoord0.z).bgra;"); //addshdr(f, "clocal = vec4(v_texcoord0.x/v_texcoord0.z, v_texcoord0.y/v_texcoord0.z, 1.0f, 1.0f);"); //addshdr(f, "clocal = vec4(1.0f, 0.0f, 0.0f, 1.0f);"); Local::MakeTexShader(fshadernum, fshadersrcs, 0, eff.tex_mode[0]); addshdr(f, "texel = tt;"); - //addshdr(f, "gl_FragColor = texel; return;"); + //addshdr(f, "fragColor = texel; return;"); } // color path @@ -3867,9 +3862,9 @@ void voodoo_ogl_mainthread() // called while emulation thread is sleeping selectshdr(f, FOGMODE_FOG_MULT(FOGMODE), "tt.rgb = ff;", "tt.rgb += ff;"); } - //addshdr(f, "gl_FragColor = pow(tt, vec4(1.0/2.2));" nl // "}"); - //addshdr(f, "gl_FragColor = tt * 2.0;" nl "}"); - addshdr(f, "gl_FragColor = tt;" nl + //addshdr(f, "fragColor = pow(tt, vec4(1.0/2.2));" nl // "}"); + //addshdr(f, "fragColor = tt * 2.0;" nl "}"); + addshdr(f, "fragColor = tt;" nl "}"); DBP_ASSERT(vshadernum <= ARRAY_LENGTH(vshadersrcs));