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>
This commit is contained in:
Paul Lemire 2018-08-09 14:03:33 +02:00
parent 5adb8f40f0
commit a488e8e371
2 changed files with 71 additions and 59 deletions

View File

@ -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

View File

@ -227,6 +227,8 @@ private:
}
QOpenGLTexture *buildGLTexture();
bool loadTextureDataFromGenerator();
void loadTextureDataFromImages();
void uploadGLTextureData();
void updateGLTextureParameters();
void destroyResources();