Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors Ghoul’s texture IO by removing the extensible reader/writer registry and replacing it with STB-based free functions under ghoul::io::texture, updating call sites and build files accordingly.
Changes:
- Removed
TextureReaderBase/TextureWriterBaseplus the STB/CMAP concrete reader/writer classes, and simplified the public API to functions inghoul::io::texture. - Implemented STB-based
loadTexture/imageSizeandsaveTexturedirectly intexturereader.cpp/texturewriter.cpp. - Updated Assimp model loading to use the new texture reader API and cleaned up
src/CMakeLists.txtsources/headers lists.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/io/texture/texturewriter.cpp | Inlined STB-based texture writing into a free-function API |
| include/ghoul/io/texture/texturewriter.h | Replaced class-based writer API with ghoul::io::texture functions/exceptions |
| src/io/texture/texturereader.cpp | Inlined STB-based texture reading and extension support helpers |
| include/ghoul/io/texture/texturereader.h | Replaced class-based reader API with ghoul::io::texture functions/exceptions |
| src/io/model/modelreaderassimp.cpp | Updated embedded/local texture loading to call texture::loadTexture and new exception types |
| src/CMakeLists.txt | Removed deleted texture IO sources/headers from the build |
| src/io/texture/texturewriterstb.cpp (deleted) | Removed STB writer class implementation |
| include/ghoul/io/texture/texturewriterstb.h (deleted) | Removed STB writer class declaration |
| src/io/texture/texturewriterbase.cpp (deleted) | Removed writer base exception implementation |
| include/ghoul/io/texture/texturewriterbase.h (deleted) | Removed writer base abstraction |
| src/io/texture/texturereaderstb.cpp (deleted) | Removed STB reader class implementation |
| include/ghoul/io/texture/texturereaderstb.h (deleted) | Removed STB reader class declaration |
| src/io/texture/texturereadercmap.cpp (deleted) | Removed CMAP reader implementation (per PR note) |
| include/ghoul/io/texture/texturereadercmap.h (deleted) | Removed CMAP reader declaration |
| src/io/texture/texturereaderbase.cpp (deleted) | Removed reader base exception implementation |
| include/ghoul/io/texture/texturereaderbase.h (deleted) | Removed reader base abstraction |
Comments suppressed due to low confidence (1)
src/io/model/modelreaderassimp.cpp:156
- The embedded-texture load path catches
texture::InvalidLoadException, buttexture::loadTexture(void* memory, ...)no longer throwsInvalidLoadExceptionwhen the format is unsupported; it throwsMissingReaderExceptioninstead. As a result, unsupported embedded textures may now escape this try/catch and fail the model load rather than falling back to a debug texture.
Either update this catch list to handle texture::MissingReaderException (and/or std::exception) for the embedded case, or restore throwing InvalidLoadException from the reader for unsupported embedded formats.
try {
textureEntry.texture = texture::loadTexture(
static_cast<void*>(texture->pcData),
texture->mWidth,
2,
{},
texture->achFormatHint
);
textureEntry.texture->setName(path.C_Str());
meshTexture.texture = textureEntry.texture.get();
}
catch (const texture::InvalidLoadException& e) {
LWARNING(std::format(
"Could not load unsupported texture from '{}' with size "
"'{}': Replacing with flashy color", e._memory, e._size
));
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| // Make sure the directory for the file exists | ||
| std::filesystem::create_directory(std::filesystem::path(filename).parent_path()); | ||
| writer->saveTexture(texture, filename); | ||
|
|
There was a problem hiding this comment.
saveTexture unconditionally calls std::filesystem::create_directory(path(filename).parent_path()). If filename is in the current directory (no parent path), parent_path() is empty and create_directory("") can throw std::filesystem::filesystem_error.
Consider guarding with if (!parent.empty()) and using create_directories to handle nested paths.
| if (extension == "jpeg" || extension == "jpg") { | ||
| stbi_write_jpg(filename.c_str(), w, h, nComponents, pixels.data(), 0); | ||
| } | ||
| else if (extension == "png") { | ||
| stbi_write_png(filename.c_str(), w, h, nComponents, pixels.data(), 0); | ||
| } | ||
| else if (extension == "bmp") { | ||
| stbi_write_bmp(filename.c_str(), w, h, nComponents, pixels.data()); | ||
| } | ||
| else if (extension == "tga") { | ||
| stbi_write_tga(filename.c_str(), w, h, nComponents, pixels.data()); | ||
| } |
There was a problem hiding this comment.
The STB write calls (stbi_write_*) return a success/failure value, but the return is currently ignored. This means write failures (permissions, disk full, invalid path) will silently succeed from the caller's perspective.
Capture the return value and throw TextureWriteException when a write fails so callers can handle errors deterministically.
Co-authored-by: Emma Broman <emmabroman740@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Alexander Bock <mail@alexanderbock.eu>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Simplifies the code by:
texturenamespaceNote that this breaks the support for reading .cmap files as textures. This has been reimplemented in the OpenSpace part of the PR:
Also not that this is a first step as part of a bigger rewrite to give us more flexibility in the texture reading, now that we can't change the format, etc., of a texture after it's created.