kis_opengl.cpp 3.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  Copyright (c) 2007 Adrian Page <adrian@pagenet.plus.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

19
#include "opengl/kis_opengl.h"
Halla Rempt's avatar
Halla Rempt committed
20

21
#ifdef HAVE_OPENGL
Halla Rempt's avatar
Halla Rempt committed
22
#include <QOpenGLContext>
23 24
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
Halla Rempt's avatar
Halla Rempt committed
25 26 27
#endif

#include <QApplication>
28
#include <QOffscreenSurface>
Halla Rempt's avatar
Halla Rempt committed
29 30 31
#include <QDir>
#include <QFile>
#include <QDesktopServices>
32 33 34
#include <QMessageBox>

#include <klocale.h>
35

36
#include <kis_debug.h>
37
#include <kis_config.h>
38

39 40
namespace
{
Halla Rempt's avatar
Halla Rempt committed
41
#ifdef HAVE_OPENGL
42 43
    QOffscreenSurface *SharedSurface = 0;
    QOpenGLContext *SharedContext = 0;
Halla Rempt's avatar
Halla Rempt committed
44
#endif
45
    bool NeedsFenceWorkaround = false;
46
    int glVersion = 0;
47
}
48

Halla Rempt's avatar
Halla Rempt committed
49

50
void KisOpenGL::initialize()
51
{
Halla Rempt's avatar
Halla Rempt committed
52
#ifdef HAVE_OPENGL
53
    dbgUI << "OpenGL: initializing";
54

55
    KisConfig cfg;
56

57
    QSurfaceFormat format;
58
    format.setProfile(QSurfaceFormat::CompatibilityProfile);
59 60 61
    format.setDepthBufferSize(24);
    format.setStencilBufferSize(8);
    format.setVersion(3, 2);
62 63
    // if (cfg.disableDoubleBuffering()) {
    if (false) {
64
        format.setSwapBehavior(QSurfaceFormat::SingleBuffer);
65
    }
66 67 68
    else {
        format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
    }
69 70
    format.setSwapInterval(0); // Disable vertical refresh syncing
    QSurfaceFormat::setDefaultFormat(format);
71
#endif
72 73 74
}

int KisOpenGL::initializeContext(QOpenGLContext* s) {
75
#ifdef HAVE_OPENGL
76 77 78 79 80 81
    KisConfig cfg;
    dbgUI << "OpenGL: Opening new context";

    // Double check we were given the version we requested
    QSurfaceFormat format = s->format();
    glVersion = 100 * format.majorVersion() + format.minorVersion();
82

83 84 85 86
    if (!SharedSurface) {
        SharedSurface = new QOffscreenSurface();
        SharedSurface->setFormat(format);
        SharedSurface->create();
87
    }
88

89
    if (!SharedContext) {
90
        SharedContext = new QOpenGLContext;
91
        SharedContext->setFormat(format);
92
        SharedContext->setShareContext(s);
93 94
        SharedContext->create();
        SharedContext->makeCurrent(SharedSurface);
95
    }
96

Halla Rempt's avatar
Halla Rempt committed
97
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
98

99 100 101 102 103 104 105 106 107 108
    QFile log(QDesktopServices::storageLocation(QDesktopServices::TempLocation) + "/krita-opengl.txt");
    log.open(QFile::WriteOnly);
    QString vendor((const char*)f->glGetString(GL_VENDOR));
    log.write(vendor.toLatin1());
    log.write(", ");
    QString renderer((const char*)f->glGetString(GL_RENDERER));
    log.write(renderer.toLatin1());
    log.write(", ");
    QString version((const char*)f->glGetString(GL_VERSION));
    log.write(version.toLatin1());
109 110

    // Check if we have a bugged driver that needs fence workaround
111
    bool isOnX11 = false;
112
#ifdef HAVE_X11
113 114 115 116
    isOnX11 = true;
#endif

    if ((isOnX11 && renderer.startsWith("AMD")) || cfg.forceOpenGLFenceWorkaround()) {
117
        NeedsFenceWorkaround = true;
Stefano Bonicatti's avatar
Stefano Bonicatti committed
118
    }
Halla Rempt's avatar
Halla Rempt committed
119
#else
120
    Q_UNUSED(s);
Halla Rempt's avatar
Halla Rempt committed
121 122
    NeedsFenceWorkaround = false;
#endif
123
    return glVersion;
124 125
}

126 127 128 129 130
bool KisOpenGL::supportsFenceSync()
{
    // return glVersion > 302;
    return true;
}
131

132 133
bool KisOpenGL::supportsGLSL13()
{
134
    // return glVersion > 300;
135
    return true; // QGLFormat::openGLVersionFlags() & QGLFormat::OpenGL_Version_3_0; -- we force 3.2 for now, since that has glFenceSync
136 137
}

138 139 140 141 142
bool KisOpenGL::needsFenceWorkaround()
{
    return NeedsFenceWorkaround;
}

Halla Rempt's avatar
Halla Rempt committed
143 144 145 146 147
bool KisOpenGL::hasOpenGL()
{
    // QT5TODO: figure out runtime whether we have opengl...
    return true;
}