2525 #include < glad.h>
2626#endif
2727
28+ #include < math.h>
29+
2830#include " imgui.h"
2931#include " imgui_impl_opengl3.h"
3032#include " emu.h"
3133#include " config.h"
34+ #include " gui.h"
3235#include " geargrafx.h"
3336
3437#define OGL_RENDERER_IMPORT
@@ -48,6 +51,7 @@ static int quad_uniform_color = -1;
4851static int quad_uniform_tex_scale = -1 ;
4952static int quad_uniform_viewport_size = -1 ;
5053static int quad_uniform_use_fragcoord = -1 ;
54+ static int quad_uniform_scanline_scale = -1 ;
5155
5256
5357
@@ -65,6 +69,40 @@ static void update_system_texture(void);
6569static void update_debug_textures (void );
6670static void update_savestates_texture (void );
6771static void render_scanlines (void );
72+ static int compute_output_scale (void );
73+
74+ static int compute_output_scale (void )
75+ {
76+ int scale = FRAME_BUFFER_SCALE_DEFAULT;
77+
78+ if (!config_video.scanlines || config_debug.debug )
79+ return scale;
80+
81+ ImGuiIO& io = ImGui::GetIO ();
82+ float fb_scale_x = io.DisplayFramebufferScale .x > 0 .0f ? io.DisplayFramebufferScale .x : 1 .0f ;
83+ float fb_scale_y = io.DisplayFramebufferScale .y > 0 .0f ? io.DisplayFramebufferScale .y : 1 .0f ;
84+ bool fractional_fb_scale = (fabsf (fb_scale_x - roundf (fb_scale_x)) > 0 .001f ) || (fabsf (fb_scale_y - roundf (fb_scale_y)) > 0 .001f );
85+
86+ if (!fractional_fb_scale)
87+ return scale;
88+
89+ if (config_video.scale == 1 )
90+ {
91+ scale = config_video.scale_manual ;
92+ }
93+ else if ((config_video.scale == 0 ) && (current_runtime.screen_height > 0 ) && (gui_main_window_height > 0 ))
94+ {
95+ int physical_height = (int )roundf ((float )gui_main_window_height * fb_scale_y);
96+ scale = physical_height / current_runtime.screen_height ;
97+ }
98+
99+ if (scale < 1 )
100+ scale = 1 ;
101+ if (scale > FRAME_BUFFER_SCALE)
102+ scale = FRAME_BUFFER_SCALE;
103+
104+ return scale;
105+ }
68106
69107bool ogl_renderer_init (void )
70108{
@@ -134,6 +172,7 @@ void ogl_renderer_begin_render(void)
134172void ogl_renderer_render (void )
135173{
136174 emu_get_runtime (current_runtime);
175+ ogl_renderer_output_scale = compute_output_scale ();
137176
138177 if (config_debug.debug )
139178 {
@@ -302,7 +341,7 @@ static void render_emu_mix(void)
302341 update_system_texture ();
303342
304343 int viewportWidth = current_runtime.screen_width ;
305- int viewportHeight = current_runtime.screen_height * FRAME_BUFFER_SCALE ;
344+ int viewportHeight = current_runtime.screen_height * ogl_renderer_output_scale ;
306345
307346 glUseProgram (quad_shader_program);
308347 glUniform2f (quad_uniform_tex_scale, tex_h, tex_v);
@@ -403,7 +442,7 @@ static void update_emu_texture(void)
403442static void render_quad (float tex_h, float tex_v)
404443{
405444 int viewportWidth = current_runtime.screen_width ;
406- int viewportHeight = current_runtime.screen_height * FRAME_BUFFER_SCALE ;
445+ int viewportHeight = current_runtime.screen_height * ogl_renderer_output_scale ;
407446
408447 glUseProgram (quad_shader_program);
409448 glUniform2f (quad_uniform_tex_scale, tex_h, tex_v);
@@ -426,7 +465,7 @@ static void render_scanlines(void)
426465 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
427466
428467 int viewportWidth = current_runtime.screen_width ;
429- int viewportHeight = current_runtime.screen_height * FRAME_BUFFER_SCALE ;
468+ int viewportHeight = current_runtime.screen_height * ogl_renderer_output_scale ;
430469
431470 float tex_h = (float )current_runtime.screen_width ;
432471 float tex_v = (float )current_runtime.screen_height ;
@@ -435,6 +474,7 @@ static void render_scanlines(void)
435474 glUniform2f (quad_uniform_tex_scale, tex_h, tex_v);
436475 glUniform2f (quad_uniform_viewport_size, (float )viewportWidth, (float )viewportHeight);
437476 glUniform1i (quad_uniform_use_fragcoord, 2 );
477+ glUniform1i (quad_uniform_scanline_scale, ogl_renderer_output_scale);
438478 glUniform4f (quad_uniform_color, 1 .0f , 1 .0f , 1 .0f , config_video.scanlines_intensity );
439479
440480 glViewport (0 , 0 , viewportWidth, viewportHeight);
@@ -475,10 +515,11 @@ static void init_shaders(void)
475515 " uniform vec2 uTexScale;\n "
476516 " uniform vec2 uViewportSize;\n "
477517 " uniform int uUseFragCoord;\n "
518+ " uniform int uScanlineScale;\n "
478519 " void main() {\n "
479520 " if (uUseFragCoord == 2) {\n "
480- " float row = mod(floor(gl_FragCoord.y), 4.0 );\n "
481- " float mask = row >= 2.0 ? 1.0 : 0.0;\n "
521+ " float row = mod(floor(gl_FragCoord.y), float(uScanlineScale) );\n "
522+ " float mask = row >= (float(uScanlineScale) * 0.5) ? 1.0 : 0.0;\n "
482523 " FragColor = vec4(0.0, 0.0, 0.0, uColor.a * mask);\n "
483524 " return;\n "
484525 " }\n "
@@ -538,10 +579,12 @@ static void init_shaders(void)
538579 quad_uniform_color = glGetUniformLocation (quad_shader_program, " uColor" );
539580 quad_uniform_viewport_size = glGetUniformLocation (quad_shader_program, " uViewportSize" );
540581 quad_uniform_use_fragcoord = glGetUniformLocation (quad_shader_program, " uUseFragCoord" );
582+ quad_uniform_scanline_scale = glGetUniformLocation (quad_shader_program, " uScanlineScale" );
541583
542584 glUseProgram (quad_shader_program);
543585 glUniform1i (quad_uniform_texture, 0 );
544586 glUniform1i (quad_uniform_use_fragcoord, 0 );
587+ glUniform1i (quad_uniform_scanline_scale, FRAME_BUFFER_SCALE_DEFAULT);
545588 glUseProgram (0 );
546589
547590 float quad_vertices[] = {
0 commit comments