Graphics: Improve TextureSampler handling

This commit is contained in:
Jérôme Leclercq
2021-01-27 18:50:49 +01:00
parent 78c3f57333
commit b9151d8a7a
15 changed files with 181 additions and 27 deletions

View File

@@ -24,11 +24,11 @@ namespace Nz
inline void EnableAlphaTest(bool alphaTest);
inline const std::shared_ptr<Texture>& GetAlphaMap() const;
inline const std::shared_ptr<TextureSampler>& GetAlphaSampler() const;
inline const TextureSamplerInfo& GetAlphaSampler() const;
float GetAlphaTestThreshold() const;
Color GetDiffuseColor() const;
inline const std::shared_ptr<Texture>& GetDiffuseMap() const;
inline const std::shared_ptr<TextureSampler>& GetDiffuseSampler() const;
inline const TextureSamplerInfo& GetDiffuseSampler() const;
inline bool HasAlphaMap() const;
inline bool HasAlphaTest() const;
@@ -37,11 +37,11 @@ namespace Nz
inline bool HasDiffuseMap() const;
inline void SetAlphaMap(std::shared_ptr<Texture> alphaMap);
inline void SetAlphaSampler(std::shared_ptr<TextureSampler> alphaSampler);
inline void SetAlphaSampler(TextureSamplerInfo alphaSampler);
void SetAlphaTestThreshold(float alphaThreshold);
void SetDiffuseColor(const Color& diffuse);
inline void SetDiffuseMap(std::shared_ptr<Texture> diffuseMap);
inline void SetDiffuseSampler(std::shared_ptr<TextureSampler> diffuseSampler);
inline void SetDiffuseSampler(TextureSamplerInfo diffuseSampler);
static inline const UniformOffsets& GetOffsets();
static inline const std::shared_ptr<MaterialSettings>& GetSettings();

View File

@@ -35,7 +35,7 @@ namespace Nz
return m_material.GetTexture(m_textureIndexes.alpha);
}
inline const std::shared_ptr<TextureSampler>& BasicMaterial::GetAlphaSampler() const
inline const TextureSamplerInfo& BasicMaterial::GetAlphaSampler() const
{
NazaraAssert(HasAlphaMap(), "Material has no alpha texture slot");
return m_material.GetTextureSampler(m_textureIndexes.alpha);
@@ -47,7 +47,7 @@ namespace Nz
return m_material.GetTexture(m_textureIndexes.diffuse);
}
inline const std::shared_ptr<TextureSampler>& BasicMaterial::GetDiffuseSampler() const
inline const TextureSamplerInfo& BasicMaterial::GetDiffuseSampler() const
{
NazaraAssert(HasDiffuseMap(), "Material has no alpha texture slot");
return m_material.GetTextureSampler(m_textureIndexes.diffuse);
@@ -88,7 +88,7 @@ namespace Nz
m_material.EnableCondition(m_conditionIndexes.hasAlphaMap, hasAlphaMap);
}
inline void BasicMaterial::SetAlphaSampler(std::shared_ptr<TextureSampler> alphaSampler)
inline void BasicMaterial::SetAlphaSampler(TextureSamplerInfo alphaSampler)
{
NazaraAssert(HasAlphaMap(), "Material has no alpha map slot");
m_material.SetTextureSampler(m_textureIndexes.alpha, std::move(alphaSampler));
@@ -104,7 +104,7 @@ namespace Nz
m_material.EnableCondition(m_conditionIndexes.hasDiffuseMap, hasDiffuseMap);
}
inline void BasicMaterial::SetDiffuseSampler(std::shared_ptr<TextureSampler> diffuseSampler)
inline void BasicMaterial::SetDiffuseSampler(TextureSamplerInfo diffuseSampler)
{
NazaraAssert(HasDiffuseMap(), "Material has no diffuse map slot");
m_material.SetTextureSampler(m_textureIndexes.diffuse, std::move(diffuseSampler));

View File

@@ -9,8 +9,10 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/TextureSamplerCache.hpp>
#include <Nazara/Renderer/Renderer.hpp>
#include <Nazara/Renderer/RenderDevice.hpp>
#include <optional>
namespace Nz
{
@@ -30,6 +32,7 @@ namespace Nz
~Graphics();
inline RenderDevice& GetRenderDevice();
inline TextureSamplerCache& GetSamplerCache();
inline const std::shared_ptr<AbstractBuffer>& GetViewerDataUBO();
struct Config
@@ -38,6 +41,7 @@ namespace Nz
};
private:
std::optional<TextureSamplerCache> m_samplerCache;
std::shared_ptr<AbstractBuffer> m_viewerDataUBO;
std::shared_ptr<RenderDevice> m_renderDevice;

View File

@@ -12,6 +12,11 @@ namespace Nz
return *m_renderDevice;
}
inline TextureSamplerCache& Graphics::GetSamplerCache()
{
return *m_samplerCache;
}
inline const std::shared_ptr<AbstractBuffer>& Graphics::GetViewerDataUBO()
{
return m_viewerDataUBO;

View File

@@ -66,7 +66,7 @@ namespace Nz
inline const std::shared_ptr<UberShader>& GetShader(ShaderStageType shaderStage) const;
inline BlendFunc GetSrcBlend() const;
inline const std::shared_ptr<Texture>& GetTexture(std::size_t textureIndex) const;
inline const std::shared_ptr<TextureSampler>& GetTextureSampler(std::size_t textureIndex) const;
inline const TextureSamplerInfo& GetTextureSampler(std::size_t textureIndex) const;
inline const std::shared_ptr<AbstractBuffer>& GetUniformBuffer(std::size_t bufferIndex) const;
inline std::vector<UInt8>& GetUniformBufferData(std::size_t bufferIndex);
inline const std::vector<UInt8>& GetUniformBufferConstData(std::size_t bufferIndex);
@@ -96,7 +96,7 @@ namespace Nz
inline void SetUniformBuffer(std::size_t bufferIndex, std::shared_ptr<AbstractBuffer> uniformBuffer);
inline void SetSrcBlend(BlendFunc func);
inline void SetTexture(std::size_t textureIndex, std::shared_ptr<Texture> texture);
inline void SetTextureSampler(std::size_t textureIndex, std::shared_ptr<TextureSampler> sampler);
inline void SetTextureSampler(std::size_t textureIndex, TextureSamplerInfo samplerInfo);
void UpdateShaderBinding(ShaderBinding& shaderBinding) const;
@@ -106,12 +106,14 @@ namespace Nz
private:
inline void InvalidatePipeline();
inline void InvalidateShaderBinding();
inline void InvalidateTextureSampler(std::size_t textureIndex);
inline void UpdatePipeline() const;
struct MaterialTexture
{
std::shared_ptr<TextureSampler> sampler;
mutable std::shared_ptr<TextureSampler> sampler;
std::shared_ptr<Texture> texture;
TextureSamplerInfo samplerInfo;
};
struct UniformBuffer

View File

@@ -436,10 +436,10 @@ namespace Nz
return m_textures[textureIndex].texture;
}
inline const std::shared_ptr<TextureSampler>& Material::GetTextureSampler(std::size_t textureIndex) const
inline const TextureSamplerInfo& Material::GetTextureSampler(std::size_t textureIndex) const
{
NazaraAssert(textureIndex < m_textures.size(), "Invalid texture index");
return m_textures[textureIndex].sampler;
return m_textures[textureIndex].samplerInfo;
}
inline const std::shared_ptr<AbstractBuffer>& Material::GetUniformBuffer(std::size_t bufferIndex) const
@@ -695,13 +695,13 @@ namespace Nz
}
}
inline void Material::SetTextureSampler(std::size_t textureIndex, std::shared_ptr<TextureSampler> sampler)
inline void Material::SetTextureSampler(std::size_t textureIndex, TextureSamplerInfo samplerInfo)
{
NazaraAssert(textureIndex < m_textures.size(), "Invalid texture index");
if (m_textures[textureIndex].sampler != sampler)
if (m_textures[textureIndex].samplerInfo != samplerInfo)
{
m_textures[textureIndex].sampler = std::move(sampler);
InvalidateShaderBinding();
m_textures[textureIndex].samplerInfo = std::move(samplerInfo);
InvalidateTextureSampler(textureIndex);
}
}
@@ -731,6 +731,14 @@ namespace Nz
//TODO
}
inline void Material::InvalidateTextureSampler(std::size_t textureIndex)
{
assert(textureIndex < m_textures.size());
m_textures[textureIndex].sampler.reset();
InvalidateShaderBinding();
}
inline void Material::UpdatePipeline() const
{
for (auto& shader : m_pipelineInfo.shaders)

View File

@@ -0,0 +1,40 @@
// 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_TEXTURESAMPLERCACHE_HPP
#define NAZARA_TEXTURESAMPLERCACHE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Renderer/TextureSampler.hpp>
#include <unordered_map>
namespace Nz
{
class RenderDevice;
class NAZARA_GRAPHICS_API TextureSamplerCache
{
public:
inline TextureSamplerCache(std::shared_ptr<RenderDevice> device);
TextureSamplerCache(const TextureSamplerCache&) = delete;
TextureSamplerCache(TextureSamplerCache&&) = delete;
~TextureSamplerCache() = default;
const std::shared_ptr<TextureSampler>& Get(const TextureSamplerInfo& info);
TextureSamplerCache& operator=(const TextureSamplerCache&) = delete;
TextureSamplerCache& operator=(TextureSamplerCache&&) = delete;
private:
std::shared_ptr<RenderDevice> m_device;
std::unordered_map<TextureSamplerInfo, std::shared_ptr<TextureSampler>> m_samplers;
};
}
#include <Nazara/Graphics/TextureSamplerCache.inl>
#endif

View File

@@ -0,0 +1,17 @@
// 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/TextureSamplerCache.hpp>
#include <functional>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline TextureSamplerCache::TextureSamplerCache(std::shared_ptr<RenderDevice> device) :
m_device(std::move(device))
{
}
}
#include <Nazara/Graphics/DebugOff.hpp>