You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf: libobs: upload async frames without the second system-memory copy #3201
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.
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.
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).
The staging surface is mapped with glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY) (gl-stagesurf.c.h:207).
obs-video.c.h:600-657 copies it into the video-io cache frame.
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:
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.
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.
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.
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.
Activity
zackees commentedon Oct 31, 2026
/investigate needs the shared frame-lifetime design with #3202 written down (overlaps #3202) before either can be specified and sized.
zackees commentedon Nov 1, 2026
Claiming: investigation. This is the shared frame-lifetime design that #3201, #3202 and #3200 wait on.
The copies today
Upload (#3201).
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.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 isglMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)(gl-texture2d.c.h:199).glTexSubImage2Dfrom the PBO (perf(libobs-opengl): update a dynamic texture in place on unmap (#3191) #3382), done by the driver.Readback (#3202, #3200).
glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY)(gl-stagesurf.c.h:207).obs-video.c.h:600-657copies it into the video-io cache 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
releaseinserts a fence.acquirewaits on the fence of the slot it reuses.ARB_buffer_storage: oneglBufferStorageper pool,GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT, plusGL_MAP_WRITE_BIT(upload) orGL_MAP_READ_BIT | GL_CLIENT_STORAGE_BIT(readback). That memory is CPU-cached, which matters below.device_*exports inside libobs-graphics, and plugins never see them.struct obs_source_framekeeps its public layout. The backing goes in libobs's private per-frame bookkeeping (struct async_framealready carries the cache entry), as{pool, slot}.obs_source_frame_decrefreleases the slot when the last reference drops.#3201 on top of it
cache_videoallocates the cache frame's planes from an upload pool, sized to the frame's plane layout, when all of these hold:obs_source_get_frameconsumer is attached.Persistent write-mapped memory is write-combined, and CPU reads of it are slow; filters, deinterlacing and
obs_source_get_frameall read the CPU frame. Otherwise the frame stays heap-backed.Copy 1 stays (API contract), but now lands in GPU-visible memory.
update_async_texturesees a pool-backed frame and calls a newgs_texture_set_image_from_stage(tex, pool, slot, offset, linesize): oneglTexSubImage2Dfrom the bound buffer at an offset. That drops copy 2 entirely.#3202 / #3200 on top of it
GL_CLIENT_STORAGE_BIT, CPU-cached).video_output_lock_framehands 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 encoderdecref, so the copy atobs-video.c.h:600-657goes.Order and size
Pool + GL backend + fallback. Tier 1 tests:
Plus a cmocka GL test like
test_gl_dynamic_texture.cfrom perf(libobs-opengl): update a dynamic texture in place on unmap (#3191) #3382. M.perf: libobs: upload async frames without the second system-memory copy #3201 upload path. Cmocka:
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.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_imageas 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.