diff options
author | Kaj Grönholm <kaj.gronholm@qt.io> | 2020-09-03 09:28:02 +0300 |
---|---|---|
committer | Kaj Grönholm <kaj.gronholm@qt.io> | 2020-09-04 08:36:18 +0300 |
commit | 14d38c6e593a4ad33997f075beeda462db5a8528 (patch) | |
tree | da8100ee054da5b33277e7754f2e8257f50e8cec | |
parent | eb7ebc423d0819698537048679e5a9e857973025 (diff) |
Support automatic detection of texture file extension
When using setAttribute() it is now possible to give filename
without the extension and engine automatically locates the most
suitable existing file. If preferKTX is set, compressed texture
formats (ktx, astc, dds) are preferred over non-compressed ones
(e.g. png, jpg). The behavior is similar to Qt Quick Image
automatic detection of file extension.
Includes autotest to confirm the behavior.
Task-number: QT3DS-4059
Change-Id: I1d4ab0d86b34f5deb704d450e5c36e86afde7d3b
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
-rw-r--r-- | src/runtimerender/resourcemanager/Qt3DSRenderLoadedTexture.cpp | 48 | ||||
-rw-r--r-- | tests/auto/studio3d/q3dssurfaceviewer/tst_q3dssurfaceviewer.cpp | 25 |
2 files changed, 68 insertions, 5 deletions
diff --git a/src/runtimerender/resourcemanager/Qt3DSRenderLoadedTexture.cpp b/src/runtimerender/resourcemanager/Qt3DSRenderLoadedTexture.cpp index f9dc0cd..953489d 100644 --- a/src/runtimerender/resourcemanager/Qt3DSRenderLoadedTexture.cpp +++ b/src/runtimerender/resourcemanager/Qt3DSRenderLoadedTexture.cpp @@ -41,6 +41,7 @@ #include <QtQuick/qquickimageprovider.h> #include <QtGui/qimage.h> #include <QtGui/qopengltexture.h> +#include <QtCore/qfileinfo.h> #include <private/qnumeric_p.h> @@ -911,6 +912,41 @@ void SLoadedTexture::ReleaseDecompressedTexture(STextureData inImage) #define stricmp strcasecmp #endif +// Locate existing file by adding a supported suffix to localFile. +// If localFile already contains suffix or exiting file isn't found +// returns localFile unchanged. +static QString existingImageFileForPath(const QString &localFile, bool preferKTX) +{ + // Do nothing if given filepath exists or already has a suffix + QFileInfo fi(localFile); + if (!fi.suffix().isEmpty() || fi.exists()) + return localFile; + + // Lists of supported image formats in preferred-first order. + const QStringList compressedFormats {"ktx", "astc", "dds"}; + const QStringList nonCompressedFormats {"hdr", "png", "jpg", "jpeg", "gif"}; + + // Depending on preferKTX, check compressed formats before or after non-compressed ones. + QStringList supportedFormats = preferKTX ? compressedFormats + nonCompressedFormats + : nonCompressedFormats + compressedFormats; + + // Check first if file exists from resources as that + // is common and optimal for integrity case. + for (const QString &suffix : supportedFormats) { + QString tryFile = ":/" + localFile + "." + suffix; + if (QFileInfo::exists(tryFile)) + return tryFile; + } + // If not found, check still file path as-is + for (const QString &suffix : supportedFormats) { + QString tryFile = localFile + "." + suffix; + if (QFileInfo::exists(tryFile)) + return tryFile; + } + + return localFile; +} + SLoadedTexture *SLoadedTexture::Load(const QString &inPath, NVFoundationBase &inFoundation, IInputStreamFactory &inFactory, bool inFlipY, bool inFlipCompressed, @@ -923,9 +959,11 @@ SLoadedTexture *SLoadedTexture::Load(const QString &inPath, NVFoundationBase &in if (QUrl(inPath).scheme() == QLatin1String("image")) return LoadQImage(inPath, inFlipY, inFoundation, renderContextType, bufferManager); - // Check KTX path first - QString path = inPath; - QString ktxSource = inPath; + // If inPath doesn't have suffix, try to find most optimal existing one + QString path = existingImageFileForPath(inPath, preferKTX); + + // If preferKTX is true, always check KTX path first + QString ktxSource = path; if (preferKTX) { ktxSource = ktxSource.left(ktxSource.lastIndexOf(QLatin1Char('.'))); ktxSource.append(QLatin1String(".ktx")); @@ -935,10 +973,10 @@ SLoadedTexture *SLoadedTexture::Load(const QString &inPath, NVFoundationBase &in // We will get invalid error logs of files not found if we don't force quiet mode // If the file is actually missing, it will be logged later (loaded image is null) NVScopedRefCounted<IRefCountedInputStream> theStream( - inFactory.GetStreamForFile(preferKTX ? ktxSource : inPath, true)); + inFactory.GetStreamForFile(preferKTX ? ktxSource : path, true)); if (!theStream.mPtr) { if (preferKTX) - theStream = inFactory.GetStreamForFile(inPath, true); + theStream = inFactory.GetStreamForFile(path, true); else return nullptr; } else { diff --git a/tests/auto/studio3d/q3dssurfaceviewer/tst_q3dssurfaceviewer.cpp b/tests/auto/studio3d/q3dssurfaceviewer/tst_q3dssurfaceviewer.cpp index 7dcf254..7197b49 100644 --- a/tests/auto/studio3d/q3dssurfaceviewer/tst_q3dssurfaceviewer.cpp +++ b/tests/auto/studio3d/q3dssurfaceviewer/tst_q3dssurfaceviewer.cpp @@ -1566,6 +1566,31 @@ void tst_Q3DSSurfaceViewer::testTextureQuery() QCOMPARE(texsize, QSize(512,256)); QCOMPARE(format, GL_RGBA8); + + // Reset textureid and format + textureid = m_viewer->presentation()->textureId("Scene.Layer", texsize, format); + if (isWindow) + QCOMPARE(textureid, 6); + else + QCOMPARE(textureid, 7); + + // Test setting sourcepath without the file extension. + // Loading should succeed finding the .hdr file. + m_viewer->presentation()->setAttribute("Scene.Layer.Rectangle.Default.diffusemap", "sourcepath", + "maps/OpenfootageNET_garage-512"); + + QGuiApplication::processEvents(); + + // Changed to HDR texture, studio-internally RGB8E but texture query should map to GL RGBA8 + textureid = m_viewer->presentation()->textureId("Scene.Layer.Rectangle.Default.diffusemap", + texsize, format); + if (isWindow) + QCOMPARE(textureid, 9); + else + QCOMPARE(textureid, 10); + + QCOMPARE(texsize, QSize(512,256)); + QCOMPARE(format, GL_RGBA8); } QTEST_MAIN(tst_Q3DSSurfaceViewer) |