-
Notifications
You must be signed in to change notification settings - Fork 3.3k
libmpv: add D3D11 render API backend #17764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kasper93
wants to merge
1
commit into
mpv-player:master
Choose a base branch
from
kasper93:render_d3d11
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add `MPV_RENDER_API_TYPE_D3D11` render API with `MPV_RENDER_PARAM_D3D11_INIT_PARAMS` and `MPV_RENDER_PARAM_D3D11_FBO`, see `mpv/render_d3d11.h` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* Copyright (C) 2024 the mpv developers | ||
| * | ||
| * Permission to use, copy, modify, and/or distribute this software for any | ||
| * purpose with or without fee is hereby granted, provided that the above | ||
| * copyright notice and this permission notice appear in all copies. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| */ | ||
|
|
||
| #ifndef MPV_CLIENT_API_RENDER_D3D11_H_ | ||
| #define MPV_CLIENT_API_RENDER_D3D11_H_ | ||
|
|
||
| #include "render.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * Direct3D 11 backend | ||
| * ------------------- | ||
| * | ||
| * This header contains definitions for using Direct3D 11 with the render.h | ||
| * API. | ||
| * | ||
| * API use | ||
| * ------- | ||
| * | ||
| * The mpv_render_* API is used. That API supports multiple backends, and this | ||
| * section documents specifics for the Direct3D 11 backend. | ||
| * | ||
| * Use mpv_render_context_create() with MPV_RENDER_PARAM_API_TYPE set to | ||
| * MPV_RENDER_API_TYPE_D3D11, and MPV_RENDER_PARAM_D3D11_INIT_PARAMS provided. | ||
| * | ||
| * Call mpv_render_context_render() with MPV_RENDER_PARAM_D3D11_FBO to render | ||
| * the video frame to a D3D11 texture. The API user is responsible for creating | ||
| * and presenting the texture (for example, via a DXGI swap chain). | ||
| * | ||
| * Threading | ||
| * --------- | ||
| * | ||
| * Unlike OpenGL, Direct3D 11 does not use a per-thread implicit context. Any | ||
| * thread may call the mpv_render_* functions with this backend, subject to the | ||
| * general restrictions described in render.h. | ||
| * | ||
| * The ID3D11Device passed in mpv_d3d11_init_params must be usable from the | ||
| * thread that calls the mpv_render_* functions. An ID3D11Device created with | ||
| * D3D11_CREATE_DEVICE_SINGLETHREADED is therefore not supported. | ||
| */ | ||
|
|
||
| /** | ||
| * For initializing the mpv D3D11 state via MPV_RENDER_PARAM_D3D11_INIT_PARAMS. | ||
| */ | ||
| typedef struct mpv_d3d11_init_params { | ||
| /** | ||
| * The ID3D11Device to use for rendering. libmpv will add a reference to | ||
| * the device, so it is safe to release the caller's reference after | ||
| * mpv_render_context_create() returns. | ||
| * | ||
| * The device must remain valid until mpv_render_context_free() is called. | ||
| * | ||
| * Type: ID3D11Device* | ||
| */ | ||
| void *device; | ||
| } mpv_d3d11_init_params; | ||
|
|
||
| /** | ||
| * For MPV_RENDER_PARAM_D3D11_FBO. | ||
| */ | ||
| typedef struct mpv_d3d11_fbo { | ||
| /** | ||
| * The D3D11 texture to render into. This must be a 2D texture | ||
| * (ID3D11Texture2D) created on the same ID3D11Device that was passed to | ||
| * mpv_render_context_create() via mpv_d3d11_init_params. | ||
| * | ||
| * The texture must have been created with at least the | ||
| * D3D11_BIND_RENDER_TARGET bind flag and D3D11_USAGE_DEFAULT usage. It | ||
| * must not be multisampled, a texture array, or mipmapped. | ||
| * | ||
| * The texture is owned by the caller. libmpv does not take a reference, | ||
| * so the caller must ensure the texture remains valid for the duration of | ||
| * the mpv_render_context_render() call. | ||
| * | ||
| * Type: ID3D11Texture2D* | ||
| */ | ||
| void *tex; | ||
| /** | ||
| * Dimensions of the render target in pixels. Must always be set, and must | ||
| * match the actual size of the texture. | ||
| */ | ||
| int w, h; | ||
| } mpv_d3d11_fbo; | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * This file is part of mpv. | ||
| * | ||
| * mpv is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * mpv is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with mpv. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "common/msg.h" | ||
| #include "mpv/render_d3d11.h" | ||
| #include "osdep/windows_utils.h" | ||
| #include "ra_d3d11.h" | ||
| #include "video/out/gpu/libmpv_gpu.h" | ||
| #include "video/out/gpu/ra.h" | ||
|
|
||
| struct priv { | ||
| ID3D11Device *device; | ||
| struct ra_tex *wrapped_tex; | ||
| ID3D11Texture2D *wrapped_d3d_tex; | ||
| }; | ||
|
|
||
| static int init(struct libmpv_gpu_context *ctx, mpv_render_param *params) | ||
| { | ||
| struct priv *p = ctx->priv = talloc_zero(NULL, struct priv); | ||
|
|
||
| mpv_d3d11_init_params *init_params = | ||
| get_mpv_render_param(params, MPV_RENDER_PARAM_D3D11_INIT_PARAMS, NULL); | ||
| if (!init_params || !init_params->device) | ||
| return MPV_ERROR_INVALID_PARAMETER; | ||
|
|
||
| p->device = init_params->device; | ||
| ID3D11Device_AddRef(p->device); | ||
|
|
||
| struct ra_ctx *ra_ctx = talloc_zero(p, struct ra_ctx); | ||
| ra_ctx->log = ctx->log; | ||
| ra_ctx->global = ctx->global; | ||
|
|
||
| if (!spirv_compiler_init(ra_ctx)) | ||
| return MPV_ERROR_UNSUPPORTED; | ||
|
|
||
| ra_ctx->ra = ra_d3d11_create(p->device, ctx->log, ra_ctx->spirv); | ||
| if (!ra_ctx->ra) | ||
| return MPV_ERROR_UNSUPPORTED; | ||
|
|
||
| ctx->ra_ctx = ra_ctx; | ||
| return 0; | ||
| } | ||
|
|
||
| static int wrap_fbo(struct libmpv_gpu_context *ctx, mpv_render_param *params, | ||
| struct ra_tex **out) | ||
| { | ||
| struct priv *p = ctx->priv; | ||
|
|
||
| mpv_d3d11_fbo *fbo = | ||
| get_mpv_render_param(params, MPV_RENDER_PARAM_D3D11_FBO, NULL); | ||
| if (!fbo || !fbo->tex) | ||
| return MPV_ERROR_INVALID_PARAMETER; | ||
|
|
||
| // Cache the ra_tex across frames when the same D3D11 texture is reused. | ||
| // This is common for swap chain back buffers, which are recycled. | ||
| if (p->wrapped_d3d_tex != fbo->tex) { | ||
| ra_tex_free(ctx->ra_ctx->ra, &p->wrapped_tex); | ||
| p->wrapped_d3d_tex = fbo->tex; | ||
| p->wrapped_tex = ra_d3d11_wrap_tex(ctx->ra_ctx->ra, | ||
| (ID3D11Resource *)fbo->tex); | ||
| if (!p->wrapped_tex) { | ||
| p->wrapped_d3d_tex = NULL; | ||
| return MPV_ERROR_UNSUPPORTED; | ||
| } | ||
| } | ||
|
|
||
| *out = p->wrapped_tex; | ||
| return 0; | ||
| } | ||
|
|
||
| static void done_frame(struct libmpv_gpu_context *ctx, bool ds) | ||
| { | ||
| ra_d3d11_flush(ctx->ra_ctx->ra); | ||
| } | ||
|
|
||
| static void destroy(struct libmpv_gpu_context *ctx) | ||
| { | ||
| struct priv *p = ctx->priv; | ||
|
|
||
| if (ctx->ra_ctx && ctx->ra_ctx->ra) { | ||
| ra_tex_free(ctx->ra_ctx->ra, &p->wrapped_tex); | ||
| // ra_d3d11 frees its own struct from inside its destroy callback. | ||
| ctx->ra_ctx->ra->fns->destroy(ctx->ra_ctx->ra); | ||
| ctx->ra_ctx->ra = NULL; | ||
| } | ||
| SAFE_RELEASE(p->device); | ||
| } | ||
|
|
||
| const struct libmpv_gpu_context_fns libmpv_gpu_context_d3d11 = { | ||
| .api_name = MPV_RENDER_API_TYPE_D3D11, | ||
| .init = init, | ||
| .wrap_fbo = wrap_fbo, | ||
| .done_frame = done_frame, | ||
| .destroy = destroy, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.