Skip to content

Commit

Permalink
[Render/Texture] Force unpack alignment on single-channel image loading
Browse files Browse the repository at this point in the history
- This allows direct upload of the image data
  • Loading branch information
Razakhel committed May 18, 2024
1 parent 235b769 commit 81d4be3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Expand All @@ -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 ()

#########################
Expand Down
10 changes: 10 additions & 0 deletions src/RaZ/Render/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/RaZ/Render/Cubemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> textureData {};
std::vector<uint8_t> textureData;

cubemap.bind();

Expand Down
5 changes: 5 additions & 0 deletions tests/src/RaZ/Render/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t>(0));
img.setPixel(1, 0, static_cast<uint8_t>(1));
Expand All @@ -190,6 +193,8 @@ TEST_CASE("Texture2D load image") {
CHECK(textureImg.recoverPixel<uint8_t>(1, 0) == 1);
CHECK(textureImg.recoverPixel<uint8_t>(0, 1) == 2);
CHECK(textureImg.recoverPixel<uint8_t>(1, 1) == 3);

Raz::Renderer::setPixelStorage(Raz::PixelStorage::UNPACK_ALIGNMENT, 1);
}

TEST_CASE("Texture3D load image slices") {
Expand Down

0 comments on commit 81d4be3

Please sign in to comment.