From dc73f1ad4b235f24657ae53cf82dd18f933cc8f0 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 20 Nov 2021 21:27:46 +0100 Subject: vo_gpu_next: implement OpenGL context support Wrapping the context is pretty straightforward. This is only complicated by needing to account for the upside-down framebuffer in a few places. --- DOCS/man/vo.rst | 2 +- video/out/gpu_next/context.c | 67 ++++++++++++++++++++++++++++++++++++++++++-- video/out/vo_gpu_next.c | 18 ++++++++---- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/DOCS/man/vo.rst b/DOCS/man/vo.rst index 7897336266..1428c632c2 100644 --- a/DOCS/man/vo.rst +++ b/DOCS/man/vo.rst @@ -262,7 +262,7 @@ Available video output drivers are: the same set of features as ``--vo=gpu``. See `GPU renderer options`_ for a list. - Currently, this only supports ``--gpu-api=vulkan``, and no hardware + Currently, this only supports Vulkan, OpenGL and no hardware decoding. Unlike ``--vo=gpu``, the FBO formats are not tunable, but you can still set ``--gpu-dumb-mode=yes`` to forcibly disable their use. diff --git a/video/out/gpu_next/context.c b/video/out/gpu_next/context.c index fdf308d7ef..8e524ac3cd 100644 --- a/video/out/gpu_next/context.c +++ b/video/out/gpu_next/context.c @@ -17,6 +17,10 @@ #include +#ifdef PL_HAVE_OPENGL +#include +#endif + #include "context.h" #include "config.h" #include "common/common.h" @@ -24,12 +28,21 @@ #include "video/out/placebo/utils.h" #include "video/out/gpu/video.h" +#if HAVE_GL +#include "video/out/opengl/context.h" +#include "video/out/opengl/ra_gl.h" +#endif + #if HAVE_VULKAN #include "video/out/vulkan/context.h" #endif struct priv { +#ifdef PL_HAVE_OPENGL + pl_opengl opengl; +#else char dummy; +#endif }; struct gpu_ctx *gpu_ctx_create(struct vo *vo, struct gl_video_opts *gl_opts) @@ -37,6 +50,7 @@ struct gpu_ctx *gpu_ctx_create(struct vo *vo, struct gl_video_opts *gl_opts) struct gpu_ctx *ctx = talloc_zero(NULL, struct gpu_ctx); ctx->log = vo->log; ctx->priv = talloc_zero(ctx, struct priv); + struct priv *p = ctx->priv; struct ra_ctx_opts *ctx_opts = mp_get_config_group(ctx, vo->global, &ra_ctx_conf); ctx_opts->want_alpha = gl_opts->alpha_mode == ALPHA_YES; @@ -54,7 +68,40 @@ struct gpu_ctx *gpu_ctx_create(struct vo *vo, struct gl_video_opts *gl_opts) } #endif - // TODO: wrap GL contexts +#if HAVE_GL && defined(PL_HAVE_OPENGL) + if (ra_is_gl(ctx->ra_ctx->ra)) { + ctx->pllog = pl_log_create(PL_API_VER, NULL); + if (!ctx->pllog) + goto err_out; + + mppl_ctx_set_log(ctx->pllog, ctx->log, vo->probing); + mp_verbose(ctx->log, "Initialized libplacebo %s (API v%d)\n", + PL_VERSION, PL_API_VER); + + p->opengl = pl_opengl_create(ctx->pllog, pl_opengl_params( + .debug = ctx_opts->debug, + .allow_software = ctx_opts->allow_sw, + )); + if (!p->opengl) + goto err_out; + ctx->gpu = p->opengl->gpu; + + mppl_ctx_set_log(ctx->pllog, ctx->log, false); // disable probing + + ctx->swapchain = pl_opengl_create_swapchain(p->opengl, pl_opengl_swapchain_params( + .max_swapchain_depth = vo->opts->swapchain_depth, + )); + if (!ctx->swapchain) + goto err_out; + + return ctx; + } +#elif HAVE_GL + if (ra_is_gl(ctx->ra_ctx->ra)) { + MP_MSG(ctx, vo->probing ? MSGL_V : MSGL_ERR, + "libplacebo was built without OpenGL support.\n"); + } +#endif err_out: gpu_ctx_destroy(&ctx); @@ -63,7 +110,14 @@ err_out: bool gpu_ctx_resize(struct gpu_ctx *ctx, int w, int h) { - // Not (yet) used + struct priv *p = ctx->priv; + +#ifdef PL_HAVE_OPENGL + // The vulkan context handles this on its own, so only for OpenGL here + if (p->opengl) + return pl_swapchain_resize(ctx->swapchain, &w, &h); +#endif + return true; } @@ -72,6 +126,15 @@ void gpu_ctx_destroy(struct gpu_ctx **ctxp) struct gpu_ctx *ctx = *ctxp; if (!ctx) return; + struct priv *p = ctx->priv; + +#if HAVE_GL && defined(PL_HAVE_OPENGL) + if (ra_is_gl(ctx->ra_ctx->ra)) { + pl_swapchain_destroy(&ctx->swapchain); + pl_opengl_destroy(&p->opengl); + pl_log_destroy(&ctx->pllog); + } +#endif ra_ctx_destroy(&ctx->ra_ctx); diff --git a/video/out/vo_gpu_next.c b/video/out/vo_gpu_next.c index b1fd5a2a7f..803d418b4a 100644 --- a/video/out/vo_gpu_next.c +++ b/video/out/vo_gpu_next.c @@ -209,7 +209,7 @@ static struct mp_image *get_image(struct vo *vo, int imgfmt, int w, int h, static void write_overlays(struct vo *vo, struct mp_osd_res res, double pts, int flags, struct osd_state *state, - struct pl_frame *frame) + struct pl_frame *frame, bool flip) { struct priv *p = vo->priv; static const bool subfmt_all[SUBBITMAP_COUNT] = { @@ -255,7 +255,7 @@ static void write_overlays(struct vo *vo, struct mp_osd_res res, double pts, for (int i = 0; i < item->num_parts; i++) { const struct sub_bitmap *b = &item->parts[i]; uint32_t c = b->libass.color; - MP_TARRAY_APPEND(p, entry->parts, entry->num_parts, (struct pl_overlay_part) { + struct pl_overlay_part part = { .src = { b->src_x, b->src_y, b->src_x + b->w, b->src_y + b->h }, .dst = { b->x, b->y, b->x + b->dw, b->y + b->dh }, .color = { @@ -264,7 +264,13 @@ static void write_overlays(struct vo *vo, struct mp_osd_res res, double pts, ((c >> 8) & 0xFF) / 255.0, 1.0 - (c & 0xFF) / 255.0, } - }); + }; + if (flip) { + assert(frame->crop.y0 > frame->crop.y1); + part.dst.y0 = frame->crop.y0 - part.dst.y0; + part.dst.y1 = frame->crop.y0 - part.dst.y1; + } + MP_TARRAY_APPEND(p, entry->parts, entry->num_parts, part); } struct pl_overlay *ol = &state->overlays[frame->num_overlays++]; @@ -474,7 +480,7 @@ static bool map_frame(pl_gpu gpu, pl_tex *tex, const struct pl_source_frame *src // compensate for anamorphic sources (render subtitles as normal) .display_par = (float) par->p_h / par->p_w, }; - write_overlays(vo, vidres, mpi->pts, OSD_DRAW_SUB_ONLY, &fp->subs, frame); + write_overlays(vo, vidres, mpi->pts, OSD_DRAW_SUB_ONLY, &fp->subs, frame, false); // Update LUT attached to this frame update_lut(p, &p->image_lut); @@ -670,8 +676,10 @@ static void draw_frame(struct vo *vo, struct vo_frame *frame) // Calculate target struct pl_frame target; pl_frame_from_swapchain(&target, &swframe); - write_overlays(vo, p->osd_res, 0, OSD_DRAW_OSD_ONLY, &p->osd_state, &target); + write_overlays(vo, p->osd_res, 0, OSD_DRAW_OSD_ONLY, &p->osd_state, &target, swframe.flipped); target.crop = (struct pl_rect2df) { p->dst.x0, p->dst.y0, p->dst.x1, p->dst.y1 }; + if (swframe.flipped) + MPSWAP(float, target.crop.y0, target.crop.y1); update_lut(p, &p->target_lut); target.lut = p->target_lut.lut; -- cgit v1.2.3