Skip to content

Commit

Permalink
[Tests/Render/Texture] Added unit tests for loading images to textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Feb 2, 2024
1 parent 65da76e commit 7b999ca
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/RaZ/Render/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,14 @@ void Texture3D::load(const std::vector<Image>& imageSlices, bool createMipmaps)
return;
}

const ImageColorspace firstImgColorspace = imageSlices.front().getColorspace();
const ImageDataType firstImgDataType = imageSlices.front().getDataType();

m_width = imageSlices.front().getWidth();
m_height = imageSlices.front().getHeight();
m_depth = static_cast<unsigned int>(imageSlices.size());
m_colorspace = static_cast<TextureColorspace>(imageSlices.front().getColorspace());
m_dataType = (imageSlices.front().getDataType() == ImageDataType::FLOAT ? TextureDataType::FLOAT16 : TextureDataType::BYTE);
m_colorspace = static_cast<TextureColorspace>(firstImgColorspace);
m_dataType = (firstImgDataType == ImageDataType::FLOAT ? TextureDataType::FLOAT16 : TextureDataType::BYTE);

#if defined(USE_OPENGL_ES)
if ((m_width & (m_width - 1)) != 0 || (m_height & (m_height - 1)) != 0 || (m_depth & (m_depth - 1)) != 0) {
Expand All @@ -397,15 +400,15 @@ void Texture3D::load(const std::vector<Image>& imageSlices, bool createMipmaps)

load();

const TextureFormat format = recoverFormat(m_colorspace);
const TextureFormat textureFormat = recoverFormat(m_colorspace);
const PixelDataType pixelDataType = (m_dataType == TextureDataType::BYTE ? PixelDataType::UBYTE : PixelDataType::FLOAT);

bind();

for (std::size_t imgIndex = 0; imgIndex < imageSlices.size(); ++imgIndex) {
const Image& img = imageSlices[imgIndex];

if (img.getWidth() != m_width || img.getHeight() != m_height || static_cast<TextureColorspace>(img.getColorspace()) != m_colorspace)
if (img.getWidth() != m_width || img.getHeight() != m_height || img.getColorspace() != firstImgColorspace || img.getDataType() != firstImgDataType)
throw std::invalid_argument("[Texture3D] The given images have different attributes.");

Renderer::sendImageSubData3D(TextureType::TEXTURE_3D,
Expand All @@ -416,7 +419,7 @@ void Texture3D::load(const std::vector<Image>& imageSlices, bool createMipmaps)
m_width,
m_height,
1,
format,
textureFormat,
pixelDataType,
img.getDataPtr());
}
Expand Down
52 changes: 52 additions & 0 deletions tests/src/RaZ/Render/Texture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Catch.hpp"

#include "RaZ/Data/Color.hpp"
#include "RaZ/Data/Image.hpp"
#include "RaZ/Render/Renderer.hpp"
#include "RaZ/Render/Texture.hpp"

Expand Down Expand Up @@ -165,6 +166,57 @@ TEST_CASE("Texture move") {
CHECK(movedTexture3DCtor.getIndex() == movedTexture3DOpIndex);
}

TEST_CASE("Texture2D load image") {
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));
img.setPixel(0, 1, static_cast<uint8_t>(2));
img.setPixel(1, 1, static_cast<uint8_t>(3));

const Raz::Texture2D texture2D(img);

CHECK(texture2D.getWidth() == img.getWidth());
CHECK(texture2D.getHeight() == img.getHeight());
CHECK(texture2D.getColorspace() == Raz::TextureColorspace::GRAY);
CHECK(texture2D.getDataType() == Raz::TextureDataType::BYTE);

#if !defined(USE_OPENGL_ES)
const Raz::Image textureImg = texture2D.recoverImage();
REQUIRE_FALSE(textureImg.isEmpty());
REQUIRE(textureImg.getWidth() == 2);
REQUIRE(textureImg.getHeight() == 2);
REQUIRE(textureImg.getColorspace() == Raz::ImageColorspace::GRAY);
REQUIRE(textureImg.getDataType() == Raz::ImageDataType::BYTE);
CHECK(textureImg.recoverPixel<uint8_t>(0, 0) == 0);
CHECK(textureImg.recoverPixel<uint8_t>(1, 0) == 1);
CHECK(textureImg.recoverPixel<uint8_t>(0, 1) == 2);
CHECK(textureImg.recoverPixel<uint8_t>(1, 1) == 3);
#endif
}

TEST_CASE("Texture3D load image slices") {
std::vector<Raz::Image> imageSlices;
imageSlices.emplace_back(2, 2, Raz::ImageColorspace::GRAY, Raz::ImageDataType::FLOAT).setPixel(0, 0, 1.234f);
imageSlices.emplace_back(2, 2, Raz::ImageColorspace::GRAY, Raz::ImageDataType::FLOAT).setPixel(1, 1, 2.345f);

const Raz::Texture3D texture3D(imageSlices);

CHECK(texture3D.getWidth() == imageSlices.front().getWidth());
CHECK(texture3D.getHeight() == imageSlices.front().getHeight());
CHECK(texture3D.getDepth() == imageSlices.size());
CHECK(texture3D.getColorspace() == Raz::TextureColorspace::GRAY);
CHECK(texture3D.getDataType() == Raz::TextureDataType::FLOAT16);

CHECK_THROWS(Raz::Texture3D({ Raz::Image(1, 1, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE),
Raz::Image(2, 1, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE) })); // Different widths
CHECK_THROWS(Raz::Texture3D({ Raz::Image(1, 1, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE),
Raz::Image(1, 2, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE) })); // Different heights
CHECK_THROWS(Raz::Texture3D({ Raz::Image(1, 1, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE),
Raz::Image(1, 1, Raz::ImageColorspace::RGB, Raz::ImageDataType::BYTE) })); // Different colorspaces
CHECK_THROWS(Raz::Texture3D({ Raz::Image(1, 1, Raz::ImageColorspace::GRAY, Raz::ImageDataType::BYTE),
Raz::Image(1, 1, Raz::ImageColorspace::GRAY, Raz::ImageDataType::FLOAT) })); // Different data types
}

TEST_CASE("Texture fill") {
Raz::Renderer::recoverErrors(); // Flushing errors

Expand Down

0 comments on commit 7b999ca

Please sign in to comment.