/src/mpv/video/out/opengl/context.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * This file is part of mpv. |
3 | | * |
4 | | * mpv is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU Lesser General Public |
6 | | * License as published by the Free Software Foundation; either |
7 | | * version 2.1 of the License, or (at your option) any later version. |
8 | | * |
9 | | * mpv is distributed in the hope that it will be useful, |
10 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | * GNU Lesser General Public License for more details. |
13 | | * |
14 | | * You should have received a copy of the GNU Lesser General Public |
15 | | * License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | |
18 | | #include "options/m_config.h" |
19 | | #include "context.h" |
20 | | #include "ra_gl.h" |
21 | | #include "utils.h" |
22 | | |
23 | | // 0-terminated list of desktop GL versions a backend should try to |
24 | | // initialize. Each entry is the minimum required version. |
25 | | const int mpgl_min_required_gl_versions[] = { |
26 | | /* |
27 | | * Nvidia drivers will not provide the highest supported version |
28 | | * when 320 core is requested. Instead, it just returns 3.2. This |
29 | | * would be bad, as we actually want compute shaders that require |
30 | | * 4.2, so we have to request a sufficiently high version. We use |
31 | | * 440 to maximise driver compatibility as we don't need anything |
32 | | * from newer versions. |
33 | | */ |
34 | | 440, |
35 | | 320, |
36 | | 210, |
37 | | 0 |
38 | | }; |
39 | | |
40 | | enum { |
41 | | FLUSH_NO = 0, |
42 | | FLUSH_YES, |
43 | | FLUSH_AUTO, |
44 | | }; |
45 | | |
46 | | struct opengl_opts { |
47 | | bool use_glfinish; |
48 | | bool waitvsync; |
49 | | int vsync_pattern[2]; |
50 | | int swapinterval; |
51 | | int early_flush; |
52 | | int gles_mode; |
53 | | }; |
54 | | |
55 | | #define OPT_BASE_STRUCT struct opengl_opts |
56 | | const struct m_sub_options opengl_conf = { |
57 | | .opts = (const struct m_option[]) { |
58 | | {"opengl-glfinish", OPT_BOOL(use_glfinish)}, |
59 | | {"opengl-waitvsync", OPT_BOOL(waitvsync)}, |
60 | | {"opengl-swapinterval", OPT_INT(swapinterval), .flags = UPDATE_VO}, |
61 | | {"opengl-check-pattern-a", OPT_INT(vsync_pattern[0])}, |
62 | | {"opengl-check-pattern-b", OPT_INT(vsync_pattern[1])}, |
63 | | {"opengl-es", OPT_CHOICE(gles_mode, |
64 | | {"auto", GLES_AUTO}, {"yes", GLES_YES}, {"no", GLES_NO}), |
65 | | .flags = UPDATE_VO, |
66 | | }, |
67 | | {"opengl-early-flush", OPT_CHOICE(early_flush, |
68 | | {"no", FLUSH_NO}, {"yes", FLUSH_YES}, {"auto", FLUSH_AUTO})}, |
69 | | {0}, |
70 | | }, |
71 | | .defaults = &(const struct opengl_opts) { |
72 | | .swapinterval = 1, |
73 | | }, |
74 | | .size = sizeof(struct opengl_opts), |
75 | | }; |
76 | | |
77 | | struct priv { |
78 | | GL *gl; |
79 | | struct mp_log *log; |
80 | | struct ra_ctx_params params; |
81 | | struct opengl_opts *opts; |
82 | | struct ra_swapchain_fns fns; |
83 | | GLuint main_fb; |
84 | | struct ra_tex *wrapped_fb; // corresponds to main_fb |
85 | | // for debugging: |
86 | | int frames_rendered; |
87 | | unsigned int prev_sgi_sync_count; |
88 | | // for gl_vsync_pattern |
89 | | int last_pattern; |
90 | | int matches, mismatches; |
91 | | // for swapchain_depth simulation |
92 | | GLsync *vsync_fences; |
93 | | int num_vsync_fences; |
94 | | }; |
95 | | |
96 | | enum gles_mode ra_gl_ctx_get_glesmode(struct ra_ctx *ctx) |
97 | 0 | { |
98 | 0 | void *tmp = talloc_new(NULL); |
99 | 0 | struct opengl_opts *opts; |
100 | 0 | enum gles_mode mode; |
101 | |
|
102 | 0 | opts = mp_get_config_group(tmp, ctx->global, &opengl_conf); |
103 | 0 | mode = opts->gles_mode; |
104 | |
|
105 | 0 | talloc_free(tmp); |
106 | 0 | return mode; |
107 | 0 | } |
108 | | |
109 | | void ra_gl_ctx_uninit(struct ra_ctx *ctx) |
110 | 0 | { |
111 | 0 | if (ctx->swapchain) { |
112 | 0 | struct priv *p = ctx->swapchain->priv; |
113 | 0 | if (ctx->ra && p->wrapped_fb) |
114 | 0 | ra_tex_free(ctx->ra, &p->wrapped_fb); |
115 | 0 | talloc_free(ctx->swapchain); |
116 | 0 | ctx->swapchain = NULL; |
117 | 0 | } |
118 | | |
119 | | // Clean up any potentially left-over debug callback |
120 | 0 | if (ctx->ra) |
121 | 0 | ra_gl_set_debug(ctx->ra, false); |
122 | |
|
123 | 0 | ra_free(&ctx->ra); |
124 | 0 | } |
125 | | |
126 | | static const struct ra_swapchain_fns ra_gl_swapchain_fns; |
127 | | |
128 | | bool ra_gl_ctx_init(struct ra_ctx *ctx, GL *gl, struct ra_ctx_params params) |
129 | 0 | { |
130 | 0 | struct ra_swapchain *sw = ctx->swapchain = talloc_ptrtype(NULL, sw); |
131 | 0 | *sw = (struct ra_swapchain) { |
132 | 0 | .ctx = ctx, |
133 | 0 | }; |
134 | |
|
135 | 0 | struct priv *p = sw->priv = talloc_ptrtype(sw, p); |
136 | 0 | *p = (struct priv) { |
137 | 0 | .gl = gl, |
138 | 0 | .log = ctx->log, |
139 | 0 | .params = params, |
140 | 0 | .opts = mp_get_config_group(p, ctx->global, &opengl_conf), |
141 | 0 | .fns = ra_gl_swapchain_fns, |
142 | 0 | }; |
143 | |
|
144 | 0 | sw->fns = &p->fns; |
145 | |
|
146 | 0 | if (!gl->version && !gl->es) |
147 | 0 | return false; |
148 | | |
149 | 0 | if (gl->mpgl_caps & MPGL_CAP_SW) { |
150 | 0 | MP_WARN(p, "Suspected software renderer or indirect context.\n"); |
151 | 0 | if (ctx->opts.probing && !ctx->opts.allow_sw) |
152 | 0 | return false; |
153 | 0 | } |
154 | | |
155 | 0 | gl->debug_context = ctx->opts.debug; |
156 | |
|
157 | 0 | if (gl->SwapInterval) { |
158 | 0 | gl->SwapInterval(p->opts->swapinterval); |
159 | 0 | } else { |
160 | 0 | MP_VERBOSE(p, "GL_*_swap_control extension missing.\n"); |
161 | 0 | } |
162 | |
|
163 | 0 | ctx->ra = ra_create_gl(p->gl, ctx->log); |
164 | 0 | return !!ctx->ra; |
165 | 0 | } |
166 | | |
167 | | void ra_gl_ctx_resize(struct ra_swapchain *sw, int w, int h, int fbo) |
168 | 0 | { |
169 | 0 | struct priv *p = sw->priv; |
170 | 0 | if (p->main_fb == fbo && p->wrapped_fb && p->wrapped_fb->params.w == w |
171 | 0 | && p->wrapped_fb->params.h == h) |
172 | 0 | return; |
173 | | |
174 | 0 | if (p->wrapped_fb) |
175 | 0 | ra_tex_free(sw->ctx->ra, &p->wrapped_fb); |
176 | |
|
177 | 0 | p->main_fb = fbo; |
178 | 0 | p->wrapped_fb = ra_create_wrapped_fb(sw->ctx->ra, fbo, w, h); |
179 | 0 | } |
180 | | |
181 | | int ra_gl_ctx_color_depth(struct ra_swapchain *sw) |
182 | 0 | { |
183 | 0 | struct priv *p = sw->priv; |
184 | 0 | GL *gl = p->gl; |
185 | |
|
186 | 0 | if (!p->wrapped_fb) |
187 | 0 | return 0; |
188 | | |
189 | 0 | if ((gl->es < 300 && !gl->version) || !(gl->mpgl_caps & MPGL_CAP_FB)) |
190 | 0 | return 0; |
191 | | |
192 | 0 | gl->BindFramebuffer(GL_FRAMEBUFFER, p->main_fb); |
193 | |
|
194 | 0 | GLenum obj = gl->version ? GL_BACK_LEFT : GL_BACK; |
195 | 0 | if (p->main_fb) |
196 | 0 | obj = GL_COLOR_ATTACHMENT0; |
197 | |
|
198 | 0 | GLint depth_g = 0; |
199 | |
|
200 | 0 | gl->GetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, obj, |
201 | 0 | GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE, &depth_g); |
202 | |
|
203 | 0 | gl->BindFramebuffer(GL_FRAMEBUFFER, 0); |
204 | |
|
205 | 0 | return depth_g; |
206 | 0 | } |
207 | | |
208 | | bool ra_gl_ctx_start_frame(struct ra_swapchain *sw, struct ra_fbo *out_fbo) |
209 | 0 | { |
210 | 0 | struct priv *p = sw->priv; |
211 | |
|
212 | 0 | bool visible = true; |
213 | 0 | if (p->params.check_visible) |
214 | 0 | visible = p->params.check_visible(sw->ctx); |
215 | | |
216 | | // If out_fbo is NULL, this was called from vo_gpu_next. Bail out. |
217 | 0 | if (!out_fbo || !visible) |
218 | 0 | return visible; |
219 | | |
220 | 0 | *out_fbo = (struct ra_fbo) { |
221 | 0 | .tex = p->wrapped_fb, |
222 | 0 | .flip = !p->gl->flipped, // OpenGL FBs are normally flipped |
223 | 0 | }; |
224 | 0 | return true; |
225 | 0 | } |
226 | | |
227 | | bool ra_gl_ctx_submit_frame(struct ra_swapchain *sw, const struct vo_frame *frame) |
228 | 0 | { |
229 | 0 | struct priv *p = sw->priv; |
230 | 0 | GL *gl = p->gl; |
231 | |
|
232 | 0 | if (p->opts->use_glfinish) |
233 | 0 | gl->Finish(); |
234 | |
|
235 | 0 | if (gl->FenceSync) { |
236 | 0 | GLsync fence = gl->FenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
237 | 0 | if (fence) |
238 | 0 | MP_TARRAY_APPEND(p, p->vsync_fences, p->num_vsync_fences, fence); |
239 | 0 | } |
240 | |
|
241 | 0 | switch (p->opts->early_flush) { |
242 | 0 | case FLUSH_AUTO: |
243 | 0 | if (frame->display_synced) |
244 | 0 | break; |
245 | 0 | MP_FALLTHROUGH; |
246 | 0 | case FLUSH_YES: |
247 | 0 | gl->Flush(); |
248 | 0 | } |
249 | | |
250 | 0 | return true; |
251 | 0 | } |
252 | | |
253 | | static void check_pattern(struct priv *p, int item) |
254 | 0 | { |
255 | 0 | int expected = p->opts->vsync_pattern[p->last_pattern]; |
256 | 0 | if (item == expected) { |
257 | 0 | p->last_pattern++; |
258 | 0 | if (p->last_pattern >= 2) |
259 | 0 | p->last_pattern = 0; |
260 | 0 | p->matches++; |
261 | 0 | } else { |
262 | 0 | p->mismatches++; |
263 | 0 | MP_WARN(p, "wrong pattern, expected %d got %d (hit: %d, miss: %d)\n", |
264 | 0 | expected, item, p->matches, p->mismatches); |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | | void ra_gl_ctx_swap_buffers(struct ra_swapchain *sw) |
269 | 0 | { |
270 | 0 | struct priv *p = sw->priv; |
271 | 0 | GL *gl = p->gl; |
272 | |
|
273 | 0 | p->params.swap_buffers(sw->ctx); |
274 | 0 | p->frames_rendered++; |
275 | |
|
276 | 0 | if (p->frames_rendered > 5 && !sw->ctx->opts.debug) |
277 | 0 | ra_gl_set_debug(sw->ctx->ra, false); |
278 | |
|
279 | 0 | if ((p->opts->waitvsync || p->opts->vsync_pattern[0]) |
280 | 0 | && gl->GetVideoSync) |
281 | 0 | { |
282 | 0 | unsigned int n1 = 0, n2 = 0; |
283 | 0 | gl->GetVideoSync(&n1); |
284 | 0 | if (p->opts->waitvsync) |
285 | 0 | gl->WaitVideoSync(2, (n1 + 1) % 2, &n2); |
286 | 0 | int step = n1 - p->prev_sgi_sync_count; |
287 | 0 | p->prev_sgi_sync_count = n1; |
288 | 0 | MP_DBG(p, "Flip counts: %u->%u, step=%d\n", n1, n2, step); |
289 | 0 | if (p->opts->vsync_pattern[0]) |
290 | 0 | check_pattern(p, step); |
291 | 0 | } |
292 | |
|
293 | 0 | while (p->num_vsync_fences >= sw->ctx->vo->opts->swapchain_depth) { |
294 | 0 | gl->ClientWaitSync(p->vsync_fences[0], GL_SYNC_FLUSH_COMMANDS_BIT, 1e9); |
295 | 0 | gl->DeleteSync(p->vsync_fences[0]); |
296 | 0 | MP_TARRAY_REMOVE_AT(p->vsync_fences, p->num_vsync_fences, 0); |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | | static void ra_gl_ctx_get_vsync(struct ra_swapchain *sw, |
301 | | struct vo_vsync_info *info) |
302 | 0 | { |
303 | 0 | struct priv *p = sw->priv; |
304 | 0 | if (p->params.get_vsync) |
305 | 0 | p->params.get_vsync(sw->ctx, info); |
306 | 0 | } |
307 | | |
308 | | static const struct ra_swapchain_fns ra_gl_swapchain_fns = { |
309 | | .color_depth = ra_gl_ctx_color_depth, |
310 | | .start_frame = ra_gl_ctx_start_frame, |
311 | | .submit_frame = ra_gl_ctx_submit_frame, |
312 | | .swap_buffers = ra_gl_ctx_swap_buffers, |
313 | | .get_vsync = ra_gl_ctx_get_vsync, |
314 | | }; |