Skip to content

perf: libobs: upload async frames without the second system-memory copy #3201

Open

Description

@zackees

What

Async frames are copied into the cache frame (obs-source.c.h:2985-3000), then again into the mapped PBO (graphics.c.h:1104-1150), row by row when linesizes differ.

Proposed change

Upload straight from the cache frame when it is consumed this tick, or hand over a refcounted frame. Shares the frame-lifetime redesign with the readback-copy sub-issue (effort M-L).

Acceptance criteria

  • Before/after numbers (perf on copy_frame_data and gs_texture_set_image with a 1080p and a 4K source) 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. added
    enhancement
    P3
    size:L
    on Oct 31, 2026
  2. zackees commented on Oct 31, 2026

    @zackees
    MemberAuthor

    /investigate needs the shared frame-lifetime design with #3202 written down (overlaps #3202) before either can be specified and sized.

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

    @zackees
    MemberAuthor

    Claiming: investigation. This is the shared frame-lifetime design that #3201, #3202 and #3200 wait on.

    The copies today

    Upload (#3201).

    1. obs_source_output_video → cache_video → copy_frame_data (obs-source.c.h:3002, :3147) on the caller's thread. This copy is required: the API lets the caller reuse its buffer as soon as the call returns.
    2. On the graphics thread, update_async_texture → gs_texture_set_image (obs-source.c.h:1924, :2074) → gs_texture_map → memcpy (row by row when linesizes differ) → unmap (graphics.c.h:1104-1150). The GL map is glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY) (gl-texture2d.c.h:199).
    3. The unmap's glTexSubImage2D from the PBO (perf(libobs-opengl): update a dynamic texture in place on unmap (#3191) #3382), done by the driver.

    Readback (#3202, #3200).

    1. The staging surface is mapped with glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY) (gl-stagesurf.c.h:207).
    2. obs-video.c.h:600-657 copies it into the video-io cache frame.
    3. The encoders read that frame.

    So the copy both sides can remove is the CPU copy between a libobs-owned frame and a GPU-visible buffer: copy 2 on upload, copy 2 on readback. It needs one thing that doesn't exist yet: a frame whose memory is GPU-visible buffer storage, released only when every reader is done and the GPU has finished with it.

    Design: a staging slot pool in libobs-graphics

    • New optional device exports, loaded like the others but allowed to be missing:
      gs_stage_pool_t *device_stage_pool_create(gs_device_t *, enum gs_stage_dir dir, size_t slot_size, uint32_t slots);
      void *device_stage_slot_acquire(gs_stage_pool_t *, uint32_t *slot);
      void  device_stage_slot_release(gs_stage_pool_t *, uint32_t slot);
      • release inserts a fence.
      • acquire waits on the fence of the slot it reuses.
      • On GL 4.4 / ARB_buffer_storage: one glBufferStorage per pool, GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT, plus GL_MAP_WRITE_BIT (upload) or GL_MAP_READ_BIT | GL_CLIENT_STORAGE_BIT (readback). That memory is CPU-cached, which matters below.
      • The pointer is valid on any thread for the pool's life, so the output thread can write into it without a GL context.
    • Fallback when the export is missing (D3D11, Metal, GL < 4.4): libobs keeps today's path. No public header or ABI change: these are device_* exports inside libobs-graphics, and plugins never see them.
    • struct obs_source_frame keeps its public layout. The backing goes in libobs's private per-frame bookkeeping (struct async_frame already carries the cache entry), as {pool, slot}. obs_source_frame_decref releases the slot when the last reference drops.

    #3201 on top of it

    • cache_video allocates the cache frame's planes from an upload pool, sized to the frame's plane layout, when all of these hold:

      • the device has the pool;
      • the source has no async filters;
      • it doesn't deinterlace;
      • no obs_source_get_frame consumer is attached.

      Persistent write-mapped memory is write-combined, and CPU reads of it are slow; filters, deinterlacing and obs_source_get_frame all read the CPU frame. Otherwise the frame stays heap-backed.

    • Copy 1 stays (API contract), but now lands in GPU-visible memory.

    • update_async_texture sees a pool-backed frame and calls a new gs_texture_set_image_from_stage(tex, pool, slot, offset, linesize): one glTexSubImage2D from the bound buffer at an offset. That drops copy 2 entirely.

    #3202 / #3200 on top of it

    • Readback staging becomes a read pool (GL_CLIENT_STORAGE_BIT, CPU-cached).
    • video_output_lock_frame hands out the slot's pointers instead of the cache frame's. The slot's release is the video-io frame's release, after the last encoder decref, so the copy at obs-video.c.h:600-657 goes.
    • The pool's slot count bounds how far encoders may lag. When none is free, fall back to the copy and log it once.

    Order and size

    1. Pool + GL backend + fallback. Tier 1 tests:

      • the slot lifecycle;
      • a fence before reuse;
      • the missing-export fallback.

      Plus a cmocka GL test like test_gl_dynamic_texture.c from perf(libobs-opengl): update a dynamic texture in place on unmap (#3191) #3382. M.

    2. perf: libobs: upload async frames without the second system-memory copy #3201 upload path. Cmocka:

      • the texture content matches the heap path bit for bit for each format: NV12, I420, BGRA, P010;
      • the eligibility rules: a filter or deinterlacing forces the heap path.

      Bench: obs_runtime_bench (test(libobs): end-to-end runtime benchmark (#3385) #3387) already runs a 960×540 BGRA async source. Add 1080p and 4K variants. M.

    3. perf: libobs: expose mapped staging memory as the video-io cache frame #3202 readback path. Cmocka: encoder frames bit-identical, pool exhaustion falls back. Bench: CPU-encoder output at 1080p/4K. M-L.

    Tier 3 stays bit-exact: every step is a copy elision, not a change to values.

    The measurement caveat

    On llvmpipe, the only GPU this host has, "GPU memory" is system memory and the driver's own copies dominate. The IPO, cross-LTO and PGO numbers in #3193/#3217/#3219 show the libobs graphics thread at about 0.2 cores next to llvmpipe's 3.5. So step 2's gain should be shown as the graphics thread's CPU at 1080p/4K, plus a perf profile of copy_frame_data/gs_texture_set_image as the issue asks. Expect it to be clearer on a real GPU. If neither moves, stop after step 1's tests instead of landing steps 2 and 3.

  5. removed their assignment
    on Nov 2, 2026
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:L

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions