Skip to content

libmpv: add D3D11 render API backend - #17764

Open
kasper93 wants to merge 1 commit into
mpv-player:masterfrom
kasper93:render_d3d11
Open

libmpv: add D3D11 render API backend#17764
kasper93 wants to merge 1 commit into
mpv-player:masterfrom
kasper93:render_d3d11

Conversation

@kasper93

Copy link
Copy Markdown
Member

Adds a new render API backend that lets libmpv embedders render directly into an application-owned ID3D11Texture2D.

The backend is selected with MPV_RENDER_API_TYPE_D3D11. The embedder supplies the ID3D11Device via MPV_RENDER_PARAM_D3D11_INIT_PARAMS at context creation, and the target texture via MPV_RENDER_PARAM_D3D11_FBO on each mpv_render_context_render() call. Swapchain creation and presentation stay entirely on the caller side.

Bumps MPV_CLIENT_API_VERSION to 2.6 and adds the public render_d3d11.h header.

Adds a new render API backend that lets libmpv embedders render directly
into an application-owned ID3D11Texture2D.

The backend is selected with MPV_RENDER_API_TYPE_D3D11. The embedder
supplies the ID3D11Device via MPV_RENDER_PARAM_D3D11_INIT_PARAMS at
context creation, and the target texture via MPV_RENDER_PARAM_D3D11_FBO
on each mpv_render_context_render() call. Swapchain creation and
presentation stay entirely on the caller side.

Bumps MPV_CLIENT_API_VERSION to 2.6 and adds the public render_d3d11.h
header.

Co-authored-by: dragonflylee <dragonflylee@outlook.com>
@kasper93

Copy link
Copy Markdown
Member Author

Also for d3d11 on Windows, window embedding may be better solution. Render api is limited.

@ikas-mc

ikas-mc commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

I wrote a demo, and got an error when calling ResizeBuffers.

D3D11 WARNING: ID3D11DeviceContext::End: End is being invoked on a Query, where the previous results have not been obtained with GetData. This is valid; but unusual. The previous results are being abandoned, and new Query results will be generated. [ EXECUTION WARNING #410: QUERY_END_ABANDONING_PREVIOUS_RESULTS]
mpv new frame...
DXGI ERROR: IDXGISwapChain::ResizeBuffers: Swapchain cannot be resized unless all outstanding buffer references have been released. [ MISCELLANEOUS ERROR #19: ]
2026-04-18 162811

Is there a way to clear this? As a temporary fix, I passed in a newly created resource before changing the size..

2026-04-18 160951

@kasper93

kasper93 commented Apr 18, 2026

Copy link
Copy Markdown
Member Author

@ikas-mc Thank you for testing. What if you add a SAFE_RELEASE(p->wrapped_tex) in done_frame() in libmpv_d3d11.c?

::

--- mpv 0.40.0 ---
2.6 - add MPV_RENDER_API_TYPE_D3D11 render API backend, along with

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to 0.42.0.

@ikas-mc

ikas-mc commented Apr 19, 2026

Copy link
Copy Markdown
Contributor

do you mean free wrapped_tex?

static void done_frame(struct libmpv_gpu_context *ctx, bool ds)
{
    struct priv* p = ctx->priv;

    ra_d3d11_flush(ctx->ra_ctx->ra);

    ra_tex_free(ctx->ra_ctx->ra, &p->wrapped_tex);
    p->wrapped_d3d_tex = NULL;
}

this can fix the error


It might not be able to cache for frames, because vo gets target size through it, unless there's a way to notify it that it's invalid and then clear it.
image

StageGuard pushed a commit to open-ani/mediamp that referenced this pull request Jul 10, 2026
…S and Windows (#37)

This PR supersedes #36 and delivers a production-ready mpv playback
backend for
desktop — macOS and Windows, hardware-accelerated and zero-copy into the
Compose
scene graph.

## Rendering

**macOS (Metal).** A native render thread drives mpv
(hwdec=videotoolbox) over an
offscreen CGL context into a triple-buffered IOSurface ring, each
surface wrapped
as an MTLTexture on Skia's device.

**Windows (D3D11).** Via the libmpv D3D11 render API (upstream
mpv-player/mpv#17764,
vendored as a patch on the 0.41.0 submodule; drop it once that PR lands
in 0.42).
A native render thread drives mpv (hwdec=d3d11va) on its own
`ID3D11Device` into a
triple-buffered ring of NT-handle shared textures, each opened as an
`ID3D12Resource`
on Skia's D3D12 device (Compose's default Windows backend). Replaces the
legacy
synchronous OpenGL path (which required forcing
`SKIKO_RENDER_API=OPENGL`).

Both platforms draw the frame **zero-copy GPU→GPU** onto the Compose
canvas via
`nativeCanvas.drawImageRect` — no per-frame GPU→CPU readback — reaching
~240fps at
4K under a full-screen danmaku overlay, with no resize crash.

**HDR.** PQ/bt2020 content is tone-mapped down to the 8-bit SDR render
target
(`gpu-dumb-mode=no` + SDR target primaries/transfer), so 10-bit HDR
clips play
correctly instead of near-black.

## Runtime & packaging

- **Zero-config consumption:** automatic runtime extraction via
`LibraryLoader` on
  first use; a single fat JVM runtime artifact works on all platforms.
- **Self-contained artifacts:** macOS bundles the external dylib closure
with
`@loader_path` rewrites; Windows collects DLL dependencies + a TLS CA
bundle;
  Linux sets `RUNPATH=$ORIGIN` (system libraries otherwise).
- FFmpeg is built with exactly the decoders/parsers/hwaccels mediamp
needs,
  including **d3d11va** on Windows.

## Build system

- buildSrc: each module's per-platform build configuration
(meson/configure flags,
  toolchain, JNI compile/link, runtime layout) is centralized into one
`*Targets.kt`; task implementations are platform-agnostic data
consumers.
  Verified end to end on Windows x64 and Android arm64-v8a.

## Testing

- Integration smoke test with pixel-level screenshot verification;
playback
  state-machine unit tests; in-repo zero-config runtime-loading test.

## CI

- Multi-platform runtime-jar build + release workflow (windows-x64,
linux-x64,
  macos-arm64).

## Known limitations

- Android cross-compilation for mpv is not yet wired into CI.
- No windows-arm64 (upstream mpv has no D3D11 render path there).
- The Linux runtime still relies on system libraries
(libplacebo/libass/…).

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
@StageGuard

Copy link
Copy Markdown

Any progress on it? I have tested the branch and it works well!

abcming added a commit to abcming/mpv that referenced this pull request Aug 11, 2026
Enables mpv to render directly into an application-provided
ID3D11Texture2D via the render API, matching the existing
OpenGL render API pattern.

Changes:
- video/out/gpu_next/context.c: D3D11 render context implementation
- video/out/vo_libmpv.c: D3D11 FBO setup in done_frame
- video/out/gpu/libmpv_gpu.c: D3D11 render API type registration
- include/mpv/render.h: D3D11 render param types
- meson.build: D3D11 dependency for libmpv

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@kasper93 kasper93 added this to the Release v0.42.0 milestone Aug 23, 2026
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.

3 participants