From 335bb82be1c8776691e8b1a446c8fdc36e68eba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Tue, 25 May 2021 15:37:55 +0200 Subject: [PATCH] Renderer/Texture: Add static helpers --- examples/DeferredShading/main.cpp | 74 +++-------------------------- examples/GraphicsTest/main.cpp | 44 +++-------------- examples/RenderTest/main.cpp | 24 ++-------- include/Nazara/Renderer/Texture.hpp | 28 ++++++++++- src/Nazara/Renderer/Texture.cpp | 63 ++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 127 deletions(-) diff --git a/examples/DeferredShading/main.cpp b/examples/DeferredShading/main.cpp index 6f613e78c..94f08a291 100644 --- a/examples/DeferredShading/main.cpp +++ b/examples/DeferredShading/main.cpp @@ -106,71 +106,9 @@ int main() std::shared_ptr gfxMesh = std::make_shared(*spaceship); - // Spaceship texture - std::shared_ptr spaceshipDiffuse = Nz::Image::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png"); - if (!spaceshipDiffuse || !spaceshipDiffuse->Convert(Nz::PixelFormat::RGBA8_SRGB)) - { - NazaraError("Failed to load image"); - return __LINE__; - } - - Nz::TextureInfo texParams; - texParams.pixelFormat = spaceshipDiffuse->GetFormat(); - texParams.type = spaceshipDiffuse->GetType(); - texParams.width = spaceshipDiffuse->GetWidth(); - texParams.height = spaceshipDiffuse->GetHeight(); - texParams.depth = spaceshipDiffuse->GetDepth(); - - std::shared_ptr spaceshipTexture = device->InstantiateTexture(texParams); - if (!spaceshipTexture->Update(spaceshipDiffuse->GetConstPixels())) - { - NazaraError("Failed to update texture"); - return __LINE__; - } - - // Plane texture - std::shared_ptr devImage = Nz::Image::LoadFromFile(resourceDir / "dev_grey.png"); - if (!devImage || !devImage->Convert(Nz::PixelFormat::RGBA8_SRGB)) - { - NazaraError("Failed to load image"); - return __LINE__; - } - - Nz::TextureInfo devTexParams; - devTexParams.pixelFormat = devImage->GetFormat(); - devTexParams.type = devImage->GetType(); - devTexParams.width = devImage->GetWidth(); - devTexParams.height = devImage->GetHeight(); - devTexParams.depth = devImage->GetDepth(); - - std::shared_ptr planeTexture = device->InstantiateTexture(devTexParams); - if (!planeTexture->Update(devImage->GetConstPixels())) - { - NazaraError("Failed to update texture"); - return __LINE__; - } - - // Texture (alpha-map) - std::shared_ptr alphaImage = Nz::Image::LoadFromFile(resourceDir / "alphatile.png"); - if (!alphaImage || !alphaImage->Convert(Nz::PixelFormat::RGBA8)) - { - NazaraError("Failed to load image"); - return __LINE__; - } - - Nz::TextureInfo alphaTexParams; - alphaTexParams.pixelFormat = alphaImage->GetFormat(); - alphaTexParams.type = alphaImage->GetType(); - alphaTexParams.width = alphaImage->GetWidth(); - alphaTexParams.height = alphaImage->GetHeight(); - alphaTexParams.depth = alphaImage->GetDepth(); - - std::shared_ptr alphaTexture = device->InstantiateTexture(alphaTexParams); - if (!alphaTexture->Update(alphaImage->GetConstPixels())) - { - NazaraError("Failed to update texture"); - return __LINE__; - } + Nz::TextureParams texParams; + texParams.renderDevice = device; + texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB; Nz::MeshParams planeParams; planeParams.storage = Nz::DataStorage::Software; @@ -193,15 +131,15 @@ int main() { Nz::BasicMaterial basicMat(*spaceshipMat); basicMat.EnableAlphaTest(false); - basicMat.SetAlphaMap(alphaTexture); - basicMat.SetDiffuseMap(spaceshipTexture); + basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); + basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); } std::shared_ptr planeMat = std::make_shared(customMatSettings); planeMat->EnableDepthBuffer(true); { Nz::BasicMaterial basicMat(*planeMat); - basicMat.SetDiffuseMap(planeTexture); + basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "dev_grey.png", texParams)); Nz::TextureSamplerInfo planeSampler; planeSampler.anisotropyLevel = 16; diff --git a/examples/GraphicsTest/main.cpp b/examples/GraphicsTest/main.cpp index 1b495fb81..64bcd4fdd 100644 --- a/examples/GraphicsTest/main.cpp +++ b/examples/GraphicsTest/main.cpp @@ -57,49 +57,17 @@ int main() return __LINE__; } - Nz::TextureInfo texParams; - texParams.pixelFormat = diffuseImage->GetFormat(); - texParams.type = diffuseImage->GetType(); - texParams.width = diffuseImage->GetWidth(); - texParams.height = diffuseImage->GetHeight(); - texParams.depth = diffuseImage->GetDepth(); - - std::shared_ptr texture = device->InstantiateTexture(texParams); - if (!texture->Update(diffuseImage->GetConstPixels())) - { - NazaraError("Failed to update texture"); - return __LINE__; - } - - // Texture (alpha-map) - std::shared_ptr alphaImage = Nz::Image::LoadFromFile(resourceDir / "alphatile.png"); - if (!alphaImage || !alphaImage->Convert(Nz::PixelFormat::RGBA8)) - { - NazaraError("Failed to load image"); - return __LINE__; - } - - Nz::TextureInfo alphaTexParams; - alphaTexParams.pixelFormat = alphaImage->GetFormat(); - alphaTexParams.type = alphaImage->GetType(); - alphaTexParams.width = alphaImage->GetWidth(); - alphaTexParams.height = alphaImage->GetHeight(); - alphaTexParams.depth = alphaImage->GetDepth(); - - std::shared_ptr alphaTexture = device->InstantiateTexture(alphaTexParams); - if (!alphaTexture->Update(alphaImage->GetConstPixels())) - { - NazaraError("Failed to update texture"); - return __LINE__; - } + Nz::TextureParams texParams; + texParams.renderDevice = device; + texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB; std::shared_ptr material = std::make_shared(Nz::BasicMaterial::GetSettings()); material->EnableDepthBuffer(true); Nz::BasicMaterial basicMat(*material); - basicMat.EnableAlphaTest(false); - basicMat.SetAlphaMap(alphaTexture); - basicMat.SetDiffuseMap(texture); + basicMat.EnableAlphaTest(true); + basicMat.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); + basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); Nz::Model model(std::move(gfxMesh)); for (std::size_t i = 0; i < model.GetSubMeshCount(); ++i) diff --git a/examples/RenderTest/main.cpp b/examples/RenderTest/main.cpp index 5b6ba47cd..49e122978 100644 --- a/examples/RenderTest/main.cpp +++ b/examples/RenderTest/main.cpp @@ -129,27 +129,11 @@ int main() std::cout << "Vertex count: " << meshVB->GetVertexCount() << std::endl; // Texture - std::shared_ptr drfreakImage = Nz::Image::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png"); - if (!drfreakImage || !drfreakImage->Convert(Nz::PixelFormat::RGBA8)) - { - NazaraError("Failed to load image"); - return __LINE__; - } - - Nz::TextureInfo texParams; - texParams.pixelFormat = drfreakImage->GetFormat(); - texParams.type = drfreakImage->GetType(); - texParams.width = drfreakImage->GetWidth(); - texParams.height = drfreakImage->GetHeight(); - texParams.depth = drfreakImage->GetDepth(); - - std::shared_ptr texture = device->InstantiateTexture(texParams); - if (!texture->Update(drfreakImage->GetConstPixels())) - { - NazaraError("Failed to update texture"); - return __LINE__; - } + Nz::TextureParams texParams; + texParams.renderDevice = device; + texParams.loadFormat = Nz::PixelFormat::RGBA8_SRGB; + std::shared_ptr texture = Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams); std::shared_ptr textureSampler = device->InstantiateTextureSampler({}); struct diff --git a/include/Nazara/Renderer/Texture.hpp b/include/Nazara/Renderer/Texture.hpp index 450144d7b..82c0bbcef 100644 --- a/include/Nazara/Renderer/Texture.hpp +++ b/include/Nazara/Renderer/Texture.hpp @@ -8,14 +8,19 @@ #define NAZARA_TEXTURE_HPP #include +#include +#include +#include #include #include #include #include -#include +#include namespace Nz { + class RenderDevice; + struct TextureInfo { PixelFormat pixelFormat; @@ -27,6 +32,20 @@ namespace Nz unsigned int width; }; + struct NAZARA_RENDERER_API TextureParams : ImageParams + { + std::shared_ptr renderDevice; + TextureUsageFlags usageFlags = TextureUsage::ShaderSampling | TextureUsage::TransferDestination; + + bool IsValid() const; + }; + + class Texture; + + using TextureLibrary = ObjectLibrary; + using TextureLoader = ResourceLoader; + using TextureManager = ResourceManager; + class NAZARA_RENDERER_API Texture : public Resource { public: @@ -44,6 +63,13 @@ namespace Nz static inline unsigned int GetLevelSize(unsigned int size, unsigned int level); + static std::shared_ptr CreateFromImage(const Image& image, const TextureParams& params); + + // Load + static std::shared_ptr LoadFromFile(const std::filesystem::path& filePath, const TextureParams& params); + static std::shared_ptr LoadFromMemory(const void* data, std::size_t size, const TextureParams& params); + static std::shared_ptr LoadFromStream(Stream& stream, const TextureParams& params); + Texture& operator=(const Texture&) = delete; Texture& operator=(Texture&&) = delete; }; diff --git a/src/Nazara/Renderer/Texture.cpp b/src/Nazara/Renderer/Texture.cpp index 78e72e65c..e05108b5a 100644 --- a/src/Nazara/Renderer/Texture.cpp +++ b/src/Nazara/Renderer/Texture.cpp @@ -3,9 +3,72 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include +#include +#include #include namespace Nz { Texture::~Texture() = default; + + bool TextureParams::IsValid() const + { + if (!ImageParams::IsValid()) + return false; + + if (!renderDevice) + { + NazaraError("no render device set"); + return false; + } + + if (!usageFlags) + { + NazaraError("a texture should have at least one usage flag"); + return false; + } + + return true; + } + + std::shared_ptr Texture::CreateFromImage(const Image& image, const TextureParams& params) + { + NazaraAssert(params.IsValid(), "Invalid TextureParams"); + + Nz::TextureInfo texParams; + texParams.depth = image.GetDepth(); + texParams.height = image.GetHeight(); + texParams.pixelFormat = image.GetFormat(); + texParams.type = image.GetType(); + texParams.width = image.GetWidth(); + texParams.usageFlags = params.usageFlags; + + std::shared_ptr texture = params.renderDevice->InstantiateTexture(texParams); + if (!texture->Update(image.GetConstPixels())) + { + NazaraError("failed to update texture"); + return {}; + } + + return texture; + } + + std::shared_ptr Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& params) + { + std::shared_ptr image = Image::LoadFromFile(filePath, params); + return CreateFromImage(*image, params); + } + + std::shared_ptr Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& params) + { + std::shared_ptr image = Image::LoadFromMemory(data, size, params); + return CreateFromImage(*image, params); + } + + std::shared_ptr Texture::LoadFromStream(Stream& stream, const TextureParams& params) + { + std::shared_ptr image = Image::LoadFromStream(stream, params); + return CreateFromImage(*image, params); + } }