From 81d4be339f44a356963333b30de7d24ceaea48ae Mon Sep 17 00:00:00 2001 From: Razakhel Date: Sat, 18 May 2024 22:24:00 +0200 Subject: [PATCH] [Render/Texture] Force unpack alignment on single-channel image loading - This allows direct upload of the image data --- CMakeLists.txt | 4 ++-- src/RaZ/Render/Texture.cpp | 10 ++++++++++ tests/src/RaZ/Render/Cubemap.cpp | 2 +- tests/src/RaZ/Render/Texture.cpp | 5 +++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 979ec46d..835ef049 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -349,6 +349,8 @@ if (RAZ_COMPILER_MSVC OR (RAZ_COMPILER_GCC AND NOT MINGW)) # FBX SDK unavailable endif () message(STATUS "[RaZ] FBX support ENABLED") + else () + message(STATUS "[RaZ] FBX support DISABLED") endif () endif () @@ -361,8 +363,6 @@ if (NOT RAZ_USE_FBX OR NOT FBX_FOUND) "${PROJECT_SOURCE_DIR}/src/RaZ/Data/FbxLoad.cpp" "${PROJECT_SOURCE_DIR}/include/RaZ/Data/FbxFormat.hpp" ) - - message(STATUS "[RaZ] FBX support DISABLED") endif () ######################### diff --git a/src/RaZ/Render/Texture.cpp b/src/RaZ/Render/Texture.cpp index ac0508e3..475c175c 100644 --- a/src/RaZ/Render/Texture.cpp +++ b/src/RaZ/Render/Texture.cpp @@ -316,6 +316,13 @@ void Texture2D::load(const Image& image, bool createMipmaps) { } #endif + int unpackAlignment = 4; + + if (image.getChannelCount() == 1) { + Renderer::getParameter(StateParameter::UNPACK_ALIGNMENT, &unpackAlignment); + Renderer::setPixelStorage(PixelStorage::UNPACK_ALIGNMENT, 1); + } + bind(); Renderer::sendImageData2D(TextureType::TEXTURE_2D, @@ -327,6 +334,9 @@ void Texture2D::load(const Image& image, bool createMipmaps) { (m_dataType == TextureDataType::BYTE ? PixelDataType::UBYTE : PixelDataType::FLOAT), image.getDataPtr()); + if (image.getChannelCount() == 1) + Renderer::setPixelStorage(PixelStorage::UNPACK_ALIGNMENT, unpackAlignment); + setLoadedParameters(createMipmaps); } diff --git a/tests/src/RaZ/Render/Cubemap.cpp b/tests/src/RaZ/Render/Cubemap.cpp index 968ffc88..ad28c021 100644 --- a/tests/src/RaZ/Render/Cubemap.cpp +++ b/tests/src/RaZ/Render/Cubemap.cpp @@ -52,7 +52,7 @@ TEST_CASE("Cubemap textures", "[render]") { CHECK_FALSE(Raz::Renderer::hasErrors()); #if !defined(USE_OPENGL_ES) // Renderer::recoverTexture*() functions are unavailable with OpenGL ES - std::vector textureData {}; + std::vector textureData; cubemap.bind(); diff --git a/tests/src/RaZ/Render/Texture.cpp b/tests/src/RaZ/Render/Texture.cpp index 571be199..46ab42c5 100644 --- a/tests/src/RaZ/Render/Texture.cpp +++ b/tests/src/RaZ/Render/Texture.cpp @@ -167,6 +167,9 @@ TEST_CASE("Texture move", "[render]") { } TEST_CASE("Texture2D load image") { + // Resetting the unpack alignment to the default value, in order to check that loading an image handles it properly + Raz::Renderer::setPixelStorage(Raz::PixelStorage::UNPACK_ALIGNMENT, 4); + Raz::Image img(2, 2, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE); img.setPixel(0, 0, static_cast(0)); img.setPixel(1, 0, static_cast(1)); @@ -190,6 +193,8 @@ TEST_CASE("Texture2D load image") { CHECK(textureImg.recoverPixel(1, 0) == 1); CHECK(textureImg.recoverPixel(0, 1) == 2); CHECK(textureImg.recoverPixel(1, 1) == 3); + + Raz::Renderer::setPixelStorage(Raz::PixelStorage::UNPACK_ALIGNMENT, 1); } TEST_CASE("Texture3D load image slices") {