Skip to content
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

video/out/vulkan: add MoltenVK context #7857

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3175,6 +3175,10 @@ Window
``--hwdec=mediacodec`` for direct rendering using MediaCodec, or with
``--vo=gpu --gpu-context=android`` (with or without ``--hwdec=mediacodec-copy``).

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 --gpu-api=vulkan --gpu-context=moltenvk``
Copy link
Member

Choose a reason for hiding this comment

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

Specifying --gpu-api=vulkan here is redundant.


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

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 @@ -50,6 +50,7 @@ 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;
extern const struct ra_ctx_fns ra_ctx_vulkan_android;
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 @@ -94,6 +95,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

#include <libplacebo/vulkan.h>

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.height, s.height);

Choose a reason for hiding this comment

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

//should pass width here not height
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,
};
12 changes: 12 additions & 0 deletions wscript
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ main_dependencies = [
'name': '--android',
'desc': 'Android environment',
'func': check_statement('android/api-level.h', '(void)__ANDROID__'), # arbitrary android-specific header
}, {
'name': '--ios',
'desc': 'iOS environment',
'func': check_statement(
['TargetConditionals.h', 'assert.h'],
'static_assert(TARGET_OS_IPHONE, "TARGET_OS_IPHONE defined to zero!")'
),
}, {
'name': '--tvos',
'desc': 'tvOS environment',
Expand Down Expand Up @@ -726,6 +733,11 @@ video_output_features = [
'desc': 'Vulkan context support',
'deps': 'libplacebo',
'func': check_pkg_config('vulkan'),
}, {
'name': '--moltenvk',
'desc': 'MoltenVK support',
'deps': 'ios || tvos || cocoa',
'func': check_cc(header_name=['MoltenVK/mvk_vulkan.h']),
}, {
'name': 'vaapi-vulkan',
'desc': 'VAAPI Vulkan',
Expand Down
1 change: 1 addition & 0 deletions wscript_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def swift(task):
( "video/out/vo_xv.c", "xv" ),
( "video/out/vulkan/context.c", "vulkan" ),
( "video/out/vulkan/context_android.c", "vulkan && android" ),
( "video/out/vulkan/context_moltenvk.m", "moltenvk" ),
( "video/out/vulkan/context_wayland.c", "vulkan && wayland" ),
( "video/out/vulkan/context_win.c", "vulkan && win32-desktop" ),
( "video/out/vulkan/context_xlib.c", "vulkan && x11" ),
Expand Down