Skip to content

Commit

Permalink
Generate compatible GLSL headers at runtime depending on the hw rende…
Browse files Browse the repository at this point in the history
…rer type and version
  • Loading branch information
schellingb committed Jul 22, 2024
1 parent 313276d commit e9ffd5c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 56 deletions.
33 changes: 26 additions & 7 deletions dosbox_pure_libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3611,23 +3611,21 @@ 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;"
"gl_Position = vec4(a_position, 0.0, 1.0);"
"}";

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" };
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 0 additions & 13 deletions include/dbp_opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
67 changes: 31 additions & 36 deletions src/hardware/voodoo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2921,35 +2921,32 @@ 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;"
"gl_Position = vec4(a_position, 1.0f);"
"}";

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 };
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -3702,15 +3697,15 @@ 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
"vec4 tt = vec4(0.0);");

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]);
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit e9ffd5c

Please sign in to comment.