Graphics: Add TextureLoader
This commit is contained in:
parent
47cb878f9d
commit
fbdc1faf8c
|
|
@ -41,6 +41,13 @@ namespace Nz
|
|||
Volume
|
||||
};
|
||||
|
||||
enum class MaterialLightingType
|
||||
{
|
||||
None,
|
||||
Phong,
|
||||
PhysicallyBased
|
||||
};
|
||||
|
||||
enum class MaterialPassFlag
|
||||
{
|
||||
SortByDistance,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/MaterialPassRegistry.hpp>
|
||||
#include <Nazara/Graphics/TextureSamplerCache.hpp>
|
||||
#include <Nazara/Renderer/RenderDevice.hpp>
|
||||
|
|
@ -40,6 +41,8 @@ namespace Nz
|
|||
inline const DefaultTextures& GetDefaultTextures() const;
|
||||
inline MaterialPassRegistry& GetMaterialPassRegistry();
|
||||
inline const MaterialPassRegistry& GetMaterialPassRegistry() const;
|
||||
MaterialLoader& GetMaterialLoader();
|
||||
const MaterialLoader& GetMaterialLoader() const;
|
||||
inline PixelFormat GetPreferredDepthStencilFormat() const;
|
||||
inline const std::shared_ptr<RenderDevice>& GetRenderDevice() const;
|
||||
inline const RenderPassCache& GetRenderPassCache() const;
|
||||
|
|
@ -73,6 +76,7 @@ namespace Nz
|
|||
std::shared_ptr<RenderPipeline> m_blitPipelineTransparent;
|
||||
std::shared_ptr<RenderPipelineLayout> m_blitPipelineLayout;
|
||||
DefaultTextures m_defaultTextures;
|
||||
MaterialLoader m_materialLoader;
|
||||
MaterialPassRegistry m_materialPassRegistry;
|
||||
PixelFormat m_preferredDepthStencilFormat;
|
||||
|
||||
|
|
|
|||
|
|
@ -32,6 +32,16 @@ namespace Nz
|
|||
return m_materialPassRegistry;
|
||||
}
|
||||
|
||||
inline MaterialLoader& Graphics::GetMaterialLoader()
|
||||
{
|
||||
return m_materialLoader;
|
||||
}
|
||||
|
||||
inline const MaterialLoader& Graphics::GetMaterialLoader() const
|
||||
{
|
||||
return m_materialLoader;
|
||||
}
|
||||
|
||||
inline PixelFormat Graphics::GetPreferredDepthStencilFormat() const
|
||||
{
|
||||
return m_preferredDepthStencilFormat;
|
||||
|
|
|
|||
|
|
@ -8,11 +8,30 @@
|
|||
#define NAZARA_GRAPHICS_MATERIAL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Graphics.hpp>
|
||||
#include <Nazara/Graphics/MaterialPass.hpp>
|
||||
#include <Nazara/Core/ObjectLibrary.hpp>
|
||||
#include <Nazara/Core/ResourceLoader.hpp>
|
||||
#include <Nazara/Core/ResourceManager.hpp>
|
||||
#include <Nazara/Core/ResourceSaver.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/Enums.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
struct NAZARA_GRAPHICS_API MaterialParams : ResourceParameters
|
||||
{
|
||||
MaterialLightingType lightingType = MaterialLightingType::None;
|
||||
|
||||
bool IsValid() const;
|
||||
};
|
||||
|
||||
class Material;
|
||||
class MaterialPass;
|
||||
|
||||
using MaterialLibrary = ObjectLibrary<Material>;
|
||||
using MaterialLoader = ResourceLoader<Material, MaterialParams>;
|
||||
using MaterialManager = ResourceManager<Material, MaterialParams>;
|
||||
using MaterialSaver = ResourceSaver<Material, MaterialParams>;
|
||||
|
||||
class NAZARA_GRAPHICS_API Material : public Resource
|
||||
{
|
||||
public:
|
||||
|
|
@ -20,9 +39,9 @@ namespace Nz
|
|||
~Material() = default;
|
||||
|
||||
inline void AddPass(std::size_t passIndex, std::shared_ptr<MaterialPass> pass);
|
||||
inline void AddPass(std::string passName, std::shared_ptr<MaterialPass> pass);
|
||||
void AddPass(std::string passName, std::shared_ptr<MaterialPass> pass);
|
||||
|
||||
inline const std::shared_ptr<MaterialPass>& FindPass(const std::string& passName) const;
|
||||
const std::shared_ptr<MaterialPass>& FindPass(const std::string& passName) const;
|
||||
|
||||
template<typename F> void ForEachPass(F&& callback);
|
||||
|
||||
|
|
@ -31,7 +50,11 @@ namespace Nz
|
|||
inline bool HasPass(std::size_t passIndex) const;
|
||||
|
||||
inline void RemovePass(std::size_t passIndex);
|
||||
inline void RemovePass(const std::string& passName);
|
||||
void RemovePass(const std::string& passName);
|
||||
|
||||
static std::shared_ptr<Material> LoadFromFile(const std::filesystem::path& filePath, const MaterialParams& params = MaterialParams());
|
||||
static std::shared_ptr<Material> LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params = MaterialParams());
|
||||
static std::shared_ptr<Material> LoadFromStream(Stream& stream, const MaterialParams& params = MaterialParams());
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<MaterialPass>> m_passes;
|
||||
|
|
|
|||
|
|
@ -15,18 +15,6 @@ namespace Nz
|
|||
m_passes[passIndex] = std::move(pass);
|
||||
}
|
||||
|
||||
inline void Material::AddPass(std::string passName, std::shared_ptr<MaterialPass> pass)
|
||||
{
|
||||
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
return AddPass(registry.GetPassIndex(passName), std::move(pass));
|
||||
}
|
||||
|
||||
inline const std::shared_ptr<MaterialPass>& Material::FindPass(const std::string& passName) const
|
||||
{
|
||||
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
return GetPass(registry.GetPassIndex(passName));
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void Material::ForEachPass(F&& callback)
|
||||
{
|
||||
|
|
@ -63,12 +51,6 @@ namespace Nz
|
|||
|
||||
m_passes[passIndex].reset();
|
||||
}
|
||||
|
||||
inline void Material::RemovePass(const std::string& passName)
|
||||
{
|
||||
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
return RemovePass(registry.GetPassIndex(passName));
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Graphics/DebugOff.hpp>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/Formats/TextureLoader.hpp>
|
||||
#include <Nazara/Graphics/Graphics.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/PhongLightingMaterial.hpp>
|
||||
#include <Nazara/Graphics/PhysicallyBasedMaterial.hpp>
|
||||
#include <Nazara/Graphics/DepthMaterial.hpp>
|
||||
#include <Nazara/Renderer/Texture.hpp>
|
||||
#include <Nazara/Utility/Utility.hpp>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace Loaders
|
||||
{
|
||||
MaterialLoader::Entry GetMaterialLoader_Texture()
|
||||
{
|
||||
MaterialLoader::Entry loaderEntry;
|
||||
loaderEntry.extensionSupport = [](std::string_view extension)
|
||||
{
|
||||
return Utility::Instance()->GetImageLoader().IsExtensionSupported(extension);
|
||||
};
|
||||
|
||||
loaderEntry.streamLoader = [](Stream& stream, const MaterialParams& parameters) -> Result<std::shared_ptr<Material>, ResourceLoadingError>
|
||||
{
|
||||
TextureParams texParams;
|
||||
texParams.renderDevice = Graphics::Instance()->GetRenderDevice();
|
||||
|
||||
std::shared_ptr<Texture> texture = Texture::LoadFromStream(stream, {});
|
||||
if (!texture)
|
||||
return Err(ResourceLoadingError::Unrecognized);
|
||||
|
||||
std::shared_ptr<Material> material = std::make_shared<Material>();
|
||||
|
||||
// ForwardPass
|
||||
{
|
||||
std::shared_ptr<MaterialPass> matPass;
|
||||
if (parameters.lightingType == MaterialLightingType::Phong)
|
||||
matPass = std::make_shared<MaterialPass>(PhongLightingMaterial::GetSettings());
|
||||
else if (parameters.lightingType == MaterialLightingType::PhysicallyBased)
|
||||
matPass = std::make_shared<MaterialPass>(PhysicallyBasedMaterial::GetSettings());
|
||||
else
|
||||
matPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
|
||||
|
||||
matPass->EnableDepthBuffer(true);
|
||||
|
||||
BasicMaterial forwardPass(*matPass);
|
||||
forwardPass.SetBaseColorMap(texture);
|
||||
|
||||
if (PixelFormatInfo::HasAlpha(texture->GetFormat()))
|
||||
forwardPass.EnableAlphaTest(true);
|
||||
|
||||
material->AddPass("ForwardPass", std::move(matPass));
|
||||
}
|
||||
|
||||
// DepthPass
|
||||
{
|
||||
std::shared_ptr<MaterialPass> matPass = std::make_shared<MaterialPass>(DepthMaterial::GetSettings());
|
||||
|
||||
if (PixelFormatInfo::HasAlpha(texture->GetFormat()))
|
||||
{
|
||||
BasicMaterial depthPass(*matPass);
|
||||
depthPass.SetBaseColorMap(texture);
|
||||
depthPass.EnableAlphaTest(true);
|
||||
}
|
||||
|
||||
material->AddPass("DepthPass", std::move(matPass));
|
||||
}
|
||||
|
||||
return material;
|
||||
};
|
||||
|
||||
loaderEntry.parameterFilter = [](const MaterialParams& parameters)
|
||||
{
|
||||
bool skip;
|
||||
if (parameters.custom.GetBooleanParameter("SkipNativeTextureLoader", &skip) && skip)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
return loaderEntry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_LOADERS_TEXTURE_HPP
|
||||
#define NAZARA_LOADERS_TEXTURE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
|
||||
namespace Nz::Loaders
|
||||
{
|
||||
MaterialLoader::Entry GetMaterialLoader_Texture();
|
||||
}
|
||||
|
||||
#endif // NAZARA_LOADERS_TEXTURE_HPP
|
||||
|
|
@ -3,8 +3,55 @@
|
|||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/Graphics.hpp>
|
||||
#include <Nazara/Graphics/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool MaterialParams::IsValid() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void Material::AddPass(std::string passName, std::shared_ptr<MaterialPass> pass)
|
||||
{
|
||||
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
return AddPass(registry.GetPassIndex(passName), std::move(pass));
|
||||
}
|
||||
|
||||
const std::shared_ptr<MaterialPass>& Material::FindPass(const std::string& passName) const
|
||||
{
|
||||
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
return GetPass(registry.GetPassIndex(passName));
|
||||
}
|
||||
|
||||
void Material::RemovePass(const std::string& passName)
|
||||
{
|
||||
auto& registry = Graphics::Instance()->GetMaterialPassRegistry();
|
||||
return RemovePass(registry.GetPassIndex(passName));
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> Material::LoadFromFile(const std::filesystem::path& filePath, const MaterialParams& params)
|
||||
{
|
||||
Graphics* graphics = Graphics::Instance();
|
||||
NazaraAssert(graphics, "Utility module has not been initialized");
|
||||
|
||||
return graphics->GetMaterialLoader().LoadFromFile(filePath, params);
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> Material::LoadFromMemory(const void* data, std::size_t size, const MaterialParams& params)
|
||||
{
|
||||
Graphics* graphics = Graphics::Instance();
|
||||
NazaraAssert(graphics, "Utility module has not been initialized");
|
||||
|
||||
return graphics->GetMaterialLoader().LoadFromMemory(data, size, params);
|
||||
}
|
||||
|
||||
std::shared_ptr<Material> Material::LoadFromStream(Stream& stream, const MaterialParams& params)
|
||||
{
|
||||
Graphics* graphics = Graphics::Instance();
|
||||
NazaraAssert(graphics, "Utility module has not been initialized");
|
||||
|
||||
return graphics->GetMaterialLoader().LoadFromStream(stream, params);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue