Renderer/Texture: Add static helpers

This commit is contained in:
Jérôme Leclercq 2021-05-25 15:37:55 +02:00
parent 59cfc74ab4
commit 335bb82be1
5 changed files with 106 additions and 127 deletions

View File

@ -106,71 +106,9 @@ int main()
std::shared_ptr<Nz::GraphicalMesh> gfxMesh = std::make_shared<Nz::GraphicalMesh>(*spaceship);
// Spaceship texture
std::shared_ptr<Nz::Image> 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<Nz::Texture> spaceshipTexture = device->InstantiateTexture(texParams);
if (!spaceshipTexture->Update(spaceshipDiffuse->GetConstPixels()))
{
NazaraError("Failed to update texture");
return __LINE__;
}
// Plane texture
std::shared_ptr<Nz::Image> 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<Nz::Texture> planeTexture = device->InstantiateTexture(devTexParams);
if (!planeTexture->Update(devImage->GetConstPixels()))
{
NazaraError("Failed to update texture");
return __LINE__;
}
// Texture (alpha-map)
std::shared_ptr<Nz::Image> 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<Nz::Texture> 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<Nz::Material> planeMat = std::make_shared<Nz::Material>(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;

View File

@ -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<Nz::Texture> texture = device->InstantiateTexture(texParams);
if (!texture->Update(diffuseImage->GetConstPixels()))
{
NazaraError("Failed to update texture");
return __LINE__;
}
// Texture (alpha-map)
std::shared_ptr<Nz::Image> 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<Nz::Texture> 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<Nz::Material> material = std::make_shared<Nz::Material>(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)

View File

@ -129,27 +129,11 @@ int main()
std::cout << "Vertex count: " << meshVB->GetVertexCount() << std::endl;
// Texture
std::shared_ptr<Nz::Image> 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<Nz::Texture> 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<Nz::Texture> texture = Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams);
std::shared_ptr<Nz::TextureSampler> textureSampler = device->InstantiateTextureSampler({});
struct

View File

@ -8,14 +8,19 @@
#define NAZARA_TEXTURE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/ObjectLibrary.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Renderer/Config.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/Image.hpp>
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> renderDevice;
TextureUsageFlags usageFlags = TextureUsage::ShaderSampling | TextureUsage::TransferDestination;
bool IsValid() const;
};
class Texture;
using TextureLibrary = ObjectLibrary<Texture>;
using TextureLoader = ResourceLoader<Texture, TextureParams>;
using TextureManager = ResourceManager<Texture, TextureParams>;
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<Texture> CreateFromImage(const Image& image, const TextureParams& params);
// Load
static std::shared_ptr<Texture> LoadFromFile(const std::filesystem::path& filePath, const TextureParams& params);
static std::shared_ptr<Texture> LoadFromMemory(const void* data, std::size_t size, const TextureParams& params);
static std::shared_ptr<Texture> LoadFromStream(Stream& stream, const TextureParams& params);
Texture& operator=(const Texture&) = delete;
Texture& operator=(Texture&&) = delete;
};

View File

@ -3,9 +3,72 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Renderer/RenderDevice.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Renderer/Debug.hpp>
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> 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<Nz::Texture> texture = params.renderDevice->InstantiateTexture(texParams);
if (!texture->Update(image.GetConstPixels()))
{
NazaraError("failed to update texture");
return {};
}
return texture;
}
std::shared_ptr<Texture> Texture::LoadFromFile(const std::filesystem::path& filePath, const TextureParams& params)
{
std::shared_ptr<Image> image = Image::LoadFromFile(filePath, params);
return CreateFromImage(*image, params);
}
std::shared_ptr<Texture> Texture::LoadFromMemory(const void* data, std::size_t size, const TextureParams& params)
{
std::shared_ptr<Image> image = Image::LoadFromMemory(data, size, params);
return CreateFromImage(*image, params);
}
std::shared_ptr<Texture> Texture::LoadFromStream(Stream& stream, const TextureParams& params)
{
std::shared_ptr<Image> image = Image::LoadFromStream(stream, params);
return CreateFromImage(*image, params);
}
}