diff options
-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) |