Debian Codegraph @

src/widget.cpp

raw
1 /*
2 * This file is part of Bino, a 3D video player.
3 *
4 * Copyright (C) 2022, 2023, 2024, 2025
5 * Martin Lambers <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <QGuiApplication>
22 #include <QMessageBox>
23 #include <QQuaternion>
24 #include <QtMath>
25
26 #include "widget.hpp"
27 #include "playlist.hpp"
28 #include "tools.hpp"
29 #include "log.hpp"
30
31 /* These might not be defined on Mac OS. Use crude replacements. */
32 #ifndef GL_MAX_FRAMEBUFFER_WIDTH
33 # define GL_MAX_FRAMEBUFFER_WIDTH GL_MAX_TEXTURE_SIZE
34 #endif
35 #ifndef GL_MAX_FRAMEBUFFER_HEIGHT
36 # define GL_MAX_FRAMEBUFFER_HEIGHT GL_MAX_TEXTURE_SIZE
37 #endif
38
39
40 static const QSize SizeBase(16, 9);
41
42 Widget::Widget(OutputMode outputMode, float surroundVerticalFOV, QWidget* parent) :
43 QOpenGLWidget(parent),
44 _sizeHint(0.5f * SizeBase),
45 _outputMode(outputMode),
46 _alternatingLastView(1),
47 _inSurroundMovement(false),
48 _surroundHorizontalAngleBase(0.0f),
49 _surroundVerticalAngleBase(0.0f),
50 _surroundHorizontalAngleCurrent(0.0f),
51 _surroundVerticalAngleCurrent(0.0f)
52 {
53 setSurroundVerticalFieldOfView(surroundVerticalFOV);
54 _surroundVerticalFOVDefault = _surroundVerticalFOV; // to make sure clamping was applied
55 setUpdateBehavior(QOpenGLWidget::PartialUpdate);
56 setMouseTracking(true);
57 setMinimumSize(8, 8);
58 QSize screenSize = QGuiApplication::primaryScreen()->availableSize();
59 QSize maxSize = 0.75f * screenSize;
60 _sizeHint = SizeBase.scaled(maxSize, Qt::KeepAspectRatio);
61 connect(Bino::instance(), &Bino::newVideoFrame, [=]() { update(); });
62 connect(Bino::instance(), &Bino::toggleFullscreen, [=]() { emit toggleFullscreen(); });
63 connect(Playlist::instance(), SIGNAL(mediaChanged(PlaylistEntry)), this, SLOT(mediaChanged(PlaylistEntry)));
64 setFocus();
65 }
66
67 bool Widget::isOpenGLStereo() const
68 {
69 return (context() ? context()->format().stereo() : false);
70 }
71
72 OutputMode Widget::outputMode() const
73 {
74 return _outputMode;
75 }
76
77 void Widget::setOutputMode(enum OutputMode mode)
78 {
79 _outputMode = mode;
80 }
81
82 void Widget::setSurroundVerticalFieldOfView(float vfov)
83 {
84 _surroundVerticalFOV = qBound(5.0f, vfov, 115.0f);
85 }
86
87 void Widget::resetSurroundView()
88 {
89 _surroundVerticalFOV = _surroundVerticalFOVDefault;
90 _surroundHorizontalAngleBase = 0.0f;
91 _surroundVerticalAngleBase = 0.0f;
92 _surroundHorizontalAngleCurrent = 0.0f;
93 _surroundVerticalAngleCurrent = 0.0f;
94 }
95
96 QSize Widget::sizeHint() const
97 {
98 return _sizeHint;
99 }
100
101 void Widget::initializeGL()
102 {
103 #ifndef Q_OS_WASM
104 bool contextIsOk = (context()->isValid() && context()->format().majorVersion() >= 3);
105 if (!contextIsOk) {
106 LOG_FATAL("%s", qPrintable(tr("Insufficient OpenGL capabilities.")));
107 QMessageBox::critical(this, tr("Error"), tr("Insufficient OpenGL capabilities."));
108 std::exit(1);
109 }
110 #endif
111 if (outputMode() == Output_OpenGL_Stereo && !isOpenGLStereo()) {
112 LOG_FATAL("%s", qPrintable(tr("OpenGL stereo mode is not available on this system.")));
113 QMessageBox::critical(this, tr("Error"), tr("OpenGL stereo mode is not available on this system."));
114 std::exit(1);
115 }
116
117 bool haveAnisotropicFiltering = checkTextureAnisotropicFilterAvailability();
118 initializeOpenGLFunctions();
119 bool isCoreProfile = (QOpenGLContext::currentContext()->format().profile() == QSurfaceFormat::CoreProfile);
120
121 QString variantString = (OpenGLType == OpenGL_Type_WebGL ? "WebGL" : OpenGLType == OpenGL_Type_OpenGLES ? "ES" : "Desktop");
122 if (OpenGLType == OpenGL_Type_Desktop)
123 variantString += isCoreProfile ? " core profile" : " compatibility profile";
124 GLint maxTexSize, maxFBWidth, maxFBHeight;
125 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
126 glGetIntegerv(GL_MAX_FRAMEBUFFER_WIDTH, &maxFBWidth);
127 glGetIntegerv(GL_MAX_FRAMEBUFFER_HEIGHT, &maxFBHeight);
128 LOG_INFO("OpenGL Variant: %s", qPrintable(variantString));
129 LOG_INFO("OpenGL Version: %s", getOpenGLString(this, GL_VERSION));
130 LOG_INFO("OpenGL GLSL Version: %s", getOpenGLString(this, GL_SHADING_LANGUAGE_VERSION));
131 LOG_INFO("OpenGL Vendor: %s", getOpenGLString(this, GL_VENDOR));
132 LOG_INFO("OpenGL Renderer: %s", getOpenGLString(this, GL_RENDERER));
133 LOG_INFO("OpenGL AnisoTexFilt: %s", haveAnisotropicFiltering ? "yes" : "no");
134 LOG_INFO("OpenGL Max Tex Size: %d", maxTexSize);
135 LOG_INFO("OpenGL Max FB Size: %dx%d", maxFBWidth, maxFBHeight);
136 LOG_INFO("OpenGL Stereo: %s", isOpenGLStereo() ? "yes" : "no");
137
138 // View textures
139 glGenTextures(2, _viewTex);
140 CHECK_GL();
141 for (int i = 0; i < 2; i++) {
142 glBindTexture(GL_TEXTURE_2D, _viewTex[i]);
143 unsigned char nullBytes[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
144 switch (OpenGLType) {
145 case OpenGL_Type_WebGL:
146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullBytes);
147 break;
148 case OpenGL_Type_OpenGLES:
149 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, 1, 1, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullBytes);
150 break;
151 case OpenGL_Type_Desktop:
152 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, 1, 1, 0, GL_RGBA, GL_UNSIGNED_SHORT, nullBytes);
153 break;
154 }
155 CHECK_GL();
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
158 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
159 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
160 if (haveAnisotropicFiltering)
161 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 4.0f);
162 CHECK_GL();
163 _viewTexWidth[i] = 1;
164 _viewTexHeight[i] = 1;
165 }
166
167 // Quad geometry
168 const float quadPositions[] = {
169 -1.0f, +1.0f, 0.0f,
170 +1.0f, +1.0f, 0.0f,
171 +1.0f, -1.0f, 0.0f,
172 -1.0f, -1.0f, 0.0f
173 };
174 const float quadTexCoords[] = {
175 0.0f, 1.0f,
176 1.0f, 1.0f,
177 1.0f, 0.0f,
178 0.0f, 0.0f
179 };
180 static const unsigned short quadIndices[] = {
181 0, 3, 1, 1, 3, 2
182 };
183 glGenVertexArrays(1, &_quadVao);
184 glBindVertexArray(_quadVao);
185 GLuint quadPositionBuf;
186 glGenBuffers(1, &quadPositionBuf);
187 glBindBuffer(GL_ARRAY_BUFFER, quadPositionBuf);
188 glBufferData(GL_ARRAY_BUFFER, sizeof(quadPositions), quadPositions, GL_STATIC_DRAW);
189 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
190 glEnableVertexAttribArray(0);
191 GLuint quadTexCoordBuf;
192 glGenBuffers(1, &quadTexCoordBuf);
193 glBindBuffer(GL_ARRAY_BUFFER, quadTexCoordBuf);
194 glBufferData(GL_ARRAY_BUFFER, sizeof(quadTexCoords), quadTexCoords, GL_STATIC_DRAW);
195 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
196 glEnableVertexAttribArray(1);
197 GLuint quadIndexBuf;
198 glGenBuffers(1, &quadIndexBuf);
199 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quadIndexBuf);
200 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quadIndices), quadIndices, GL_STATIC_DRAW);
201 CHECK_GL();
202
203 // Initialize Bino
204 Bino::instance()->initProcess();
205 }
206
207 void Widget::rebuildDisplayPrgIfNecessary(OutputMode outputMode)
208 {
209 if (outputMode == Output_Right)
210 outputMode = Output_Left; // these are handled specially; see shader
211 if (_displayPrg.isLinked() && _displayPrgOutputMode == outputMode)
212 return;
213
214 LOG_DEBUG("rebuilding display program for output mode %s", outputModeToString(outputMode));
215 QString vertexShaderSource = readFile(":src/shader-display.vert.glsl");
216 QString fragmentShaderSource = readFile(":src/shader-display.frag.glsl");
217 fragmentShaderSource.replace("$OUTPUT_MODE", QString::number(int(outputMode)));
218 if (OpenGLType != OpenGL_Type_Desktop) {
219 vertexShaderSource.prepend("#version 300 es\n");
220 fragmentShaderSource.prepend("#version 300 es\n"
221 "precision mediump float;\n");
222 } else {
223 vertexShaderSource.prepend("#version 330\n");
224 fragmentShaderSource.prepend("#version 330\n");
225 }
226 _displayPrg.removeAllShaders();
227 _displayPrg.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
228 _displayPrg.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
229 _displayPrg.link();
230 _displayPrgOutputMode = outputMode;
231 }
232
233 void Widget::paintGL()
234 {
235 // Support for HighDPI output
236 int width = _width * devicePixelRatioF();
237 int height = _height * devicePixelRatioF();
238
239 // Find out about the views we have
240 int viewCount, viewWidth, viewHeight;
241 float frameDisplayAspectRatio;
242 bool surround;
243 Bino::instance()->preRenderProcess(width, height, &viewCount, &viewWidth, &viewHeight, &frameDisplayAspectRatio, &surround);
244
245 // Adjust the stereo mode if necessary
246 bool frameIsStereo = (viewCount == 2);
247 OutputMode outputMode = _outputMode;
248 if (!frameIsStereo)
249 outputMode = Output_Left;
250 if (outputMode == Output_Left_Right || outputMode == Output_Right_Left)
251 frameDisplayAspectRatio *= 2.0f;
252 else if (outputMode == Output_Top_Bottom || outputMode == Output_Bottom_Top || outputMode == Output_HDMI_Frame_Pack)
253 frameDisplayAspectRatio *= 0.5f;
254 LOG_FIREHOSE("%s: %d views, %dx%d, %g, surround %s", Q_FUNC_INFO, viewCount, viewWidth, viewHeight, frameDisplayAspectRatio, surround ? "on" : "off");
255
256 // Fill the view texture(s) as needed
257 for (int v = 0; v <= 1; v++) {
258 bool needThisView = true;
259 switch (outputMode) {
260 case Output_Left:
261 needThisView = (v == 0);
262 break;
263 case Output_Right:
264 needThisView = (v == 1);
265 break;
266 case Output_Alternating:
267 needThisView = (v != _alternatingLastView);
268 break;
269 case Output_HDMI_Frame_Pack:
270 case Output_OpenGL_Stereo:
271 case Output_Left_Right:
272 case Output_Left_Right_Half:
273 case Output_Right_Left:
274 case Output_Right_Left_Half:
275 case Output_Top_Bottom:
276 case Output_Top_Bottom_Half:
277 case Output_Bottom_Top:
278 case Output_Bottom_Top_Half:
279 case Output_Even_Odd_Rows:
280 case Output_Even_Odd_Columns:
281 case Output_Checkerboard:
282 case Output_Red_Cyan_Dubois:
283 case Output_Red_Cyan_FullColor:
284 case Output_Red_Cyan_HalfColor:
285 case Output_Red_Cyan_Monochrome:
286 case Output_Green_Magenta_Dubois:
287 case Output_Green_Magenta_FullColor:
288 case Output_Green_Magenta_HalfColor:
289 case Output_Green_Magenta_Monochrome:
290 case Output_Amber_Blue_Dubois:
291 case Output_Amber_Blue_FullColor:
292 case Output_Amber_Blue_HalfColor:
293 case Output_Amber_Blue_Monochrome:
294 case Output_Red_Green_Monochrome:
295 case Output_Red_Blue_Monochrome:
296 break;
297 }
298 if (!needThisView)
299 continue;
300 // prepare view texture
301 glBindTexture(GL_TEXTURE_2D, _viewTex[v]);
302 if (_viewTexWidth[v] != viewWidth || _viewTexHeight[v] != viewHeight) {
303 switch (OpenGLType) {
304 case OpenGL_Type_WebGL:
305 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewWidth, viewHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
306 break;
307 case OpenGL_Type_OpenGLES:
308 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, viewWidth, viewHeight, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
309 break;
310 case OpenGL_Type_Desktop:
311 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16, viewWidth, viewHeight, 0, GL_RGBA, GL_UNSIGNED_SHORT, nullptr);
312 break;
313 }
314 _viewTexWidth[v] = viewWidth;
315 _viewTexHeight[v] = viewHeight;
316 }
317 // render view into view texture
318 LOG_FIREHOSE("%s: getting view %d for stereo mode %s", Q_FUNC_INFO, v, outputModeToString(outputMode));
319 QMatrix4x4 projectionMatrix;
320 QMatrix4x4 orientationMatrix;
321 QMatrix4x4 viewMatrix;
322 if (Bino::instance()->assumeSurroundMode() != Surround_Off) {
323 float verticalFieldOfView = qDegreesToRadians(_surroundVerticalFOV);
324 float aspectRatio = 2.0f; // always 2:1 for surround video!
325 float top = qTan(verticalFieldOfView * 0.5f);
326 float bottom = -top;
327 float right = top * aspectRatio;
328 float left = -right;
329 projectionMatrix.frustum(left, right, bottom, top, 1.0f, 100.0f);
330 QQuaternion orientation = QQuaternion::fromEulerAngles(
331 (_surroundVerticalAngleBase + _surroundVerticalAngleCurrent),
332 (_surroundHorizontalAngleBase + _surroundHorizontalAngleCurrent), 0.0f);
333 orientationMatrix.rotate(orientation.inverted());
334 }
335 Bino::instance()->render(
336 QVector3D(), QVector3D(), QVector3D(), QVector3D(), QVector3D(), QVector3D(),
337 projectionMatrix, orientationMatrix, viewMatrix, v, viewWidth, viewHeight, _viewTex[v]);
338 // generate mipmaps for the view texture
339 glBindTexture(GL_TEXTURE_2D, _viewTex[v]);
340 glGenerateMipmap(GL_TEXTURE_2D);
341 }
342
343 // Put the views on screen in the current mode
344 glViewport(0, 0, width, height);
345 glDisable(GL_DEPTH_TEST);
346 float relWidth = 1.0f;
347 float relHeight = 1.0f;
348 float screenAspectRatio = width / float(height);
349 if (outputMode == Output_HDMI_Frame_Pack)
350 screenAspectRatio = width / (height - height / 49.0f);
351 if (screenAspectRatio < frameDisplayAspectRatio)
352 relHeight = screenAspectRatio / frameDisplayAspectRatio;
353 else
354 relWidth = frameDisplayAspectRatio / screenAspectRatio;
355 rebuildDisplayPrgIfNecessary((outputMode == Output_OpenGL_Stereo || outputMode == Output_Alternating)
356 ? Output_Left /* also covers Output_Right */ : outputMode);
357 glUseProgram(_displayPrg.programId());
358 _displayPrg.setUniformValue("view0", 0);
359 _displayPrg.setUniformValue("view1", 1);
360 _displayPrg.setUniformValue("relativeWidth", relWidth);
361 _displayPrg.setUniformValue("relativeHeight", relHeight);
362 QPoint globalLowerLeft = mapToGlobal(QPoint(0, height - 1));
363 _displayPrg.setUniformValue("fragOffsetX", float(globalLowerLeft.x()));
364 _displayPrg.setUniformValue("fragOffsetY", float(screen()->geometry().height() - 1 - globalLowerLeft.y()));
365 LOG_FIREHOSE("lower left widget corner in screen coordinates: x=%d y=%d", globalLowerLeft.x(), screen()->geometry().height() - 1 - globalLowerLeft.y());
366 glActiveTexture(GL_TEXTURE0);
367 glBindTexture(GL_TEXTURE_2D, _viewTex[0]);
368 glActiveTexture(GL_TEXTURE1);
369 glBindTexture(GL_TEXTURE_2D, _viewTex[1]);
370 glBindVertexArray(_quadVao);
371 if (isOpenGLStereo()) {
372 LOG_FIREHOSE("widget draw mode: opengl stereo");
373 if (outputMode == Output_OpenGL_Stereo) {
374 if (currentTargetBuffer() == QOpenGLWidget::LeftBuffer) {
375 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject(QOpenGLWidget::LeftBuffer));
376 _displayPrg.setUniformValue("outputModeLeftRightView", 0);
377 } else {
378 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject(QOpenGLWidget::RightBuffer));
379 _displayPrg.setUniformValue("outputModeLeftRightView", 1);
380 }
381 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
382 } else {
383 if (outputMode == Output_Alternating)
384 outputMode = (_alternatingLastView == 0 ? Output_Right : Output_Left);
385 _displayPrg.setUniformValue("outputModeLeftRightView", outputMode == Output_Left ? 0 : 1);
386 if (currentTargetBuffer() == QOpenGLWidget::LeftBuffer) {
387 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject(QOpenGLWidget::LeftBuffer));
388 } else {
389 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject(QOpenGLWidget::RightBuffer));
390 }
391 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
392 }
393 } else {
394 LOG_FIREHOSE("widget draw mode: normal");
395 glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebufferObject());
396 if (outputMode == Output_Alternating)
397 outputMode = (_alternatingLastView == 0 ? Output_Right : Output_Left);
398 _displayPrg.setUniformValue("outputModeLeftRightView", outputMode == Output_Left ? 0 : 1);
399 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
400 }
401
402 // Update Output_Alternating
403 if (_outputMode == Output_Alternating && frameIsStereo) {
404 _alternatingLastView = (_alternatingLastView == 0 ? 1 : 0);
405 update();
406 }
407 }
408
409 void Widget::resizeGL(int w, int h)
410 {
411 _width = w;
412 _height = h;
413 }
414
415 void Widget::keyPressEvent(QKeyEvent* e)
416 {
417 Bino::instance()->keyPressEvent(e);
418 }
419
420 void Widget::mousePressEvent(QMouseEvent* e)
421 {
422 _inSurroundMovement = true;
423 _surroundMovementStart = e->position();
424 _surroundHorizontalAngleCurrent = 0.0f;
425 _surroundVerticalAngleCurrent = 0.0f;
426 }
427
428 void Widget::mouseReleaseEvent(QMouseEvent*)
429 {
430 _inSurroundMovement = false;
431 _surroundHorizontalAngleBase += _surroundHorizontalAngleCurrent;
432 _surroundVerticalAngleBase += _surroundVerticalAngleCurrent;
433 _surroundHorizontalAngleCurrent = 0.0f;
434 _surroundVerticalAngleCurrent = 0.0f;
435 }
436
437 void Widget::mouseMoveEvent(QMouseEvent* e)
438 {
439 // Support for HighDPI output
440 int width = _width * devicePixelRatioF();
441 int height = _height * devicePixelRatioF();
442
443 if (_inSurroundMovement) {
444 // position delta
445 QPointF posDelta = e->position() - _surroundMovementStart;
446 // horizontal angle delta
447 float dx = posDelta.x();
448 float xf = dx / width; // in [-1,+1]
449 _surroundHorizontalAngleCurrent = xf * 180.0f;
450 // vertical angle
451 float dy = posDelta.y();
452 float yf = dy / height; // in [-1,+1]
453 _surroundVerticalAngleCurrent = yf * 90.0f;
454 update();
455 }
456 }
457
458 void Widget::wheelEvent(QWheelEvent* e)
459 {
460 setSurroundVerticalFieldOfView(_surroundVerticalFOV - e->angleDelta().y() / 120.0f);
461 update();
462 }
463
464 void Widget::mediaChanged(PlaylistEntry)
465 {
466 _inSurroundMovement = false;
467 _surroundHorizontalAngleBase = 0.0f;
468 _surroundVerticalAngleBase = 0.0f;
469 _surroundHorizontalAngleCurrent = 0.0f;
470 _surroundVerticalAngleCurrent = 0.0f;
471 }