]> git.sesse.net Git - nageru/blob - shared/context.cpp
Make Futatabi fades apply white balance.
[nageru] / shared / context.cpp
1 #include <QGL>
2 #include <QOffscreenSurface>
3 #include <QOpenGLContext>
4 #include <QSurface>
5 #include <QSurfaceFormat>
6 #include <stdio.h>
7 #include <string>
8
9 QGLWidget *global_share_widget = nullptr;
10
11 using namespace std;
12
13 QSurface *create_surface()
14 {
15         QSurfaceFormat fmt;
16         fmt.setDepthBufferSize(0);
17         fmt.setStencilBufferSize(0);
18         fmt.setProfile(QSurfaceFormat::CoreProfile);
19         fmt.setMajorVersion(4);
20         fmt.setMinorVersion(5);
21         fmt.setSwapInterval(0);
22         QOffscreenSurface *surface = new QOffscreenSurface;
23         surface->setFormat(fmt);
24         surface->create();
25         if (!surface->isValid()) {
26                 fprintf(stderr, "ERROR: surface not valid!\n");
27                 abort();
28         }
29         return surface;
30 }
31
32 QSurface *create_surface(const QSurfaceFormat &format)
33 {
34         QOffscreenSurface *surface = new QOffscreenSurface;
35         surface->setFormat(format);
36         surface->create();
37         if (!surface->isValid()) {
38                 fprintf(stderr, "ERROR: surface not valid!\n");
39                 abort();
40         }
41         return surface;
42 }
43
44 QSurface *create_surface_with_same_format(const QSurface *surface)
45 {
46         return create_surface(surface->format());
47 }
48
49 QOpenGLContext *create_context(const QSurface *surface)
50 {
51         QOpenGLContext *context = new QOpenGLContext;
52         context->setShareContext(global_share_widget->context()->contextHandle());
53
54         // Qt has a bug (QTBUG-76299) where, when using EGL, the surface ignores
55         // the requested OpenGL context version and just becomes 2.0. Mesa honors
56         // this and gives us a 3.0 compatibility context, but then has a bug related
57         // to its shader cache (Mesa bug #110872) that causes spurious linker failures
58         // when we need to re-link a Movit shader in the same context. However,
59         // the surface itself doesn't use the OpenGL version in its format for anything,
60         // so we can just override it and get a proper context.
61         QSurfaceFormat fmt = surface->format();
62         fmt.setProfile(QSurfaceFormat::CoreProfile);
63         fmt.setMajorVersion(4);
64         fmt.setMinorVersion(5);
65         context->setFormat(fmt);
66
67         context->create();
68         return context;
69 }
70
71 bool make_current(QOpenGLContext *context, QSurface *surface)
72 {
73         return context->makeCurrent(surface);
74 }
75
76 void delete_context(QOpenGLContext *context)
77 {
78         delete context;
79 }