Skip to content

perf(video): direct RGB565 upload, dual source textures, shader reuse, and GLES2 cleanup - #916

Open
pvaibhav wants to merge 6 commits into
LoveRetro:mainfrom
pvaibhav:upstream/perf-video-gles
Open

pvaibhav wants to merge 6 commits into
LoveRetro:mainfrom
pvaibhav:upstream/perf-video-gles

Conversation

@pvaibhav

Copy link
Copy Markdown

This PR streamlines the OpenGL ES video pipeline in minarch by avoiding redundant pixel conversions, texture uploads, shader reruns, and per-frame GL driver overhead. It makes gameplay frames finish faster and lets the CPU stay at lower clock speeds without changing image output.

On a Trimui Brick Pro running GBA with a shader, overlay, and rewind enabled, active frame work drops from 8.03 ms to 7.76 ms (p95 down from 10.06 ms to 9.42 ms) and average CPU frequency drops by 39 MHz (1506 → 1467 MHz).

Changes

  • Duplicate frame uploads and shader passes (workspace/all/common/generic_video.c, workspace/all/minarch/ma_video.c): When a core emitted a duplicate frame (common in 30 fps games and static screens), minarch still re-uploaded the source texture and re-ran every user shader pass. Tracking frame dirtiness and reusing valid FBO outputs skips those redundant uploads and passes whenever neither the source pixels nor frame-counter shaders changed.
  • RGB565 pixel conversion (generic_video.c, ma_video.c, api.h): RGB565 cores (GB/GBC/GBA/SNES/MD/PSX) were converted to 32-bit RGBA on the CPU every frame before uploading. Uploading RGB565 textures directly to GLES (including during fast-forward)—while retaining a packed RGB565 copy for duplicate frames/context resets and keeping the non-GL SDL path safe—eliminates that per-frame CPU conversion completely.
  • Single source texture contention (generic_video.c, PLAT_GL_Swap): Uploading into a texture the PowerVR GPU was still sampling for the previous frame forced the driver to allocate fresh backing storage every frame. Alternating between two source textures with per-slot dimension/format tracking and allocation error checks avoids that repeated reallocation.
  • Unversioned shader binary cache (generic_video.c, init_shader_program): Cached shader binaries were keyed only by filename and the source string was leaked after parsing. Hashing the shader source into the .shadercache key and freeing the buffer prevents stale binaries after shader edits and fixes the load-time leak.
  • Per-frame GLES2 queries, FBO churn, and context switches (generic_video.c, runShaderPass, PLAT_blitRenderer, PLAT_flip, PLAT_GL_Swap): Every frame looked up attribute/uniform locations by name, re-attached a shared FBO, ran glGetError/glIsTexture/glCheckFramebufferStatus checks, and called SDL_RenderClear() in PLAT_blitRenderer() (triggering an SDL GL context switch on every GL frame). Caching locations at link time, giving each shader pass a persistent FBO, checking completeness only on attach, moving SDL_RenderClear() to the non-GL PLAT_flip() path, and clearing the GL backbuffer once per swap cuts ~0.45 ms/frame of CPU driver overhead while guaranteeing clean letterbox bars on all EGL surfaces.

Results

Measured on Trimui Brick Pro (tg5040, GBA gpsp with shader + overlay + rewind @ 16 ms):

Metric Before (upstream/main) After
Avg active frame work 8.032 ms 7.755 ms
p95 active frame work 10.061 ms 9.424 ms
Avg CPU frequency 1506 MHz 1467 MHz (-39 MHz)

Blast Radius

  • Affected areas: Shared OpenGL ES / SDL video renderer (generic_video.c), GFX_Renderer struct (api.h), and minarch frame callback (ma_video.c).
  • Platforms: All platforms using generic_video.c (tg5040, tg5050, h700, my355, desktop).
  • Risk: Low-to-moderate. Touches the core rendering path, so GL state management, duplicate-frame reuse, and fallback SDL blitting were specifically audited and tested.

Testing and Verification

  • Compiled minarch cleanly for both tg5040 and h700 toolchains with zero warnings.
  • Verified live on Trimui Brick Pro (tg5040) across GBA (gpsp) and PAL SNES (snes9x).
  • Edge cases caught and handled during testing and review:
    • Letterbox bars on discarding EGL surfaces: Moving SDL_RenderClear() out of PLAT_blitRenderer() could leave undefined letterbox pixels on EGL surfaces that discard backbuffers; added an explicit glClear(GL_COLOR_BUFFER_BIT) on FBO 0 every swap (which also acts as a TBDR fast-clear on PowerVR/Mali) and kept SDL_RenderClear() on the non-GL PLAT_flip() path.
    • Duplicate frames and context resets under RGB565: Retained a packed RGB565 copy in ma_video.c and validated per-slot glTexImage2D allocations so duplicate callbacks, fast-forward drops, or GL resets never display stale or uninitialized textures.
    • Non-GL SDL presentation: Converted RGB565 via SDL_LockTexture / SDL_ConvertPixels in PLAT_flip() so source format changes remain safe when the SDL path is used.

The source texture already holds the pixels for a duplicate frame. Keep
uploading when the debug HUD or fade-in modifies those pixels.
Only the debug HUD, fade-in and ambient LEDs need the RGBA frame on the
CPU. When none of them is active, upload the core's RGB565 buffer
directly. Draw duplicates from the texture because there is no CPU copy.
Uploading to a texture that the current frame still samples makes the
PowerVR driver allocate storage on every frame. Upload to the texture
that the previous frame did not use.
A duplicate frame ran every user shader pass again on the same source
texture. Reuse the output in the FBO textures and run only the final
draw to the screen.
…, and hash shader cache keys

Keep packed RGB565 frames across duplicate callbacks and context resets, track dual source texture storage per slot with GL allocation error checks, keep SDL presentation safe across source changes, include source FNV-1a hash in shader cache keys, and allow direct RGB565 upload during fast-forward.
…L swap

Cache MVPMatrix, VertexCoord, and TexCoord locations at shader link time, keep one persistent FBO per shader pass instead of re-attaching every frame, and drop per-frame glGetError/glIsTexture/glCheckFramebufferStatus queries (checking completeness on attach). Move SDL_RenderClear() out of PLAT_blitRenderer() into the non-GL PLAT_flip() blit path to avoid per-frame SDL GL context switches, and clear FBO 0 inside PLAT_GL_Swap() before the final pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant