1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-24 15:52:25 +00:00
mpv/video/out/gpu/context.c
James Ross-Gowan 68eac1a1e7 vo_gpu: d3d11: initial implementation
This is a new RA/vo_gpu backend that uses Direct3D 11. The GLSL
generated by vo_gpu is cross-compiled to HLSL with SPIRV-Cross.

What works:

- All of mpv's internal shaders should work, including compute shaders.

- Some external shaders have been tested and work, including RAVU and
  adaptive-sharpen.

- Non-dumb mode works, even on very old hardware. Most features work at
  feature level 9_3 and all features work at feature level 10_0. Some
  features also work at feature level 9_1 and 9_2, but without high-bit-
  depth FBOs, it's not very useful. (Hardware this old is probably not
  fast enough for advanced features anyway.)

  Note: This is more compatible than ANGLE, which requires 9_3 to work
  at all (GLES 2.0,) and 10_1 for non-dumb-mode (GLES 3.0.)

- Hardware decoding with D3D11VA, including decoding of 10-bit formats
  without truncation to 8-bit.

What doesn't work / can be improved:

- PBO upload and direct rendering does not work yet. Direct rendering
  requires persistent-mapped PBOs because the decoder needs to be able
  to read data from images that have already been decoded and uploaded.
  Unfortunately, it seems like persistent-mapped PBOs are fundamentally
  incompatible with D3D11, which requires all resources to use driver-
  managed memory and requires memory to be unmapped (and hence pointers
  to be invalidated) when a resource is used in a draw or copy
  operation.

  However it might be possible to use D3D11's limited multithreading
  capabilities to emulate some features of PBOs, like asynchronous
  texture uploading.

- The blit() and clear() operations don't have equivalents in the D3D11
  API that handle all cases, so in most cases, they have to be emulated
  with a shader. This is currently done inside ra_d3d11, but ideally it
  would be done in generic code, so it can take advantage of mpv's
  shader generation utilities.

- SPIRV-Cross is used through a NIH C-compatible wrapper library, since
  it does not expose a C interface itself.

  The library is available here: https://github.com/rossy/crossc

- The D3D11 context could be made to support more modern DXGI features
  in future. For example, it should be possible to add support for
  high-bit-depth and HDR output with DXGI 1.5/1.6.
2017-11-07 20:27:13 +11:00

224 lines
6.0 KiB
C

/*
* 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 <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include "config.h"
#include "common/common.h"
#include "common/msg.h"
#include "options/options.h"
#include "options/m_option.h"
#include "video/out/vo.h"
#include "context.h"
#include "spirv.h"
/* OpenGL */
extern const struct ra_ctx_fns ra_ctx_glx;
extern const struct ra_ctx_fns ra_ctx_glx_probe;
extern const struct ra_ctx_fns ra_ctx_x11_egl;
extern const struct ra_ctx_fns ra_ctx_drm_egl;
extern const struct ra_ctx_fns ra_ctx_cocoa;
extern const struct ra_ctx_fns ra_ctx_wayland_egl;
extern const struct ra_ctx_fns ra_ctx_wgl;
extern const struct ra_ctx_fns ra_ctx_angle;
extern const struct ra_ctx_fns ra_ctx_dxgl;
extern const struct ra_ctx_fns ra_ctx_rpi;
extern const struct ra_ctx_fns ra_ctx_android;
extern const struct ra_ctx_fns ra_ctx_mali_fbdev;
extern const struct ra_ctx_fns ra_ctx_vdpauglx;
/* Vulkan */
extern const struct ra_ctx_fns ra_ctx_vulkan_wayland;
extern const struct ra_ctx_fns ra_ctx_vulkan_win;
extern const struct ra_ctx_fns ra_ctx_vulkan_xlib;
/* Direct3D 11 */
extern const struct ra_ctx_fns ra_ctx_d3d11;
static const struct ra_ctx_fns *contexts[] = {
#if HAVE_D3D11
&ra_ctx_d3d11,
#endif
// OpenGL contexts:
#if HAVE_ANDROID
&ra_ctx_android,
#endif
#if HAVE_RPI
&ra_ctx_rpi,
#endif
#if HAVE_GL_COCOA
&ra_ctx_cocoa,
#endif
#if HAVE_EGL_ANGLE_WIN32
&ra_ctx_angle,
#endif
#if HAVE_GL_WIN32
&ra_ctx_wgl,
#endif
#if HAVE_GL_DXINTEROP
&ra_ctx_dxgl,
#endif
#if HAVE_GL_X11
&ra_ctx_glx_probe,
#endif
#if HAVE_EGL_X11
&ra_ctx_x11_egl,
#endif
#if HAVE_GL_X11
&ra_ctx_glx,
#endif
#if HAVE_GL_WAYLAND
&ra_ctx_wayland_egl,
#endif
#if HAVE_EGL_DRM
&ra_ctx_drm_egl,
#endif
#if HAVE_MALI_FBDEV
&ra_ctx_mali_fbdev,
#endif
#if HAVE_VDPAU_GL_X11
&ra_ctx_vdpauglx,
#endif
// Vulkan contexts:
#if HAVE_VULKAN
#if HAVE_WIN32_DESKTOP
&ra_ctx_vulkan_win,
#endif
#if HAVE_WAYLAND
&ra_ctx_vulkan_wayland,
#endif
#if HAVE_X11
&ra_ctx_vulkan_xlib,
#endif
#endif
};
int ra_ctx_validate_api(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param)
{
if (bstr_equals0(param, "help")) {
mp_info(log, "GPU APIs (contexts):\n");
mp_info(log, " auto (autodetect)\n");
for (int n = 0; n < MP_ARRAY_SIZE(contexts); n++)
mp_info(log, " %s (%s)\n", contexts[n]->type, contexts[n]->name);
return M_OPT_EXIT;
}
if (bstr_equals0(param, "auto"))
return 1;
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
if (bstr_equals0(param, contexts[i]->type))
return 1;
}
return M_OPT_INVALID;
}
int ra_ctx_validate_context(struct mp_log *log, const struct m_option *opt,
struct bstr name, struct bstr param)
{
if (bstr_equals0(param, "help")) {
mp_info(log, "GPU contexts (APIs):\n");
mp_info(log, " auto (autodetect)\n");
for (int n = 0; n < MP_ARRAY_SIZE(contexts); n++)
mp_info(log, " %s (%s)\n", contexts[n]->name, contexts[n]->type);
return M_OPT_EXIT;
}
if (bstr_equals0(param, "auto"))
return 1;
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
if (bstr_equals0(param, contexts[i]->name))
return 1;
}
return M_OPT_INVALID;
}
// Create a VO window and create a RA context on it.
// vo_flags: passed to the backend's create window function
struct ra_ctx *ra_ctx_create(struct vo *vo, const char *context_type,
const char *context_name, struct ra_ctx_opts opts)
{
bool api_auto = !context_type || strcmp(context_type, "auto") == 0;
bool ctx_auto = !context_name || strcmp(context_name, "auto") == 0;
if (ctx_auto) {
MP_VERBOSE(vo, "Probing for best GPU context.\n");
opts.probing = true;
}
// Hack to silence backend (X11/Wayland/etc.) errors. Kill it once backends
// are separate from `struct vo`
bool old_probing = vo->probing;
vo->probing = opts.probing;
for (int i = 0; i < MP_ARRAY_SIZE(contexts); i++) {
if (!opts.probing && strcmp(contexts[i]->name, context_name) != 0)
continue;
if (!api_auto && strcmp(contexts[i]->type, context_type) != 0)
continue;
struct ra_ctx *ctx = talloc_ptrtype(NULL, ctx);
*ctx = (struct ra_ctx) {
.vo = vo,
.global = vo->global,
.log = mp_log_new(ctx, vo->log, contexts[i]->type),
.opts = opts,
.fns = contexts[i],
};
MP_VERBOSE(ctx, "Initializing GPU context '%s'\n", ctx->fns->name);
if (contexts[i]->init(ctx)) {
vo->probing = old_probing;
return ctx;
}
talloc_free(ctx);
}
vo->probing = old_probing;
// If we've reached this point, then none of the contexts matched the name
// requested, or the backend creation failed for all of them.
if (!vo->probing)
MP_ERR(vo, "Failed initializing any suitable GPU context!\n");
return NULL;
}
void ra_ctx_destroy(struct ra_ctx **ctx_ptr)
{
struct ra_ctx *ctx = *ctx_ptr;
if (!ctx)
return;
if (ctx->spirv && ctx->spirv->fns->uninit)
ctx->spirv->fns->uninit(ctx);
ctx->fns->uninit(ctx);
talloc_free(ctx);
*ctx_ptr = NULL;
}