diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2018-08-09 14:03:33 +0200 |
---|---|---|
committer | Paul Lemire <paul.lemire@kdab.com> | 2018-08-14 04:25:04 +0000 |
commit | a488e8e37130afdbad7ffa4d0972d2708204b431 (patch) | |
tree | 244ff8f906628a481da5f91c410f493007ddd980 | |
parent | 5adb8f40f03ddeb6095ed741d60ec452c2f7ed47 (diff) |
GLTexture: simplify createOrUpdateGLFunction
By dividing code into function which make following the bigger picture
easier
Change-Id: I7c43178d5484b7d387f6ce7b9c36025d8e13c642
Reviewed-by: Mike Krus <mike.krus@kdab.com>
-rw-r--r-- | src/render/renderers/opengl/textures/gltexture.cpp | 128 | ||||
-rw-r--r-- | src/render/renderers/opengl/textures/gltexture_p.h | 2 |
2 files changed, 71 insertions, 59 deletions
diff --git a/src/render/renderers/opengl/textures/gltexture.cpp b/src/render/renderers/opengl/textures/gltexture.cpp index bb7e06e60..bff0eafee 100644 --- a/src/render/renderers/opengl/textures/gltexture.cpp +++ b/src/render/renderers/opengl/textures/gltexture.cpp @@ -105,6 +105,72 @@ void GLTexture::destroyGLTexture() destroyResources(); } +bool GLTexture::loadTextureDataFromGenerator() +{ + m_textureData = m_textureDataManager->getData(m_dataFunctor); + // if there is a texture generator, most properties will be defined by it + if (m_textureData) { + if (m_properties.target != QAbstractTexture::TargetAutomatic) + qWarning() << "[Qt3DRender::GLTexture] When a texture provides a generator, it's target is expected to be TargetAutomatic"; + + m_actualTarget = m_textureData->target(); + m_properties.width = m_textureData->width(); + m_properties.height = m_textureData->height(); + m_properties.depth = m_textureData->depth(); + m_properties.layers = m_textureData->layers(); + m_properties.format = m_textureData->format(); + + const QVector<QTextureImageDataPtr> imageData = m_textureData->imageData(); + + if (imageData.size() > 0) { + // Set the mips level based on the first image if autoMipMapGeneration is disabled + if (!m_properties.generateMipMaps) + m_properties.mipLevels = imageData.first()->mipLevels(); + } + } + return !m_textureData.isNull(); +} + +void GLTexture::loadTextureDataFromImages() +{ + int maxMipLevel = 0; + for (const Image &img : qAsConst(m_images)) { + const QTextureImageDataPtr imgData = m_textureImageDataManager->getData(img.generator); + // imgData may be null in the following cases: + // - Texture is created with TextureImages which have yet to be + // loaded (skybox where you don't yet know the path, source set by + // a property binding, queued connection ...) + // - TextureImage whose generator failed to return a valid data + // (invalid url, error opening file...) + if (imgData.isNull()) + continue; + + m_imageData.push_back(imgData); + maxMipLevel = qMax(maxMipLevel, img.mipLevel); + + // If the texture doesn't have a texture generator, we will + // derive some properties from the first TextureImage (layer=0, miplvl=0, face=0) + if (!m_textureData && img.layer == 0 && img.mipLevel == 0 && img.face == QAbstractTexture::CubeMapPositiveX) { + if (imgData->width() != -1 && imgData->height() != -1 && imgData->depth() != -1) { + m_properties.width = imgData->width(); + m_properties.height = imgData->height(); + m_properties.depth = imgData->depth(); + } + // Set the format of the texture if the texture format is set to Automatic + if (m_properties.format == QAbstractTexture::Automatic) { + m_properties.format = static_cast<QAbstractTexture::TextureFormat>(imgData->format()); + } + setDirtyFlag(Properties, true); + } + } + + // make sure the number of mip levels is set when there is no texture data generator + if (!m_dataFunctor) { + m_properties.mipLevels = maxMipLevel + 1; + setDirtyFlag(Properties, true); + } +} + GLTexture::TextureUpdateInfo GLTexture::createOrUpdateGLTexture() { QMutexLocker locker(&m_textureMutex); @@ -117,28 +183,8 @@ GLTexture::TextureUpdateInfo GLTexture::createOrUpdateGLTexture() // evaluate the texture data generator output // (this might change some property values) if (m_dataFunctor && !m_textureData) { - m_textureData = m_textureDataManager->getData(m_dataFunctor); - - // if there is a texture generator, most properties will be defined by it - if (m_textureData) { - if (m_properties.target != QAbstractTexture::TargetAutomatic) - qWarning() << "[Qt3DRender::GLTexture] When a texture provides a generator, it's target is expected to be TargetAutomatic"; - - m_actualTarget = m_textureData->target(); - m_properties.width = m_textureData->width(); - m_properties.height = m_textureData->height(); - m_properties.depth = m_textureData->depth(); - m_properties.layers = m_textureData->layers(); - m_properties.format = m_textureData->format(); - - const QVector<QTextureImageDataPtr> imageData = m_textureData->imageData(); - - if (imageData.size() > 0) { - // Set the mips level based on the first image if autoMipMapGeneration is disabled - if (!m_properties.generateMipMaps) - m_properties.mipLevels = imageData.first()->mipLevels(); - } - + const bool successfullyLoadedTextureData = loadTextureDataFromGenerator(); + if (successfullyLoadedTextureData) { setDirtyFlag(Properties, true); needUpload = true; } else { @@ -151,44 +197,8 @@ GLTexture::TextureUpdateInfo GLTexture::createOrUpdateGLTexture() // additional texture images may be defined through image data generators if (testDirtyFlag(TextureData)) { m_imageData.clear(); + loadTextureDataFromImages(); needUpload = true; - - int maxMipLevel = 0; - for (const Image &img : qAsConst(m_images)) { - const QTextureImageDataPtr imgData = m_textureImageDataManager->getData(img.generator); - // imgData may be null in the following cases: - // - Texture is created with TextureImages which have yet to be - // loaded (skybox where you don't yet know the path, source set by - // a property binding, queued connection ...) - // - TextureImage whose generator failed to return a valid data - // (invalid url, error opening file...) - if (imgData.isNull()) - continue; - - m_imageData.push_back(imgData); - maxMipLevel = qMax(maxMipLevel, img.mipLevel); - - // If the texture doesn't have a texture generator, we will - // derive some properties from the first TextureImage (layer=0, miplvl=0, face=0) - if (!m_textureData && img.layer == 0 && img.mipLevel == 0 && img.face == QAbstractTexture::CubeMapPositiveX) { - if (imgData->width() != -1 && imgData->height() != -1 && imgData->depth() != -1) { - m_properties.width = imgData->width(); - m_properties.height = imgData->height(); - m_properties.depth = imgData->depth(); - } - // Set the format of the texture if the texture format is set to Automatic - if (m_properties.format == QAbstractTexture::Automatic) { - m_properties.format = static_cast<QAbstractTexture::TextureFormat>(imgData->format()); - } - setDirtyFlag(Properties, true); - } - } - - // make sure the number of mip levels is set when there is no texture data generator - if (!m_dataFunctor) { - m_properties.mipLevels = maxMipLevel + 1; - setDirtyFlag(Properties, true); - } } // don't try to create the texture if the format was not set diff --git a/src/render/renderers/opengl/textures/gltexture_p.h b/src/render/renderers/opengl/textures/gltexture_p.h index e26fdf397..8f9d335df 100644 --- a/src/render/renderers/opengl/textures/gltexture_p.h +++ b/src/render/renderers/opengl/textures/gltexture_p.h @@ -227,6 +227,8 @@ private: } QOpenGLTexture *buildGLTexture(); + bool loadTextureDataFromGenerator(); + void loadTextureDataFromImages(); void uploadGLTextureData(); void updateGLTextureParameters(); void destroyResources(); |