Skip to content

perf: libobs-opengl: stop re-specifying the texture on every gs_texture_unmap #3191

Open

Description

@zackees

What

libobs-opengl/gl-texture2d.c.h:227 calls glTexImage2D from the PBO after glUnmapBuffer, which may reallocate texture storage every async frame; gs_texture_map (:196-199) maps with no invalidate, so it can wait on the previous upload.

Proposed change

glTexSubImage2D on unmap; glMapBufferRange(GL_MAP_INVALIDATE_BUFFER_BIT) (or orphan with glBufferData(NULL)) on map.

Acceptance criteria

  • Before/after numbers (frame time of update_async_textures with a 1080p60 and a 4K30 async source (apitrace/Nsight)) in the PR; nothing lands unmeasured.
  • RED then GREEN; Tier 3 parity stays bit-exact unless this issue says otherwise.

Found by a read-only code scout and not yet measured. Parent: #3176.

Activity

  1. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    Worker note: I skipped this for now. Every test/cmocka graphics test uses a fake device (for example test_texture_render.c), so a GL change here can't be checked in CI. Doing it properly needs a real-GL harness (EGL surfaceless on Mesa llvmpipe) that runs map, write, unmap and readback, plus a GPU-traced frame-time measurement. Whoever takes it should build that harness first.

  2. added
    enhancement
    P3
    size:M
    on Oct 31, 2026
  3. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    /investigate needs a real-GL harness (EGL surfaceless on Mesa llvmpipe) for the RED test plus a GPU-traced frame-time measurement; CI graphics tests use a fake device.

  4. added
    needs-investigation
    on Oct 31, 2026
  5. zackees commented on Nov 1, 2026

    @zackees
    MemberAuthor

    Claiming: perf: libobs-opengl: stop re-specifying the texture on every gs_texture_unmap

    On the /investigate note: the real-GL harness already exists in Tier 2. test_ffmpeg_mux_unwritable_path and test_encoder_error_stop start libobs with GRAPHICS_MODULE_PATH set to libobs-opengl and run on Mesa software GL under xvfb-run on Linux, skipping without a display. A texture test can use the same setup.

    Plan, one PR:

    1. RED: a new cmocka test in that style. It creates a GS_DYNAMIC BGRA texture (1920×1080 and 3840×2160) and, for several frames, runs gs_texture_map, writes a frame-specific pattern, runs gs_texture_unmap, then reads the texture back through a stage surface and requires the exact bytes. It also records the GL texture storage identity across unmaps: the texture name and GL_TEXTURE_IMMUTABLE_FORMAT/size via glGetTexLevelParameteriv, and counts glTexImage2D calls through a wrapped gl entry point if the loader allows it. Otherwise it checks the timing of a map/unmap loop. The readback check is the bit-exact guard; the RED failure is the per-unmap re-specification.
    2. GREEN: gs_texture_unmap uploads from the PBO with glTexSubImage2D (storage was already specified at creation). gs_texture_map orphans the PBO with glBufferData(NULL) before mapping, so it doesn't wait on the previous frame's upload.
    3. Numbers: the map/write/unmap loop time per frame at 1080p and 4K, before and after, on software GL in the test. I'll say plainly that llvmpipe is not a GPU. An apitrace/Nsight frame time on real hardware isn't available here.

    First PR within a day.

  6. github-actions commented on Nov 1, 2026

    @github-actions

    Claim bot: @zackees, this can't be claimed yet: it needs investigation before anyone can implement it. Comment Claiming: investigation to take the research.

  7. zackees commented on Nov 1, 2026

    @zackees
    MemberAuthor

    The map half, measured. Not landing it yet.

    Change tried: gs_texture_map maps with glMapBufferRange(GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT) instead of glMapBuffer(GL_WRITE_ONLY). This matches D3D11's D3D11_MAP_WRITE_DISCARD in gs_texture_map (d3d11-subsystem.cpp.hpp:2742), so neither backend keeps the old contents across a map.

    Result: test_gl_dynamic_texture passes, with exact bytes on every frame. Its median map + write + unmap times, under Mesa llvmpipe and xvfb, 3 runs each:

    1080p BGRA 4K BGRA
    master (after #3382) 4.08 / 3.99 / 4.22 ms 15.9 / 16.5 / 15.9 ms
    invalidate map 3.83 / 3.88 / 4.19 ms 15.5 / 16.8 / 16.7 ms

    That's no difference. llvmpipe completes each upload synchronously at unmap, so there is never a pending upload for the invalidate to skip. The win this half targets (not stalling on the previous frame's DMA) only exists on a real GPU driver.

    What's needed to land it: one before/after on real hardware, either test_gl_dynamic_texture on a Mesa radeonsi/iris or NVIDIA box, or an apitrace of update_async_textures with a 1080p60 async source. The one-hunk change is ready. Anyone with a GPU host can take it; I can't measure it on this headless host.

  8. zackees commented on Nov 7, 2026

    @zackees
    MemberAuthor

    Backlog verification on origin/master d3953a2: the unmap half is already merged in #3382, commit 7f407fd. Checked libobs-opengl/gl-texture2d.c.h: gs_texture_unmap uses glTexSubImage2D (line 232); gs_texture_map still uses glMapBuffer(GL_WRITE_ONLY) (line 199). Do not recreate the in-place upload fix or its real-GL test_gl_dynamic_texture harness. What remains is hardware evidence at the requested async-source workloads, and a map-stall optimization only if those real GPU traces demonstrate a benefit. The thread already measured invalidate-map as a null result on llvmpipe, so that software-only timing is not acceptance evidence. Keep open for a GPU-equipped worker; no GPU measurement was run by this cleanup.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3enhancementneeds-investigationsize:M

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions