From 10eb353126afa9239720adb59e36a75a1f29b459 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 22 May 2017 18:26:08 -0700 Subject: [PATCH 01/14] Ensure user never loses its running scripts --- interface/src/Application.cpp | 1 - libraries/script-engine/src/ScriptEngines.cpp | 101 +++++++++--------- libraries/script-engine/src/ScriptEngines.h | 1 - 3 files changed, 51 insertions(+), 52 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index bd66a96879..fc7ea6e692 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1622,7 +1622,6 @@ void Application::cleanupBeforeQuit() { // Clear any queued processing (I/O, FBX/OBJ/Texture parsing) QThreadPool::globalInstance()->clear(); - DependencyManager::get()->saveScripts(); DependencyManager::get()->shutdownScripting(); // stop all currently running global scripts DependencyManager::destroy(); diff --git a/libraries/script-engine/src/ScriptEngines.cpp b/libraries/script-engine/src/ScriptEngines.cpp index 2076657288..1cb0c117da 100644 --- a/libraries/script-engine/src/ScriptEngines.cpp +++ b/libraries/script-engine/src/ScriptEngines.cpp @@ -26,8 +26,12 @@ #define __LOC__ __FILE__ "(" __STR1__(__LINE__) ") : Warning Msg: " static const QString DESKTOP_LOCATION = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); - static const bool HIFI_SCRIPT_DEBUGGABLES { true }; +static const QString SETTINGS_KEY { "RunningScripts" }; +static const QUrl DEFAULT_SCRIPTS_LOCATION { "file:///~//defaultScripts.js" }; +// Using a QVariantList so this is human-readable in the settings file +static Setting::Handle runningScriptsHandle(SETTINGS_KEY, { QVariant(DEFAULT_SCRIPTS_LOCATION) }); + ScriptsModel& getScriptsModel() { static ScriptsModel scriptsModel; @@ -61,19 +65,6 @@ ScriptEngines::ScriptEngines(ScriptEngine::Context context) _scriptsModelFilter.setSourceModel(&_scriptsModel); _scriptsModelFilter.sort(0, Qt::AscendingOrder); _scriptsModelFilter.setDynamicSortFilter(true); - - static const int SCRIPT_SAVE_COUNTDOWN_INTERVAL_MS = 5000; - QTimer* scriptSaveTimer = new QTimer(this); - scriptSaveTimer->setSingleShot(true); - QMetaObject::Connection timerConnection = connect(scriptSaveTimer, &QTimer::timeout, [] { - DependencyManager::get()->saveScripts(); - }); - connect(qApp, &QCoreApplication::aboutToQuit, [=] { - disconnect(timerConnection); - }); - connect(this, &ScriptEngines::scriptCountChanged, this, [scriptSaveTimer] { - scriptSaveTimer->start(SCRIPT_SAVE_COUNTDOWN_INTERVAL_MS); - }, Qt::QueuedConnection); } QUrl normalizeScriptURL(const QUrl& rawScriptURL) { @@ -280,13 +271,8 @@ QVariantList ScriptEngines::getRunning() { return result; } - -static const QString SETTINGS_KEY = "RunningScripts"; - void ScriptEngines::loadDefaultScripts() { - QUrl defaultScriptsLoc = defaultScriptsLocation(); - defaultScriptsLoc.setPath(defaultScriptsLoc.path() + "/defaultScripts.js"); - loadScript(defaultScriptsLoc.toString()); + loadScript(DEFAULT_SCRIPTS_LOCATION); } void ScriptEngines::loadOneScript(const QString& scriptFilename) { @@ -294,17 +280,11 @@ void ScriptEngines::loadOneScript(const QString& scriptFilename) { } void ScriptEngines::loadScripts() { - // check first run... - Setting::Handle firstRun { Settings::firstRun, true }; - if (firstRun.get()) { - qCDebug(scriptengine) << "This is a first run..."; - // clear the scripts, and set out script to our default scripts - clearScripts(); - loadDefaultScripts(); - return; - } - - // loads all saved scripts + // START BACKWARD COMPATIBILITY CODE + // The following code makes sure people don't lose all their scripts + // This should be removed after a reasonable ammount of time went by + // Load old setting format if present + bool foundDeprecatedSetting = false; Settings settings; int size = settings.beginReadArray(SETTINGS_KEY); for (int i = 0; i < size; ++i) { @@ -312,35 +292,51 @@ void ScriptEngines::loadScripts() { QString string = settings.value("script").toString(); if (!string.isEmpty()) { loadScript(string); + foundDeprecatedSetting = true; } } settings.endArray(); -} + if (foundDeprecatedSetting) { + // Remove old settings found and return + settings.beginWriteArray(SETTINGS_KEY); + settings.remove(""); + settings.endArray(); + settings.remove(SETTINGS_KEY + "/size"); + return; + } + // END BACKWARD COMPATIBILITY CODE -void ScriptEngines::clearScripts() { - // clears all scripts from the settingsSettings settings; - Settings settings; - settings.beginWriteArray(SETTINGS_KEY); - settings.remove(""); - settings.endArray(); + // loads all saved scripts + auto runningScripts = runningScriptsHandle.get(); + for (auto script : runningScripts) { + auto string = script.toString(); + if (!string.isEmpty()) { + loadScript(string); + } + } } void ScriptEngines::saveScripts() { - // Saves all currently running user-loaded scripts - Settings settings; - settings.beginWriteArray(SETTINGS_KEY); - settings.remove(""); + // Do not save anything if we are in the process of shutting down + if (qApp->closingDown()) { + qWarning() << "Trying to save scripts during shutdown."; + return; + } - QStringList runningScripts = getRunningScripts(); - int i = 0; - for (auto it = runningScripts.begin(); it != runningScripts.end(); ++it) { - if (getScriptEngine(*it)->isUserLoaded()) { - settings.setArrayIndex(i); - settings.setValue("script", normalizeScriptURL(*it).toString()); - ++i; + // Saves all currently running user-loaded scripts + QVariantList list; + + { + QReadLocker lock(&_scriptEnginesHashLock); + for (auto it = _scriptEnginesHash.begin(); it != _scriptEnginesHash.end(); ++it) { + if (it.value() && it.value()->isUserLoaded()) { + auto normalizedUrl = normalizeScriptURL(it.key()); + list.append(normalizedUrl.toString()); + } } } - settings.endArray(); + + runningScriptsHandle.set(list); } QStringList ScriptEngines::getRunningScripts() { @@ -513,6 +509,9 @@ void ScriptEngines::onScriptEngineLoaded(const QString& rawScriptURL) { QUrl normalized = normalizeScriptURL(url); _scriptEnginesHash.insertMulti(normalized, scriptEngine); } + + // Update settings with new script + saveScripts(); emit scriptCountChanged(); } @@ -553,6 +552,8 @@ void ScriptEngines::onScriptFinished(const QString& rawScriptURL, ScriptEngine* } if (removed) { + // Update settings with removed script + saveScripts(); emit scriptCountChanged(); } } diff --git a/libraries/script-engine/src/ScriptEngines.h b/libraries/script-engine/src/ScriptEngines.h index 63b7e8f11c..5152c3952a 100644 --- a/libraries/script-engine/src/ScriptEngines.h +++ b/libraries/script-engine/src/ScriptEngines.h @@ -40,7 +40,6 @@ public: void loadScripts(); void saveScripts(); - void clearScripts(); QString getScriptsLocation() const; void loadDefaultScripts(); From 077e56a96fc98d0892485a25b1c94e2882ef9ff3 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Sat, 13 May 2017 18:07:35 -0700 Subject: [PATCH 02/14] Add KTX validation routines --- libraries/ktx/src/ktx/KTX.h | 169 ++++++------ libraries/ktx/src/ktx/Validation.cpp | 376 +++++++++++++++++++++++++++ tests/ktx/src/main.cpp | 60 ++++- 3 files changed, 522 insertions(+), 83 deletions(-) create mode 100644 libraries/ktx/src/ktx/Validation.cpp diff --git a/libraries/ktx/src/ktx/KTX.h b/libraries/ktx/src/ktx/KTX.h index 3f220abac3..6dc8e01131 100644 --- a/libraries/ktx/src/ktx/KTX.h +++ b/libraries/ktx/src/ktx/KTX.h @@ -180,105 +180,107 @@ namespace ktx { RGB16F = 0x881B, RGBA16F = 0x881A, - R32F = 0x822E, - RG32F = 0x8230, - RGB32F = 0x8815, - RGBA32F = 0x8814, + R32F = 0x822E, + RG32F = 0x8230, + RGB32F = 0x8815, + RGBA32F = 0x8814, - R11F_G11F_B10F = 0x8C3A, - RGB9_E5 = 0x8C3D, + R11F_G11F_B10F = 0x8C3A, + RGB9_E5 = 0x8C3D, - R8I = 0x8231, - R8UI = 0x8232, - R16I = 0x8233, - R16UI = 0x8234, - R32I = 0x8235, - R32UI = 0x8236, - RG8I = 0x8237, - RG8UI = 0x8238, - RG16I = 0x8239, - RG16UI = 0x823A, - RG32I = 0x823B, - RG32UI = 0x823C, + R8I = 0x8231, + R8UI = 0x8232, + R16I = 0x8233, + R16UI = 0x8234, + R32I = 0x8235, + R32UI = 0x8236, + RG8I = 0x8237, + RG8UI = 0x8238, + RG16I = 0x8239, + RG16UI = 0x823A, + RG32I = 0x823B, + RG32UI = 0x823C, - RGB8I = 0x8D8F, - RGB8UI = 0x8D7D, - RGB16I = 0x8D89, - RGB16UI = 0x8D77, + RGB8I = 0x8D8F, + RGB8UI = 0x8D7D, + RGB16I = 0x8D89, + RGB16UI = 0x8D77, - RGB32I = 0x8D83, - RGB32UI = 0x8D71, - RGBA8I = 0x8D8E, - RGBA8UI = 0x8D7C, - RGBA16I = 0x8D88, - RGBA16UI = 0x8D76, - RGBA32I = 0x8D82, + RGB32I = 0x8D83, + RGB32UI = 0x8D71, + RGBA8I = 0x8D8E, + RGBA8UI = 0x8D7C, + RGBA16I = 0x8D88, + RGBA16UI = 0x8D76, + RGBA32I = 0x8D82, - RGBA32UI = 0x8D70, + RGBA32UI = 0x8D70, // GL 4.4 Table 8.13 - DEPTH_COMPONENT16 = 0x81A5, - DEPTH_COMPONENT24 = 0x81A6, - DEPTH_COMPONENT32 = 0x81A7, + DEPTH_COMPONENT16 = 0x81A5, + DEPTH_COMPONENT24 = 0x81A6, + DEPTH_COMPONENT32 = 0x81A7, - DEPTH_COMPONENT32F = 0x8CAC, - DEPTH24_STENCIL8 = 0x88F0, - DEPTH32F_STENCIL8 = 0x8CAD, + DEPTH_COMPONENT32F = 0x8CAC, + DEPTH24_STENCIL8 = 0x88F0, + DEPTH32F_STENCIL8 = 0x8CAD, - STENCIL_INDEX1 = 0x8D46, - STENCIL_INDEX4 = 0x8D47, - STENCIL_INDEX8 = 0x8D48, - STENCIL_INDEX16 = 0x8D49, + STENCIL_INDEX1 = 0x8D46, + STENCIL_INDEX4 = 0x8D47, + STENCIL_INDEX8 = 0x8D48, + STENCIL_INDEX16 = 0x8D49, }; enum class GLInternalFormat_Compressed : uint32_t { // GL 4.4 Table 8.14 - COMPRESSED_RED = 0x8225, - COMPRESSED_RG = 0x8226, - COMPRESSED_RGB = 0x84ED, - COMPRESSED_RGBA = 0x84EE, + COMPRESSED_RED = 0x8225, + COMPRESSED_RG = 0x8226, + COMPRESSED_RGB = 0x84ED, + COMPRESSED_RGBA = 0x84EE, - COMPRESSED_SRGB = 0x8C48, - COMPRESSED_SRGB_ALPHA = 0x8C49, + COMPRESSED_SRGB = 0x8C48, + COMPRESSED_SRGB_ALPHA = 0x8C49, - COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C, - COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D, - COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E, - COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F, + COMPRESSED_ETC1_RGB8_OES = 0x8D64, - COMPRESSED_RED_RGTC1 = 0x8DBB, - COMPRESSED_SIGNED_RED_RGTC1 = 0x8DBC, - COMPRESSED_RG_RGTC2 = 0x8DBD, - COMPRESSED_SIGNED_RG_RGTC2 = 0x8DBE, + COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C, + COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D, + COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E, + COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F, - COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C, - COMPRESSED_SRGB_ALPHA_BPTC_UNORM = 0x8E8D, - COMPRESSED_RGB_BPTC_SIGNED_FLOAT = 0x8E8E, - COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = 0x8E8F, + COMPRESSED_RED_RGTC1 = 0x8DBB, + COMPRESSED_SIGNED_RED_RGTC1 = 0x8DBC, + COMPRESSED_RG_RGTC2 = 0x8DBD, + COMPRESSED_SIGNED_RG_RGTC2 = 0x8DBE, - COMPRESSED_RGB8_ETC2 = 0x9274, - COMPRESSED_SRGB8_ETC2 = 0x9275, - COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276, - COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277, - COMPRESSED_RGBA8_ETC2_EAC = 0x9278, - COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279, + COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C, + COMPRESSED_SRGB_ALPHA_BPTC_UNORM = 0x8E8D, + COMPRESSED_RGB_BPTC_SIGNED_FLOAT = 0x8E8E, + COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = 0x8E8F, - COMPRESSED_R11_EAC = 0x9270, - COMPRESSED_SIGNED_R11_EAC = 0x9271, - COMPRESSED_RG11_EAC = 0x9272, - COMPRESSED_SIGNED_RG11_EAC = 0x9273, + COMPRESSED_RGB8_ETC2 = 0x9274, + COMPRESSED_SRGB8_ETC2 = 0x9275, + COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276, + COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277, + COMPRESSED_RGBA8_ETC2_EAC = 0x9278, + COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279, + + COMPRESSED_R11_EAC = 0x9270, + COMPRESSED_SIGNED_R11_EAC = 0x9271, + COMPRESSED_RG11_EAC = 0x9272, + COMPRESSED_SIGNED_RG11_EAC = 0x9273, }; enum class GLBaseInternalFormat : uint32_t { // GL 4.4 Table 8.11 - DEPTH_COMPONENT = 0x1902, - DEPTH_STENCIL = 0x84F9, - RED = 0x1903, - RG = 0x8227, - RGB = 0x1907, - RGBA = 0x1908, - STENCIL_INDEX = 0x1901, + DEPTH_COMPONENT = 0x1902, + DEPTH_STENCIL = 0x84F9, + RED = 0x1903, + RG = 0x8227, + RGB = 0x1907, + RGBA = 0x1908, + STENCIL_INDEX = 0x1901, }; enum CubeMapFace { @@ -316,14 +318,14 @@ namespace ktx { Byte identifier[IDENTIFIER_LENGTH]; uint32_t endianness { ENDIAN_TEST }; - uint32_t glType; + uint32_t glType { static_cast(GLType::UNSIGNED_BYTE) }; uint32_t glTypeSize { 0 }; - uint32_t glFormat; - uint32_t glInternalFormat; - uint32_t glBaseInternalFormat; + uint32_t glFormat { static_cast(GLFormat::RGBA) }; + uint32_t glInternalFormat { static_cast(GLInternalFormat_Uncompressed::RGBA8) }; + uint32_t glBaseInternalFormat { static_cast(GLBaseInternalFormat::RGBA) }; uint32_t pixelWidth { 1 }; - uint32_t pixelHeight { 0 }; + uint32_t pixelHeight { 1 }; uint32_t pixelDepth { 0 }; uint32_t numberOfArrayElements { 0 }; uint32_t numberOfFaces { 1 }; @@ -386,8 +388,12 @@ namespace ktx { void setCube(uint32_t width, uint32_t height) { setDimensions(width, height, 0, 0, NUM_CUBEMAPFACES); } void setCubeArray(uint32_t width, uint32_t height, uint32_t numSlices) { setDimensions(width, height, 0, (numSlices > 0 ? numSlices : 1), NUM_CUBEMAPFACES); } + bool isValid() const; + + // Generate a set of image descriptors based on the assumption that the full mip pyramid is populated ImageDescriptors generateImageDescriptors() const; }; + static const size_t KTX_HEADER_SIZE = 64; static_assert(sizeof(Header) == KTX_HEADER_SIZE, "KTX Header size is static and should not change from the spec"); static const size_t KV_SIZE_WIDTH = 4; // Number of bytes for keyAndValueByteSize @@ -426,6 +432,7 @@ namespace ktx { const uint32_t _imageSize; const uint32_t _faceSize; const uint32_t _padding; + ImageHeader(bool cube, size_t imageOffset, uint32_t imageSize, uint32_t padding) : _numFaces(cube ? NUM_CUBEMAPFACES : 1), _imageOffset(imageOffset), @@ -486,6 +493,8 @@ namespace ktx { public: ~KTX(); + static bool validate(const StoragePointer& src); + // Define a KTX object manually to write it somewhere (in a file on disk?) // This path allocate the Storage where to store header, keyvalues and copy mips // Then COPY all the data diff --git a/libraries/ktx/src/ktx/Validation.cpp b/libraries/ktx/src/ktx/Validation.cpp new file mode 100644 index 0000000000..74af58d311 --- /dev/null +++ b/libraries/ktx/src/ktx/Validation.cpp @@ -0,0 +1,376 @@ +// +// Created by Bradley Austin Davis on 2017/05/13 +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "KTX.h" + +#include +#include + +using namespace ktx; + +static const std::unordered_set VALID_GL_TYPES { + (uint32_t)GLType::UNSIGNED_BYTE, + (uint32_t)GLType::BYTE, + (uint32_t)GLType::UNSIGNED_SHORT, + (uint32_t)GLType::SHORT, + (uint32_t)GLType::UNSIGNED_INT, + (uint32_t)GLType::INT, + (uint32_t)GLType::HALF_FLOAT, + (uint32_t)GLType::FLOAT, + (uint32_t)GLType::UNSIGNED_BYTE_3_3_2, + (uint32_t)GLType::UNSIGNED_BYTE_2_3_3_REV, + (uint32_t)GLType::UNSIGNED_SHORT_5_6_5, + (uint32_t)GLType::UNSIGNED_SHORT_5_6_5_REV, + (uint32_t)GLType::UNSIGNED_SHORT_4_4_4_4, + (uint32_t)GLType::UNSIGNED_SHORT_4_4_4_4_REV, + (uint32_t)GLType::UNSIGNED_SHORT_5_5_5_1, + (uint32_t)GLType::UNSIGNED_SHORT_1_5_5_5_REV, + (uint32_t)GLType::UNSIGNED_INT_8_8_8_8, + (uint32_t)GLType::UNSIGNED_INT_8_8_8_8_REV, + (uint32_t)GLType::UNSIGNED_INT_10_10_10_2, + (uint32_t)GLType::UNSIGNED_INT_2_10_10_10_REV, + (uint32_t)GLType::UNSIGNED_INT_24_8, + (uint32_t)GLType::UNSIGNED_INT_10F_11F_11F_REV, + (uint32_t)GLType::UNSIGNED_INT_5_9_9_9_REV, + (uint32_t)GLType::FLOAT_32_UNSIGNED_INT_24_8_REV, +}; + +static const std::unordered_set VALID_GL_FORMATS { + (uint32_t)GLFormat::STENCIL_INDEX, + (uint32_t)GLFormat::DEPTH_COMPONENT, + (uint32_t)GLFormat::DEPTH_STENCIL, + (uint32_t)GLFormat::RED, + (uint32_t)GLFormat::GREEN, + (uint32_t)GLFormat::BLUE, + (uint32_t)GLFormat::RG, + (uint32_t)GLFormat::RGB, + (uint32_t)GLFormat::RGBA, + (uint32_t)GLFormat::BGR, + (uint32_t)GLFormat::BGRA, + (uint32_t)GLFormat::RG_INTEGER, + (uint32_t)GLFormat::RED_INTEGER, + (uint32_t)GLFormat::GREEN_INTEGER, + (uint32_t)GLFormat::BLUE_INTEGER, + (uint32_t)GLFormat::RGB_INTEGER, + (uint32_t)GLFormat::RGBA_INTEGER, + (uint32_t)GLFormat::BGR_INTEGER, + (uint32_t)GLFormat::BGRA_INTEGER, +}; + +static const std::unordered_set VALID_GL_INTERNAL_FORMATS { + (uint32_t)GLInternalFormat_Uncompressed::R8, + (uint32_t)GLInternalFormat_Uncompressed::R8_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::R16, + (uint32_t)GLInternalFormat_Uncompressed::R16_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::RG8, + (uint32_t)GLInternalFormat_Uncompressed::RG8_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::RG16, + (uint32_t)GLInternalFormat_Uncompressed::RG16_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::R3_G3_B2, + (uint32_t)GLInternalFormat_Uncompressed::RGB4, + (uint32_t)GLInternalFormat_Uncompressed::RGB5, + (uint32_t)GLInternalFormat_Uncompressed::RGB565, + (uint32_t)GLInternalFormat_Uncompressed::RGB8, + (uint32_t)GLInternalFormat_Uncompressed::RGB8_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::RGB10, + (uint32_t)GLInternalFormat_Uncompressed::RGB12, + (uint32_t)GLInternalFormat_Uncompressed::RGB16, + (uint32_t)GLInternalFormat_Uncompressed::RGB16_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::RGBA2, + (uint32_t)GLInternalFormat_Uncompressed::RGBA4, + (uint32_t)GLInternalFormat_Uncompressed::RGB5_A1, + (uint32_t)GLInternalFormat_Uncompressed::RGBA8, + (uint32_t)GLInternalFormat_Uncompressed::RGBA8_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::RGB10_A2, + (uint32_t)GLInternalFormat_Uncompressed::RGB10_A2UI, + (uint32_t)GLInternalFormat_Uncompressed::RGBA12, + (uint32_t)GLInternalFormat_Uncompressed::RGBA16, + (uint32_t)GLInternalFormat_Uncompressed::RGBA16_SNORM, + (uint32_t)GLInternalFormat_Uncompressed::SRGB8, + (uint32_t)GLInternalFormat_Uncompressed::SRGB8_ALPHA8, + (uint32_t)GLInternalFormat_Uncompressed::R16F, + (uint32_t)GLInternalFormat_Uncompressed::RG16F, + (uint32_t)GLInternalFormat_Uncompressed::RGB16F, + (uint32_t)GLInternalFormat_Uncompressed::RGBA16F, + (uint32_t)GLInternalFormat_Uncompressed::R32F, + (uint32_t)GLInternalFormat_Uncompressed::RG32F, + (uint32_t)GLInternalFormat_Uncompressed::RGBA32F, + (uint32_t)GLInternalFormat_Uncompressed::R11F_G11F_B10F, + (uint32_t)GLInternalFormat_Uncompressed::RGB9_E5, + (uint32_t)GLInternalFormat_Uncompressed::R8I, + (uint32_t)GLInternalFormat_Uncompressed::R8UI, + (uint32_t)GLInternalFormat_Uncompressed::R16I, + (uint32_t)GLInternalFormat_Uncompressed::R16UI, + (uint32_t)GLInternalFormat_Uncompressed::R32I, + (uint32_t)GLInternalFormat_Uncompressed::R32UI, + (uint32_t)GLInternalFormat_Uncompressed::RG8I, + (uint32_t)GLInternalFormat_Uncompressed::RG8UI, + (uint32_t)GLInternalFormat_Uncompressed::RG16I, + (uint32_t)GLInternalFormat_Uncompressed::RG16UI, + (uint32_t)GLInternalFormat_Uncompressed::RG32I, + (uint32_t)GLInternalFormat_Uncompressed::RG32UI, + (uint32_t)GLInternalFormat_Uncompressed::RGB8I, + (uint32_t)GLInternalFormat_Uncompressed::RGB8UI, + (uint32_t)GLInternalFormat_Uncompressed::RGB16I, + (uint32_t)GLInternalFormat_Uncompressed::RGB16UI, + (uint32_t)GLInternalFormat_Uncompressed::RGB32I, + (uint32_t)GLInternalFormat_Uncompressed::RGB32UI, + (uint32_t)GLInternalFormat_Uncompressed::RGBA8I, + (uint32_t)GLInternalFormat_Uncompressed::RGBA8UI, + (uint32_t)GLInternalFormat_Uncompressed::RGBA16I, + (uint32_t)GLInternalFormat_Uncompressed::RGBA16UI, + (uint32_t)GLInternalFormat_Uncompressed::RGBA32I, + (uint32_t)GLInternalFormat_Uncompressed::RGBA32UI, + (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT16, + (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT24, + (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT32, + (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT32F, + (uint32_t)GLInternalFormat_Uncompressed::DEPTH24_STENCIL8, + (uint32_t)GLInternalFormat_Uncompressed::DEPTH32F_STENCIL8, + (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX1, + (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX4, + (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX8, + (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX16, +}; + +static const std::unordered_set VALID_GL_INTERNAL_COMPRESSED_FORMATS { + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RED, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RG, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGBA, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_ETC1_RGB8_OES, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_S3TC_DXT1_EXT, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RED_RGTC1, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_RED_RGTC1, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RG_RGTC2, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_RG_RGTC2, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGBA_BPTC_UNORM, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB_BPTC_SIGNED_FLOAT, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB8_ETC2, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB8_ETC2, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGBA8_ETC2_EAC, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_R11_EAC, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_R11_EAC, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RG11_EAC, + (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_RG11_EAC, +}; + +static const std::unordered_set VALID_GL_BASE_INTERNAL_FORMATS { + (uint32_t)GLBaseInternalFormat::DEPTH_COMPONENT, + (uint32_t)GLBaseInternalFormat::DEPTH_STENCIL, + (uint32_t)GLBaseInternalFormat::RED, + (uint32_t)GLBaseInternalFormat::RG, + (uint32_t)GLBaseInternalFormat::RGB, + (uint32_t)GLBaseInternalFormat::RGBA, + (uint32_t)GLBaseInternalFormat::STENCIL_INDEX, +}; + +bool Header::isValid() const { + if (0 != memcmp(identifier, IDENTIFIER.data(), IDENTIFIER_LENGTH)) { + qDebug() << "Invalid header identifier"; + return false; + } + + if (endianness != ENDIAN_TEST && endianness != REVERSE_ENDIAN_TEST) { + qDebug("Invalid endian marker 0x%x", endianness); + return false; + } + + // + // GL enum validity + // + if (VALID_GL_BASE_INTERNAL_FORMATS.count(glBaseInternalFormat) != 1) { + qDebug("Invalid base internal format 0x%x", glBaseInternalFormat); + return false; + } + + if (glFormat == (uint32_t)GLFormat::COMPRESSED_FORMAT && glType == (uint32_t)GLType::COMPRESSED_TYPE) { + if (VALID_GL_INTERNAL_COMPRESSED_FORMATS.count(glInternalFormat) != 1) { + qDebug("Invalid compressed internal format 0x%x", glInternalFormat); + return false; + } + } else { + if (VALID_GL_TYPES.count(glType) != 1) { + qDebug("Invalid type 0x%x", glType); + return false; + } + + if (VALID_GL_FORMATS.count(glFormat) != 1) { + qDebug("Invalid format 0x%x", glFormat); + return false; + } + + if (VALID_GL_INTERNAL_FORMATS.count(glInternalFormat) != 1) { + qDebug("Invalid internal format 0x%x", glInternalFormat); + return false; + } + } + + // + // Dimensions validity + // + + // Textures must at least have a width + // If they have a depth, they must have a height + if ((pixelWidth == 0) || (pixelDepth != 0 && pixelHeight == 0)) { + qDebug() << "Invalid dimensions " << pixelWidth << "x" << pixelHeight << "x" << pixelDepth; + return false; + } + + + if (numberOfFaces != 1 && numberOfFaces != NUM_CUBEMAPFACES) { + qDebug() << "Invalid number of faces " << numberOfFaces; + return false; + } + + // FIXME validate numberOfMipmapLevels based on the dimensions? + + if ((bytesOfKeyValueData % 4) != 0) { + qDebug() << "Invalid keyvalue data size " << numberOfFaces; + return false; + } + + return true; +} + +struct AlignedStreamBuffer { + AlignedStreamBuffer(size_t size, const uint8_t* data) + : _size(size), _data(data) { } + + AlignedStreamBuffer(const StoragePointer& storage) + : AlignedStreamBuffer(storage->size(), storage->data()) { } + + + template + bool read(T& t) { + // Ensure we don't read more than we have + if (sizeof(T) > _size) { + return false; + } + + // Grab the data + memcpy(&t, _data, sizeof(T)); + + // Advance the pointer + return skip(sizeof(T)); + } + + bool skip(size_t skipSize) { + if ((skipSize % 4) != 0) { + skipSize += (3 - ((skipSize + 3) % 4)); + } + if (skipSize > _size) { + return false; + } + _data += skipSize; + _size -= skipSize; + return true; + } + + AlignedStreamBuffer front(size_t size) const { + return AlignedStreamBuffer { std::min(size, _size), _data }; + } + + bool empty() const { + return _size == 0; + } + +private: + size_t _size; + const uint8_t* _data; +}; + +bool validateKeyValueData(AlignedStreamBuffer kvbuffer) { + while (!kvbuffer.empty()) { + uint32_t keyValueSize; + // Try to fetch the size of the next key value block + if (!kvbuffer.read(keyValueSize)) { + qDebug() << "Unable to read past key value size"; + return false; + } + if (!kvbuffer.skip(keyValueSize)) { + qDebug() << "Unable to skip past key value data"; + return false; + } + } + + return true; +} + +bool KTX::validate(const StoragePointer& src) { + if ((src->size() % 4) != 0) { + // All KTX data is 4-byte aligned + qDebug() << "Invalid size, not 4 byte aligned"; + return false; + } + + Header header; + AlignedStreamBuffer buffer { src }; + if (!buffer.read(header)) { + qDebug() << "Unable to read header"; + return false; + } + + // Basic header validation, are the enums and size valid? + if (!header.isValid()) { + qDebug() << "Invalid header"; + return false; + } + + // Validate the key value pairs + if (!validateKeyValueData(buffer.front(header.bytesOfKeyValueData))) { + qDebug() << "Invalid key value data"; + return false; + } + + // now skip the KV data + if (!buffer.skip(header.bytesOfKeyValueData)) { + qDebug() << "Unable to read past key value data"; + return false; + } + + + // Validate the images + for (uint32_t mip = 0; mip < header.numberOfMipmapLevels; ++mip) { + uint32_t imageSize; + if (!buffer.read(imageSize)) { + qDebug() << "Unable to read image size"; + return false; + } + + if (header.numberOfArrayElements == 0 && header.numberOfFaces == 6) { + for (uint8_t face = 0; face < NUM_CUBEMAPFACES; ++face) { + if (!buffer.skip(imageSize)) { + qDebug() << "Unable to skip past cubemap data"; + return false; + } + } + } else { + if (!buffer.skip(imageSize)) { + return false; + } + } + } + + // The buffer should be empty afer we've skipped all of the KTX data + if (!buffer.empty()) { + return false; + } + + return true; +} diff --git a/tests/ktx/src/main.cpp b/tests/ktx/src/main.cpp index 225fcbb2ed..92b9091533 100644 --- a/tests/ktx/src/main.cpp +++ b/tests/ktx/src/main.cpp @@ -38,14 +38,12 @@ #include #include - #include #include #include #include #include - QSharedPointer logger; gpu::Texture* cacheTexture(const std::string& name, gpu::Texture* srcTexture, bool write = true, bool read = true); @@ -59,7 +57,9 @@ void messageHandler(QtMsgType type, const QMessageLogContext& context, const QSt OutputDebugStringA(logMessage.toLocal8Bit().constData()); OutputDebugStringA("\n"); #endif - logger->addMessage(qPrintable(logMessage + "\n")); + if (logger) { + logger->addMessage(qPrintable(logMessage + "\n")); + } } } @@ -149,5 +149,59 @@ int main(int argc, char** argv) { return 0; } +#if 0 +static const QString TEST_FOLDER { "H:/ktx_cacheold" }; +//static const QString TEST_FOLDER { "C:/Users/bdavis/Git/KTX/testimages" }; + +//static const QString EXTENSIONS { "4bbdf8f786470e4ab3e672d44b8e8df2.ktx" }; +static const QString EXTENSIONS { "*.ktx" }; + +int mainTemp(int, char**) { + qInstallMessageHandler(messageHandler); + auto fileInfoList = QDir { TEST_FOLDER }.entryInfoList(QStringList { EXTENSIONS }); + for (auto fileInfo : fileInfoList) { + qDebug() << fileInfo.filePath(); + std::shared_ptr storage { new storage::FileStorage { fileInfo.filePath() } }; + + if (!ktx::KTX::validate(storage)) { + qDebug() << "KTX invalid"; + } + + auto ktxFile = ktx::KTX::create(storage); + ktx::KTXDescriptor ktxDescriptor = ktxFile->toDescriptor(); + assert(ktxFile->_keyValues == ktxDescriptor.keyValues); + + qDebug() << "Contains " << ktxDescriptor.keyValues.size() << " key value pairs"; + for (const auto& kv : ktxDescriptor.keyValues) { + qDebug() << "\t" << kv._key.c_str(); + } + + auto offsetToMinMipKV = ktxDescriptor.getValueOffsetForKey(ktx::HIFI_MIN_POPULATED_MIP_KEY); + if (offsetToMinMipKV) { + auto data = storage->data() + ktx::KTX_HEADER_SIZE + offsetToMinMipKV; + auto minMipLevelAvailable = *data; + qDebug() << "\tMin mip available " << minMipLevelAvailable; + assert(minMipLevelAvailable < ktxDescriptor.header.numberOfMipmapLevels); + } + auto storageSize = storage->size(); + for (const auto& faceImageDesc : ktxDescriptor.images) { + //assert(0 == (faceImageDesc._faceSize % 4)); + for (const auto& faceOffset : faceImageDesc._faceOffsets) { + assert(0 == (faceOffset % 4)); + auto faceEndOffset = faceOffset + faceImageDesc._faceSize; + assert(faceEndOffset <= storageSize); + } + } + + for (const auto& faceImage : ktxFile->_images) { + for (const ktx::Byte* faceBytes : faceImage._faceBytes) { + assert(0 == (reinterpret_cast(faceBytes) % 4)); + } + } + } + return 0; +} +#endif + #include "main.moc" From 191b633081e1c8a89d475c4c820f7fdba10d9f3a Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Mon, 15 May 2017 10:23:08 -0700 Subject: [PATCH 03/14] Refactor KTX defines and validation --- libraries/gpu/src/gpu/Texture_ktx.cpp | 50 ++-- libraries/ktx/src/khronos/KHR.h | 238 +++++++++++++++++++ libraries/ktx/src/ktx/KTX.cpp | 98 ++++---- libraries/ktx/src/ktx/KTX.h | 327 +++++++------------------- libraries/ktx/src/ktx/Reader.cpp | 4 +- libraries/ktx/src/ktx/Validation.cpp | 265 +++++++++++++-------- libraries/ktx/src/ktx/Writer.cpp | 8 +- tests/ktx/src/main.cpp | 24 +- 8 files changed, 570 insertions(+), 444 deletions(-) create mode 100644 libraries/ktx/src/khronos/KHR.h diff --git a/libraries/gpu/src/gpu/Texture_ktx.cpp b/libraries/gpu/src/gpu/Texture_ktx.cpp index 5f677d7424..f455fde009 100644 --- a/libraries/gpu/src/gpu/Texture_ktx.cpp +++ b/libraries/gpu/src/gpu/Texture_ktx.cpp @@ -510,29 +510,29 @@ TexturePointer Texture::unserialize(const std::string& ktxfile, const ktx::KTXDe bool Texture::evalKTXFormat(const Element& mipFormat, const Element& texelFormat, ktx::Header& header) { if (texelFormat == Format::COLOR_RGBA_32 && mipFormat == Format::COLOR_BGRA_32) { - header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::BGRA, ktx::GLInternalFormat_Uncompressed::RGBA8, ktx::GLBaseInternalFormat::RGBA); + header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::BGRA, ktx::GLInternalFormat::RGBA8, ktx::GLBaseInternalFormat::RGBA); } else if (texelFormat == Format::COLOR_RGBA_32 && mipFormat == Format::COLOR_RGBA_32) { - header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RGBA, ktx::GLInternalFormat_Uncompressed::RGBA8, ktx::GLBaseInternalFormat::RGBA); + header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RGBA, ktx::GLInternalFormat::RGBA8, ktx::GLBaseInternalFormat::RGBA); } else if (texelFormat == Format::COLOR_SRGBA_32 && mipFormat == Format::COLOR_SBGRA_32) { - header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::BGRA, ktx::GLInternalFormat_Uncompressed::SRGB8_ALPHA8, ktx::GLBaseInternalFormat::RGBA); + header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::BGRA, ktx::GLInternalFormat::SRGB8_ALPHA8, ktx::GLBaseInternalFormat::RGBA); } else if (texelFormat == Format::COLOR_SRGBA_32 && mipFormat == Format::COLOR_SRGBA_32) { - header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RGBA, ktx::GLInternalFormat_Uncompressed::SRGB8_ALPHA8, ktx::GLBaseInternalFormat::RGBA); + header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RGBA, ktx::GLInternalFormat::SRGB8_ALPHA8, ktx::GLBaseInternalFormat::RGBA); } else if (texelFormat == Format::COLOR_R_8 && mipFormat == Format::COLOR_R_8) { - header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RED, ktx::GLInternalFormat_Uncompressed::R8, ktx::GLBaseInternalFormat::RED); + header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RED, ktx::GLInternalFormat::R8, ktx::GLBaseInternalFormat::RED); } else if (texelFormat == Format::VEC2NU8_XY && mipFormat == Format::VEC2NU8_XY) { - header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RG, ktx::GLInternalFormat_Uncompressed::RG8, ktx::GLBaseInternalFormat::RG); + header.setUncompressed(ktx::GLType::UNSIGNED_BYTE, 1, ktx::GLFormat::RG, ktx::GLInternalFormat::RG8, ktx::GLBaseInternalFormat::RG); } else if (texelFormat == Format::COLOR_COMPRESSED_SRGB && mipFormat == Format::COLOR_COMPRESSED_SRGB) { - header.setCompressed(ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_S3TC_DXT1_EXT, ktx::GLBaseInternalFormat::RGB); + header.setCompressed(ktx::GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT, ktx::GLBaseInternalFormat::RGB); } else if (texelFormat == Format::COLOR_COMPRESSED_SRGBA_MASK && mipFormat == Format::COLOR_COMPRESSED_SRGBA_MASK) { - header.setCompressed(ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, ktx::GLBaseInternalFormat::RGBA); + header.setCompressed(ktx::GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, ktx::GLBaseInternalFormat::RGBA); } else if (texelFormat == Format::COLOR_COMPRESSED_SRGBA && mipFormat == Format::COLOR_COMPRESSED_SRGBA) { - header.setCompressed(ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, ktx::GLBaseInternalFormat::RGBA); + header.setCompressed(ktx::GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, ktx::GLBaseInternalFormat::RGBA); } else if (texelFormat == Format::COLOR_COMPRESSED_RED && mipFormat == Format::COLOR_COMPRESSED_RED) { - header.setCompressed(ktx::GLInternalFormat_Compressed::COMPRESSED_RED_RGTC1, ktx::GLBaseInternalFormat::RED); + header.setCompressed(ktx::GLInternalFormat::COMPRESSED_RED_RGTC1, ktx::GLBaseInternalFormat::RED); } else if (texelFormat == Format::COLOR_COMPRESSED_XY && mipFormat == Format::COLOR_COMPRESSED_XY) { - header.setCompressed(ktx::GLInternalFormat_Compressed::COMPRESSED_RG_RGTC2, ktx::GLBaseInternalFormat::RG); + header.setCompressed(ktx::GLInternalFormat::COMPRESSED_RG_RGTC2, ktx::GLBaseInternalFormat::RG); } else if (texelFormat == Format::COLOR_COMPRESSED_SRGBA_HIGH && mipFormat == Format::COLOR_COMPRESSED_SRGBA_HIGH) { - header.setCompressed(ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, ktx::GLBaseInternalFormat::RGBA); + header.setCompressed(ktx::GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, ktx::GLBaseInternalFormat::RGBA); } else { return false; } @@ -542,20 +542,20 @@ bool Texture::evalKTXFormat(const Element& mipFormat, const Element& texelFormat bool Texture::evalTextureFormat(const ktx::Header& header, Element& mipFormat, Element& texelFormat) { if (header.getGLFormat() == ktx::GLFormat::BGRA && header.getGLType() == ktx::GLType::UNSIGNED_BYTE && header.getTypeSize() == 1) { - if (header.getGLInternaFormat_Uncompressed() == ktx::GLInternalFormat_Uncompressed::RGBA8) { + if (header.getGLInternaFormat() == ktx::GLInternalFormat::RGBA8) { mipFormat = Format::COLOR_BGRA_32; texelFormat = Format::COLOR_RGBA_32; - } else if (header.getGLInternaFormat_Uncompressed() == ktx::GLInternalFormat_Uncompressed::SRGB8_ALPHA8) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::SRGB8_ALPHA8) { mipFormat = Format::COLOR_SBGRA_32; texelFormat = Format::COLOR_SRGBA_32; } else { return false; } } else if (header.getGLFormat() == ktx::GLFormat::RGBA && header.getGLType() == ktx::GLType::UNSIGNED_BYTE && header.getTypeSize() == 1) { - if (header.getGLInternaFormat_Uncompressed() == ktx::GLInternalFormat_Uncompressed::RGBA8) { + if (header.getGLInternaFormat() == ktx::GLInternalFormat::RGBA8) { mipFormat = Format::COLOR_RGBA_32; texelFormat = Format::COLOR_RGBA_32; - } else if (header.getGLInternaFormat_Uncompressed() == ktx::GLInternalFormat_Uncompressed::SRGB8_ALPHA8) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::SRGB8_ALPHA8) { mipFormat = Format::COLOR_SRGBA_32; texelFormat = Format::COLOR_SRGBA_32; } else { @@ -563,35 +563,35 @@ bool Texture::evalTextureFormat(const ktx::Header& header, Element& mipFormat, E } } else if (header.getGLFormat() == ktx::GLFormat::RED && header.getGLType() == ktx::GLType::UNSIGNED_BYTE && header.getTypeSize() == 1) { mipFormat = Format::COLOR_R_8; - if (header.getGLInternaFormat_Uncompressed() == ktx::GLInternalFormat_Uncompressed::R8) { + if (header.getGLInternaFormat() == ktx::GLInternalFormat::R8) { texelFormat = Format::COLOR_R_8; } else { return false; } } else if (header.getGLFormat() == ktx::GLFormat::RG && header.getGLType() == ktx::GLType::UNSIGNED_BYTE && header.getTypeSize() == 1) { mipFormat = Format::VEC2NU8_XY; - if (header.getGLInternaFormat_Uncompressed() == ktx::GLInternalFormat_Uncompressed::RG8) { + if (header.getGLInternaFormat() == ktx::GLInternalFormat::RG8) { texelFormat = Format::VEC2NU8_XY; } else { return false; } - } else if (header.getGLFormat() == ktx::GLFormat::COMPRESSED_FORMAT && header.getGLType() == ktx::GLType::COMPRESSED_TYPE) { - if (header.getGLInternaFormat_Compressed() == ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_S3TC_DXT1_EXT) { + } else if (header.isCompressed()) { + if (header.getGLInternaFormat() == ktx::GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT) { mipFormat = Format::COLOR_COMPRESSED_SRGB; texelFormat = Format::COLOR_COMPRESSED_SRGB; - } else if (header.getGLInternaFormat_Compressed() == ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT) { mipFormat = Format::COLOR_COMPRESSED_SRGBA_MASK; texelFormat = Format::COLOR_COMPRESSED_SRGBA_MASK; - } else if (header.getGLInternaFormat_Compressed() == ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) { mipFormat = Format::COLOR_COMPRESSED_SRGBA; texelFormat = Format::COLOR_COMPRESSED_SRGBA; - } else if (header.getGLInternaFormat_Compressed() == ktx::GLInternalFormat_Compressed::COMPRESSED_RED_RGTC1) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::COMPRESSED_RED_RGTC1) { mipFormat = Format::COLOR_COMPRESSED_RED; texelFormat = Format::COLOR_COMPRESSED_RED; - } else if (header.getGLInternaFormat_Compressed() == ktx::GLInternalFormat_Compressed::COMPRESSED_RG_RGTC2) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::COMPRESSED_RG_RGTC2) { mipFormat = Format::COLOR_COMPRESSED_XY; texelFormat = Format::COLOR_COMPRESSED_XY; - } else if (header.getGLInternaFormat_Compressed() == ktx::GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_BPTC_UNORM) { + } else if (header.getGLInternaFormat() == ktx::GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM) { mipFormat = Format::COLOR_COMPRESSED_SRGBA_HIGH; texelFormat = Format::COLOR_COMPRESSED_SRGBA_HIGH; } else { diff --git a/libraries/ktx/src/khronos/KHR.h b/libraries/ktx/src/khronos/KHR.h new file mode 100644 index 0000000000..7b3e3453c7 --- /dev/null +++ b/libraries/ktx/src/khronos/KHR.h @@ -0,0 +1,238 @@ +// +// Created by Bradley Austin Davis on 2017/05/13 +// Copyright 2017 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#pragma once +#ifndef khronos_khr_hpp +#define khronos_khr_hpp + +namespace khronos { + + namespace gl { + + enum class Type : uint32_t { + // GL 4.4 Table 8.2 + UNSIGNED_BYTE = 0x1401, + BYTE = 0x1400, + UNSIGNED_SHORT = 0x1403, + SHORT = 0x1402, + UNSIGNED_INT = 0x1405, + INT = 0x1404, + HALF_FLOAT = 0x140B, + FLOAT = 0x1406, + UNSIGNED_BYTE_3_3_2 = 0x8032, + UNSIGNED_BYTE_2_3_3_REV = 0x8362, + UNSIGNED_SHORT_5_6_5 = 0x8363, + UNSIGNED_SHORT_5_6_5_REV = 0x8364, + UNSIGNED_SHORT_4_4_4_4 = 0x8033, + UNSIGNED_SHORT_4_4_4_4_REV = 0x8365, + UNSIGNED_SHORT_5_5_5_1 = 0x8034, + UNSIGNED_SHORT_1_5_5_5_REV = 0x8366, + UNSIGNED_INT_8_8_8_8 = 0x8035, + UNSIGNED_INT_8_8_8_8_REV = 0x8367, + UNSIGNED_INT_10_10_10_2 = 0x8036, + UNSIGNED_INT_2_10_10_10_REV = 0x8368, + UNSIGNED_INT_24_8 = 0x84FA, + UNSIGNED_INT_10F_11F_11F_REV = 0x8C3B, + UNSIGNED_INT_5_9_9_9_REV = 0x8C3E, + FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD, + }; + + namespace texture { + + enum class Format : uint32_t { + COMPRESSED_FORMAT = 0, + + // GL 4.4 Table 8.3 + STENCIL_INDEX = 0x1901, + DEPTH_COMPONENT = 0x1902, + DEPTH_STENCIL = 0x84F9, + + RED = 0x1903, + GREEN = 0x1904, + BLUE = 0x1905, + RG = 0x8227, + RGB = 0x1907, + RGBA = 0x1908, + BGR = 0x80E0, + BGRA = 0x80E1, + + RG_INTEGER = 0x8228, + RED_INTEGER = 0x8D94, + GREEN_INTEGER = 0x8D95, + BLUE_INTEGER = 0x8D96, + RGB_INTEGER = 0x8D98, + RGBA_INTEGER = 0x8D99, + BGR_INTEGER = 0x8D9A, + BGRA_INTEGER = 0x8D9B, + }; + + enum class InternalFormat : uint32_t { + // GL 4.4 Table 8.12 + R8 = 0x8229, + R8_SNORM = 0x8F94, + + R16 = 0x822A, + R16_SNORM = 0x8F98, + + RG8 = 0x822B, + RG8_SNORM = 0x8F95, + + RG16 = 0x822C, + RG16_SNORM = 0x8F99, + + R3_G3_B2 = 0x2A10, + RGB4 = 0x804F, + RGB5 = 0x8050, + RGB565 = 0x8D62, + + RGB8 = 0x8051, + RGB8_SNORM = 0x8F96, + RGB10 = 0x8052, + RGB12 = 0x8053, + + RGB16 = 0x8054, + RGB16_SNORM = 0x8F9A, + + RGBA2 = 0x8055, + RGBA4 = 0x8056, + RGB5_A1 = 0x8057, + RGBA8 = 0x8058, + RGBA8_SNORM = 0x8F97, + + RGB10_A2 = 0x8059, + RGB10_A2UI = 0x906F, + + RGBA12 = 0x805A, + RGBA16 = 0x805B, + RGBA16_SNORM = 0x8F9B, + + SRGB8 = 0x8C41, + SRGB8_ALPHA8 = 0x8C43, + + R16F = 0x822D, + RG16F = 0x822F, + RGB16F = 0x881B, + RGBA16F = 0x881A, + + R32F = 0x822E, + RG32F = 0x8230, + RGB32F = 0x8815, + RGBA32F = 0x8814, + + R11F_G11F_B10F = 0x8C3A, + RGB9_E5 = 0x8C3D, + + + R8I = 0x8231, + R8UI = 0x8232, + R16I = 0x8233, + R16UI = 0x8234, + R32I = 0x8235, + R32UI = 0x8236, + RG8I = 0x8237, + RG8UI = 0x8238, + RG16I = 0x8239, + RG16UI = 0x823A, + RG32I = 0x823B, + RG32UI = 0x823C, + + RGB8I = 0x8D8F, + RGB8UI = 0x8D7D, + RGB16I = 0x8D89, + RGB16UI = 0x8D77, + + RGB32I = 0x8D83, + RGB32UI = 0x8D71, + RGBA8I = 0x8D8E, + RGBA8UI = 0x8D7C, + RGBA16I = 0x8D88, + RGBA16UI = 0x8D76, + RGBA32I = 0x8D82, + + RGBA32UI = 0x8D70, + + // GL 4.4 Table 8.13 + DEPTH_COMPONENT16 = 0x81A5, + DEPTH_COMPONENT24 = 0x81A6, + DEPTH_COMPONENT32 = 0x81A7, + + DEPTH_COMPONENT32F = 0x8CAC, + DEPTH24_STENCIL8 = 0x88F0, + DEPTH32F_STENCIL8 = 0x8CAD, + + STENCIL_INDEX1 = 0x8D46, + STENCIL_INDEX4 = 0x8D47, + STENCIL_INDEX8 = 0x8D48, + STENCIL_INDEX16 = 0x8D49, + + // GL 4.4 Table 8.14 + COMPRESSED_RED = 0x8225, + COMPRESSED_RG = 0x8226, + COMPRESSED_RGB = 0x84ED, + COMPRESSED_RGBA = 0x84EE, + + COMPRESSED_SRGB = 0x8C48, + COMPRESSED_SRGB_ALPHA = 0x8C49, + + COMPRESSED_ETC1_RGB8_OES = 0x8D64, + + COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C, + COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D, + COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E, + COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F, + + COMPRESSED_RED_RGTC1 = 0x8DBB, + COMPRESSED_SIGNED_RED_RGTC1 = 0x8DBC, + COMPRESSED_RG_RGTC2 = 0x8DBD, + COMPRESSED_SIGNED_RG_RGTC2 = 0x8DBE, + + COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C, + COMPRESSED_SRGB_ALPHA_BPTC_UNORM = 0x8E8D, + COMPRESSED_RGB_BPTC_SIGNED_FLOAT = 0x8E8E, + COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = 0x8E8F, + + COMPRESSED_RGB8_ETC2 = 0x9274, + COMPRESSED_SRGB8_ETC2 = 0x9275, + COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276, + COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277, + COMPRESSED_RGBA8_ETC2_EAC = 0x9278, + COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279, + + COMPRESSED_R11_EAC = 0x9270, + COMPRESSED_SIGNED_R11_EAC = 0x9271, + COMPRESSED_RG11_EAC = 0x9272, + COMPRESSED_SIGNED_RG11_EAC = 0x9273, + }; + + enum class BaseInternalFormat : uint32_t { + // GL 4.4 Table 8.11 + DEPTH_COMPONENT = 0x1902, + DEPTH_STENCIL = 0x84F9, + RED = 0x1903, + RG = 0x8227, + RGB = 0x1907, + RGBA = 0x1908, + STENCIL_INDEX = 0x1901, + }; + + enum CubeMapFace { + POS_X = 0, + NEG_X = 1, + POS_Y = 2, + NEG_Y = 3, + POS_Z = 4, + NEG_Z = 5, + NUM_CUBEMAPFACES = 6, + }; + } + + } + +} + +#endif // khronos_khr_hpp diff --git a/libraries/ktx/src/ktx/KTX.cpp b/libraries/ktx/src/ktx/KTX.cpp index c366daf7ed..1900c4a4b1 100644 --- a/libraries/ktx/src/ktx/KTX.cpp +++ b/libraries/ktx/src/ktx/KTX.cpp @@ -16,16 +16,6 @@ using namespace ktx; -uint32_t Header::evalPadding(size_t byteSize) { - //auto padding = byteSize % PACKING_SIZE; - // return (uint32_t) (padding ? PACKING_SIZE - padding : 0); - return (uint32_t) (3 - (byteSize + 3) % PACKING_SIZE);// padding ? PACKING_SIZE - padding : 0); -} - -bool Header::checkAlignment(size_t byteSize) { - return ((byteSize & 0x3) == 0); -} - const Header::Identifier ktx::Header::IDENTIFIER {{ 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }}; @@ -40,24 +30,24 @@ uint32_t Header::evalMaxDimension() const { uint32_t Header::evalPixelOrBlockWidth(uint32_t level) const { auto pixelWidth = std::max(getPixelWidth() >> level, 1U); - if (getGLType() == GLType::COMPRESSED_TYPE) { - return (pixelWidth + 3) / 4; + if (isCompressed()) { + return evalAlignedCount(pixelWidth); } else { return pixelWidth; } } uint32_t Header::evalPixelOrBlockHeight(uint32_t level) const { auto pixelWidth = std::max(getPixelHeight() >> level, 1U); - if (getGLType() == GLType::COMPRESSED_TYPE) { - auto format = getGLInternaFormat_Compressed(); + if (glType == COMPRESSED_TYPE) { + auto format = getGLInternaFormat(); switch (format) { - case GLInternalFormat_Compressed::COMPRESSED_SRGB_S3TC_DXT1_EXT: // BC1 - case GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: // BC1A - case GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: // BC3 - case GLInternalFormat_Compressed::COMPRESSED_RED_RGTC1: // BC4 - case GLInternalFormat_Compressed::COMPRESSED_RG_RGTC2: // BC5 - case GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: // BC7 - return (pixelWidth + 3) / 4; + case GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: // BC1 + case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: // BC1A + case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: // BC3 + case GLInternalFormat::COMPRESSED_RED_RGTC1: // BC4 + case GLInternalFormat::COMPRESSED_RG_RGTC2: // BC5 + case GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: // BC7 + return evalAlignedCount(pixelWidth); default: throw std::runtime_error("Unknown format"); } @@ -70,31 +60,33 @@ uint32_t Header::evalPixelOrBlockDepth(uint32_t level) const { } size_t Header::evalPixelOrBlockSize() const { - if (getGLType() == GLType::COMPRESSED_TYPE) { - auto format = getGLInternaFormat_Compressed(); - if (format == GLInternalFormat_Compressed::COMPRESSED_SRGB_S3TC_DXT1_EXT) { - return 8; - } else if (format == GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT) { - return 8; - } else if (format == GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT) { - return 16; - } else if (format == GLInternalFormat_Compressed::COMPRESSED_RED_RGTC1) { - return 8; - } else if (format == GLInternalFormat_Compressed::COMPRESSED_RG_RGTC2) { - return 16; - } else if (format == GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_BPTC_UNORM) { - return 16; + if (isCompressed()) { + auto format = getGLInternaFormat(); + switch (format) { + case GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: + case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: + case GLInternalFormat::COMPRESSED_RED_RGTC1: + return 8; + case GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: + case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: + case GLInternalFormat::COMPRESSED_RG_RGTC2: + return 16; + default: + break; } } else { auto baseFormat = getGLBaseInternalFormat(); - if (baseFormat == GLBaseInternalFormat::RED) { - return 1; - } else if (baseFormat == GLBaseInternalFormat::RG) { - return 2; - } else if (baseFormat == GLBaseInternalFormat::RGB) { - return 3; - } else if (baseFormat == GLBaseInternalFormat::RGBA) { - return 4; + switch (baseFormat) { + case GLBaseInternalFormat::RED: + return 1; + case GLBaseInternalFormat::RG: + return 2; + case GLBaseInternalFormat::RGB: + return 3; + case GLBaseInternalFormat::RGBA: + return 4; + default: + break; } } @@ -108,16 +100,16 @@ size_t Header::evalRowSize(uint32_t level) const { if (pixSize == 0) { return 0; } - auto netSize = pixWidth * pixSize; - auto padding = evalPadding(netSize); - return netSize + padding; + return evalPadded(pixWidth * pixSize); } + size_t Header::evalFaceSize(uint32_t level) const { auto pixHeight = evalPixelOrBlockHeight(level); auto pixDepth = evalPixelOrBlockDepth(level); auto rowSize = evalRowSize(level); return pixDepth * pixHeight * rowSize; } + size_t Header::evalImageSize(uint32_t level) const { auto faceSize = evalFaceSize(level); if (!checkAlignment(faceSize)) { @@ -192,7 +184,7 @@ KeyValue::KeyValue(const std::string& key, const std::string& value) : } uint32_t KeyValue::serializedByteSize() const { - return (uint32_t) (sizeof(uint32_t) + _byteSize + Header::evalPadding(_byteSize)); + return (uint32_t)sizeof(uint32_t) + evalPadded(_byteSize); } uint32_t KeyValue::serializedKeyValuesByteSize(const KeyValues& keyValues) { @@ -200,14 +192,8 @@ uint32_t KeyValue::serializedKeyValuesByteSize(const KeyValues& keyValues) { for (auto& keyval : keyValues) { keyValuesSize += keyval.serializedByteSize(); } - return (keyValuesSize + Header::evalPadding(keyValuesSize)); -} - - -KTX::KTX() { -} - -KTX::~KTX() { + Q_ASSERT(keyValuesSize % 4 == 0); + return keyValuesSize; } void KTX::resetStorage(const StoragePointer& storage) { @@ -230,7 +216,7 @@ size_t KTX::getTexelsDataSize() const { if (!_storage) { return 0; } - return (_storage->data() + _storage->size()) - getTexelsData(); + return _storage->size() - sizeof(Header) - getKeyValueDataSize(); } const Byte* KTX::getKeyValueData() const { diff --git a/libraries/ktx/src/ktx/KTX.h b/libraries/ktx/src/ktx/KTX.h index 6dc8e01131..c6570f87c6 100644 --- a/libraries/ktx/src/ktx/KTX.h +++ b/libraries/ktx/src/ktx/KTX.h @@ -22,7 +22,14 @@ #include -/* KTX Spec: +#include "../khronos/KHR.h" + +/* + +KTX Specification: https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/ + + +**** A KTX header is 64 bytes layed out as follows Byte[12] identifier UInt32 endianness @@ -38,6 +45,8 @@ UInt32 numberOfArrayElements UInt32 numberOfFaces UInt32 numberOfMipmapLevels UInt32 bytesOfKeyValueData + +**** Each KTX key value pair block is 4 byte aligned for each keyValuePair that fits in bytesOfKeyValueData UInt32 keyAndValueByteSize @@ -45,6 +54,8 @@ for each keyValuePair that fits in bytesOfKeyValueData Byte valuePadding[3 - ((keyAndValueByteSize + 3) % 4)] end +**** Each mip and cube face is 4 byte aligned + for each mipmap_level in numberOfMipmapLevels* UInt32 imageSize; for each array_element in numberOfArrayElements* @@ -67,231 +78,23 @@ end ** Uncompressed texture data matches a GL_UNPACK_ALIGNMENT of 4. */ - - namespace ktx { - const uint32_t PACKING_SIZE { sizeof(uint32_t) }; - const std::string HIFI_MIN_POPULATED_MIP_KEY{ "hifi.minMip" }; + // Alignment constants + static const uint32_t ALIGNMENT { sizeof(uint32_t) }; + static const uint32_t ALIGNMENT_REMAINDER { ALIGNMENT - 1 }; + static const uint32_t NUM_CUBEMAPFACES = khronos::gl::texture::CubeMapFace::NUM_CUBEMAPFACES; + + // FIXME move out of this header, not specific to ktx + const std::string HIFI_MIN_POPULATED_MIP_KEY { "hifi.minMip" }; + using Byte = uint8_t; - enum class GLType : uint32_t { - COMPRESSED_TYPE = 0, - - // GL 4.4 Table 8.2 - UNSIGNED_BYTE = 0x1401, - BYTE = 0x1400, - UNSIGNED_SHORT = 0x1403, - SHORT = 0x1402, - UNSIGNED_INT = 0x1405, - INT = 0x1404, - HALF_FLOAT = 0x140B, - FLOAT = 0x1406, - UNSIGNED_BYTE_3_3_2 = 0x8032, - UNSIGNED_BYTE_2_3_3_REV = 0x8362, - UNSIGNED_SHORT_5_6_5 = 0x8363, - UNSIGNED_SHORT_5_6_5_REV = 0x8364, - UNSIGNED_SHORT_4_4_4_4 = 0x8033, - UNSIGNED_SHORT_4_4_4_4_REV = 0x8365, - UNSIGNED_SHORT_5_5_5_1 = 0x8034, - UNSIGNED_SHORT_1_5_5_5_REV = 0x8366, - UNSIGNED_INT_8_8_8_8 = 0x8035, - UNSIGNED_INT_8_8_8_8_REV = 0x8367, - UNSIGNED_INT_10_10_10_2 = 0x8036, - UNSIGNED_INT_2_10_10_10_REV = 0x8368, - UNSIGNED_INT_24_8 = 0x84FA, - UNSIGNED_INT_10F_11F_11F_REV = 0x8C3B, - UNSIGNED_INT_5_9_9_9_REV = 0x8C3E, - FLOAT_32_UNSIGNED_INT_24_8_REV = 0x8DAD, - }; - - enum class GLFormat : uint32_t { - COMPRESSED_FORMAT = 0, - - // GL 4.4 Table 8.3 - STENCIL_INDEX = 0x1901, - DEPTH_COMPONENT = 0x1902, - DEPTH_STENCIL = 0x84F9, - - RED = 0x1903, - GREEN = 0x1904, - BLUE = 0x1905, - RG = 0x8227, - RGB = 0x1907, - RGBA = 0x1908, - BGR = 0x80E0, - BGRA = 0x80E1, - - RG_INTEGER = 0x8228, - RED_INTEGER = 0x8D94, - GREEN_INTEGER = 0x8D95, - BLUE_INTEGER = 0x8D96, - RGB_INTEGER = 0x8D98, - RGBA_INTEGER = 0x8D99, - BGR_INTEGER = 0x8D9A, - BGRA_INTEGER = 0x8D9B, - }; - - enum class GLInternalFormat_Uncompressed : uint32_t { - // GL 4.4 Table 8.12 - R8 = 0x8229, - R8_SNORM = 0x8F94, - - R16 = 0x822A, - R16_SNORM = 0x8F98, - - RG8 = 0x822B, - RG8_SNORM = 0x8F95, - - RG16 = 0x822C, - RG16_SNORM = 0x8F99, - - R3_G3_B2 = 0x2A10, - RGB4 = 0x804F, - RGB5 = 0x8050, - RGB565 = 0x8D62, - - RGB8 = 0x8051, - RGB8_SNORM = 0x8F96, - RGB10 = 0x8052, - RGB12 = 0x8053, - - RGB16 = 0x8054, - RGB16_SNORM = 0x8F9A, - - RGBA2 = 0x8055, - RGBA4 = 0x8056, - RGB5_A1 = 0x8057, - RGBA8 = 0x8058, - RGBA8_SNORM = 0x8F97, - - RGB10_A2 = 0x8059, - RGB10_A2UI = 0x906F, - - RGBA12 = 0x805A, - RGBA16 = 0x805B, - RGBA16_SNORM = 0x8F9B, - - SRGB8 = 0x8C41, - SRGB8_ALPHA8 = 0x8C43, - - R16F = 0x822D, - RG16F = 0x822F, - RGB16F = 0x881B, - RGBA16F = 0x881A, - - R32F = 0x822E, - RG32F = 0x8230, - RGB32F = 0x8815, - RGBA32F = 0x8814, - - R11F_G11F_B10F = 0x8C3A, - RGB9_E5 = 0x8C3D, - - - R8I = 0x8231, - R8UI = 0x8232, - R16I = 0x8233, - R16UI = 0x8234, - R32I = 0x8235, - R32UI = 0x8236, - RG8I = 0x8237, - RG8UI = 0x8238, - RG16I = 0x8239, - RG16UI = 0x823A, - RG32I = 0x823B, - RG32UI = 0x823C, - - RGB8I = 0x8D8F, - RGB8UI = 0x8D7D, - RGB16I = 0x8D89, - RGB16UI = 0x8D77, - - RGB32I = 0x8D83, - RGB32UI = 0x8D71, - RGBA8I = 0x8D8E, - RGBA8UI = 0x8D7C, - RGBA16I = 0x8D88, - RGBA16UI = 0x8D76, - RGBA32I = 0x8D82, - - RGBA32UI = 0x8D70, - - // GL 4.4 Table 8.13 - DEPTH_COMPONENT16 = 0x81A5, - DEPTH_COMPONENT24 = 0x81A6, - DEPTH_COMPONENT32 = 0x81A7, - - DEPTH_COMPONENT32F = 0x8CAC, - DEPTH24_STENCIL8 = 0x88F0, - DEPTH32F_STENCIL8 = 0x8CAD, - - STENCIL_INDEX1 = 0x8D46, - STENCIL_INDEX4 = 0x8D47, - STENCIL_INDEX8 = 0x8D48, - STENCIL_INDEX16 = 0x8D49, - }; - - enum class GLInternalFormat_Compressed : uint32_t { - // GL 4.4 Table 8.14 - COMPRESSED_RED = 0x8225, - COMPRESSED_RG = 0x8226, - COMPRESSED_RGB = 0x84ED, - COMPRESSED_RGBA = 0x84EE, - - COMPRESSED_SRGB = 0x8C48, - COMPRESSED_SRGB_ALPHA = 0x8C49, - - COMPRESSED_ETC1_RGB8_OES = 0x8D64, - - COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C, - COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D, - COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E, - COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F, - - COMPRESSED_RED_RGTC1 = 0x8DBB, - COMPRESSED_SIGNED_RED_RGTC1 = 0x8DBC, - COMPRESSED_RG_RGTC2 = 0x8DBD, - COMPRESSED_SIGNED_RG_RGTC2 = 0x8DBE, - - COMPRESSED_RGBA_BPTC_UNORM = 0x8E8C, - COMPRESSED_SRGB_ALPHA_BPTC_UNORM = 0x8E8D, - COMPRESSED_RGB_BPTC_SIGNED_FLOAT = 0x8E8E, - COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT = 0x8E8F, - - COMPRESSED_RGB8_ETC2 = 0x9274, - COMPRESSED_SRGB8_ETC2 = 0x9275, - COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276, - COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277, - COMPRESSED_RGBA8_ETC2_EAC = 0x9278, - COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279, - - COMPRESSED_R11_EAC = 0x9270, - COMPRESSED_SIGNED_R11_EAC = 0x9271, - COMPRESSED_RG11_EAC = 0x9272, - COMPRESSED_SIGNED_RG11_EAC = 0x9273, - }; - - enum class GLBaseInternalFormat : uint32_t { - // GL 4.4 Table 8.11 - DEPTH_COMPONENT = 0x1902, - DEPTH_STENCIL = 0x84F9, - RED = 0x1903, - RG = 0x8227, - RGB = 0x1907, - RGBA = 0x1908, - STENCIL_INDEX = 0x1901, - }; - - enum CubeMapFace { - POS_X = 0, - NEG_X = 1, - POS_Y = 2, - NEG_Y = 3, - POS_Z = 4, - NEG_Z = 5, - NUM_CUBEMAPFACES = 6, - }; + using GLType = khronos::gl::Type; + using GLFormat = khronos::gl::texture::Format; + using GLInternalFormat = khronos::gl::texture::InternalFormat; + using GLBaseInternalFormat = khronos::gl::texture::BaseInternalFormat; + using CubeMapFace = khronos::gl::texture::CubeMapFace; using Storage = storage::Storage; using StoragePointer = std::shared_ptr; @@ -301,18 +104,41 @@ namespace ktx { bool checkIdentifier(const Byte* identifier); + // Returns the number of bytes required be added to the passed value to make it 4 byte aligned + template + inline uint8_t evalPadding(T value) { + return ALIGNMENT_REMAINDER - ((value + ALIGNMENT_REMAINDER) % ALIGNMENT); + } + + // Returns the passed value rounded up to the next 4 byte aligned value, if it's not already 4 byte aligned + template + inline T evalPadded(T value) { + return (value + ALIGNMENT_REMAINDER) & ~(T)ALIGNMENT_REMAINDER; + } + + template + inline T evalAlignedCount(T value) { + return (value + ALIGNMENT_REMAINDER) / ALIGNMENT; + } + + template + inline bool checkAlignment(T value) { + return ((value & 0x3) == 0); + } + + // Header struct Header { - static const size_t IDENTIFIER_LENGTH = 12; + static const uint32_t COMPRESSED_FORMAT { 0 }; + static const uint32_t COMPRESSED_TYPE { 0 }; + static const uint32_t COMPRESSED_TYPE_SIZE { 1 }; + static const size_t IDENTIFIER_LENGTH { 12 }; using Identifier = std::array; static const Identifier IDENTIFIER; static const uint32_t ENDIAN_TEST = 0x04030201; static const uint32_t REVERSE_ENDIAN_TEST = 0x01020304; - static uint32_t evalPadding(size_t byteSize); - static bool checkAlignment(size_t byteSize); - Header(); Byte identifier[IDENTIFIER_LENGTH]; @@ -321,7 +147,7 @@ namespace ktx { uint32_t glType { static_cast(GLType::UNSIGNED_BYTE) }; uint32_t glTypeSize { 0 }; uint32_t glFormat { static_cast(GLFormat::RGBA) }; - uint32_t glInternalFormat { static_cast(GLInternalFormat_Uncompressed::RGBA8) }; + uint32_t glInternalFormat { static_cast(GLInternalFormat::RGBA8) }; uint32_t glBaseInternalFormat { static_cast(GLBaseInternalFormat::RGBA) }; uint32_t pixelWidth { 1 }; @@ -338,6 +164,7 @@ namespace ktx { uint32_t getPixelDepth() const { return (pixelDepth ? pixelDepth : 1); } uint32_t getNumberOfSlices() const { return (numberOfArrayElements ? numberOfArrayElements : 1); } uint32_t getNumberOfLevels() const { return (numberOfMipmapLevels ? numberOfMipmapLevels : 1); } + bool isCompressed() const { return glFormat == COMPRESSED_FORMAT; } uint32_t evalMaxDimension() const; uint32_t evalPixelOrBlockWidth(uint32_t level) const; @@ -349,17 +176,21 @@ namespace ktx { size_t evalFaceSize(uint32_t level) const; size_t evalImageSize(uint32_t level) const; - void setUncompressed(GLType type, uint32_t typeSize, GLFormat format, GLInternalFormat_Uncompressed internalFormat, GLBaseInternalFormat baseInternalFormat) { + // FIXME base internal format should automatically be determined by internal format + // FIXME type size should automatically be determined by type + void setUncompressed(GLType type, uint32_t typeSize, GLFormat format, GLInternalFormat internalFormat, GLBaseInternalFormat baseInternalFormat) { glType = (uint32_t) type; glTypeSize = typeSize; glFormat = (uint32_t) format; glInternalFormat = (uint32_t) internalFormat; glBaseInternalFormat = (uint32_t) baseInternalFormat; } - void setCompressed(GLInternalFormat_Compressed internalFormat, GLBaseInternalFormat baseInternalFormat) { - glType = (uint32_t) GLType::COMPRESSED_TYPE; - glTypeSize = 1; - glFormat = (uint32_t) GLFormat::COMPRESSED_FORMAT; + + // FIXME base internal format should automatically be determined by internal format + void setCompressed(GLInternalFormat internalFormat, GLBaseInternalFormat baseInternalFormat) { + glType = COMPRESSED_TYPE; + glFormat = COMPRESSED_FORMAT; + glTypeSize = COMPRESSED_TYPE_SIZE; glInternalFormat = (uint32_t) internalFormat; glBaseInternalFormat = (uint32_t) baseInternalFormat; } @@ -367,18 +198,9 @@ namespace ktx { GLType getGLType() const { return (GLType)glType; } uint32_t getTypeSize() const { return glTypeSize; } GLFormat getGLFormat() const { return (GLFormat)glFormat; } - GLInternalFormat_Uncompressed getGLInternaFormat_Uncompressed() const { return (GLInternalFormat_Uncompressed)glInternalFormat; } - GLInternalFormat_Compressed getGLInternaFormat_Compressed() const { return (GLInternalFormat_Compressed)glInternalFormat; } + GLInternalFormat getGLInternaFormat() const { return (GLInternalFormat)glInternalFormat; } GLBaseInternalFormat getGLBaseInternalFormat() const { return (GLBaseInternalFormat)glBaseInternalFormat; } - - void setDimensions(uint32_t width, uint32_t height = 0, uint32_t depth = 0, uint32_t numSlices = 0, uint32_t numFaces = 1) { - pixelWidth = (width > 0 ? width : 1); - pixelHeight = height; - pixelDepth = depth; - numberOfArrayElements = numSlices; - numberOfFaces = ((numFaces == 1) || (numFaces == NUM_CUBEMAPFACES) ? numFaces : 1); - } void set1D(uint32_t width) { setDimensions(width); } void set1DArray(uint32_t width, uint32_t numSlices) { setDimensions(width, 0, 0, (numSlices > 0 ? numSlices : 1)); } void set2D(uint32_t width, uint32_t height) { setDimensions(width, height); } @@ -392,12 +214,22 @@ namespace ktx { // Generate a set of image descriptors based on the assumption that the full mip pyramid is populated ImageDescriptors generateImageDescriptors() const; + + private: + void setDimensions(uint32_t width, uint32_t height = 0, uint32_t depth = 0, uint32_t numSlices = 0, uint32_t numFaces = 1) { + pixelWidth = (width > 0 ? width : 1); + pixelHeight = height; + pixelDepth = depth; + numberOfArrayElements = numSlices; + numberOfFaces = numFaces; + } }; - static const size_t KTX_HEADER_SIZE = 64; + // Size as specified by the KTX specification + static const size_t KTX_HEADER_SIZE { 64 }; static_assert(sizeof(Header) == KTX_HEADER_SIZE, "KTX Header size is static and should not change from the spec"); - static const size_t KV_SIZE_WIDTH = 4; // Number of bytes for keyAndValueByteSize - static const size_t IMAGE_SIZE_WIDTH = 4; // Number of bytes for imageSize + static const size_t KV_SIZE_WIDTH { ALIGNMENT }; // Number of bytes for keyAndValueByteSize + static const size_t IMAGE_SIZE_WIDTH { ALIGNMENT }; // Number of bytes for imageSize // Key Values struct KeyValue { @@ -488,11 +320,9 @@ namespace ktx { class KTX { void resetStorage(const StoragePointer& src); - KTX(); + KTX() {} KTX(const StoragePointer& storage, const Header& header, const KeyValues& keyValues, const Images& images); public: - ~KTX(); - static bool validate(const StoragePointer& src); // Define a KTX object manually to write it somewhere (in a file on disk?) @@ -539,6 +369,7 @@ namespace ktx { KTXDescriptor toDescriptor() const; size_t getKeyValueDataSize() const; size_t getTexelsDataSize() const; + bool isValid() const; Header _header; StoragePointer _storage; diff --git a/libraries/ktx/src/ktx/Reader.cpp b/libraries/ktx/src/ktx/Reader.cpp index 1b63af5262..cfd9111ee3 100644 --- a/libraries/ktx/src/ktx/Reader.cpp +++ b/libraries/ktx/src/ktx/Reader.cpp @@ -151,7 +151,7 @@ namespace ktx { auto expectedImageSize = header.evalImageSize((uint32_t) images.size()); if (imageSize != expectedImageSize) { break; - } else if (!Header::checkAlignment(imageSize)) { + } else if (!checkAlignment(imageSize)) { break; } @@ -163,7 +163,7 @@ namespace ktx { // If enough data ahead then capture the pointer if ((currentPtr - srcBytes) + imageSize <= (srcSize)) { - auto padding = Header::evalPadding(imageSize); + auto padding = evalPadding(imageSize); if (numFaces == NUM_CUBEMAPFACES) { Image::FaceBytes faces(NUM_CUBEMAPFACES); diff --git a/libraries/ktx/src/ktx/Validation.cpp b/libraries/ktx/src/ktx/Validation.cpp index 74af58d311..a59dde92f1 100644 --- a/libraries/ktx/src/ktx/Validation.cpp +++ b/libraries/ktx/src/ktx/Validation.cpp @@ -63,111 +63,111 @@ static const std::unordered_set VALID_GL_FORMATS { }; static const std::unordered_set VALID_GL_INTERNAL_FORMATS { - (uint32_t)GLInternalFormat_Uncompressed::R8, - (uint32_t)GLInternalFormat_Uncompressed::R8_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::R16, - (uint32_t)GLInternalFormat_Uncompressed::R16_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::RG8, - (uint32_t)GLInternalFormat_Uncompressed::RG8_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::RG16, - (uint32_t)GLInternalFormat_Uncompressed::RG16_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::R3_G3_B2, - (uint32_t)GLInternalFormat_Uncompressed::RGB4, - (uint32_t)GLInternalFormat_Uncompressed::RGB5, - (uint32_t)GLInternalFormat_Uncompressed::RGB565, - (uint32_t)GLInternalFormat_Uncompressed::RGB8, - (uint32_t)GLInternalFormat_Uncompressed::RGB8_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::RGB10, - (uint32_t)GLInternalFormat_Uncompressed::RGB12, - (uint32_t)GLInternalFormat_Uncompressed::RGB16, - (uint32_t)GLInternalFormat_Uncompressed::RGB16_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::RGBA2, - (uint32_t)GLInternalFormat_Uncompressed::RGBA4, - (uint32_t)GLInternalFormat_Uncompressed::RGB5_A1, - (uint32_t)GLInternalFormat_Uncompressed::RGBA8, - (uint32_t)GLInternalFormat_Uncompressed::RGBA8_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::RGB10_A2, - (uint32_t)GLInternalFormat_Uncompressed::RGB10_A2UI, - (uint32_t)GLInternalFormat_Uncompressed::RGBA12, - (uint32_t)GLInternalFormat_Uncompressed::RGBA16, - (uint32_t)GLInternalFormat_Uncompressed::RGBA16_SNORM, - (uint32_t)GLInternalFormat_Uncompressed::SRGB8, - (uint32_t)GLInternalFormat_Uncompressed::SRGB8_ALPHA8, - (uint32_t)GLInternalFormat_Uncompressed::R16F, - (uint32_t)GLInternalFormat_Uncompressed::RG16F, - (uint32_t)GLInternalFormat_Uncompressed::RGB16F, - (uint32_t)GLInternalFormat_Uncompressed::RGBA16F, - (uint32_t)GLInternalFormat_Uncompressed::R32F, - (uint32_t)GLInternalFormat_Uncompressed::RG32F, - (uint32_t)GLInternalFormat_Uncompressed::RGBA32F, - (uint32_t)GLInternalFormat_Uncompressed::R11F_G11F_B10F, - (uint32_t)GLInternalFormat_Uncompressed::RGB9_E5, - (uint32_t)GLInternalFormat_Uncompressed::R8I, - (uint32_t)GLInternalFormat_Uncompressed::R8UI, - (uint32_t)GLInternalFormat_Uncompressed::R16I, - (uint32_t)GLInternalFormat_Uncompressed::R16UI, - (uint32_t)GLInternalFormat_Uncompressed::R32I, - (uint32_t)GLInternalFormat_Uncompressed::R32UI, - (uint32_t)GLInternalFormat_Uncompressed::RG8I, - (uint32_t)GLInternalFormat_Uncompressed::RG8UI, - (uint32_t)GLInternalFormat_Uncompressed::RG16I, - (uint32_t)GLInternalFormat_Uncompressed::RG16UI, - (uint32_t)GLInternalFormat_Uncompressed::RG32I, - (uint32_t)GLInternalFormat_Uncompressed::RG32UI, - (uint32_t)GLInternalFormat_Uncompressed::RGB8I, - (uint32_t)GLInternalFormat_Uncompressed::RGB8UI, - (uint32_t)GLInternalFormat_Uncompressed::RGB16I, - (uint32_t)GLInternalFormat_Uncompressed::RGB16UI, - (uint32_t)GLInternalFormat_Uncompressed::RGB32I, - (uint32_t)GLInternalFormat_Uncompressed::RGB32UI, - (uint32_t)GLInternalFormat_Uncompressed::RGBA8I, - (uint32_t)GLInternalFormat_Uncompressed::RGBA8UI, - (uint32_t)GLInternalFormat_Uncompressed::RGBA16I, - (uint32_t)GLInternalFormat_Uncompressed::RGBA16UI, - (uint32_t)GLInternalFormat_Uncompressed::RGBA32I, - (uint32_t)GLInternalFormat_Uncompressed::RGBA32UI, - (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT16, - (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT24, - (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT32, - (uint32_t)GLInternalFormat_Uncompressed::DEPTH_COMPONENT32F, - (uint32_t)GLInternalFormat_Uncompressed::DEPTH24_STENCIL8, - (uint32_t)GLInternalFormat_Uncompressed::DEPTH32F_STENCIL8, - (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX1, - (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX4, - (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX8, - (uint32_t)GLInternalFormat_Uncompressed::STENCIL_INDEX16, + (uint32_t)GLInternalFormat::R8, + (uint32_t)GLInternalFormat::R8_SNORM, + (uint32_t)GLInternalFormat::R16, + (uint32_t)GLInternalFormat::R16_SNORM, + (uint32_t)GLInternalFormat::RG8, + (uint32_t)GLInternalFormat::RG8_SNORM, + (uint32_t)GLInternalFormat::RG16, + (uint32_t)GLInternalFormat::RG16_SNORM, + (uint32_t)GLInternalFormat::R3_G3_B2, + (uint32_t)GLInternalFormat::RGB4, + (uint32_t)GLInternalFormat::RGB5, + (uint32_t)GLInternalFormat::RGB565, + (uint32_t)GLInternalFormat::RGB8, + (uint32_t)GLInternalFormat::RGB8_SNORM, + (uint32_t)GLInternalFormat::RGB10, + (uint32_t)GLInternalFormat::RGB12, + (uint32_t)GLInternalFormat::RGB16, + (uint32_t)GLInternalFormat::RGB16_SNORM, + (uint32_t)GLInternalFormat::RGBA2, + (uint32_t)GLInternalFormat::RGBA4, + (uint32_t)GLInternalFormat::RGB5_A1, + (uint32_t)GLInternalFormat::RGBA8, + (uint32_t)GLInternalFormat::RGBA8_SNORM, + (uint32_t)GLInternalFormat::RGB10_A2, + (uint32_t)GLInternalFormat::RGB10_A2UI, + (uint32_t)GLInternalFormat::RGBA12, + (uint32_t)GLInternalFormat::RGBA16, + (uint32_t)GLInternalFormat::RGBA16_SNORM, + (uint32_t)GLInternalFormat::SRGB8, + (uint32_t)GLInternalFormat::SRGB8_ALPHA8, + (uint32_t)GLInternalFormat::R16F, + (uint32_t)GLInternalFormat::RG16F, + (uint32_t)GLInternalFormat::RGB16F, + (uint32_t)GLInternalFormat::RGBA16F, + (uint32_t)GLInternalFormat::R32F, + (uint32_t)GLInternalFormat::RG32F, + (uint32_t)GLInternalFormat::RGBA32F, + (uint32_t)GLInternalFormat::R11F_G11F_B10F, + (uint32_t)GLInternalFormat::RGB9_E5, + (uint32_t)GLInternalFormat::R8I, + (uint32_t)GLInternalFormat::R8UI, + (uint32_t)GLInternalFormat::R16I, + (uint32_t)GLInternalFormat::R16UI, + (uint32_t)GLInternalFormat::R32I, + (uint32_t)GLInternalFormat::R32UI, + (uint32_t)GLInternalFormat::RG8I, + (uint32_t)GLInternalFormat::RG8UI, + (uint32_t)GLInternalFormat::RG16I, + (uint32_t)GLInternalFormat::RG16UI, + (uint32_t)GLInternalFormat::RG32I, + (uint32_t)GLInternalFormat::RG32UI, + (uint32_t)GLInternalFormat::RGB8I, + (uint32_t)GLInternalFormat::RGB8UI, + (uint32_t)GLInternalFormat::RGB16I, + (uint32_t)GLInternalFormat::RGB16UI, + (uint32_t)GLInternalFormat::RGB32I, + (uint32_t)GLInternalFormat::RGB32UI, + (uint32_t)GLInternalFormat::RGBA8I, + (uint32_t)GLInternalFormat::RGBA8UI, + (uint32_t)GLInternalFormat::RGBA16I, + (uint32_t)GLInternalFormat::RGBA16UI, + (uint32_t)GLInternalFormat::RGBA32I, + (uint32_t)GLInternalFormat::RGBA32UI, + (uint32_t)GLInternalFormat::DEPTH_COMPONENT16, + (uint32_t)GLInternalFormat::DEPTH_COMPONENT24, + (uint32_t)GLInternalFormat::DEPTH_COMPONENT32, + (uint32_t)GLInternalFormat::DEPTH_COMPONENT32F, + (uint32_t)GLInternalFormat::DEPTH24_STENCIL8, + (uint32_t)GLInternalFormat::DEPTH32F_STENCIL8, + (uint32_t)GLInternalFormat::STENCIL_INDEX1, + (uint32_t)GLInternalFormat::STENCIL_INDEX4, + (uint32_t)GLInternalFormat::STENCIL_INDEX8, + (uint32_t)GLInternalFormat::STENCIL_INDEX16, }; static const std::unordered_set VALID_GL_INTERNAL_COMPRESSED_FORMATS { - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RED, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RG, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGBA, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_ETC1_RGB8_OES, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_S3TC_DXT1_EXT, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RED_RGTC1, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_RED_RGTC1, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RG_RGTC2, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_RG_RGTC2, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGBA_BPTC_UNORM, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB_BPTC_SIGNED_FLOAT, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB8_ETC2, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB8_ETC2, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RGBA8_ETC2_EAC, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_R11_EAC, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_R11_EAC, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_RG11_EAC, - (uint32_t)GLInternalFormat_Compressed::COMPRESSED_SIGNED_RG11_EAC, + (uint32_t)GLInternalFormat::COMPRESSED_RED, + (uint32_t)GLInternalFormat::COMPRESSED_RG, + (uint32_t)GLInternalFormat::COMPRESSED_RGB, + (uint32_t)GLInternalFormat::COMPRESSED_RGBA, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB_ALPHA, + (uint32_t)GLInternalFormat::COMPRESSED_ETC1_RGB8_OES, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, + (uint32_t)GLInternalFormat::COMPRESSED_RED_RGTC1, + (uint32_t)GLInternalFormat::COMPRESSED_SIGNED_RED_RGTC1, + (uint32_t)GLInternalFormat::COMPRESSED_RG_RGTC2, + (uint32_t)GLInternalFormat::COMPRESSED_SIGNED_RG_RGTC2, + (uint32_t)GLInternalFormat::COMPRESSED_RGBA_BPTC_UNORM, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM, + (uint32_t)GLInternalFormat::COMPRESSED_RGB_BPTC_SIGNED_FLOAT, + (uint32_t)GLInternalFormat::COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT, + (uint32_t)GLInternalFormat::COMPRESSED_RGB8_ETC2, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB8_ETC2, + (uint32_t)GLInternalFormat::COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, + (uint32_t)GLInternalFormat::COMPRESSED_RGBA8_ETC2_EAC, + (uint32_t)GLInternalFormat::COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, + (uint32_t)GLInternalFormat::COMPRESSED_R11_EAC, + (uint32_t)GLInternalFormat::COMPRESSED_SIGNED_R11_EAC, + (uint32_t)GLInternalFormat::COMPRESSED_RG11_EAC, + (uint32_t)GLInternalFormat::COMPRESSED_SIGNED_RG11_EAC, }; static const std::unordered_set VALID_GL_BASE_INTERNAL_FORMATS { @@ -199,7 +199,17 @@ bool Header::isValid() const { return false; } - if (glFormat == (uint32_t)GLFormat::COMPRESSED_FORMAT && glType == (uint32_t)GLType::COMPRESSED_TYPE) { + if (isCompressed()) { + if (glType != COMPRESSED_TYPE) { + qDebug("Invalid type for compressed texture 0x%x", glType); + return false; + } + + if (glTypeSize != COMPRESSED_TYPE_SIZE) { + qDebug("Invalid type size for compressed texture %d", glTypeSize); + return false; + } + if (VALID_GL_INTERNAL_COMPRESSED_FORMATS.count(glInternalFormat) != 1) { qDebug("Invalid compressed internal format 0x%x", glInternalFormat); return false; @@ -241,7 +251,7 @@ bool Header::isValid() const { // FIXME validate numberOfMipmapLevels based on the dimensions? if ((bytesOfKeyValueData % 4) != 0) { - qDebug() << "Invalid keyvalue data size " << numberOfFaces; + qDebug() << "Invalid keyvalue data size " << bytesOfKeyValueData; return false; } @@ -374,3 +384,50 @@ bool KTX::validate(const StoragePointer& src) { return true; } + + + +bool KTX::isValid() const { + if (!_header.isValid()) { + return false; + } + + if (_images.size() != _header.numberOfMipmapLevels) { + return false; + } + + const auto start = _storage->data(); + const auto end = start + _storage->size(); + + // FIXME, do key value checks? + + for (const auto& image : _images) { + if (image._numFaces != _header.numberOfFaces) { + return false; + } + + for (const auto& facePointer : image._faceBytes) { + if (facePointer + image._faceSize > end) { + return false; + } + } + } + + + for (uint8_t mip = 0; mip < _header.numberOfMipmapLevels; ++mip) { + for (uint8_t face = 0; face < _header.numberOfFaces; ++face) { + auto faceStorage = getMipFaceTexelsData(mip, face); + // The face start offset must be 4 byte aligned + if (!checkAlignment(faceStorage->data() - start)) { + return false; + } + + // The face size must be 4 byte aligned + if (!checkAlignment(faceStorage->size())) { + return false; + } + } + } + + return true; +} \ No newline at end of file diff --git a/libraries/ktx/src/ktx/Writer.cpp b/libraries/ktx/src/ktx/Writer.cpp index 23f9d05596..e18a3f1adb 100644 --- a/libraries/ktx/src/ktx/Writer.cpp +++ b/libraries/ktx/src/ktx/Writer.cpp @@ -70,8 +70,7 @@ namespace ktx { for (uint32_t l = 0; l < numMips; l++) { if (images.size() > l) { storageSize += sizeof(uint32_t); - storageSize += images[l]._imageSize; - storageSize += Header::evalPadding(images[l]._imageSize); + storageSize += evalPadded(images[l]._imageSize); } } return storageSize; @@ -89,8 +88,7 @@ namespace ktx { for (uint32_t l = 0; l < numMips; l++) { if (imageDescriptors.size() > l) { storageSize += sizeof(uint32_t); - storageSize += imageDescriptors[l]._imageSize; - storageSize += Header::evalPadding(imageDescriptors[l]._imageSize); + storageSize += evalPadded(imageDescriptors[l]._imageSize); } } return storageSize; @@ -221,7 +219,7 @@ namespace ktx { // If enough data ahead then capture the copy source pointer if (currentDataSize + imageSize <= (allocatedImagesDataSize)) { - auto padding = Header::evalPadding(imageSize); + auto padding = evalPadding(imageSize); // Single face vs cubes if (srcImages[l]._numFaces == 1) { diff --git a/tests/ktx/src/main.cpp b/tests/ktx/src/main.cpp index 92b9091533..282c537ed2 100644 --- a/tests/ktx/src/main.cpp +++ b/tests/ktx/src/main.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -88,6 +89,20 @@ int main(int argc, char** argv) { QCoreApplication::setOrganizationDomain("highfidelity.com"); logger.reset(new FileLogger()); + Q_ASSERT(ktx::evalPadding(0) == 0); + Q_ASSERT(ktx::evalPadding(1) == 3); + Q_ASSERT(ktx::evalPadding(2) == 2); + Q_ASSERT(ktx::evalPadding(3) == 1); + Q_ASSERT(ktx::evalPadding(4) == 0); + Q_ASSERT(ktx::evalPadding(1024) == 0); + Q_ASSERT(ktx::evalPadding(1025) == 3); + Q_ASSERT(ktx::evalPadded(0) == 0); + Q_ASSERT(ktx::evalPadded(1) == 4); + Q_ASSERT(ktx::evalPadded(2) == 4); + Q_ASSERT(ktx::evalPadded(3) == 4); + Q_ASSERT(ktx::evalPadded(4) == 4); + Q_ASSERT(ktx::evalPadded(1024) == 1024); + Q_ASSERT(ktx::evalPadded(1025) == 1028); Q_ASSERT(sizeof(ktx::Header) == 12 + (sizeof(uint32_t) * 13)); DependencyManager::set(); @@ -100,8 +115,10 @@ int main(int argc, char** argv) { auto ktxMemory = gpu::Texture::serialize(*testTexture); { const auto& ktxStorage = ktxMemory->getStorage(); - QFile outFile(TEST_IMAGE_KTX); - if (!outFile.open(QFile::Truncate | QFile::ReadWrite)) { + Q_ASSERT_X(ktx::KTX::validate(ktxStorage), __FUNCTION__, "KTX storage validation failed"); + Q_ASSERT_X(ktxMemory->isValid(), __FUNCTION__, "KTX self-validation failed"); + QSaveFile outFile(TEST_IMAGE_KTX); + if (!outFile.open(QFile::WriteOnly)) { throw std::runtime_error("Unable to open file"); } auto ktxSize = ktxStorage->size(); @@ -109,7 +126,7 @@ int main(int argc, char** argv) { auto dest = outFile.map(0, ktxSize); memcpy(dest, ktxStorage->data(), ktxSize); outFile.unmap(dest); - outFile.close(); + outFile.commit(); } { @@ -169,7 +186,6 @@ int mainTemp(int, char**) { auto ktxFile = ktx::KTX::create(storage); ktx::KTXDescriptor ktxDescriptor = ktxFile->toDescriptor(); - assert(ktxFile->_keyValues == ktxDescriptor.keyValues); qDebug() << "Contains " << ktxDescriptor.keyValues.size() << " key value pairs"; for (const auto& kv : ktxDescriptor.keyValues) { From da41b5dc003ba3801fdd5996af3c21fef8ff635f Mon Sep 17 00:00:00 2001 From: seefo Date: Thu, 25 May 2017 09:20:04 -0700 Subject: [PATCH 04/14] Focus is now changed when adding a new list item in DS settings --- domain-server/resources/web/settings/js/settings.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/domain-server/resources/web/settings/js/settings.js b/domain-server/resources/web/settings/js/settings.js index 69bdf1df3f..8066223318 100644 --- a/domain-server/resources/web/settings/js/settings.js +++ b/domain-server/resources/web/settings/js/settings.js @@ -1379,6 +1379,8 @@ function addTableRow(row) { var setting_name = table.attr("name"); row.addClass(Settings.DATA_ROW_CLASS + " " + Settings.NEW_ROW_CLASS); + var focusChanged = false; + _.each(row.children(), function(element) { if ($(element).hasClass("numbered")) { // Index row @@ -1429,6 +1431,11 @@ function addTableRow(row) { }); } + if (!focusChanged) { + input.focus(); + focusChanged = true; + } + if (isCheckbox) { $(input).find("input").attr("data-changed", "true"); } else { From babae456d28b96376e4c3472c4ad7e1b0a73b8e1 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Wed, 24 May 2017 19:28:46 -0700 Subject: [PATCH 05/14] Fix infinite loop in fbx reader --- libraries/fbx/src/FBXReader.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 64ee0bc869..bde8e83fc5 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -212,7 +212,10 @@ public: glm::mat4 getGlobalTransform(const QMultiMap& _connectionParentMap, const QHash& models, QString nodeID, bool mixamoHack) { glm::mat4 globalTransform; + QVector visitedNodes; // Used to prevent following a cycle while (!nodeID.isNull()) { + visitedNodes.append(nodeID); // Append each node we visit + const FBXModel& model = models.value(nodeID); globalTransform = glm::translate(model.translation) * model.preTransform * glm::mat4_cast(model.preRotation * model.rotation * model.postRotation) * model.postTransform * globalTransform; @@ -223,7 +226,7 @@ glm::mat4 getGlobalTransform(const QMultiMap& _connectionParen QList parentIDs = _connectionParentMap.values(nodeID); nodeID = QString(); foreach (const QString& parentID, parentIDs) { - if (models.contains(parentID)) { + if (models.contains(parentID) && !visitedNodes.contains(parentID)) { nodeID = parentID; break; } @@ -349,9 +352,12 @@ void addBlendshapes(const ExtractedBlendshape& extracted, const QList& connectionParentMap, const QHash& models, const QString& modelID) { QString topID = modelID; + QVector visitedNodes; // Used to prevent following a cycle forever { + visitedNodes.append(topID); // Append each node we visit + foreach (const QString& parentID, connectionParentMap.values(topID)) { - if (models.contains(parentID)) { + if (models.contains(parentID) && !visitedNodes.contains(parentID)) { topID = parentID; goto outerContinue; } From ea870881eeb4111b2ea94a27785b36256dba2157 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 25 May 2017 12:48:21 -0700 Subject: [PATCH 06/14] Log warning when we detect a connection loop --- libraries/fbx/src/FBXReader.cpp | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index bde8e83fc5..236daf6443 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -210,7 +210,7 @@ public: }; glm::mat4 getGlobalTransform(const QMultiMap& _connectionParentMap, - const QHash& models, QString nodeID, bool mixamoHack) { + const QHash& models, QString nodeID, bool mixamoHack, const QString& url) { glm::mat4 globalTransform; QVector visitedNodes; // Used to prevent following a cycle while (!nodeID.isNull()) { @@ -226,7 +226,12 @@ glm::mat4 getGlobalTransform(const QMultiMap& _connectionParen QList parentIDs = _connectionParentMap.values(nodeID); nodeID = QString(); foreach (const QString& parentID, parentIDs) { - if (models.contains(parentID) && !visitedNodes.contains(parentID)) { + if (visitedNodes.contains(parentID)) { + qCWarning(modelformat) << "Ignoring loop detected in FBX connection map for" << url; + continue; + } + + if (models.contains(parentID)) { nodeID = parentID; break; } @@ -350,14 +355,19 @@ void addBlendshapes(const ExtractedBlendshape& extracted, const QList& connectionParentMap, - const QHash& models, const QString& modelID) { + const QHash& models, const QString& modelID, const QString& url) { QString topID = modelID; QVector visitedNodes; // Used to prevent following a cycle forever { visitedNodes.append(topID); // Append each node we visit foreach (const QString& parentID, connectionParentMap.values(topID)) { - if (models.contains(parentID) && !visitedNodes.contains(parentID)) { + if (visitedNodes.contains(parentID)) { + qCWarning(modelformat) << "Ignoring loop detected in FBX connection map for" << url; + continue; + } + + if (models.contains(parentID)) { topID = parentID; goto outerContinue; } @@ -1313,7 +1323,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS if (!clusters.contains(clusterID)) { continue; } - QString topID = getTopModelID(_connectionParentMap, models, _connectionChildMap.value(clusterID)); + QString topID = getTopModelID(_connectionParentMap, models, _connectionChildMap.value(clusterID), url); _connectionChildMap.remove(_connectionParentMap.take(model.key()), model.key()); _connectionParentMap.insert(model.key(), topID); goto outerBreak; @@ -1335,7 +1345,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS first = id; } } - QString topID = getTopModelID(_connectionParentMap, models, first); + QString topID = getTopModelID(_connectionParentMap, models, first, url); appendModelIDs(_connectionParentMap.value(topID), _connectionChildMap, models, remainingModels, modelIDs); } @@ -1517,7 +1527,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS // accumulate local transforms QString modelID = models.contains(it.key()) ? it.key() : _connectionParentMap.value(it.key()); - glm::mat4 modelTransform = getGlobalTransform(_connectionParentMap, models, modelID, geometry.applicationName == "mixamo.com"); + glm::mat4 modelTransform = getGlobalTransform(_connectionParentMap, models, modelID, geometry.applicationName == "mixamo.com", url); // compute the mesh extents from the transformed vertices foreach (const glm::vec3& vertex, extracted.mesh.vertices) { From 4d791211eef403095e8b6ea459d9a6d6c5cf5999 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Tue, 23 May 2017 10:11:16 -0700 Subject: [PATCH 07/14] Make the changes - big thanks to Andrew! --- libraries/entities-renderer/src/EntityTreeRenderer.cpp | 9 ++++++--- libraries/entities/src/EntityItemProperties.cpp | 7 ++++++- libraries/entities/src/EntityItemProperties.h | 1 + libraries/entities/src/EntityScriptingInterface.cpp | 10 ++++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 09308baabb..d62181c651 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -1091,15 +1091,18 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons // trigger scripted collision sounds and events for locally owned objects EntityItemPointer entityA = entityTree->findEntityByEntityItemID(idA); - if ((bool)entityA && myNodeID == entityA->getSimulatorID()) { + EntityItemPointer entityB = entityTree->findEntityByEntityItemID(idB); + QUuid entityASimulatorID = entityA->getSimulatorID(); + QUuid entityBSimulatorID = entityB->getSimulatorID(); + if ((bool)entityA && (myNodeID == entityASimulatorID || ((bool)entityB && myNodeID == entityBSimulatorID && entityASimulatorID.isNull()))) { playEntityCollisionSound(entityA, collision); emit collisionWithEntity(idA, idB, collision); if (_entitiesScriptEngine) { _entitiesScriptEngine->callEntityScriptMethod(idA, "collisionWithEntity", idB, collision); } } - EntityItemPointer entityB = entityTree->findEntityByEntityItemID(idB); - if ((bool)entityB && myNodeID == entityB->getSimulatorID()) { + + if ((bool)entityB && (myNodeID == entityBSimulatorID || ((bool)entityA && myNodeID == entityASimulatorID && entityBSimulatorID.isNull()))) { playEntityCollisionSound(entityB, collision); // since we're swapping A and B we need to send the inverted collision Collision invertedCollision(collision); diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 1ed020e592..4595ebf70d 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -1824,10 +1824,15 @@ bool EntityItemProperties::hasTerseUpdateChanges() const { return _positionChanged || _velocityChanged || _rotationChanged || _angularVelocityChanged || _accelerationChanged; } +bool EntityItemProperties::hasDynamicPhysicsChanges() const { + return _dynamicChanged || _velocityChanged || _angularVelocityChanged || _accelerationChanged; +} + bool EntityItemProperties::hasMiscPhysicsChanges() const { return _gravityChanged || _dimensionsChanged || _densityChanged || _frictionChanged || _restitutionChanged || _dampingChanged || _angularDampingChanged || _registrationPointChanged || - _compoundShapeURLChanged || _dynamicChanged || _collisionlessChanged || _collisionMaskChanged; + _compoundShapeURLChanged || _collisionlessChanged || _collisionMaskChanged || + _rotationChanged || _positionChanged; } void EntityItemProperties::clearSimulationOwner() { diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 590298e102..697c634c67 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -274,6 +274,7 @@ public: void setCreated(QDateTime& v); bool hasTerseUpdateChanges() const; + bool hasDynamicPhysicsChanges() const; bool hasMiscPhysicsChanges() const; void clearSimulationOwner(); diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index b184d648da..367343cb60 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -414,15 +414,17 @@ QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& entityFound = true; // make sure the properties has a type, so that the encode can know which properties to include properties.setType(entity->getType()); - bool hasTerseUpdateChanges = properties.hasTerseUpdateChanges(); - bool hasPhysicsChanges = properties.hasMiscPhysicsChanges() || hasTerseUpdateChanges; - if (_bidOnSimulationOwnership && hasPhysicsChanges) { + bool hasMiscPhysicsChanges = properties.hasMiscPhysicsChanges(); + bool hasDynamicsChanges = properties.hasDynamicPhysicsChanges(); + // _bidOnSimulationOwnership is set per-instance of the scripting interface. + // It essentially corresponds to "Am I an AC or an Interface client?" - ACs will never bid. + if ((_bidOnSimulationOwnership && ((hasMiscPhysicsChanges && entity->isMoving()) || hasDynamicsChanges))) { auto nodeList = DependencyManager::get(); const QUuid myNodeID = nodeList->getSessionUUID(); if (entity->getSimulatorID() == myNodeID) { // we think we already own the simulation, so make sure to send ALL TerseUpdate properties - if (hasTerseUpdateChanges) { + if (properties.hasTerseUpdateChanges()) { entity->getAllTerseUpdateProperties(properties); } // TODO: if we knew that ONLY TerseUpdate properties have changed in properties AND the object From acd85b379f67d3ab12836cc814c9d2374a422755 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Tue, 23 May 2017 12:50:43 -0700 Subject: [PATCH 08/14] Make this PR a protocol change --- libraries/networking/src/udt/PacketHeaders.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/networking/src/udt/PacketHeaders.h b/libraries/networking/src/udt/PacketHeaders.h index f88015a4e4..0dbbcd771f 100644 --- a/libraries/networking/src/udt/PacketHeaders.h +++ b/libraries/networking/src/udt/PacketHeaders.h @@ -114,7 +114,8 @@ public: EntityServerScriptLog, AdjustAvatarSorting, OctreeFileReplacement, - LAST_PACKET_TYPE = OctreeFileReplacement + SimulationBiddingChanges, + LAST_PACKET_TYPE = SimulationBiddingChanges }; }; From 0ef507c03791c399e5aebc4430275e060243b2e5 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Tue, 23 May 2017 14:41:02 -0700 Subject: [PATCH 09/14] Revert 'dynamicChanged' to be a 'misc' physics property --- libraries/entities/src/EntityItemProperties.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 4595ebf70d..fa4df72387 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -1825,13 +1825,13 @@ bool EntityItemProperties::hasTerseUpdateChanges() const { } bool EntityItemProperties::hasDynamicPhysicsChanges() const { - return _dynamicChanged || _velocityChanged || _angularVelocityChanged || _accelerationChanged; + return _velocityChanged || _angularVelocityChanged || _accelerationChanged; } bool EntityItemProperties::hasMiscPhysicsChanges() const { return _gravityChanged || _dimensionsChanged || _densityChanged || _frictionChanged || _restitutionChanged || _dampingChanged || _angularDampingChanged || _registrationPointChanged || - _compoundShapeURLChanged || _collisionlessChanged || _collisionMaskChanged || + _compoundShapeURLChanged || _dynamicChanged || _collisionlessChanged || _collisionMaskChanged || _rotationChanged || _positionChanged; } From f6195cdb1eb712c61c6965abdc99c05b95146eec Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 24 May 2017 10:16:51 -0700 Subject: [PATCH 10/14] Committing checkpoint changes, then testing --- .../src/EntityTreeRenderer.cpp | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index d62181c651..7b2333ade9 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -1092,24 +1092,29 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons // trigger scripted collision sounds and events for locally owned objects EntityItemPointer entityA = entityTree->findEntityByEntityItemID(idA); EntityItemPointer entityB = entityTree->findEntityByEntityItemID(idB); - QUuid entityASimulatorID = entityA->getSimulatorID(); - QUuid entityBSimulatorID = entityB->getSimulatorID(); - if ((bool)entityA && (myNodeID == entityASimulatorID || ((bool)entityB && myNodeID == entityBSimulatorID && entityASimulatorID.isNull()))) { - playEntityCollisionSound(entityA, collision); - emit collisionWithEntity(idA, idB, collision); - if (_entitiesScriptEngine) { - _entitiesScriptEngine->callEntityScriptMethod(idA, "collisionWithEntity", idB, collision); - } - } + if ((bool)entityA && (bool)entityB) { + QUuid entityASimulatorID = entityA->getSimulatorID(); + QUuid entityBSimulatorID = entityB->getSimulatorID(); + bool entityAIsStatic = !entityA->getDynamic() && !entityA->isMoving(); + bool entityBIsStatic = !entityB->getDynamic() && !entityB->isMoving(); - if ((bool)entityB && (myNodeID == entityBSimulatorID || ((bool)entityA && myNodeID == entityASimulatorID && entityBSimulatorID.isNull()))) { - playEntityCollisionSound(entityB, collision); - // since we're swapping A and B we need to send the inverted collision - Collision invertedCollision(collision); - invertedCollision.invert(); - emit collisionWithEntity(idB, idA, invertedCollision); - if (_entitiesScriptEngine) { - _entitiesScriptEngine->callEntityScriptMethod(idB, "collisionWithEntity", idA, invertedCollision); + if (myNodeID == entityASimulatorID || (myNodeID == entityBSimulatorID && entityAIsStatic)) { + playEntityCollisionSound(entityA, collision); + emit collisionWithEntity(idA, idB, collision); + if (_entitiesScriptEngine) { + _entitiesScriptEngine->callEntityScriptMethod(idA, "collisionWithEntity", idB, collision); + } + } + + if (myNodeID == entityBSimulatorID || (myNodeID == entityASimulatorID && entityBIsStatic)) { + playEntityCollisionSound(entityB, collision); + // since we're swapping A and B we need to send the inverted collision + Collision invertedCollision(collision); + invertedCollision.invert(); + emit collisionWithEntity(idB, idA, invertedCollision); + if (_entitiesScriptEngine) { + _entitiesScriptEngine->callEntityScriptMethod(idB, "collisionWithEntity", idA, invertedCollision); + } } } } From be398749997c78c8acf3e4de35afe03e866244ce Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 24 May 2017 11:17:03 -0700 Subject: [PATCH 11/14] Changes after discussion with Brad --- .../src/EntityTreeRenderer.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 7b2333ade9..fccc6f512f 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -1095,10 +1095,20 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons if ((bool)entityA && (bool)entityB) { QUuid entityASimulatorID = entityA->getSimulatorID(); QUuid entityBSimulatorID = entityB->getSimulatorID(); - bool entityAIsStatic = !entityA->getDynamic() && !entityA->isMoving(); - bool entityBIsStatic = !entityB->getDynamic() && !entityB->isMoving(); + bool entityAIsDynamic = entityA->getDynamic(); + bool entityBIsDynamic = entityB->getDynamic(); - if (myNodeID == entityASimulatorID || (myNodeID == entityBSimulatorID && entityAIsStatic)) { +#ifdef WANT_DEBUG + bool bothEntitiesStatic = !entityAIsDynamic && !entityBIsDynamic; + if (bothEntitiesStatic) { + qCDebug(entities) << "A collision has occurred between two static entities!"; + qCDebug(entities) << "Entity A ID:" << entityA->getID(); + qCDebug(entities) << "Entity B ID:" << entityB->getID(); + } + assert(!bothEntitiesStatic); +#endif + + if ((myNodeID == entityASimulatorID && entityAIsDynamic) || (myNodeID == entityBSimulatorID && !entityAIsDynamic)) { playEntityCollisionSound(entityA, collision); emit collisionWithEntity(idA, idB, collision); if (_entitiesScriptEngine) { @@ -1106,7 +1116,7 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons } } - if (myNodeID == entityBSimulatorID || (myNodeID == entityASimulatorID && entityBIsStatic)) { + if ((myNodeID == entityBSimulatorID && entityBIsDynamic) || (myNodeID == entityASimulatorID && !entityBIsDynamic)) { playEntityCollisionSound(entityB, collision); // since we're swapping A and B we need to send the inverted collision Collision invertedCollision(collision); From b86b07c08f03b8c11b0f593d97a6f68d92845269 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 24 May 2017 14:15:59 -0700 Subject: [PATCH 12/14] Pull out ownership bidding changes --- libraries/entities/src/EntityItemProperties.cpp | 7 +------ libraries/entities/src/EntityItemProperties.h | 1 - libraries/entities/src/EntityScriptingInterface.cpp | 10 ++++------ libraries/networking/src/udt/PacketHeaders.h | 4 ++-- 4 files changed, 7 insertions(+), 15 deletions(-) diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index fa4df72387..1ed020e592 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -1824,15 +1824,10 @@ bool EntityItemProperties::hasTerseUpdateChanges() const { return _positionChanged || _velocityChanged || _rotationChanged || _angularVelocityChanged || _accelerationChanged; } -bool EntityItemProperties::hasDynamicPhysicsChanges() const { - return _velocityChanged || _angularVelocityChanged || _accelerationChanged; -} - bool EntityItemProperties::hasMiscPhysicsChanges() const { return _gravityChanged || _dimensionsChanged || _densityChanged || _frictionChanged || _restitutionChanged || _dampingChanged || _angularDampingChanged || _registrationPointChanged || - _compoundShapeURLChanged || _dynamicChanged || _collisionlessChanged || _collisionMaskChanged || - _rotationChanged || _positionChanged; + _compoundShapeURLChanged || _dynamicChanged || _collisionlessChanged || _collisionMaskChanged; } void EntityItemProperties::clearSimulationOwner() { diff --git a/libraries/entities/src/EntityItemProperties.h b/libraries/entities/src/EntityItemProperties.h index 697c634c67..590298e102 100644 --- a/libraries/entities/src/EntityItemProperties.h +++ b/libraries/entities/src/EntityItemProperties.h @@ -274,7 +274,6 @@ public: void setCreated(QDateTime& v); bool hasTerseUpdateChanges() const; - bool hasDynamicPhysicsChanges() const; bool hasMiscPhysicsChanges() const; void clearSimulationOwner(); diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 367343cb60..b184d648da 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -414,17 +414,15 @@ QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& entityFound = true; // make sure the properties has a type, so that the encode can know which properties to include properties.setType(entity->getType()); - bool hasMiscPhysicsChanges = properties.hasMiscPhysicsChanges(); - bool hasDynamicsChanges = properties.hasDynamicPhysicsChanges(); - // _bidOnSimulationOwnership is set per-instance of the scripting interface. - // It essentially corresponds to "Am I an AC or an Interface client?" - ACs will never bid. - if ((_bidOnSimulationOwnership && ((hasMiscPhysicsChanges && entity->isMoving()) || hasDynamicsChanges))) { + bool hasTerseUpdateChanges = properties.hasTerseUpdateChanges(); + bool hasPhysicsChanges = properties.hasMiscPhysicsChanges() || hasTerseUpdateChanges; + if (_bidOnSimulationOwnership && hasPhysicsChanges) { auto nodeList = DependencyManager::get(); const QUuid myNodeID = nodeList->getSessionUUID(); if (entity->getSimulatorID() == myNodeID) { // we think we already own the simulation, so make sure to send ALL TerseUpdate properties - if (properties.hasTerseUpdateChanges()) { + if (hasTerseUpdateChanges) { entity->getAllTerseUpdateProperties(properties); } // TODO: if we knew that ONLY TerseUpdate properties have changed in properties AND the object diff --git a/libraries/networking/src/udt/PacketHeaders.h b/libraries/networking/src/udt/PacketHeaders.h index 0dbbcd771f..2cc3a2c42e 100644 --- a/libraries/networking/src/udt/PacketHeaders.h +++ b/libraries/networking/src/udt/PacketHeaders.h @@ -114,8 +114,8 @@ public: EntityServerScriptLog, AdjustAvatarSorting, OctreeFileReplacement, - SimulationBiddingChanges, - LAST_PACKET_TYPE = SimulationBiddingChanges + CollisionEventChanges, + LAST_PACKET_TYPE = CollisionEventChanges }; }; From f2fab5718735e5ffce04dc8fca8a9cf6612514da Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Thu, 25 May 2017 09:28:11 -0700 Subject: [PATCH 13/14] Update conditional to handle 'other unowned' case --- libraries/entities-renderer/src/EntityTreeRenderer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index fccc6f512f..e029ca6ada 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -1108,7 +1108,7 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons assert(!bothEntitiesStatic); #endif - if ((myNodeID == entityASimulatorID && entityAIsDynamic) || (myNodeID == entityBSimulatorID && !entityAIsDynamic)) { + if ((myNodeID == entityASimulatorID && entityAIsDynamic) || (myNodeID == entityBSimulatorID && (!entityAIsDynamic || entityASimulatorID.isNull()))) { playEntityCollisionSound(entityA, collision); emit collisionWithEntity(idA, idB, collision); if (_entitiesScriptEngine) { @@ -1116,7 +1116,7 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons } } - if ((myNodeID == entityBSimulatorID && entityBIsDynamic) || (myNodeID == entityASimulatorID && !entityBIsDynamic)) { + if ((myNodeID == entityBSimulatorID && entityBIsDynamic) || (myNodeID == entityASimulatorID && (!entityBIsDynamic || entityBSimulatorID.isNull()))) { playEntityCollisionSound(entityB, collision); // since we're swapping A and B we need to send the inverted collision Collision invertedCollision(collision); From aa5aba428a218a4871e56358fe27b0c9ea6dfcb4 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 25 May 2017 11:49:32 -0700 Subject: [PATCH 14/14] PR feedback --- libraries/ktx/src/khronos/KHR.h | 87 ++++++++++++++++++++++++---- libraries/ktx/src/ktx/KTX.cpp | 79 +++++++++---------------- libraries/ktx/src/ktx/KTX.h | 14 +++-- libraries/ktx/src/ktx/Validation.cpp | 17 ++---- libraries/ktx/src/ktx/Writer.cpp | 4 +- tests/ktx/src/main.cpp | 14 ++--- 6 files changed, 128 insertions(+), 87 deletions(-) diff --git a/libraries/ktx/src/khronos/KHR.h b/libraries/ktx/src/khronos/KHR.h index 7b3e3453c7..d710ca7b40 100644 --- a/libraries/ktx/src/khronos/KHR.h +++ b/libraries/ktx/src/khronos/KHR.h @@ -209,6 +209,44 @@ namespace khronos { COMPRESSED_SIGNED_RG11_EAC = 0x9273, }; + template + inline uint32_t evalAlignedCompressedBlockCount(uint32_t value) { + // FIXME add static assert that ALIGNMENT is a power of 2 + return (value + (ALIGNMENT - 1) / ALIGNMENT); + } + + inline uint8_t evalBlockAlignemnt(InternalFormat format, uint32_t value) { + switch (format) { + case InternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: // BC1 + case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: // BC1A + case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: // BC3 + case InternalFormat::COMPRESSED_RED_RGTC1: // BC4 + case InternalFormat::COMPRESSED_RG_RGTC2: // BC5 + case InternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: // BC7 + return evalAlignedCompressedBlockCount<4>(value); + + default: + throw std::runtime_error("Unknown format"); + } + } + + inline uint8_t evalCompressedBlockSize(InternalFormat format) { + switch (format) { + case InternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: + case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: + case InternalFormat::COMPRESSED_RED_RGTC1: + return 8; + + case InternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: + case InternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: + case InternalFormat::COMPRESSED_RG_RGTC2: + return 16; + + default: + return 0; + } + } + enum class BaseInternalFormat : uint32_t { // GL 4.4 Table 8.11 DEPTH_COMPONENT = 0x1902, @@ -220,19 +258,46 @@ namespace khronos { STENCIL_INDEX = 0x1901, }; - enum CubeMapFace { - POS_X = 0, - NEG_X = 1, - POS_Y = 2, - NEG_Y = 3, - POS_Z = 4, - NEG_Z = 5, - NUM_CUBEMAPFACES = 6, - }; + inline uint8_t evalComponentCount(BaseInternalFormat format) { + switch (format) { + case BaseInternalFormat::DEPTH_COMPONENT: + case BaseInternalFormat::STENCIL_INDEX: + case BaseInternalFormat::RED: + return 1; + + case BaseInternalFormat::DEPTH_STENCIL: + case BaseInternalFormat::RG: + return 2; + + case BaseInternalFormat::RGB: + return 3; + + case BaseInternalFormat::RGBA: + return 4; + + default: + break; + } + + return 0; + } + + namespace cubemap { + enum Constants { + NUM_CUBEMAPFACES = 6, + }; + + enum class Face { + POSITIVE_X = 0x8515, + NEGATIVE_X = 0x8516, + POSITIVE_Y = 0x8517, + NEGATIVE_Y = 0x8518, + POSITIVE_Z = 0x8519, + NEGATIVE_Z = 0x851A, + }; + } } - } - } #endif // khronos_khr_hpp diff --git a/libraries/ktx/src/ktx/KTX.cpp b/libraries/ktx/src/ktx/KTX.cpp index 1900c4a4b1..d6faee4cc7 100644 --- a/libraries/ktx/src/ktx/KTX.cpp +++ b/libraries/ktx/src/ktx/KTX.cpp @@ -28,70 +28,45 @@ uint32_t Header::evalMaxDimension() const { return std::max(getPixelWidth(), std::max(getPixelHeight(), getPixelDepth())); } -uint32_t Header::evalPixelOrBlockWidth(uint32_t level) const { - auto pixelWidth = std::max(getPixelWidth() >> level, 1U); +uint32_t Header::evalPixelOrBlockDimension(uint32_t pixelDimension) const { if (isCompressed()) { - return evalAlignedCount(pixelWidth); - } else { - return pixelWidth; - } + return khronos::gl::texture::evalBlockAlignemnt(getGLInternaFormat(), pixelDimension); + } + return pixelDimension; } + +uint32_t Header::evalMipPixelOrBlockDimension(uint32_t mipLevel, uint32_t pixelDimension) const { + uint32_t mipPixelDimension = evalMipDimension(mipLevel, pixelDimension); + return evalPixelOrBlockDimension(mipPixelDimension); +} + +uint32_t Header::evalPixelOrBlockWidth(uint32_t level) const { + return evalMipPixelOrBlockDimension(level, getPixelWidth()); +} + uint32_t Header::evalPixelOrBlockHeight(uint32_t level) const { - auto pixelWidth = std::max(getPixelHeight() >> level, 1U); - if (glType == COMPRESSED_TYPE) { - auto format = getGLInternaFormat(); - switch (format) { - case GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: // BC1 - case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: // BC1A - case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: // BC3 - case GLInternalFormat::COMPRESSED_RED_RGTC1: // BC4 - case GLInternalFormat::COMPRESSED_RG_RGTC2: // BC5 - case GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: // BC7 - return evalAlignedCount(pixelWidth); - default: - throw std::runtime_error("Unknown format"); - } - } else { - return pixelWidth; - } + return evalMipPixelOrBlockDimension(level, getPixelHeight()); } + uint32_t Header::evalPixelOrBlockDepth(uint32_t level) const { - return std::max(getPixelDepth() >> level, 1U); + return evalMipDimension(level, getPixelDepth()); } size_t Header::evalPixelOrBlockSize() const { + size_t result = 0; if (isCompressed()) { auto format = getGLInternaFormat(); - switch (format) { - case GLInternalFormat::COMPRESSED_SRGB_S3TC_DXT1_EXT: - case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: - case GLInternalFormat::COMPRESSED_RED_RGTC1: - return 8; - case GLInternalFormat::COMPRESSED_SRGB_ALPHA_BPTC_UNORM: - case GLInternalFormat::COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: - case GLInternalFormat::COMPRESSED_RG_RGTC2: - return 16; - default: - break; - } + result = khronos::gl::texture::evalCompressedBlockSize(format); } else { + // FIXME should really be using the internal format, not the base internal format auto baseFormat = getGLBaseInternalFormat(); - switch (baseFormat) { - case GLBaseInternalFormat::RED: - return 1; - case GLBaseInternalFormat::RG: - return 2; - case GLBaseInternalFormat::RGB: - return 3; - case GLBaseInternalFormat::RGBA: - return 4; - default: - break; - } + result = khronos::gl::texture::evalComponentCount(baseFormat); } - qWarning() << "Unknown ktx format: " << glFormat << " " << glBaseInternalFormat << " " << glInternalFormat; - return 0; + if (0 == result) { + qWarning() << "Unknown ktx format: " << glFormat << " " << glBaseInternalFormat << " " << glInternalFormat; + } + return result; } size_t Header::evalRowSize(uint32_t level) const { @@ -100,7 +75,7 @@ size_t Header::evalRowSize(uint32_t level) const { if (pixSize == 0) { return 0; } - return evalPadded(pixWidth * pixSize); + return evalPaddedSize(pixWidth * pixSize); } size_t Header::evalFaceSize(uint32_t level) const { @@ -184,7 +159,7 @@ KeyValue::KeyValue(const std::string& key, const std::string& value) : } uint32_t KeyValue::serializedByteSize() const { - return (uint32_t)sizeof(uint32_t) + evalPadded(_byteSize); + return (uint32_t)sizeof(uint32_t) + evalPaddedSize(_byteSize); } uint32_t KeyValue::serializedKeyValuesByteSize(const KeyValues& keyValues) { diff --git a/libraries/ktx/src/ktx/KTX.h b/libraries/ktx/src/ktx/KTX.h index c6570f87c6..b02e2ada75 100644 --- a/libraries/ktx/src/ktx/KTX.h +++ b/libraries/ktx/src/ktx/KTX.h @@ -82,7 +82,7 @@ namespace ktx { // Alignment constants static const uint32_t ALIGNMENT { sizeof(uint32_t) }; static const uint32_t ALIGNMENT_REMAINDER { ALIGNMENT - 1 }; - static const uint32_t NUM_CUBEMAPFACES = khronos::gl::texture::CubeMapFace::NUM_CUBEMAPFACES; + static const uint32_t NUM_CUBEMAPFACES = khronos::gl::texture::cubemap::NUM_CUBEMAPFACES; // FIXME move out of this header, not specific to ktx const std::string HIFI_MIN_POPULATED_MIP_KEY { "hifi.minMip" }; @@ -94,7 +94,6 @@ namespace ktx { using GLFormat = khronos::gl::texture::Format; using GLInternalFormat = khronos::gl::texture::InternalFormat; using GLBaseInternalFormat = khronos::gl::texture::BaseInternalFormat; - using CubeMapFace = khronos::gl::texture::CubeMapFace; using Storage = storage::Storage; using StoragePointer = std::shared_ptr; @@ -112,7 +111,7 @@ namespace ktx { // Returns the passed value rounded up to the next 4 byte aligned value, if it's not already 4 byte aligned template - inline T evalPadded(T value) { + inline T evalPaddedSize(T value) { return (value + ALIGNMENT_REMAINDER) & ~(T)ALIGNMENT_REMAINDER; } @@ -123,7 +122,7 @@ namespace ktx { template inline bool checkAlignment(T value) { - return ((value & 0x3) == 0); + return ((value & ALIGNMENT_REMAINDER) == 0); } @@ -216,6 +215,13 @@ namespace ktx { ImageDescriptors generateImageDescriptors() const; private: + uint32_t evalPixelOrBlockDimension(uint32_t pixelDimension) const; + uint32_t evalMipPixelOrBlockDimension(uint32_t level, uint32_t pixelDimension) const; + + static inline uint32_t evalMipDimension(uint32_t mipLevel, uint32_t pixelDimension) { + return std::max(pixelDimension >> mipLevel, 1U); + } + void setDimensions(uint32_t width, uint32_t height = 0, uint32_t depth = 0, uint32_t numSlices = 0, uint32_t numFaces = 1) { pixelWidth = (width > 0 ? width : 1); pixelHeight = height; diff --git a/libraries/ktx/src/ktx/Validation.cpp b/libraries/ktx/src/ktx/Validation.cpp index a59dde92f1..c54a259ab1 100644 --- a/libraries/ktx/src/ktx/Validation.cpp +++ b/libraries/ktx/src/ktx/Validation.cpp @@ -281,9 +281,7 @@ struct AlignedStreamBuffer { } bool skip(size_t skipSize) { - if ((skipSize % 4) != 0) { - skipSize += (3 - ((skipSize + 3) % 4)); - } + skipSize = ktx::evalPaddedSize(skipSize); if (skipSize > _size) { return false; } @@ -323,7 +321,7 @@ bool validateKeyValueData(AlignedStreamBuffer kvbuffer) { } bool KTX::validate(const StoragePointer& src) { - if ((src->size() % 4) != 0) { + if (!checkAlignment(src->size())) { // All KTX data is 4-byte aligned qDebug() << "Invalid size, not 4 byte aligned"; return false; @@ -363,17 +361,14 @@ bool KTX::validate(const StoragePointer& src) { return false; } - if (header.numberOfArrayElements == 0 && header.numberOfFaces == 6) { - for (uint8_t face = 0; face < NUM_CUBEMAPFACES; ++face) { + uint32_t arrayElements = header.numberOfArrayElements == 0 ? 1 : header.numberOfArrayElements; + for (uint32_t arrayElement = 0; arrayElement < arrayElements; ++arrayElement) { + for (uint8_t face = 0; face < header.numberOfFaces; ++face) { if (!buffer.skip(imageSize)) { - qDebug() << "Unable to skip past cubemap data"; + qDebug() << "Unable to skip past image data"; return false; } } - } else { - if (!buffer.skip(imageSize)) { - return false; - } } } diff --git a/libraries/ktx/src/ktx/Writer.cpp b/libraries/ktx/src/ktx/Writer.cpp index e18a3f1adb..c94856e598 100644 --- a/libraries/ktx/src/ktx/Writer.cpp +++ b/libraries/ktx/src/ktx/Writer.cpp @@ -70,7 +70,7 @@ namespace ktx { for (uint32_t l = 0; l < numMips; l++) { if (images.size() > l) { storageSize += sizeof(uint32_t); - storageSize += evalPadded(images[l]._imageSize); + storageSize += evalPaddedSize(images[l]._imageSize); } } return storageSize; @@ -88,7 +88,7 @@ namespace ktx { for (uint32_t l = 0; l < numMips; l++) { if (imageDescriptors.size() > l) { storageSize += sizeof(uint32_t); - storageSize += evalPadded(imageDescriptors[l]._imageSize); + storageSize += evalPaddedSize(imageDescriptors[l]._imageSize); } } return storageSize; diff --git a/tests/ktx/src/main.cpp b/tests/ktx/src/main.cpp index 282c537ed2..3b62b89948 100644 --- a/tests/ktx/src/main.cpp +++ b/tests/ktx/src/main.cpp @@ -96,13 +96,13 @@ int main(int argc, char** argv) { Q_ASSERT(ktx::evalPadding(4) == 0); Q_ASSERT(ktx::evalPadding(1024) == 0); Q_ASSERT(ktx::evalPadding(1025) == 3); - Q_ASSERT(ktx::evalPadded(0) == 0); - Q_ASSERT(ktx::evalPadded(1) == 4); - Q_ASSERT(ktx::evalPadded(2) == 4); - Q_ASSERT(ktx::evalPadded(3) == 4); - Q_ASSERT(ktx::evalPadded(4) == 4); - Q_ASSERT(ktx::evalPadded(1024) == 1024); - Q_ASSERT(ktx::evalPadded(1025) == 1028); + Q_ASSERT(ktx::evalPaddedSize(0) == 0); + Q_ASSERT(ktx::evalPaddedSize(1) == 4); + Q_ASSERT(ktx::evalPaddedSize(2) == 4); + Q_ASSERT(ktx::evalPaddedSize(3) == 4); + Q_ASSERT(ktx::evalPaddedSize(4) == 4); + Q_ASSERT(ktx::evalPaddedSize(1024) == 1024); + Q_ASSERT(ktx::evalPaddedSize(1025) == 1028); Q_ASSERT(sizeof(ktx::Header) == 12 + (sizeof(uint32_t) * 13)); DependencyManager::set();