From d3dfffdf020e0c4de1159b45a69e938cb300a2e1 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sat, 22 Jul 2017 21:53:50 +0200 Subject: vo_opengl: osd: use new VAO mechanism In addition to using the new VAO mechanism introduced in the previous commit, this tries to keep the OSD code self-contained. This doesn't work all too well (because of the pass and CMS stuff), but it's still better than before. --- video/out/opengl/osd.c | 59 ++++++++++++++++++++++++++++-------------------- video/out/opengl/osd.h | 7 +++--- video/out/opengl/video.c | 26 ++++----------------- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/video/out/opengl/osd.c b/video/out/opengl/osd.c index 64b08a67fc..18eb36694f 100644 --- a/video/out/opengl/osd.c +++ b/video/out/opengl/osd.c @@ -25,6 +25,8 @@ #include "utils.h" #include "osd.h" +#define GLSL(x) gl_sc_add(sc, #x "\n"); + // glBlendFuncSeparate() arguments static const int blend_factors[SUBBITMAP_COUNT][4] = { [SUBBITMAP_LIBASS] = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, @@ -67,7 +69,6 @@ struct mpgl_osd { struct mpgl_osd_part *parts[MAX_OSD_PARTS]; const struct gl_format *fmt_table[SUBBITMAP_COUNT]; bool formats[SUBBITMAP_COUNT]; - struct gl_vao vao; int64_t change_counter; // temporary int stereo_mode; @@ -94,8 +95,6 @@ struct mpgl_osd *mpgl_osd_init(GL *gl, struct mp_log *log, struct osd_state *osd for (int n = 0; n < SUBBITMAP_COUNT; n++) ctx->formats[n] = !!ctx->fmt_table[n]; - gl_vao_init(&ctx->vao, gl, sizeof(struct vertex), vertex_vao); - return ctx; } @@ -106,8 +105,6 @@ void mpgl_osd_destroy(struct mpgl_osd *ctx) GL *gl = ctx->gl; - gl_vao_uninit(&ctx->vao); - for (int n = 0; n < MAX_OSD_PARTS; n++) { struct mpgl_osd_part *p = ctx->parts[n]; gl->DeleteTextures(1, &p->texture); @@ -209,6 +206,36 @@ static void gen_osd_cb(void *pctx, struct sub_bitmaps *imgs) osd->num_subparts * sizeof(osd->subparts[0])); } +bool mpgl_osd_draw_prepare(struct mpgl_osd *ctx, int index, + struct gl_shader_cache *sc) +{ + assert(index >= 0 && index < MAX_OSD_PARTS); + struct mpgl_osd_part *part = ctx->parts[index]; + + enum sub_bitmap_format fmt = part->format; + if (!fmt || !part->num_subparts) + return false; + + gl_sc_uniform_tex(sc, "osdtex", GL_TEXTURE_2D, part->texture); + switch (fmt) { + case SUBBITMAP_RGBA: { + GLSL(color = texture(osdtex, texcoord).bgra;) + break; + } + case SUBBITMAP_LIBASS: { + GLSL(color = + vec4(ass_color.rgb, ass_color.a * texture(osdtex, texcoord).r);) + break; + } + default: + abort(); + } + + gl_sc_set_vertex_format(sc, vertex_vao, sizeof(struct vertex)); + + return true; +} + static void write_quad(struct vertex *va, struct gl_transform t, float x0, float y0, float x1, float y1, float tx0, float ty0, float tx1, float ty1, @@ -263,7 +290,8 @@ static void get_3d_side_by_side(int stereo_mode, int div[2]) } } -void mpgl_osd_draw_part(struct mpgl_osd *ctx, int vp_w, int vp_h, int index) +void mpgl_osd_draw_finish(struct mpgl_osd *ctx, int vp_w, int vp_h, int index, + struct gl_shader_cache *sc) { GL *gl = ctx->gl; struct mpgl_osd_part *part = ctx->parts[index]; @@ -297,29 +325,12 @@ void mpgl_osd_draw_part(struct mpgl_osd *ctx, int vp_w, int vp_h, int index) ctx->gl->Viewport(0, 0, vp_w, abs(vp_h)); - gl_vao_draw_data(&ctx->vao, GL_TRIANGLES, part->vertices, part->num_vertices); + gl_sc_draw_data(sc, GL_TRIANGLES, part->vertices, part->num_vertices); gl->BindTexture(GL_TEXTURE_2D, 0); gl->Disable(GL_BLEND); } -enum sub_bitmap_format mpgl_osd_get_part_format(struct mpgl_osd *ctx, int index) -{ - assert(index >= 0 && index < MAX_OSD_PARTS); - return ctx->parts[index]->format; -} - -GLuint mpgl_osd_get_part_texture(struct mpgl_osd *ctx, int index) -{ - assert(index >= 0 && index < MAX_OSD_PARTS); - return ctx->parts[index]->texture; -} - -struct gl_vao *mpgl_osd_get_vao(struct mpgl_osd *ctx) -{ - return &ctx->vao; -} - static void set_res(struct mpgl_osd *ctx, struct mp_osd_res res, int stereo_mode) { int div[2]; diff --git a/video/out/opengl/osd.h b/video/out/opengl/osd.h index 197bb72501..7ea851f8bf 100644 --- a/video/out/opengl/osd.h +++ b/video/out/opengl/osd.h @@ -15,9 +15,10 @@ void mpgl_osd_set_options(struct mpgl_osd *ctx, bool pbo); void mpgl_osd_generate(struct mpgl_osd *ctx, struct mp_osd_res res, double pts, int stereo_mode, int draw_flags); void mpgl_osd_resize(struct mpgl_osd *ctx, struct mp_osd_res res, int stereo_mode); -enum sub_bitmap_format mpgl_osd_get_part_format(struct mpgl_osd *ctx, int index); -GLuint mpgl_osd_get_part_texture(struct mpgl_osd *ctx, int index); -struct gl_vao *mpgl_osd_get_vao(struct mpgl_osd *ctx); +bool mpgl_osd_draw_prepare(struct mpgl_osd *ctx, int index, + struct gl_shader_cache *sc); +void mpgl_osd_draw_finish(struct mpgl_osd *ctx, int vp_w, int vp_h, int index, + struct gl_shader_cache *sc); void mpgl_osd_draw_part(struct mpgl_osd *ctx, int vp_w, int vp_h, int index); int64_t mpgl_get_change_counter(struct mpgl_osd *ctx); diff --git a/video/out/opengl/video.c b/video/out/opengl/video.c index 17433b4647..2fe0d14986 100644 --- a/video/out/opengl/video.c +++ b/video/out/opengl/video.c @@ -2407,26 +2407,10 @@ static void pass_draw_osd(struct gl_video *p, int draw_flags, double pts, p->gl->BindFramebuffer(GL_FRAMEBUFFER, fbo); for (int n = 0; n < MAX_OSD_PARTS; n++) { - enum sub_bitmap_format fmt = mpgl_osd_get_part_format(p->osd, n); - if (!fmt) + // (This returns false if this part is empty with nothing to draw.) + if (!mpgl_osd_draw_prepare(p->osd, n, p->sc)) continue; - gl_sc_uniform_tex(p->sc, "osdtex", GL_TEXTURE_2D, - mpgl_osd_get_part_texture(p->osd, n)); - switch (fmt) { - case SUBBITMAP_RGBA: { - pass_describe(p, "drawing osd (rgba)"); - GLSL(color = texture(osdtex, texcoord).bgra;) - break; - } - case SUBBITMAP_LIBASS: { - pass_describe(p, "drawing osd (libass)"); - GLSL(color = - vec4(ass_color.rgb, ass_color.a * texture(osdtex, texcoord).r);) - break; - } - default: - abort(); - } + pass_describe(p, "drawing osd"); // When subtitles need to be color managed, assume they're in sRGB // (for lack of anything saner to do) if (cms) { @@ -2438,11 +2422,9 @@ static void pass_draw_osd(struct gl_video *p, int draw_flags, double pts, pass_colormanage(p, csp_srgb, true); } - gl_sc_set_vao(p->sc, mpgl_osd_get_vao(p->osd)); pass_record(p, gl_sc_generate(p->sc)); - mpgl_osd_draw_part(p->osd, vp_w, vp_h, n); + mpgl_osd_draw_finish(p->osd, vp_w, vp_h, n, p->sc); gl_sc_reset(p->sc); - gl_sc_set_vao(p->sc, NULL); } } -- cgit v1.2.3