Skip to content

Commit 325c8ed

Browse files
committed
Reduce redundant rendering work per frame
1 parent cc44008 commit 325c8ed

4 files changed

Lines changed: 84 additions & 80 deletions

File tree

platforms/shared/desktop/emu.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ void emu_load_state_file(const char* file_path)
552552

553553
void update_savestates_data(void)
554554
{
555+
emu_savestates_generation++;
556+
555557
if (emu_is_empty())
556558
return;
557559

platforms/shared/desktop/emu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum Directory_Location
4646
EXTERN u8* emu_frame_buffer;
4747
EXTERN GS_SaveState_Header emu_savestates[5];
4848
EXTERN GS_SaveState_Screenshot emu_savestates_screenshots[5];
49+
EXTERN u32 emu_savestates_generation;
4950
EXTERN u8* emu_debug_sprite_buffers[64];
5051
EXTERN u8* emu_debug_background_buffer;
5152
EXTERN u8* emu_debug_tile_buffer;

platforms/shared/desktop/ogl_renderer.cpp

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static uint32_t system_texture;
4141
static uint32_t frame_buffer_object;
4242
static GS_RuntimeInfo current_runtime;
4343
static OglRendererScreenGeometry screen_geometry;
44+
static int savestates_texture_slot = -1;
45+
static u32 savestates_texture_generation = 0;
4446

4547
static uint32_t quad_shader_program = 0;
4648
static uint32_t quad_vao = 0;
@@ -60,7 +62,6 @@ static void render_internal_shader_chain(void);
6062
static void render_external_shader_chain(void);
6163
static void render_internal_shader_chain_feedback(void);
6264
static void render_emu_normal(void);
63-
static void update_emu_texture(void);
6465
static void render_quad(uint32_t program, uint32_t texture, int viewport_width, int viewport_height, float tex_h, float tex_v, float red, float green, float blue, float alpha);
6566
static void render_quad_preset(int pass_index, uint32_t program, uint32_t texture, int input_width, int input_height, int viewport_width, int viewport_height);
6667
static void update_system_texture(void);
@@ -162,8 +163,6 @@ void ogl_renderer_render(void)
162163
else
163164
render_emu_normal();
164165

165-
update_emu_texture();
166-
167166
ImVec4 clear_color = ImVec4(config_video.background_color[0], config_video.background_color[1], config_video.background_color[2], 1.00f);
168167

169168
ImGuiIO& io = ImGui::GetIO();
@@ -321,6 +320,8 @@ static void init_ogl_debug(void)
321320
static void init_ogl_savestates(void)
322321
{
323322
create_texture_2d(&ogl_renderer_emu_savestates, SYSTEM_TEXTURE_WIDTH, SYSTEM_TEXTURE_HEIGHT, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, NULL, false);
323+
savestates_texture_slot = -1;
324+
savestates_texture_generation = 0;
324325
}
325326

326327
static void render_gui(void)
@@ -340,8 +341,7 @@ static bool should_use_internal_shader_chain(void)
340341

341342
static void render_internal_shader_chain(void)
342343
{
343-
bool has_preset = ogl_shader_chain_has_preset();
344-
bool filter_linear = has_preset ? ogl_shader_chain_get_preset_filter_linear() : false;
344+
bool filter_linear = ogl_shader_chain_get_preset_filter_linear();
345345

346346
OglShaderChainSourceTexture source_texture;
347347
source_texture.width = current_runtime.screen_width;
@@ -355,33 +355,7 @@ static void render_internal_shader_chain(void)
355355
return;
356356
}
357357

358-
if (has_preset)
359-
{
360-
render_external_shader_chain();
361-
return;
362-
}
363-
364-
OglShaderChainFramebufferTexture pass_texture;
365-
pass_texture.width = screen_geometry.physical_width;
366-
pass_texture.height = screen_geometry.physical_height;
367-
pass_texture.filter_linear = false;
368-
pass_texture.float_framebuffer = false;
369-
370-
if (!ogl_shader_chain_resize_pass_texture(&pass_texture))
371-
{
372-
render_emu_normal();
373-
return;
374-
}
375-
376-
glBindFramebuffer(GL_FRAMEBUFFER, ogl_shader_chain_get_pass_framebuffer());
377-
glDisable(GL_BLEND);
378-
379-
render_quad(quad_shader_program, ogl_shader_chain_get_source_texture(),
380-
ogl_shader_chain_get_pass_width(), ogl_shader_chain_get_pass_height(),
381-
1.0f, 1.0f,
382-
1.0f, 1.0f, 1.0f, 1.0f);
383-
384-
glBindFramebuffer(GL_FRAMEBUFFER, 0);
358+
render_external_shader_chain();
385359
}
386360

387361
static void render_external_shader_chain(void)
@@ -497,8 +471,6 @@ static void update_system_texture(void)
497471
glBindTexture(GL_TEXTURE_2D, system_texture);
498472
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, current_runtime.screen_width, current_runtime.screen_height,
499473
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) emu_frame_buffer);
500-
501-
configure_texture_2d(false);
502474
}
503475

504476
static void update_debug_textures(void)
@@ -531,6 +503,14 @@ static void update_debug_textures(void)
531503
static void update_savestates_texture(void)
532504
{
533505
int i = config_emulator.save_slot;
506+
if (i < 0 || i >= 5)
507+
return;
508+
509+
if ((savestates_texture_slot == i) && (savestates_texture_generation == emu_savestates_generation))
510+
return;
511+
512+
savestates_texture_slot = i;
513+
savestates_texture_generation = emu_savestates_generation;
534514

535515
if (IsValidPointer(emu_savestates_screenshots[i].data))
536516
{
@@ -541,13 +521,6 @@ static void update_savestates_texture(void)
541521
}
542522
}
543523

544-
static void update_emu_texture(void)
545-
{
546-
glBindTexture(GL_TEXTURE_2D, ogl_renderer_emu_texture);
547-
548-
configure_texture_2d(false);
549-
}
550-
551524
static void render_quad(uint32_t program, uint32_t texture, int viewport_width, int viewport_height, float tex_h, float tex_v, float red, float green, float blue, float alpha)
552525
{
553526
glActiveTexture(GL_TEXTURE0);

platforms/shared/desktop/ogl_shader_chain.cpp

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@ static uint32_t intermediate_textures[SHADER_PRESET_MAX_PASSES - 1];
2626
static uint32_t intermediate_fbos[SHADER_PRESET_MAX_PASSES - 1];
2727
static int intermediate_widths[SHADER_PRESET_MAX_PASSES - 1];
2828
static int intermediate_heights[SHADER_PRESET_MAX_PASSES - 1];
29+
static bool intermediate_filter_linears[SHADER_PRESET_MAX_PASSES - 1];
2930
static bool intermediate_float_framebuffers[SHADER_PRESET_MAX_PASSES - 1];
3031
static uint32_t pass_history_textures[SHADER_PRESET_MAX_PASSES][SHADER_PRESET_MAX_HISTORY_TEXTURES];
3132
static int pass_history_widths[SHADER_PRESET_MAX_PASSES];
3233
static int pass_history_heights[SHADER_PRESET_MAX_PASSES];
34+
static bool pass_history_filter_linears[SHADER_PRESET_MAX_PASSES];
3335
static int pass_history_counts[SHADER_PRESET_MAX_PASSES];
3436
static int pass_history_write_indices[SHADER_PRESET_MAX_PASSES];
3537
static bool pass_history_float_framebuffers[SHADER_PRESET_MAX_PASSES];
3638
static int source_width = 1;
3739
static int source_height = 1;
40+
static bool source_filter_linear = false;
3841
static int pass_width = 1;
3942
static int pass_height = 1;
43+
static bool pass_filter_linear = false;
44+
static bool feedback_filter_linear = false;
4045
static bool pass_float_framebuffer = false;
4146
static bool initialized = false;
4247
static const char* framebuffer_name = "shader-chain framebuffer";
@@ -98,8 +103,11 @@ bool ogl_shader_chain_init(const char* name)
98103
framebuffer_name = name ? name : "shader-chain framebuffer";
99104
source_width = 1;
100105
source_height = 1;
106+
source_filter_linear = false;
101107
pass_width = 1;
102108
pass_height = 1;
109+
pass_filter_linear = false;
110+
feedback_filter_linear = false;
103111
pass_float_framebuffer = false;
104112

105113
glGenTextures(1, &source_texture);
@@ -127,13 +135,15 @@ bool ogl_shader_chain_init(const char* name)
127135
intermediate_fbos[i] = 0;
128136
intermediate_widths[i] = 1;
129137
intermediate_heights[i] = 1;
138+
intermediate_filter_linears[i] = false;
130139
intermediate_float_framebuffers[i] = false;
131140
}
132141

133142
for (int i = 0; i < SHADER_PRESET_MAX_PASSES; i++)
134143
{
135144
pass_history_widths[i] = 1;
136145
pass_history_heights[i] = 1;
146+
pass_history_filter_linears[i] = false;
137147
pass_history_counts[i] = 0;
138148
pass_history_write_indices[i] = 0;
139149
pass_history_float_framebuffers[i] = false;
@@ -175,6 +185,7 @@ void ogl_shader_chain_destroy(void)
175185

176186
pass_history_widths[i] = 1;
177187
pass_history_heights[i] = 1;
188+
pass_history_filter_linears[i] = false;
178189
pass_history_counts[i] = 0;
179190
pass_history_write_indices[i] = 0;
180191
pass_history_float_framebuffers[i] = false;
@@ -197,8 +208,12 @@ void ogl_shader_chain_destroy(void)
197208
source_texture = 0;
198209
source_width = 1;
199210
source_height = 1;
211+
source_filter_linear = false;
200212
pass_width = 1;
201213
pass_height = 1;
214+
pass_filter_linear = false;
215+
feedback_filter_linear = false;
216+
pass_float_framebuffer = false;
202217
initialized = false;
203218
}
204219

@@ -225,11 +240,16 @@ bool ogl_shader_chain_update_source_texture(const OglShaderChainSourceTexture* t
225240
resize_texture_2d(source_texture, width, height, GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, NULL, texture->filter_linear);
226241
source_width = width;
227242
source_height = height;
243+
source_filter_linear = texture->filter_linear;
228244
}
229245

230246
glBindTexture(GL_TEXTURE_2D, source_texture);
231247
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
232-
configure_texture_2d(texture->filter_linear);
248+
if (source_filter_linear != texture->filter_linear)
249+
{
250+
configure_texture_2d(texture->filter_linear);
251+
source_filter_linear = texture->filter_linear;
252+
}
233253

234254
return true;
235255
}
@@ -249,10 +269,20 @@ bool ogl_shader_chain_resize_pass_texture(const OglShaderChainFramebufferTexture
249269

250270
if (pass_width == width && pass_height == height && pass_float_framebuffer == texture->float_framebuffer)
251271
{
252-
glBindTexture(GL_TEXTURE_2D, pass_texture);
253-
configure_texture_2d(texture->filter_linear);
254-
glBindTexture(GL_TEXTURE_2D, feedback_texture);
255-
configure_texture_2d(texture->filter_linear);
272+
if (pass_filter_linear != texture->filter_linear)
273+
{
274+
glBindTexture(GL_TEXTURE_2D, pass_texture);
275+
configure_texture_2d(texture->filter_linear);
276+
pass_filter_linear = texture->filter_linear;
277+
}
278+
279+
if (feedback_filter_linear != texture->filter_linear)
280+
{
281+
glBindTexture(GL_TEXTURE_2D, feedback_texture);
282+
configure_texture_2d(texture->filter_linear);
283+
feedback_filter_linear = texture->filter_linear;
284+
}
285+
256286
return true;
257287
}
258288

@@ -267,6 +297,8 @@ bool ogl_shader_chain_resize_pass_texture(const OglShaderChainFramebufferTexture
267297
{
268298
pass_width = width;
269299
pass_height = height;
300+
pass_filter_linear = texture->filter_linear;
301+
feedback_filter_linear = texture->filter_linear;
270302
pass_float_framebuffer = texture->float_framebuffer;
271303
clear_feedback_texture();
272304
}
@@ -292,8 +324,13 @@ bool ogl_shader_chain_resize_intermediate_texture(int index, const OglShaderChai
292324

293325
if (intermediate_widths[index] == width && intermediate_heights[index] == height && intermediate_float_framebuffers[index] == texture->float_framebuffer)
294326
{
295-
glBindTexture(GL_TEXTURE_2D, intermediate_textures[index]);
296-
configure_texture_2d(texture->filter_linear);
327+
if (intermediate_filter_linears[index] != texture->filter_linear)
328+
{
329+
glBindTexture(GL_TEXTURE_2D, intermediate_textures[index]);
330+
configure_texture_2d(texture->filter_linear);
331+
intermediate_filter_linears[index] = texture->filter_linear;
332+
}
333+
297334
return true;
298335
}
299336

@@ -302,6 +339,7 @@ bool ogl_shader_chain_resize_intermediate_texture(int index, const OglShaderChai
302339

303340
intermediate_widths[index] = width;
304341
intermediate_heights[index] = height;
342+
intermediate_filter_linears[index] = texture->filter_linear;
305343
intermediate_float_framebuffers[index] = texture->float_framebuffer;
306344
return true;
307345
}
@@ -534,27 +572,6 @@ void ogl_shader_chain_apply_preset_uniforms(int index, const OglShaderChainUnifo
534572
if (!state->program)
535573
return;
536574

537-
if (state->uniform_source >= 0)
538-
glUniform1i(state->uniform_source, 0);
539-
540-
if (state->uniform_original >= 0)
541-
glUniform1i(state->uniform_original, 1);
542-
543-
if (state->uniform_feedback >= 0)
544-
glUniform1i(state->uniform_feedback, 2);
545-
546-
for (int i = 0; i < SHADER_PRESET_MAX_HISTORY_TEXTURES; i++)
547-
{
548-
if (state->uniform_source_history[i] >= 0)
549-
glUniform1i(state->uniform_source_history[i], 3 + i);
550-
}
551-
552-
for (int i = 0; i < SHADER_PRESET_MAX_PASS_OUTPUT_TEXTURES; i++)
553-
{
554-
if (state->uniform_pass_output[i] >= 0)
555-
glUniform1i(state->uniform_pass_output[i], 3 + SHADER_PRESET_MAX_HISTORY_TEXTURES + i);
556-
}
557-
558575
if (state->uniform_source_size >= 0)
559576
glUniform4f(state->uniform_source_size, (float)uniforms->input_width, (float)uniforms->input_height, safe_inverse(uniforms->input_width), safe_inverse(uniforms->input_height));
560577

@@ -671,7 +688,6 @@ bool ogl_shader_chain_store_pass_history(int index, uint32_t texture, int width,
671688

672689
glBindTexture(GL_TEXTURE_2D, target_texture);
673690
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, pass_history_widths[index], pass_history_heights[index]);
674-
configure_texture_2d(filter_linear);
675691

676692
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
677693
glBindFramebuffer(GL_FRAMEBUFFER, 0);
@@ -896,6 +912,7 @@ static void release_intermediate_textures(int keep_count)
896912
intermediate_textures[i] = 0;
897913
intermediate_widths[i] = 1;
898914
intermediate_heights[i] = 1;
915+
intermediate_filter_linears[i] = false;
899916
intermediate_float_framebuffers[i] = false;
900917
}
901918
}
@@ -957,39 +974,50 @@ static bool resize_pass_history_textures(int index, int width, int height, bool
957974
height = 1;
958975

959976
bool same_size = pass_history_widths[index] == width && pass_history_heights[index] == height && pass_history_float_framebuffers[index] == float_framebuffer;
977+
bool same_filter = pass_history_filter_linears[index] == filter_linear;
960978
int internal_format = float_framebuffer ? GL_RGBA16F : GL_RGBA8;
961979
uint32_t type = float_framebuffer ? GL_FLOAT : GL_UNSIGNED_BYTE;
962-
bool created = false;
980+
bool needs_create = false;
981+
982+
for (int i = 0; i < SHADER_PRESET_MAX_HISTORY_TEXTURES; i++)
983+
{
984+
if (!pass_history_textures[index][i])
985+
needs_create = true;
986+
}
987+
988+
bool resize = !same_size || needs_create;
963989

964990
for (int i = 0; i < SHADER_PRESET_MAX_HISTORY_TEXTURES; i++)
965991
{
966992
if (!pass_history_textures[index][i])
967-
{
968993
glGenTextures(1, &pass_history_textures[index][i]);
969-
created = true;
970-
}
971994

972995
if (!pass_history_textures[index][i])
973996
return false;
974997

975-
if (same_size && !created)
998+
if (resize)
976999
{
977-
glBindTexture(GL_TEXTURE_2D, pass_history_textures[index][i]);
978-
configure_texture_2d(filter_linear);
1000+
resize_texture_2d(pass_history_textures[index][i], width, height, internal_format, GL_RGBA, type, NULL, filter_linear);
9791001
}
980-
else
1002+
else if (!same_filter)
9811003
{
982-
resize_texture_2d(pass_history_textures[index][i], width, height, internal_format, GL_RGBA, type, NULL, filter_linear);
1004+
glBindTexture(GL_TEXTURE_2D, pass_history_textures[index][i]);
1005+
configure_texture_2d(filter_linear);
9831006
}
9841007
}
9851008

986-
if (!same_size || created)
1009+
if (resize)
9871010
{
9881011
pass_history_widths[index] = width;
9891012
pass_history_heights[index] = height;
1013+
pass_history_filter_linears[index] = filter_linear;
9901014
pass_history_float_framebuffers[index] = float_framebuffer;
9911015
clear_pass_history_textures(index);
9921016
}
1017+
else if (!same_filter)
1018+
{
1019+
pass_history_filter_linears[index] = filter_linear;
1020+
}
9931021

9941022
return true;
9951023
}

0 commit comments

Comments
 (0)