render3d: gvk_read_screen reads back into one buffer kept in the state

Every read of the screen made bytes(screen_w * screen_h * 4) and dropped it:
the user's manual play left 22,970,368 B unreachable (exactly 3024x1898x4)
at gpu_vk_draw.ludic:1673 - one per photograph (shots' ph_grab, through
gpu_read_screen_bytes). The read-back is now gvk_read_px, made once and made
again (the old one freed) only when the screen's size changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-29 12:09:11 +03:00
parent 28ad6f62ea
commit 87587b00b6
2 changed files with 11 additions and 1 deletions

View file

@ -166,6 +166,8 @@ export state Render3dState {
gvk_family: int = -1
gvk_mp: bytes = null # VkPhysicalDeviceMemoryProperties
gvk_device_name: string = ""
gvk_read_px: bytes = null # gvk_read_screen's read-back, gvk_read_cap bytes, made again only at a new size
gvk_read_cap: int = 0
gvk_renderer_name: string = "" # "<device> (Vulkan)", made once with the device
gvk_why: string = "" # why the device did not come up, for the fallback notice
gvk_max_aniso: int = 0x3F800000 # the device's anisotropy limit as float bits; 1.0 when it has none

View file

@ -1670,7 +1670,15 @@ function gvk_blit(render3d_st: mut Render3dState, w: int, h: int, mask: int) ->
@alloc_ok("asked for by the player or a tool, not by the frame")
function gvk_read_screen(render3d_st: mut Render3dState, w: int, h: int, out: pointer) -> void {
gvk_present(render3d_st)
let px = bytes(render3d_st.gvk_screen_w * render3d_st.gvk_screen_h * 4)
# one read-back buffer, kept and replaced only when the screen's size changes: a buffer the size of
# the screen was made on every read and dropped (23 MB a photograph at 3024x1898)
let need = render3d_st.gvk_screen_w * render3d_st.gvk_screen_h * 4
if render3d_st.gvk_read_px == null or render3d_st.gvk_read_cap != need {
if render3d_st.gvk_read_px != null { free(render3d_st.gvk_read_px) }
render3d_st.gvk_read_px = bytes(need)
render3d_st.gvk_read_cap = need
}
let px = render3d_st.gvk_read_px
if not gvk_tex_read(render3d_st, render3d_st.gvk_screen_color, GL_RGBA8, render3d_st.gvk_screen_w, render3d_st.gvk_screen_h, GL_RGBA, GL_UNSIGNED_BYTE, px) { return }
let dst: pointer = out
for y in 0 .. h {