diff options
Diffstat (limited to 'src/gui/opengl')
-rw-r--r-- | src/gui/opengl/qopenglengineshadermanager.cpp | 12 | ||||
-rw-r--r-- | src/gui/opengl/qopenglengineshadermanager_p.h | 6 | ||||
-rw-r--r-- | src/gui/opengl/qopenglengineshadersource_p.h | 16 | ||||
-rw-r--r-- | src/gui/opengl/qopenglfunctions.cpp | 5 | ||||
-rw-r--r-- | src/gui/opengl/qopenglfunctions.h | 3 | ||||
-rw-r--r-- | src/gui/opengl/qopenglpaintengine.cpp | 14 | ||||
-rw-r--r-- | src/gui/opengl/qopenglshaderprogram.cpp | 152 | ||||
-rw-r--r-- | src/gui/opengl/qopengltexture.cpp | 470 | ||||
-rw-r--r-- | src/gui/opengl/qopengltexture.h | 211 | ||||
-rw-r--r-- | src/gui/opengl/qopengltexture_p.h | 4 | ||||
-rw-r--r-- | src/gui/opengl/qopengltexturecache.cpp | 33 | ||||
-rw-r--r-- | src/gui/opengl/qopengltexturecache_p.h | 1 | ||||
-rw-r--r-- | src/gui/opengl/qopengltexturehelper.cpp | 5 | ||||
-rw-r--r-- | src/gui/opengl/qopenglvertexarrayobject.cpp | 5 | ||||
-rw-r--r-- | src/gui/opengl/qtriangulator.cpp | 6 |
15 files changed, 778 insertions, 165 deletions
diff --git a/src/gui/opengl/qopenglengineshadermanager.cpp b/src/gui/opengl/qopenglengineshadermanager.cpp index 5bdb2f35ac..93425b0b3a 100644 --- a/src/gui/opengl/qopenglengineshadermanager.cpp +++ b/src/gui/opengl/qopenglengineshadermanager.cpp @@ -154,6 +154,8 @@ QOpenGLEngineSharedShaders::QOpenGLEngineSharedShaders(QOpenGLContext* context) code[ImageSrcFragmentShader] = qopenglslImageSrcFragmentShader; code[ImageSrcWithPatternFragmentShader] = qopenglslImageSrcWithPatternFragmentShader; code[NonPremultipliedImageSrcFragmentShader] = qopenglslNonPremultipliedImageSrcFragmentShader; + code[GrayscaleImageSrcFragmentShader] = qopenglslGrayscaleImageSrcFragmentShader; + code[AlphaImageSrcFragmentShader] = qopenglslAlphaImageSrcFragmentShader; code[CustomImageSrcFragmentShader] = qopenglslCustomSrcFragmentShader; // Calls "customShader", which must be appended code[SolidBrushSrcFragmentShader] = qopenglslSolidBrushSrcFragmentShader; if (context->isOpenGLES()) @@ -700,6 +702,16 @@ bool QOpenGLEngineShaderManager::useCorrectShaderProg() requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; texCoords = true; break; + case QOpenGLEngineShaderManager::GrayscaleImageSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::GrayscaleImageSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; + case QOpenGLEngineShaderManager::AlphaImageSrc: + requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::AlphaImageSrcFragmentShader; + requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; + texCoords = true; + break; case QOpenGLEngineShaderManager::PatternSrc: requiredProgram.srcPixelFragShader = QOpenGLEngineSharedShaders::ImageSrcWithPatternFragmentShader; requiredProgram.positionVertexShader = QOpenGLEngineSharedShaders::PositionOnlyVertexShader; diff --git a/src/gui/opengl/qopenglengineshadermanager_p.h b/src/gui/opengl/qopenglengineshadermanager_p.h index 2ae0ae0866..56aa18cb6b 100644 --- a/src/gui/opengl/qopenglengineshadermanager_p.h +++ b/src/gui/opengl/qopenglengineshadermanager_p.h @@ -288,6 +288,8 @@ public: ImageSrcFragmentShader, ImageSrcWithPatternFragmentShader, NonPremultipliedImageSrcFragmentShader, + GrayscaleImageSrcFragmentShader, + AlphaImageSrcFragmentShader, CustomImageSrcFragmentShader, SolidBrushSrcFragmentShader, TextureBrushSrcFragmentShader, @@ -414,7 +416,9 @@ public: ImageSrc = Qt::TexturePattern+1, NonPremultipliedImageSrc = Qt::TexturePattern+2, PatternSrc = Qt::TexturePattern+3, - TextureSrcWithPattern = Qt::TexturePattern+4 + TextureSrcWithPattern = Qt::TexturePattern+4, + GrayscaleImageSrc = Qt::TexturePattern+5, + AlphaImageSrc = Qt::TexturePattern+6, }; enum Uniform { diff --git a/src/gui/opengl/qopenglengineshadersource_p.h b/src/gui/opengl/qopenglengineshadersource_p.h index bc81fe4c77..acda09a8ee 100644 --- a/src/gui/opengl/qopenglengineshadersource_p.h +++ b/src/gui/opengl/qopenglengineshadersource_p.h @@ -367,6 +367,22 @@ static const char* const qopenglslNonPremultipliedImageSrcFragmentShader = "\n\ return sample; \n\ }\n"; +static const char* const qopenglslGrayscaleImageSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return texture2D(imageTexture, textureCoords).rrra; \n\ + }\n"; + +static const char* const qopenglslAlphaImageSrcFragmentShader = "\n\ + varying highp vec2 textureCoords; \n\ + uniform sampler2D imageTexture; \n\ + lowp vec4 srcPixel() \n\ + { \n\ + return vec4(0, 0, 0, texture2D(imageTexture, textureCoords).r); \n\ + }\n"; + static const char* const qopenglslShockingPinkSrcFragmentShader = "\n\ lowp vec4 srcPixel() \n\ { \n\ diff --git a/src/gui/opengl/qopenglfunctions.cpp b/src/gui/opengl/qopenglfunctions.cpp index 44b56699df..67b978a570 100644 --- a/src/gui/opengl/qopenglfunctions.cpp +++ b/src/gui/opengl/qopenglfunctions.cpp @@ -173,6 +173,7 @@ QT_BEGIN_NAMESPACE \value NPOTTextures Non power of two textures are available. \value NPOTTextureRepeat Non power of two textures can use GL_REPEAT as wrap parameter. \value FixedFunctionPipeline The fixed function pipeline is available. + \value TextureRGFormats The GL_RED and GL_RG texture formats are available. */ // Hidden private fields for additional extension data. @@ -284,10 +285,12 @@ static int qt_gl_resolve_features() if (extensions.match("GL_OES_texture_npot")) features |= QOpenGLFunctions::NPOTTextures | QOpenGLFunctions::NPOTTextureRepeat; + if (ctx->format().majorVersion() >= 3 || extensions.match("GL_EXT_texture_rg")) + features |= QOpenGLFunctions::TextureRGFormats; return features; } else { // OpenGL - int features = 0; + int features = QOpenGLFunctions::TextureRGFormats; QSurfaceFormat format = QOpenGLContext::currentContext()->format(); QOpenGLExtensionMatcher extensions; diff --git a/src/gui/opengl/qopenglfunctions.h b/src/gui/opengl/qopenglfunctions.h index 479a280abe..85d255fe61 100644 --- a/src/gui/opengl/qopenglfunctions.h +++ b/src/gui/opengl/qopenglfunctions.h @@ -240,7 +240,8 @@ public: StencilSeparate = 0x0800, NPOTTextures = 0x1000, NPOTTextureRepeat = 0x2000, - FixedFunctionPipeline = 0x4000 + FixedFunctionPipeline = 0x4000, + TextureRGFormats = 0x8000 }; Q_DECLARE_FLAGS(OpenGLFeatures, OpenGLFeature) diff --git a/src/gui/opengl/qopenglpaintengine.cpp b/src/gui/opengl/qopenglpaintengine.cpp index c490726359..299d3da73d 100644 --- a/src/gui/opengl/qopenglpaintengine.cpp +++ b/src/gui/opengl/qopenglpaintengine.cpp @@ -1421,6 +1421,20 @@ void QOpenGL2PaintEngineEx::drawImage(const QRectF& dest, const QImage& image, c d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::NonPremultipliedImageSrc); bindOption = 0; break; + case QImage::Format_Alpha8: + if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::TextureRGFormats)) { + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::AlphaImageSrc); + bindOption = QOpenGLTextureCache::UseRedFor8BitBindOption; + } else + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + break; + case QImage::Format_Grayscale8: + if (ctx->functions()->hasOpenGLFeature(QOpenGLFunctions::TextureRGFormats)) { + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::GrayscaleImageSrc); + bindOption = QOpenGLTextureCache::UseRedFor8BitBindOption; + } else + d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); + break; default: d->shaderManager->setSrcPixelType(QOpenGLEngineShaderManager::ImageSrc); break; diff --git a/src/gui/opengl/qopenglshaderprogram.cpp b/src/gui/opengl/qopenglshaderprogram.cpp index 5ea7a10e0d..d64847510b 100644 --- a/src/gui/opengl/qopenglshaderprogram.cpp +++ b/src/gui/opengl/qopenglshaderprogram.cpp @@ -39,6 +39,7 @@ #include <QtCore/qfile.h> #include <QtCore/qvarlengtharray.h> #include <QtCore/qvector.h> +#include <QtCore/qregularexpression.h> #include <QtGui/qtransform.h> #include <QtGui/QColor> #include <QtGui/QSurfaceFormat> @@ -47,6 +48,8 @@ #include <QtGui/qopenglfunctions_4_0_core.h> #endif +#include <algorithm> + QT_BEGIN_NAMESPACE /*! @@ -401,6 +404,95 @@ static const char redefineHighp[] = "#endif\n"; #endif +struct QVersionDirectivePosition +{ + Q_DECL_CONSTEXPR QVersionDirectivePosition(int position = 0, int line = -1) + : position(position) + , line(line) + { + } + + Q_DECL_CONSTEXPR bool hasPosition() const + { + return position > 0; + } + + const int position; + const int line; +}; + +static QVersionDirectivePosition findVersionDirectivePosition(const char *source) +{ + Q_ASSERT(source); + + QString working = QString::fromUtf8(source); + + // According to the GLSL spec the #version directive must not be + // preceded by anything but whitespace and comments. + // In order to not get confused by #version directives within a + // multiline comment, we need to run a minimal preprocessor first. + enum { + Normal, + CommentStarting, + MultiLineComment, + SingleLineComment, + CommentEnding + } state = Normal; + + for (QChar *c = working.begin(); c != working.end(); ++c) { + switch (state) { + case Normal: + if (*c == QLatin1Char('/')) + state = CommentStarting; + break; + case CommentStarting: + if (*c == QLatin1Char('*')) + state = MultiLineComment; + else if (*c == QLatin1Char('/')) + state = SingleLineComment; + else + state = Normal; + break; + case MultiLineComment: + if (*c == QLatin1Char('*')) + state = CommentEnding; + else if (*c == QLatin1Char('#')) + *c = QLatin1Char('_'); + break; + case SingleLineComment: + if (*c == QLatin1Char('\n')) + state = Normal; + else if (*c == QLatin1Char('#')) + *c = QLatin1Char('_'); + break; + case CommentEnding: + if (*c == QLatin1Char('/')) { + state = Normal; + } else { + if (*c == QLatin1Char('#')) + *c = QLatin1Char('_'); + state = MultiLineComment; + } + break; + } + } + + // Search for #version directive + int splitPosition = 0; + int linePosition = 1; + + static const QRegularExpression pattern(QStringLiteral("^#\\s*version.*(\\n)?"), + QRegularExpression::MultilineOption + | QRegularExpression::OptimizeOnFirstUsageOption); + QRegularExpressionMatch match = pattern.match(working); + if (match.hasMatch()) { + splitPosition = match.capturedEnd(); + linePosition += int(std::count(working.begin(), working.begin() + splitPosition, QLatin1Char('\n'))); + } + + return QVersionDirectivePosition(splitPosition, linePosition); +} + /*! Sets the \a source code for this shader and compiles it. Returns \c true if the source was successfully compiled, false otherwise. @@ -410,26 +502,24 @@ static const char redefineHighp[] = bool QOpenGLShader::compileSourceCode(const char *source) { Q_D(QOpenGLShader); - if (d->shaderGuard && d->shaderGuard->id()) { - QVarLengthArray<const char *, 4> src; - QVarLengthArray<GLint, 4> srclen; - int headerLen = 0; - while (source && source[headerLen] == '#') { - // Skip #version and #extension directives at the start of - // the shader code. We need to insert the qualifierDefines - // and redefineHighp just after them. - if (qstrncmp(source + headerLen, "#version", 8) != 0 && - qstrncmp(source + headerLen, "#extension", 10) != 0) { - break; - } - while (source[headerLen] != '\0' && source[headerLen] != '\n') - ++headerLen; - if (source[headerLen] == '\n') - ++headerLen; - } - if (headerLen > 0) { - src.append(source); - srclen.append(GLint(headerLen)); + // This method breaks the shader code into two parts: + // 1. Up to and including an optional #version directive. + // 2. The rest. + // If a #version directive exists, qualifierDefines and redefineHighp + // are inserted after. Otherwise they are inserted right at the start. + // In both cases a #line directive is appended in order to compensate + // for line number changes in case of compiler errors. + + if (d->shaderGuard && d->shaderGuard->id() && source) { + const QVersionDirectivePosition versionDirectivePosition = findVersionDirectivePosition(source); + + QVarLengthArray<const char *, 5> sourceChunks; + QVarLengthArray<GLint, 5> sourceChunkLengths; + + if (versionDirectivePosition.hasPosition()) { + // Append source up to #version directive + sourceChunks.append(source); + sourceChunkLengths.append(GLint(versionDirectivePosition.position)); } // The precision qualifiers are useful on OpenGL/ES systems, @@ -442,20 +532,28 @@ bool QOpenGLShader::compileSourceCode(const char *source) || true #endif ) { - src.append(qualifierDefines); - srclen.append(GLint(sizeof(qualifierDefines) - 1)); + sourceChunks.append(qualifierDefines); + sourceChunkLengths.append(GLint(sizeof(qualifierDefines) - 1)); } #ifdef QOpenGL_REDEFINE_HIGHP if (d->shaderType == Fragment && !ctx_d->workaround_missingPrecisionQualifiers && QOpenGLContext::currentContext()->isOpenGLES()) { - src.append(redefineHighp); - srclen.append(GLint(sizeof(redefineHighp) - 1)); + sourceChunks.append(redefineHighp); + sourceChunkLengths.append(GLint(sizeof(redefineHighp) - 1)); } #endif - src.append(source + headerLen); - srclen.append(GLint(qstrlen(source + headerLen))); - d->glfuncs->glShaderSource(d->shaderGuard->id(), src.size(), src.data(), srclen.data()); + + // Append #line directive in order to compensate for text insertion + QByteArray lineDirective = QStringLiteral("#line %1\n").arg(versionDirectivePosition.line).toUtf8(); + sourceChunks.append(lineDirective.constData()); + sourceChunkLengths.append(GLint(lineDirective.length())); + + // Append rest of shader code + sourceChunks.append(source + versionDirectivePosition.position); + sourceChunkLengths.append(GLint(qstrlen(source + versionDirectivePosition.position))); + + d->glfuncs->glShaderSource(d->shaderGuard->id(), sourceChunks.size(), sourceChunks.data(), sourceChunkLengths.data()); return d->compile(this); } else { return false; diff --git a/src/gui/opengl/qopengltexture.cpp b/src/gui/opengl/qopengltexture.cpp index f6083b8cf9..8ba139d003 100644 --- a/src/gui/opengl/qopengltexture.cpp +++ b/src/gui/opengl/qopengltexture.cpp @@ -294,16 +294,166 @@ int QOpenGLTexturePrivate::evaluateMipLevels() const } } -void QOpenGLTexturePrivate::allocateStorage() +static bool isSizedTextureFormat(QOpenGLTexture::TextureFormat internalFormat) +{ + switch (internalFormat) { + case QOpenGLTexture::NoFormat: + return false; + + case QOpenGLTexture::R8_UNorm: + case QOpenGLTexture::RG8_UNorm: + case QOpenGLTexture::RGB8_UNorm: + case QOpenGLTexture::RGBA8_UNorm: + case QOpenGLTexture::R16_UNorm: + case QOpenGLTexture::RG16_UNorm: + case QOpenGLTexture::RGB16_UNorm: + case QOpenGLTexture::RGBA16_UNorm: + case QOpenGLTexture::R8_SNorm: + case QOpenGLTexture::RG8_SNorm: + case QOpenGLTexture::RGB8_SNorm: + case QOpenGLTexture::RGBA8_SNorm: + case QOpenGLTexture::R16_SNorm: + case QOpenGLTexture::RG16_SNorm: + case QOpenGLTexture::RGB16_SNorm: + case QOpenGLTexture::RGBA16_SNorm: + case QOpenGLTexture::R8U: + case QOpenGLTexture::RG8U: + case QOpenGLTexture::RGB8U: + case QOpenGLTexture::RGBA8U: + case QOpenGLTexture::R16U: + case QOpenGLTexture::RG16U: + case QOpenGLTexture::RGB16U: + case QOpenGLTexture::RGBA16U: + case QOpenGLTexture::R32U: + case QOpenGLTexture::RG32U: + case QOpenGLTexture::RGB32U: + case QOpenGLTexture::RGBA32U: + case QOpenGLTexture::R8I: + case QOpenGLTexture::RG8I: + case QOpenGLTexture::RGB8I: + case QOpenGLTexture::RGBA8I: + case QOpenGLTexture::R16I: + case QOpenGLTexture::RG16I: + case QOpenGLTexture::RGB16I: + case QOpenGLTexture::RGBA16I: + case QOpenGLTexture::R32I: + case QOpenGLTexture::RG32I: + case QOpenGLTexture::RGB32I: + case QOpenGLTexture::RGBA32I: + case QOpenGLTexture::R16F: + case QOpenGLTexture::RG16F: + case QOpenGLTexture::RGB16F: + case QOpenGLTexture::RGBA16F: + case QOpenGLTexture::R32F: + case QOpenGLTexture::RG32F: + case QOpenGLTexture::RGB32F: + case QOpenGLTexture::RGBA32F: + case QOpenGLTexture::RGB9E5: + case QOpenGLTexture::RG11B10F: + case QOpenGLTexture::RG3B2: + case QOpenGLTexture::R5G6B5: + case QOpenGLTexture::RGB5A1: + case QOpenGLTexture::RGBA4: + case QOpenGLTexture::RGB10A2: + + case QOpenGLTexture::D16: + case QOpenGLTexture::D24: + case QOpenGLTexture::D32: + case QOpenGLTexture::D32F: + + case QOpenGLTexture::D24S8: + case QOpenGLTexture::D32FS8X24: + + case QOpenGLTexture::S8: + + case QOpenGLTexture::RGB_DXT1: + case QOpenGLTexture::RGBA_DXT1: + case QOpenGLTexture::RGBA_DXT3: + case QOpenGLTexture::RGBA_DXT5: + case QOpenGLTexture::R_ATI1N_UNorm: + case QOpenGLTexture::R_ATI1N_SNorm: + case QOpenGLTexture::RG_ATI2N_UNorm: + case QOpenGLTexture::RG_ATI2N_SNorm: + case QOpenGLTexture::RGB_BP_UNSIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_SIGNED_FLOAT: + case QOpenGLTexture::RGB_BP_UNorm: + case QOpenGLTexture::SRGB8: + case QOpenGLTexture::SRGB8_Alpha8: + case QOpenGLTexture::SRGB_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT1: + case QOpenGLTexture::SRGB_Alpha_DXT3: + case QOpenGLTexture::SRGB_Alpha_DXT5: + case QOpenGLTexture::SRGB_BP_UNorm: + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + return true; + + case QOpenGLTexture::DepthFormat: + case QOpenGLTexture::AlphaFormat: + + case QOpenGLTexture::RGBFormat: + case QOpenGLTexture::RGBAFormat: + + case QOpenGLTexture::LuminanceFormat: + + case QOpenGLTexture::LuminanceAlphaFormat: + return false; + } + + Q_UNREACHABLE(); + return false; +} + +static bool isTextureTargetMultisample(QOpenGLTexture::Target target) +{ + switch (target) { + case QOpenGLTexture::Target1D: + case QOpenGLTexture::Target1DArray: + case QOpenGLTexture::Target2D: + case QOpenGLTexture::Target2DArray: + case QOpenGLTexture::Target3D: + case QOpenGLTexture::TargetCubeMap: + case QOpenGLTexture::TargetCubeMapArray: + return false; + + case QOpenGLTexture::Target2DMultisample: + case QOpenGLTexture::Target2DMultisampleArray: + return true; + + case QOpenGLTexture::TargetRectangle: + case QOpenGLTexture::TargetBuffer: + return false; + } + + Q_UNREACHABLE(); + return false; +} + +void QOpenGLTexturePrivate::allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType) { // Resolve the actual number of mipmap levels we can use mipLevels = evaluateMipLevels(); - // Use immutable storage whenever possible, falling back to mutable when not available - if (features.testFlag(QOpenGLTexture::ImmutableStorage)) + // Use immutable storage whenever possible, falling back to mutable + // Note that if multisample textures are not supported at all, we'll still fail into + // the mutable storage allocation + const bool useImmutableStorage = isSizedTextureFormat(format) + && (isTextureTargetMultisample(target) + ? features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage) + : features.testFlag(QOpenGLTexture::ImmutableStorage)); + + if (useImmutableStorage) allocateImmutableStorage(); else - allocateMutableStorage(); + allocateMutableStorage(pixelFormat, pixelType); } static QOpenGLTexture::PixelFormat pixelFormatCompatibleWithInternalFormat(QOpenGLTexture::TextureFormat internalFormat) @@ -313,59 +463,167 @@ static QOpenGLTexture::PixelFormat pixelFormatCompatibleWithInternalFormat(QOpen return QOpenGLTexture::NoSourceFormat; case QOpenGLTexture::R8_UNorm: + return QOpenGLTexture::Red; + case QOpenGLTexture::RG8_UNorm: + return QOpenGLTexture::RG; + case QOpenGLTexture::RGB8_UNorm: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGBA8_UNorm: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::R16_UNorm: + return QOpenGLTexture::Red; + case QOpenGLTexture::RG16_UNorm: + return QOpenGLTexture::RG; + case QOpenGLTexture::RGB16_UNorm: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGBA16_UNorm: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::R8_SNorm: + return QOpenGLTexture::Red; + case QOpenGLTexture::RG8_SNorm: + return QOpenGLTexture::RG; + case QOpenGLTexture::RGB8_SNorm: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGBA8_SNorm: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::R16_SNorm: + return QOpenGLTexture::Red; + case QOpenGLTexture::RG16_SNorm: + return QOpenGLTexture::RG; + case QOpenGLTexture::RGB16_SNorm: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGBA16_SNorm: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::R8U: + return QOpenGLTexture::Red_Integer; + case QOpenGLTexture::RG8U: + return QOpenGLTexture::RG_Integer; + case QOpenGLTexture::RGB8U: + return QOpenGLTexture::RGB_Integer; + case QOpenGLTexture::RGBA8U: + return QOpenGLTexture::RGBA_Integer; + case QOpenGLTexture::R16U: + return QOpenGLTexture::Red_Integer; + case QOpenGLTexture::RG16U: + return QOpenGLTexture::RG_Integer; + case QOpenGLTexture::RGB16U: + return QOpenGLTexture::RGB_Integer; + case QOpenGLTexture::RGBA16U: + return QOpenGLTexture::RGBA_Integer; + case QOpenGLTexture::R32U: + return QOpenGLTexture::Red_Integer; + case QOpenGLTexture::RG32U: + return QOpenGLTexture::RG_Integer; + case QOpenGLTexture::RGB32U: + return QOpenGLTexture::RGB_Integer; + case QOpenGLTexture::RGBA32U: + return QOpenGLTexture::RGBA_Integer; + case QOpenGLTexture::R8I: + return QOpenGLTexture::Red_Integer; + case QOpenGLTexture::RG8I: + return QOpenGLTexture::RG_Integer; + case QOpenGLTexture::RGB8I: + return QOpenGLTexture::RGB_Integer; + case QOpenGLTexture::RGBA8I: + return QOpenGLTexture::RGBA_Integer; + case QOpenGLTexture::R16I: + return QOpenGLTexture::Red_Integer; + case QOpenGLTexture::RG16I: + return QOpenGLTexture::RG_Integer; + case QOpenGLTexture::RGB16I: + return QOpenGLTexture::RGB_Integer; + case QOpenGLTexture::RGBA16I: + return QOpenGLTexture::RGBA_Integer; + case QOpenGLTexture::R32I: + return QOpenGLTexture::Red_Integer; + case QOpenGLTexture::RG32I: + return QOpenGLTexture::RG_Integer; + case QOpenGLTexture::RGB32I: + return QOpenGLTexture::RGB_Integer; + case QOpenGLTexture::RGBA32I: + return QOpenGLTexture::RGBA_Integer; + case QOpenGLTexture::R16F: + return QOpenGLTexture::Red; + case QOpenGLTexture::RG16F: + return QOpenGLTexture::RG; + case QOpenGLTexture::RGB16F: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGBA16F: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::R32F: + return QOpenGLTexture::Red; + case QOpenGLTexture::RG32F: + return QOpenGLTexture::RG; + case QOpenGLTexture::RGB32F: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGBA32F: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::RGB9E5: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RG11B10F: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RG3B2: + return QOpenGLTexture::RGB; + case QOpenGLTexture::R5G6B5: + return QOpenGLTexture::RGB; + case QOpenGLTexture::RGB5A1: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::RGBA4: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::RGB10A2: return QOpenGLTexture::RGBA; @@ -402,6 +660,26 @@ static QOpenGLTexture::PixelFormat pixelFormatCompatibleWithInternalFormat(QOpen case QOpenGLTexture::SRGB_BP_UNorm: return QOpenGLTexture::RGBA; + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + return QOpenGLTexture::Red; + + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + return QOpenGLTexture::RG; + + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + return QOpenGLTexture::RGB; + + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + return QOpenGLTexture::RGBA; + + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: + return QOpenGLTexture::RGBA; + case QOpenGLTexture::DepthFormat: return QOpenGLTexture::Depth; @@ -437,6 +715,8 @@ static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTe case QOpenGLTexture::RG16_UNorm: case QOpenGLTexture::RGB16_UNorm: case QOpenGLTexture::RGBA16_UNorm: + return QOpenGLTexture::UInt8; + case QOpenGLTexture::R8_SNorm: case QOpenGLTexture::RG8_SNorm: case QOpenGLTexture::RGB8_SNorm: @@ -445,6 +725,8 @@ static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTe case QOpenGLTexture::RG16_SNorm: case QOpenGLTexture::RGB16_SNorm: case QOpenGLTexture::RGBA16_SNorm: + return QOpenGLTexture::Int8; + case QOpenGLTexture::R8U: case QOpenGLTexture::RG8U: case QOpenGLTexture::RGB8U: @@ -457,6 +739,8 @@ static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTe case QOpenGLTexture::RG32U: case QOpenGLTexture::RGB32U: case QOpenGLTexture::RGBA32U: + return QOpenGLTexture::UInt8; + case QOpenGLTexture::R8I: case QOpenGLTexture::RG8I: case QOpenGLTexture::RGB8I: @@ -469,28 +753,50 @@ static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTe case QOpenGLTexture::RG32I: case QOpenGLTexture::RGB32I: case QOpenGLTexture::RGBA32I: + return QOpenGLTexture::Int8; + case QOpenGLTexture::R16F: case QOpenGLTexture::RG16F: case QOpenGLTexture::RGB16F: case QOpenGLTexture::RGBA16F: + return QOpenGLTexture::Float16; + case QOpenGLTexture::R32F: case QOpenGLTexture::RG32F: case QOpenGLTexture::RGB32F: case QOpenGLTexture::RGBA32F: + return QOpenGLTexture::Float32; + case QOpenGLTexture::RGB9E5: + return QOpenGLTexture::UInt16_RGB5A1_Rev; + case QOpenGLTexture::RG11B10F: + return QOpenGLTexture::UInt32_RG11B10F; + case QOpenGLTexture::RG3B2: + return QOpenGLTexture::UInt8_RG3B2; + case QOpenGLTexture::R5G6B5: + return QOpenGLTexture::UInt16_R5G6B5; + case QOpenGLTexture::RGB5A1: + return QOpenGLTexture::UInt16_RGB5A1; + case QOpenGLTexture::RGBA4: + return QOpenGLTexture::UInt16_RGBA4; + case QOpenGLTexture::RGB10A2: - return QOpenGLTexture::UInt8; + return QOpenGLTexture::UInt32_RGB10A2; case QOpenGLTexture::D16: + return QOpenGLTexture::UInt16; + case QOpenGLTexture::D24: case QOpenGLTexture::D32: + return QOpenGLTexture::UInt32; + case QOpenGLTexture::D32F: - return QOpenGLTexture::UInt8; + return QOpenGLTexture::Float32; case QOpenGLTexture::D24S8: return QOpenGLTexture::UInt32_D24S8; @@ -519,6 +825,16 @@ static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTe case QOpenGLTexture::SRGB_Alpha_DXT3: case QOpenGLTexture::SRGB_Alpha_DXT5: case QOpenGLTexture::SRGB_BP_UNorm: + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: return QOpenGLTexture::UInt8; case QOpenGLTexture::DepthFormat: @@ -534,11 +850,8 @@ static QOpenGLTexture::PixelType pixelTypeCompatibleWithInternalFormat(QOpenGLTe return QOpenGLTexture::NoPixelType; } -void QOpenGLTexturePrivate::allocateMutableStorage() +void QOpenGLTexturePrivate::allocateMutableStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType) { - const QOpenGLTexture::PixelFormat pixelFormat = pixelFormatCompatibleWithInternalFormat(format); - const QOpenGLTexture::PixelType pixelType = pixelTypeCompatibleWithInternalFormat(format); - switch (target) { case QOpenGLTexture::TargetBuffer: // Buffer textures get their storage from an external OpenGL buffer @@ -747,7 +1060,7 @@ void QOpenGLTexturePrivate::allocateImmutableStorage() break; case QOpenGLTexture::Target2DMultisample: - if (features.testFlag(QOpenGLTexture::TextureMultisample)) { + if (features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage)) { texFuncs->glTextureStorage2DMultisample(textureId, target, bindingTarget, samples, format, dimensions[0], dimensions[1], fixedSamplePositions); @@ -758,7 +1071,7 @@ void QOpenGLTexturePrivate::allocateImmutableStorage() break; case QOpenGLTexture::Target2DMultisampleArray: - if (features.testFlag(QOpenGLTexture::TextureMultisample) + if (features.testFlag(QOpenGLTexture::ImmutableMultisampleStorage) && features.testFlag(QOpenGLTexture::TextureArrays)) { texFuncs->glTextureStorage3DMultisample(textureId, target, bindingTarget, samples, format, dimensions[0], dimensions[1], layers, @@ -1525,6 +1838,16 @@ QOpenGLTexture *QOpenGLTexturePrivate::createTextureView(QOpenGLTexture::Target \value RGB_BP_UNSIGNED_FLOAT Equivalent to GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB \value RGB_BP_SIGNED_FLOAT Equivalent to GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB \value RGB_BP_UNorm Equivalent to GL_COMPRESSED_RGBA_BPTC_UNORM_ARB + \value R11_EAC_UNorm Equivalent to GL_COMPRESSED_R11_EAC + \value R11_EAC_SNorm Equivalent to GL_COMPRESSED_SIGNED_R11_EAC + \value RG11_EAC_UNorm Equivalent to GL_COMPRESSED_RG11_EAC + \value RG11_EAC_SNorm Equivalent to GL_COMPRESSED_SIGNED_RG11_EAC + \value RGB8_ETC2 Equivalent to GL_COMPRESSED_RGB8_ETC2 + \value SRGB8_ETC2 Equivalent to GL_COMPRESSED_SRGB8_ETC2 + \value RGB8_PunchThrough_Alpha1_ETC2 Equivalent to GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 + \value SRGB8_PunchThrough_Alpha1_ETC2 Equivalent to GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 + \value RGBA8_ETC2_EAC Equivalent to GL_COMPRESSED_RGBA8_ETC2_EAC + \value SRGB8_Alpha8_ETC2_EAC Equivalent to GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC \value SRGB8 Equivalent to GL_SRGB8 \value SRGB8_Alpha8 Equivalent to GL_SRGB8_ALPHA8 @@ -2050,6 +2373,16 @@ void QOpenGLTexture::setFormat(TextureFormat format) d->formatClass = FormatClass_S3TC_DXT5_RGBA; break; + case QOpenGLTexture::R11_EAC_UNorm: + case QOpenGLTexture::R11_EAC_SNorm: + case QOpenGLTexture::RG11_EAC_UNorm: + case QOpenGLTexture::RG11_EAC_SNorm: + case QOpenGLTexture::RGB8_ETC2: + case QOpenGLTexture::SRGB8_ETC2: + case QOpenGLTexture::RGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::SRGB8_PunchThrough_Alpha1_ETC2: + case QOpenGLTexture::RGBA8_ETC2_EAC: + case QOpenGLTexture::SRGB8_Alpha8_ETC2_EAC: case RG3B2: case R5G6B5: case RGB5A1: @@ -2436,17 +2769,58 @@ bool QOpenGLTexture::isFixedSamplePositions() const Once storage has been allocated for the texture then pixel data can be uploaded via one of the setData() overloads. + \note If immutable texture storage is not available, + then a default pixel format and pixel type will be used to + create the mutable storage. You can use the other + allocateStorage() overload to specify exactly the pixel format + and the pixel type to use when allocating mutable storage; + this is particulary useful under certain OpenGL ES implementations + (notably, OpenGL ES 2), where the pixel format and the pixel type + used at allocation time must perfectly match the format + and the type passed to any subsequent setData() call. + \sa isStorageAllocated(), setData() */ void QOpenGLTexture::allocateStorage() { Q_D(QOpenGLTexture); if (d->create()) { - d->allocateStorage(); + const QOpenGLTexture::PixelFormat pixelFormat = pixelFormatCompatibleWithInternalFormat(d->format); + const QOpenGLTexture::PixelType pixelType = pixelTypeCompatibleWithInternalFormat(d->format); + d->allocateStorage(pixelFormat, pixelType); } } /*! + \since 5.5 + + Allocates server-side storage for this texture object taking + into account, the format, dimensions, mipmap levels, array + layers and cubemap faces. + + Once storage has been allocated it is no longer possible to change + these properties. + + If supported QOpenGLTexture makes use of immutable texture + storage. However, if immutable texture storage is not available, + then the specified \a pixelFormat and \a pixelType will be used + to allocate mutable storage; note that in certain OpenGL implementations + (notably, OpenGL ES 2) they must perfectly match the format + and the type passed to any subsequent setData() call. + + Once storage has been allocated for the texture then pixel data + can be uploaded via one of the setData() overloads. + + \sa isStorageAllocated(), setData() +*/ +void QOpenGLTexture::allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType) +{ + Q_D(QOpenGLTexture); + if (d->create()) + d->allocateStorage(pixelFormat, pixelType); +} + +/*! Returns \c true if server-side storage for this texture as been allocated. @@ -2547,7 +2921,7 @@ void QOpenGLTexture::setData(int mipLevel, int layer, CubeMapFace cubeFace, Q_ASSERT(d->textureId); if (!isStorageAllocated()) { qWarning("Cannot set data on a texture that does not have storage allocated.\n" - "To do so call allocate() before this function"); + "To do so call allocateStorage() before this function"); return; } d->setData(mipLevel, layer, cubeFace, sourceFormat, sourceType, data, options); @@ -2605,7 +2979,7 @@ void QOpenGLTexture::setData(int mipLevel, int layer, CubeMapFace cubeFace, Q_ASSERT(d->textureId); if (!isStorageAllocated()) { qWarning("Cannot set data on a texture that does not have storage allocated.\n" - "To do so call allocate() before this function"); + "To do so call allocateStorage() before this function"); return; } d->setData(mipLevel, layer, cubeFace, sourceFormat, sourceType, data, options); @@ -2670,7 +3044,7 @@ void QOpenGLTexture::setData(const QImage& image, MipMapGeneration genMipMaps) setSize(image.width(), image.height()); setMipLevels(genMipMaps == GenerateMipMaps ? maximumMipLevels() : 1); - allocateStorage(); + allocateStorage(QOpenGLTexture::RGBA, QOpenGLTexture::UInt8); // Upload pixel data and generate mipmaps QImage glImage = image.convertToFormat(QImage::Format_RGBA8888); @@ -2697,7 +3071,7 @@ void QOpenGLTexture::setCompressedData(int mipLevel, int layer, CubeMapFace cube Q_ASSERT(d->textureId); if (!isStorageAllocated()) { qWarning("Cannot set data on a texture that does not have storage allocated.\n" - "To do so call allocate() before this function"); + "To do so call allocateStorage() before this function"); return; } d->setCompressedData(mipLevel, layer, cubeFace, dataSize, data, options); @@ -2748,7 +3122,7 @@ void QOpenGLTexture::setCompressedData(int mipLevel, int layer, CubeMapFace cube Q_ASSERT(d->textureId); if (!isStorageAllocated()) { qWarning("Cannot set data on a texture that does not have storage allocated.\n" - "To do so call allocate() before this function"); + "To do so call allocateStorage() before this function"); return; } d->setCompressedData(mipLevel, layer, cubeFace, dataSize, data, options); @@ -2826,7 +3200,8 @@ bool QOpenGLTexture::hasFeature(Feature feature) case ImmutableStorage: supported = f.version() >= qMakePair(4, 2) - || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage")); + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_storage")) + || ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_storage")); break; case TextureCubeMapArrays: @@ -2873,9 +3248,6 @@ bool QOpenGLTexture::hasFeature(Feature feature) case MaxFeatureFlag: break; - - default: - break; } } @@ -2883,21 +3255,59 @@ bool QOpenGLTexture::hasFeature(Feature feature) #endif { switch (feature) { + case ImmutableStorage: + supported = f.version() >= qMakePair(3, 0) + || ctx->hasExtension(QByteArrayLiteral("EXT_texture_storage")); + break; + + case ImmutableMultisampleStorage: + supported = f.version() >= qMakePair(3, 1); + break; + + case TextureRectangle: + break; + + case TextureArrays: + supported = f.version() >= qMakePair(3, 0); + break; + case Texture3D: - supported = ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_3D")); + supported = f.version() >= qMakePair(3, 0) + || ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_3D")); + break; + + case TextureMultisample: + supported = f.version() >= qMakePair(3, 1); break; + + case TextureBuffer: + break; + + case TextureCubeMapArrays: + break; + + case Swizzle: + supported = f.version() >= qMakePair(3, 0); + break; + + case StencilTexturing: + break; + case AnisotropicFiltering: supported = ctx->hasExtension(QByteArrayLiteral("GL_EXT_texture_filter_anisotropic")); break; + case NPOTTextures: case NPOTTextureRepeat: - supported = f.version() >= qMakePair(3,0); - if (!supported) { - supported = ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_npot")); - if (!supported) - supported = ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_non_power_of_two")); - } - default: + supported = f.version() >= qMakePair(3,0) + || ctx->hasExtension(QByteArrayLiteral("GL_OES_texture_npot")) + || ctx->hasExtension(QByteArrayLiteral("GL_ARB_texture_non_power_of_two")); + break; + + case Texture1D: + break; + + case MaxFeatureFlag: break; } } diff --git a/src/gui/opengl/qopengltexture.h b/src/gui/opengl/qopengltexture.h index a53b1730b7..0f999a8409 100644 --- a/src/gui/opengl/qopengltexture.h +++ b/src/gui/opengl/qopengltexture.h @@ -88,29 +88,6 @@ public: DontResetTextureUnit }; - explicit QOpenGLTexture(Target target); - explicit QOpenGLTexture(const QImage& image, MipMapGeneration genMipMaps = GenerateMipMaps); - ~QOpenGLTexture(); - - Target target() const; - - // Creation and destruction - bool create(); - void destroy(); - bool isCreated() const; - GLuint textureId() const; - - // Binding and releasing - void bind(); - void bind(uint unit, TextureUnitReset reset = DontResetTextureUnit); - void release(); - void release(uint unit, TextureUnitReset reset = DontResetTextureUnit); - - bool isBound() const; - bool isBound(uint unit); - static GLuint boundTextureId(BindingTarget target); - static GLuint boundTextureId(uint unit, BindingTarget target); - enum TextureFormat { NoFormat = 0, // GL_NONE @@ -209,6 +186,16 @@ public: RGB_BP_UNSIGNED_FLOAT = 0x8E8F, // GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB RGB_BP_SIGNED_FLOAT = 0x8E8E, // GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB RGB_BP_UNorm = 0x8E8C, // GL_COMPRESSED_RGBA_BPTC_UNORM_ARB + R11_EAC_UNorm = 0x9270, // GL_COMPRESSED_R11_EAC + R11_EAC_SNorm = 0x9271, // GL_COMPRESSED_SIGNED_R11_EAC + RG11_EAC_UNorm = 0x9272, // GL_COMPRESSED_RG11_EAC + RG11_EAC_SNorm = 0x9273, // GL_COMPRESSED_SIGNED_RG11_EAC + RGB8_ETC2 = 0x9274, // GL_COMPRESSED_RGB8_ETC2 + SRGB8_ETC2 = 0x9275, // GL_COMPRESSED_SRGB8_ETC2 + RGB8_PunchThrough_Alpha1_ETC2 = 0x9276, // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 + SRGB8_PunchThrough_Alpha1_ETC2 = 0x9277, // GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 + RGBA8_ETC2_EAC = 0x9278, // GL_COMPRESSED_RGBA8_ETC2_EAC + SRGB8_Alpha8_ETC2_EAC = 0x9279, // GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC // sRGB formats SRGB8 = 0x8C41, // GL_SRGB8 @@ -254,32 +241,6 @@ public: }; #endif - // Storage allocation - void setFormat(TextureFormat format); - TextureFormat format() const; - void setSize(int width, int height = 1, int depth = 1); - int width() const; - int height() const; - int depth() const; - void setMipLevels(int levels); - int mipLevels() const; - int maximumMipLevels() const; - void setLayers(int layers); - int layers() const; - int faces() const; - void setSamples(int samples); - int samples() const; - void setFixedSamplePositions(bool fixed); - bool isFixedSamplePositions() const; - void allocateStorage(); - bool isStorageAllocated() const; - - QOpenGLTexture *createTextureView(Target target, - TextureFormat viewFormat, - int minimumMipmapLevel, int maximumMipmapLevel, - int minimumLayer, int maximumLayer) const; - bool isTextureView() const; - enum CubeMapFace { CubeMapPositiveX = 0x8515, // GL_TEXTURE_CUBE_MAP_POSITIVE_X CubeMapNegativeX = 0x8516, // GL_TEXTURE_CUBE_MAP_NEGATIVE_X @@ -340,6 +301,107 @@ public: Float32_D32_UInt32_S8_X24 = 0x8DAD // GL_FLOAT_32_UNSIGNED_INT_24_8_REV }; + enum SwizzleComponent { + SwizzleRed = 0x8E42, // GL_TEXTURE_SWIZZLE_R + SwizzleGreen = 0x8E43, // GL_TEXTURE_SWIZZLE_G + SwizzleBlue = 0x8E44, // GL_TEXTURE_SWIZZLE_B + SwizzleAlpha = 0x8E45 // GL_TEXTURE_SWIZZLE_A + }; + + enum SwizzleValue { + RedValue = 0x1903, // GL_RED + GreenValue = 0x1904, // GL_GREEN + BlueValue = 0x1905, // GL_BLUE + AlphaValue = 0x1906, // GL_ALPHA + ZeroValue = 0, // GL_ZERO + OneValue = 1 // GL_ONE + }; + + enum WrapMode { + Repeat = 0x2901, // GL_REPEAT + MirroredRepeat = 0x8370, // GL_MIRRORED_REPEAT + ClampToEdge = 0x812F, // GL_CLAMP_TO_EDGE + ClampToBorder = 0x812D // GL_CLAMP_TO_BORDER + }; + + enum CoordinateDirection { + DirectionS = 0x2802, // GL_TEXTURE_WRAP_S + DirectionT = 0x2803, // GL_TEXTURE_WRAP_T + DirectionR = 0x8072 // GL_TEXTURE_WRAP_R + }; + + // Features + enum Feature { + ImmutableStorage = 0x00000001, + ImmutableMultisampleStorage = 0x00000002, + TextureRectangle = 0x00000004, + TextureArrays = 0x00000008, + Texture3D = 0x00000010, + TextureMultisample = 0x00000020, + TextureBuffer = 0x00000040, + TextureCubeMapArrays = 0x00000080, + Swizzle = 0x00000100, + StencilTexturing = 0x00000200, + AnisotropicFiltering = 0x00000400, + NPOTTextures = 0x00000800, + NPOTTextureRepeat = 0x00001000, + Texture1D = 0x00002000, +#ifndef Q_QDOC + MaxFeatureFlag = 0x00004000 +#endif + }; + Q_DECLARE_FLAGS(Features, Feature) + + explicit QOpenGLTexture(Target target); + explicit QOpenGLTexture(const QImage& image, MipMapGeneration genMipMaps = GenerateMipMaps); + ~QOpenGLTexture(); + + Target target() const; + + // Creation and destruction + bool create(); + void destroy(); + bool isCreated() const; + GLuint textureId() const; + + // Binding and releasing + void bind(); + void bind(uint unit, TextureUnitReset reset = DontResetTextureUnit); + void release(); + void release(uint unit, TextureUnitReset reset = DontResetTextureUnit); + + bool isBound() const; + bool isBound(uint unit); + static GLuint boundTextureId(BindingTarget target); + static GLuint boundTextureId(uint unit, BindingTarget target); + + // Storage allocation + void setFormat(TextureFormat format); + TextureFormat format() const; + void setSize(int width, int height = 1, int depth = 1); + int width() const; + int height() const; + int depth() const; + void setMipLevels(int levels); + int mipLevels() const; + int maximumMipLevels() const; + void setLayers(int layers); + int layers() const; + int faces() const; + void setSamples(int samples); + int samples() const; + void setFixedSamplePositions(bool fixed); + bool isFixedSamplePositions() const; + void allocateStorage(); + void allocateStorage(PixelFormat pixelFormat, PixelType pixelType); + bool isStorageAllocated() const; + + QOpenGLTexture *createTextureView(Target target, + TextureFormat viewFormat, + int minimumMipmapLevel, int maximumMipmapLevel, + int minimumLayer, int maximumLayer) const; + bool isTextureView() const; + // Pixel transfer // ### Qt 6: remove the non-const void * overloads QT_DEPRECATED void setData(int mipLevel, int layer, CubeMapFace cubeFace, @@ -393,28 +455,6 @@ public: // Helpful overloads for setData void setData(const QImage& image, MipMapGeneration genMipMaps = GenerateMipMaps); - // Features - enum Feature { - ImmutableStorage = 0x00000001, - ImmutableMultisampleStorage = 0x00000002, - TextureRectangle = 0x00000004, - TextureArrays = 0x00000008, - Texture3D = 0x00000010, - TextureMultisample = 0x00000020, - TextureBuffer = 0x00000040, - TextureCubeMapArrays = 0x00000080, - Swizzle = 0x00000100, - StencilTexturing = 0x00000200, - AnisotropicFiltering = 0x00000400, - NPOTTextures = 0x00000800, - NPOTTextureRepeat = 0x00001000, - Texture1D = 0x00002000, -#ifndef Q_QDOC - MaxFeatureFlag = 0x00004000 -#endif - }; - Q_DECLARE_FLAGS(Features, Feature) - static bool hasFeature(Feature feature); // Texture Parameters @@ -431,22 +471,6 @@ public: void generateMipMaps(); void generateMipMaps(int baseLevel, bool resetBaseLevel = true); - enum SwizzleComponent { - SwizzleRed = 0x8E42, // GL_TEXTURE_SWIZZLE_R - SwizzleGreen = 0x8E43, // GL_TEXTURE_SWIZZLE_G - SwizzleBlue = 0x8E44, // GL_TEXTURE_SWIZZLE_B - SwizzleAlpha = 0x8E45 // GL_TEXTURE_SWIZZLE_A - }; - - enum SwizzleValue { - RedValue = 0x1903, // GL_RED - GreenValue = 0x1904, // GL_GREEN - BlueValue = 0x1905, // GL_BLUE - AlphaValue = 0x1906, // GL_ALPHA - ZeroValue = 0, // GL_ZERO - OneValue = 1 // GL_ONE - }; - void setSwizzleMask(SwizzleComponent component, SwizzleValue value); void setSwizzleMask(SwizzleValue r, SwizzleValue g, SwizzleValue b, SwizzleValue a); @@ -480,19 +504,6 @@ public: void setMaximumAnisotropy(float anisotropy); float maximumAnisotropy() const; - enum WrapMode { - Repeat = 0x2901, // GL_REPEAT - MirroredRepeat = 0x8370, // GL_MIRRORED_REPEAT - ClampToEdge = 0x812F, // GL_CLAMP_TO_EDGE - ClampToBorder = 0x812D // GL_CLAMP_TO_BORDER - }; - - enum CoordinateDirection { - DirectionS = 0x2802, // GL_TEXTURE_WRAP_S - DirectionT = 0x2803, // GL_TEXTURE_WRAP_T - DirectionR = 0x8072 // GL_TEXTURE_WRAP_R - }; - void setWrapMode(WrapMode mode); void setWrapMode(CoordinateDirection direction, WrapMode mode); WrapMode wrapMode(CoordinateDirection direction) const; diff --git a/src/gui/opengl/qopengltexture_p.h b/src/gui/opengl/qopengltexture_p.h index b584d3ec8f..3b70010231 100644 --- a/src/gui/opengl/qopengltexture_p.h +++ b/src/gui/opengl/qopengltexture_p.h @@ -87,8 +87,8 @@ public: bool isBound() const; bool isBound(uint unit) const; - void allocateStorage(); - void allocateMutableStorage(); + void allocateStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType); + void allocateMutableStorage(QOpenGLTexture::PixelFormat pixelFormat, QOpenGLTexture::PixelType pixelType); void allocateImmutableStorage(); void setData(int mipLevel, int layer, QOpenGLTexture::CubeMapFace cubeFace, QOpenGLTexture::PixelFormat sourceFormat, QOpenGLTexture::PixelType sourceType, diff --git a/src/gui/opengl/qopengltexturecache.cpp b/src/gui/opengl/qopengltexturecache.cpp index 3b24e1f576..f85a26cd6e 100644 --- a/src/gui/opengl/qopengltexturecache.cpp +++ b/src/gui/opengl/qopengltexturecache.cpp @@ -41,6 +41,10 @@ QT_BEGIN_NAMESPACE +#ifndef GL_RED +#define GL_RED 0x1903 +#endif + #ifndef GL_RGB10_A2 #define GL_RGB10_A2 0x8059 #endif @@ -273,6 +277,35 @@ GLuint QOpenGLTextureCache::bindTexture(QOpenGLContext *context, qint64 key, con pixelType = GL_UNSIGNED_BYTE; targetFormat = image.format(); break; + case QImage::Format_Indexed8: + if (options & UseRedFor8BitBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } + break; + case QImage::Format_Alpha8: + if (options & UseRedFor8BitBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) { + externalFormat = internalFormat = GL_ALPHA; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } + break; + case QImage::Format_Grayscale8: + if (options & UseRedFor8BitBindOption) { + externalFormat = internalFormat = GL_RED; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } else if (context->isOpenGLES() || context->format().profile() != QSurfaceFormat::CoreProfile) { + externalFormat = internalFormat = GL_LUMINANCE; + pixelType = GL_UNSIGNED_BYTE; + targetFormat = image.format(); + } + break; default: break; } diff --git a/src/gui/opengl/qopengltexturecache_p.h b/src/gui/opengl/qopengltexturecache_p.h index f856054a85..45dc20e146 100644 --- a/src/gui/opengl/qopengltexturecache_p.h +++ b/src/gui/opengl/qopengltexturecache_p.h @@ -78,6 +78,7 @@ public: enum BindOption { NoBindOption = 0x0000, PremultipliedAlphaBindOption = 0x0001, + UseRedFor8BitBindOption = 0x0002, }; Q_DECLARE_FLAGS(BindOptions, BindOption) diff --git a/src/gui/opengl/qopengltexturehelper.cpp b/src/gui/opengl/qopengltexturehelper.cpp index 29cecf0ea8..1eeab64911 100644 --- a/src/gui/opengl/qopengltexturehelper.cpp +++ b/src/gui/opengl/qopengltexturehelper.cpp @@ -200,8 +200,13 @@ QOpenGLTextureHelper::QOpenGLTextureHelper(QOpenGLContext *context) TexImage2DMultisample = 0; // OpenGL 4.2 +#ifdef QT_OPENGL_ES_3 + TexStorage3D = ::glTexStorage3D; + TexStorage2D = ::glTexStorage2D; +#else TexStorage3D = 0; TexStorage2D = 0; +#endif TexStorage1D = 0; // OpenGL 4.3 diff --git a/src/gui/opengl/qopenglvertexarrayobject.cpp b/src/gui/opengl/qopenglvertexarrayobject.cpp index 5520dfed08..d8979fd98f 100644 --- a/src/gui/opengl/qopenglvertexarrayobject.cpp +++ b/src/gui/opengl/qopenglvertexarrayobject.cpp @@ -146,6 +146,11 @@ bool QOpenGLVertexArrayObjectPrivate::create() qWarning("QOpenGLVertexArrayObject::create() requires a valid current OpenGL context"); return false; } + + //Fail early, if context is the same as ctx, it means we have tried to initialize for this context and failed + if (ctx == context) + return false; + context = ctx; QObject::connect(context, SIGNAL(aboutToBeDestroyed()), q, SLOT(_q_contextAboutToBeDestroyed())); diff --git a/src/gui/opengl/qtriangulator.cpp b/src/gui/opengl/qtriangulator.cpp index f44c8adcf5..74b8f01985 100644 --- a/src/gui/opengl/qtriangulator.cpp +++ b/src/gui/opengl/qtriangulator.cpp @@ -441,8 +441,8 @@ T QMaxHeap<T>::pop() // Copied from qhash.cpp static const uchar prime_deltas[] = { - 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3, - 1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0 + 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 17, 27, 3, + 1, 29, 3, 21, 7, 17, 15, 9, 43, 35, 15, 0, 0, 0, 0, 0 }; // Copied from qhash.cpp @@ -457,7 +457,7 @@ static inline int primeForCount(int count) int high = 32; for (int i = 0; i < 5; ++i) { int mid = (high + low) / 2; - if (count >= 1 << mid) + if (uint(count) >= (1u << mid)) low = mid; else high = mid; |