Skip to content

Commit

Permalink
player: add moltenvk context
Browse files Browse the repository at this point in the history
  • Loading branch information
karelrooted committed Dec 14, 2023
1 parent 0441102 commit 39945a1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,10 @@ Window
``--hwdec=mediacodec`` for direct rendering using MediaCodec, or with
``--vo=gpu --gpu-context=android`` (with or without ``--hwdec=mediacodec``).

If compiled with MoltenVK on iOS/tvOS/macOS, the ID is interpreted as
``CAMetalLayer *``. Pass it as a value cast to ``intptr_t``. Use with
``--vo=gpu-next --gpu-api=vulkan``

``--no-window-dragging``
Don't move the window when clicking on it and moving the mouse pointer.

Expand Down
11 changes: 11 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,17 @@ if features['vulkan'] and features['x11']
sources += files('video/out/vulkan/context_xlib.c')
endif

if host_machine.system() == 'darwin'
moltenvk = get_option('moltenvk').require(
features['vulkan'],
error_message: 'vulkan or moltenvk header could not be found!',
)
features += {'moltenvk': moltenvk.allowed()}
if features['vulkan'] and features['moltenvk']
sources += files('video/out/vulkan/context_moltenvk.m')
endif
endif

features += {'vk-khr-display': cc.has_function('vkCreateDisplayPlaneSurfaceKHR', prefix: '#include <vulkan/vulkan_core.h>',
dependencies: [vulkan])}
if features['vk-khr-display']
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ option('rpi-mmal', type: 'feature', value: 'auto', description: 'Raspberry Pi MM
option('videotoolbox-gl', type: 'feature', value: 'auto', description: 'Videotoolbox with OpenGL')
option('videotoolbox-pl', type: 'feature', value: 'auto', description: 'Videotoolbox with libplacebo')
option('vulkan-interop', type: 'feature', value: 'auto', description: 'Vulkan graphics interop')
option('moltenvk', type: 'feature', value: 'auto', description: 'Moltenvk context')

# macOS features
option('macos-cocoa-cb', type: 'feature', value: 'auto', description: 'macOS libmpv backend')
Expand Down
4 changes: 4 additions & 0 deletions video/out/gpu/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern const struct ra_ctx_fns ra_ctx_vulkan_xlib;
extern const struct ra_ctx_fns ra_ctx_vulkan_android;
extern const struct ra_ctx_fns ra_ctx_vulkan_display;
extern const struct ra_ctx_fns ra_ctx_vulkan_mac;
extern const struct ra_ctx_fns ra_ctx_vulkan_moltenvk;

/* Direct3D 11 */
extern const struct ra_ctx_fns ra_ctx_d3d11;
Expand Down Expand Up @@ -95,6 +96,9 @@ static const struct ra_ctx_fns *contexts[] = {
// Vulkan contexts:
#if HAVE_VULKAN

#if HAVE_MOLTENVK
&ra_ctx_vulkan_moltenvk,
#endif
#if HAVE_ANDROID
&ra_ctx_vulkan_android,
#endif
Expand Down
3 changes: 3 additions & 0 deletions video/out/vulkan/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#if HAVE_WIN32_DESKTOP
#define VK_USE_PLATFORM_WIN32_KHR
#endif
#if HAVE_MOLTENVK
#include <MoltenVK/mvk_vulkan.h>
#endif
#if HAVE_COCOA
#define VK_USE_PLATFORM_MACOS_MVK
#define VK_USE_PLATFORM_METAL_EXT
Expand Down
96 changes: 96 additions & 0 deletions video/out/vulkan/context_moltenvk.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 <CoreGraphics/CoreGraphics.h>
#include <QuartzCore/CAMetalLayer.h>
#include <MoltenVK/mvk_vulkan.h>

#include "common.h"
#include "context.h"
#include "utils.h"

struct priv {
struct mpvk_ctx vk;
CAMetalLayer *layer;
};

static void moltenvk_uninit(struct ra_ctx *ctx)
{
struct priv *p = ctx->priv;
ra_vk_ctx_uninit(ctx);
mpvk_uninit(&p->vk);
}

static bool moltenvk_init(struct ra_ctx *ctx)
{
struct priv *p = ctx->priv = talloc_zero(ctx, struct priv);
struct mpvk_ctx *vk = &p->vk;
int msgl = ctx->opts.probing ? MSGL_V : MSGL_ERR;

if (ctx->vo->opts->WinID == -1) {
MP_MSG(ctx, msgl, "WinID missing\n");
goto fail;
}

if (!mpvk_init(vk, ctx, VK_EXT_METAL_SURFACE_EXTENSION_NAME))
goto fail;

p->layer = (__bridge CAMetalLayer *)(intptr_t)ctx->vo->opts->WinID;
VkMetalSurfaceCreateInfoEXT info = {
.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT,
.pLayer = p->layer,
};

struct ra_vk_ctx_params params = {0};

VkInstance inst = vk->vkinst->instance;
VkResult res = vkCreateMetalSurfaceEXT(inst, &info, NULL, &vk->surface);
if (res != VK_SUCCESS) {
MP_MSG(ctx, msgl, "Failed creating MoltenVK surface\n");
goto fail;
}

if (!ra_vk_ctx_init(ctx, vk, params, VK_PRESENT_MODE_FIFO_KHR))
goto fail;

return true;
fail:
moltenvk_uninit(ctx);
return false;
}

static bool moltenvk_reconfig(struct ra_ctx *ctx)
{
struct priv *p = ctx->priv;
CGSize s = p->layer.drawableSize;
ra_vk_ctx_resize(ctx, s.width, s.height);
return true;
}

static int moltenvk_control(struct ra_ctx *ctx, int *events, int request, void *arg)
{
return VO_NOTIMPL;
}

const struct ra_ctx_fns ra_ctx_vulkan_moltenvk = {
.type = "vulkan",
.name = "moltenvk",
.reconfig = moltenvk_reconfig,
.control = moltenvk_control,
.init = moltenvk_init,
.uninit = moltenvk_uninit,
};

0 comments on commit 39945a1

Please sign in to comment.