Skip to content

Commit

Permalink
IGL: Tidy up STB texture loaders
Browse files Browse the repository at this point in the history
Summary: This diff does some tidy up on the STB Image-based texture loaders. More of the functionality is moved into the common base class, and some unnecessary includes are removed.

Reviewed By: MichaelTay

Differential Revision: D49274140

fbshipit-source-id: 18a7244c129af4f360f3c284e757631bf37f5b4d
  • Loading branch information
Eric Griffith authored and facebook-github-bot committed Sep 16, 2023
1 parent 7e7d49a commit e3e092f
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 125 deletions.
34 changes: 4 additions & 30 deletions IGLU/texture_loader/stb_hdr/TextureLoaderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,18 @@
#include <IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h>

#include <IGLU/texture_loader/stb_hdr/Header.h>
#include <igl/IGLSafeC.h>
#include <numeric>
#include <vector>

namespace iglu::textureloader::stb::hdr {

TextureLoaderFactory::TextureLoaderFactory() noexcept : image::TextureLoaderFactory(true) {}

uint32_t TextureLoaderFactory::headerLength() const noexcept {
return kHeaderLength;
}

bool TextureLoaderFactory::isFloatFormat() const noexcept {
return true;
}

igl::TextureFormat TextureLoaderFactory::format() const noexcept {
return igl::TextureFormat::RGBA_F32;
}

bool TextureLoaderFactory::canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept {
if (headerReader.data() == nullptr) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr.");
return false;
}
if (headerReader.length() < kHeaderLength) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header.");
return false;
}

bool TextureLoaderFactory::isIdentifierValid(DataReader headerReader) const noexcept {
const Header* header = headerReader.as<Header>();
if (!header->tagIsValid()) {
igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier.");
return false;
}

return true;
return header->tagIsValid();
}

} // namespace iglu::textureloader::stb::hdr
8 changes: 2 additions & 6 deletions IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ namespace iglu::textureloader::stb::hdr {

class TextureLoaderFactory final : public image::TextureLoaderFactory {
public:
explicit TextureLoaderFactory() noexcept = default;
TextureLoaderFactory() noexcept;

[[nodiscard]] uint32_t headerLength() const noexcept final;

private:
[[nodiscard]] bool isFloatFormat() const noexcept final;
[[nodiscard]] igl::TextureFormat format() const noexcept final;

[[nodiscard]] bool canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept final;
[[nodiscard]] bool isIdentifierValid(DataReader headerReader) const noexcept final;
};

} // namespace iglu::textureloader::stb::hdr
41 changes: 29 additions & 12 deletions IGLU/texture_loader/stb_image/TextureLoaderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ class TextureLoader : public ITextureLoader {
using Super = ITextureLoader;

public:
explicit TextureLoader(DataReader reader,
int width,
int height,
igl::TextureFormat format,
bool isFloatFormat) noexcept;
explicit TextureLoader(DataReader reader, int width, int height, bool isFloatFormat) noexcept;

[[nodiscard]] bool canUploadSourceData() const noexcept final;
[[nodiscard]] bool shouldGenerateMipmaps() const noexcept final;
Expand All @@ -66,14 +62,11 @@ class TextureLoader : public ITextureLoader {
bool isFloatFormat_;
};

TextureLoader::TextureLoader(DataReader reader,
int width,
int height,
igl::TextureFormat format,
bool isFloatFormat) noexcept :
TextureLoader::TextureLoader(DataReader reader, int width, int height, bool isFloatFormat) noexcept
:
Super(reader), isFloatFormat_(isFloatFormat) {
auto& desc = mutableDescriptor();
desc.format = format;
desc.format = isFloatFormat ? igl::TextureFormat::RGBA_F32 : igl::TextureFormat::RGBA_UNorm8;
desc.numLayers = 1;
desc.width = static_cast<size_t>(width);
desc.height = static_cast<size_t>(height);
Expand Down Expand Up @@ -116,6 +109,30 @@ std::unique_ptr<IData> TextureLoader::loadInternal(
}
} // namespace

TextureLoaderFactory::TextureLoaderFactory(bool isFloatFormat) noexcept :
isFloatFormat_(isFloatFormat) {}

bool TextureLoaderFactory::canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept {
if (headerReader.data() == nullptr) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr.");
return false;
}
if (headerReader.length() < headerLength()) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header.");
return false;
}

if (!isIdentifierValid(headerReader)) {
igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier.");
return false;
}

return true;
}

std::unique_ptr<ITextureLoader> TextureLoaderFactory::tryCreateInternal(
DataReader reader,
igl::Result* IGL_NULLABLE outResult) const noexcept {
Expand All @@ -140,7 +157,7 @@ std::unique_ptr<ITextureLoader> TextureLoaderFactory::tryCreateInternal(
return nullptr;
}

return std::make_unique<TextureLoader>(reader, x, y, format(), isFloatFormat());
return std::make_unique<TextureLoader>(reader, x, y, isFloatFormat_);
}

} // namespace iglu::textureloader::stb::image
13 changes: 8 additions & 5 deletions IGLU/texture_loader/stb_image/TextureLoaderFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@
namespace iglu::textureloader::stb::image {

class TextureLoaderFactory : public ITextureLoaderFactory {
public:
explicit TextureLoaderFactory() noexcept = default;

protected:
[[nodiscard]] virtual bool isFloatFormat() const noexcept = 0;
[[nodiscard]] virtual igl::TextureFormat format() const noexcept = 0;
explicit TextureLoaderFactory(bool isFloatFormat = false) noexcept;

[[nodiscard]] virtual bool isIdentifierValid(DataReader headerReader) const noexcept = 0;

private:
[[nodiscard]] bool canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept final;

[[nodiscard]] std::unique_ptr<ITextureLoader> tryCreateInternal(
DataReader reader,
igl::Result* IGL_NULLABLE outResult) const noexcept final;

bool isFloatFormat_;
};

} // namespace iglu::textureloader::stb::image
32 changes: 2 additions & 30 deletions IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,16 @@
#include <IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h>

#include <IGLU/texture_loader/stb_jpeg/Header.h>
#include <igl/IGLSafeC.h>
#include <numeric>
#include <vector>

namespace iglu::textureloader::stb::jpeg {

uint32_t TextureLoaderFactory::headerLength() const noexcept {
return kHeaderLength;
}

bool TextureLoaderFactory::isFloatFormat() const noexcept {
return false;
}

igl::TextureFormat TextureLoaderFactory::format() const noexcept {
return igl::TextureFormat::RGBA_SRGB;
}

bool TextureLoaderFactory::canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept {
if (headerReader.data() == nullptr) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr.");
return false;
}
if (headerReader.length() < kHeaderLength) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header.");
return false;
}

bool TextureLoaderFactory::isIdentifierValid(DataReader headerReader) const noexcept {
const Header* header = headerReader.as<Header>();
if (!header->tagIsValid()) {
igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier.");
return false;
}

return true;
return header->tagIsValid();
}

} // namespace iglu::textureloader::stb::jpeg
8 changes: 2 additions & 6 deletions IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ namespace iglu::textureloader::stb::jpeg {

class TextureLoaderFactory final : public image::TextureLoaderFactory {
public:
explicit TextureLoaderFactory() noexcept = default;
TextureLoaderFactory() noexcept = default;

[[nodiscard]] uint32_t headerLength() const noexcept final;

private:
[[nodiscard]] bool isFloatFormat() const noexcept final;
[[nodiscard]] igl::TextureFormat format() const noexcept final;

[[nodiscard]] bool canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept final;
[[nodiscard]] bool isIdentifierValid(DataReader headerReader) const noexcept final;
};

} // namespace iglu::textureloader::stb::jpeg
32 changes: 2 additions & 30 deletions IGLU/texture_loader/stb_png/TextureLoaderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,16 @@
#include <IGLU/texture_loader/stb_png/TextureLoaderFactory.h>

#include <IGLU/texture_loader/stb_png/Header.h>
#include <igl/IGLSafeC.h>
#include <numeric>
#include <vector>

namespace iglu::textureloader::stb::png {

uint32_t TextureLoaderFactory::headerLength() const noexcept {
return kHeaderLength;
}

bool TextureLoaderFactory::isFloatFormat() const noexcept {
return false;
}

igl::TextureFormat TextureLoaderFactory::format() const noexcept {
return igl::TextureFormat::RGBA_SRGB;
}

bool TextureLoaderFactory::canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept {
if (headerReader.data() == nullptr) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr.");
return false;
}
if (headerReader.length() < kHeaderLength) {
igl::Result::setResult(
outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header.");
return false;
}

bool TextureLoaderFactory::isIdentifierValid(DataReader headerReader) const noexcept {
const Header* header = headerReader.as<Header>();
if (!header->tagIsValid()) {
igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier.");
return false;
}

return true;
return header->tagIsValid();
}

} // namespace iglu::textureloader::stb::png
8 changes: 2 additions & 6 deletions IGLU/texture_loader/stb_png/TextureLoaderFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ namespace iglu::textureloader::stb::png {

class TextureLoaderFactory final : public image::TextureLoaderFactory {
public:
explicit TextureLoaderFactory() noexcept = default;
TextureLoaderFactory() noexcept = default;

[[nodiscard]] uint32_t headerLength() const noexcept final;

private:
[[nodiscard]] bool isFloatFormat() const noexcept final;
[[nodiscard]] igl::TextureFormat format() const noexcept final;

[[nodiscard]] bool canCreateInternal(DataReader headerReader,
igl::Result* IGL_NULLABLE outResult) const noexcept final;
[[nodiscard]] bool isIdentifierValid(DataReader headerReader) const noexcept final;
};

} // namespace iglu::textureloader::stb::png

0 comments on commit e3e092f

Please sign in to comment.