Graphics: Add DepthMaterial

This commit is contained in:
Jérôme Leclercq 2021-08-10 10:36:16 +02:00
parent 7aafcfaae9
commit a2a0e6bd54
11 changed files with 213 additions and 73 deletions

View File

@ -77,14 +77,9 @@ int main()
std::shared_ptr<Nz::Material> material = std::make_shared<Nz::Material>(); std::shared_ptr<Nz::Material> material = std::make_shared<Nz::Material>();
auto customSettings = Nz::BasicMaterial::GetSettings()->GetBuilderData(); std::shared_ptr<Nz::MaterialPass> depthPass = std::make_shared<Nz::MaterialPass>(Nz::DepthMaterial::GetSettings());
customSettings.shaders.clear();
customSettings.shaders.emplace_back(std::make_shared<Nz::UberShader>(Nz::ShaderStageType::Fragment | Nz::ShaderStageType::Vertex, Nz::ShaderLang::Parse(resourceDir / "depth_pass.nzsl")));
auto depthSettings = std::make_shared<Nz::MaterialSettings>(std::move(customSettings));
std::shared_ptr<Nz::MaterialPass> depthPass = std::make_shared<Nz::MaterialPass>(depthSettings);
depthPass->EnableDepthBuffer(true); depthPass->EnableDepthBuffer(true);
depthPass->EnableDepthClamp(true);
depthPass->EnableFaceCulling(true); depthPass->EnableFaceCulling(true);
std::shared_ptr<Nz::MaterialPass> materialPass = std::make_shared<Nz::MaterialPass>(Nz::BasicMaterial::GetSettings()); std::shared_ptr<Nz::MaterialPass> materialPass = std::make_shared<Nz::MaterialPass>(Nz::BasicMaterial::GetSettings());
@ -104,7 +99,7 @@ int main()
basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams)); basicMat.SetDiffuseMap(Nz::Texture::LoadFromFile(resourceDir / "Spaceship/Texture/diffuse.png", texParams));
basicMat.SetDiffuseSampler(samplerInfo); basicMat.SetDiffuseSampler(samplerInfo);
Nz::BasicMaterial basicMatDepth(*depthPass); Nz::DepthMaterial basicMatDepth(*depthPass);
basicMatDepth.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams)); basicMatDepth.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams));
std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh)); std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh));

View File

@ -35,6 +35,7 @@
#include <Nazara/Graphics/Camera.hpp> #include <Nazara/Graphics/Camera.hpp>
#include <Nazara/Graphics/Config.hpp> #include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/CullingList.hpp> #include <Nazara/Graphics/CullingList.hpp>
#include <Nazara/Graphics/DepthMaterial.hpp>
#include <Nazara/Graphics/ElementRenderer.hpp> #include <Nazara/Graphics/ElementRenderer.hpp>
#include <Nazara/Graphics/Enums.hpp> #include <Nazara/Graphics/Enums.hpp>
#include <Nazara/Graphics/ForwardFramePipeline.hpp> #include <Nazara/Graphics/ForwardFramePipeline.hpp>

View File

@ -12,6 +12,8 @@
namespace Nz namespace Nz
{ {
class FieldOffsets;
class NAZARA_GRAPHICS_API BasicMaterial class NAZARA_GRAPHICS_API BasicMaterial
{ {
friend class MaterialPipeline; friend class MaterialPipeline;
@ -56,7 +58,7 @@ namespace Nz
std::size_t totalSize; std::size_t totalSize;
}; };
private: protected:
struct OptionIndexes struct OptionIndexes
{ {
std::size_t alphaTest; std::size_t alphaTest;
@ -70,6 +72,11 @@ namespace Nz
std::size_t diffuse; std::size_t diffuse;
}; };
static MaterialSettings::Builder Build(const UniformOffsets& offsets, std::vector<UInt8> defaultValues, std::vector<std::shared_ptr<UberShader>> uberShaders, std::size_t* uniformBlockIndex = nullptr, OptionIndexes* optionIndexes = nullptr, TextureIndexes* textureIndexes = nullptr);
static std::vector<std::shared_ptr<UberShader>> BuildShaders();
static std::pair<UniformOffsets, FieldOffsets> BuildUniformOffsets();
private:
static bool Initialize(); static bool Initialize();
static void Uninitialize(); static void Uninitialize();

View File

@ -0,0 +1,38 @@
// 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_DEPTH_MATERIAL_HPP
#define NAZARA_DEPTH_MATERIAL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp>
namespace Nz
{
class NAZARA_GRAPHICS_API DepthMaterial : public BasicMaterial
{
friend class MaterialPipeline;
public:
using BasicMaterial::BasicMaterial;
~DepthMaterial() = default;
static inline const std::shared_ptr<MaterialSettings>& GetSettings();
protected:
static std::vector<std::shared_ptr<UberShader>> BuildShaders();
private:
static bool Initialize();
static void Uninitialize();
static std::shared_ptr<MaterialSettings> s_materialSettings;
};
}
#include <Nazara/Graphics/DepthMaterial.inl>
#endif

View File

@ -0,0 +1,16 @@
// 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/DepthMaterial.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline const std::shared_ptr<MaterialSettings>& DepthMaterial::GetSettings()
{
return s_materialSettings;
}
}
#include <Nazara/Graphics/DebugOff.hpp>

View File

@ -9,6 +9,7 @@
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/Enums.hpp> #include <Nazara/Graphics/Enums.hpp>
#include <Nazara/Graphics/UberShader.hpp>
#include <Nazara/Renderer/RenderPipelineLayout.hpp> #include <Nazara/Renderer/RenderPipelineLayout.hpp>
#include <Nazara/Renderer/ShaderModule.hpp> #include <Nazara/Renderer/ShaderModule.hpp>
#include <Nazara/Utility/Enums.hpp> #include <Nazara/Utility/Enums.hpp>
@ -19,8 +20,6 @@
namespace Nz namespace Nz
{ {
class UberShader;
class MaterialSettings class MaterialSettings
{ {
public: public:
@ -56,6 +55,8 @@ namespace Nz
static constexpr std::size_t InvalidIndex = std::numeric_limits<std::size_t>::max(); static constexpr std::size_t InvalidIndex = std::numeric_limits<std::size_t>::max();
static void BuildOption(std::vector<Option>& options, const std::vector<std::shared_ptr<UberShader>>& uberShaders, std::string optionName, const std::string& shaderOptionName);
struct Builder struct Builder
{ {
std::vector<std::shared_ptr<UberShader>> shaders; std::vector<std::shared_ptr<UberShader>> shaders;

View File

@ -165,6 +165,32 @@ namespace Nz
return InvalidIndex; return InvalidIndex;
} }
inline void MaterialSettings::BuildOption(std::vector<Option>& options, const std::vector<std::shared_ptr<UberShader>>& uberShaders, std::string optionName, const std::string& shaderOptionName)
{
std::array<UInt64, ShaderStageTypeCount> shaderOptions;
shaderOptions.fill(0);
for (std::size_t i = 0; i < ShaderStageTypeCount; ++i)
{
for (const auto& uberShader : uberShaders)
{
if (uberShader->GetSupportedStages() & static_cast<ShaderStageType>(i))
{
assert(shaderOptions[i] == 0);
shaderOptions[i] |= uberShader->GetOptionFlagByName(shaderOptionName);
}
}
}
if (std::any_of(shaderOptions.begin(), shaderOptions.end(), [&](UInt64 flags) { return flags != 0; }))
{
options.push_back({
std::move(optionName),
shaderOptions
});
}
}
} }
#include <Nazara/Graphics/DebugOff.hpp> #include <Nazara/Graphics/DebugOff.hpp>

View File

@ -94,107 +94,115 @@ namespace Nz
colorPtr[3] = diffuse.a / 255.f; colorPtr[3] = diffuse.a / 255.f;
} }
bool BasicMaterial::Initialize() MaterialSettings::Builder BasicMaterial::Build(const UniformOffsets& offsets, std::vector<UInt8> defaultValues, std::vector<std::shared_ptr<UberShader>> uberShaders, std::size_t* uniformBlockIndex, OptionIndexes* optionIndexes, TextureIndexes* textureIndexes)
{ {
FieldOffsets fieldOffsets(StructLayout::Std140);
s_uniformOffsets.alphaThreshold = fieldOffsets.AddField(StructFieldType::Float1);
s_uniformOffsets.diffuseColor = fieldOffsets.AddField(StructFieldType::Float4);
s_uniformOffsets.totalSize = fieldOffsets.GetSize();
MaterialSettings::Builder settings; MaterialSettings::Builder settings;
std::vector<MaterialSettings::UniformVariable> variables; std::vector<MaterialSettings::UniformVariable> variables;
variables.assign({ if (offsets.alphaThreshold != std::numeric_limits<std::size_t>::max())
{ {
variables.push_back({
"AlphaThreshold", "AlphaThreshold",
s_uniformOffsets.alphaThreshold offsets.alphaThreshold
}, });
{ }
if (offsets.diffuseColor != std::numeric_limits<std::size_t>::max())
{
variables.push_back({
"DiffuseColor", "DiffuseColor",
s_uniformOffsets.diffuseColor offsets.diffuseColor
} });
}); }
if (offsets.alphaThreshold != std::numeric_limits<std::size_t>::max())
AccessByOffset<float&>(defaultValues.data(), offsets.alphaThreshold) = 0.2f;
static_assert(sizeof(Vector4f) == 4 * sizeof(float), "Vector4f is expected to be exactly 4 floats wide"); static_assert(sizeof(Vector4f) == 4 * sizeof(float), "Vector4f is expected to be exactly 4 floats wide");
if (offsets.diffuseColor != std::numeric_limits<std::size_t>::max())
AccessByOffset<Vector4f&>(defaultValues.data(), offsets.diffuseColor) = Vector4f(1.f, 1.f, 1.f, 1.f);
// Textures
if (textureIndexes)
textureIndexes->alpha = settings.textures.size();
std::vector<UInt8> defaultValues(fieldOffsets.GetSize());
AccessByOffset<Vector4f&>(defaultValues.data(), s_uniformOffsets.diffuseColor) = Vector4f(1.f, 1.f, 1.f, 1.f);
AccessByOffset<float&>(defaultValues.data(), s_uniformOffsets.alphaThreshold) = 0.2f;
s_textureIndexes.alpha = settings.textures.size();
settings.textures.push_back({ settings.textures.push_back({
2, 2,
"Alpha", "Alpha",
ImageType::E2D ImageType::E2D
}); });
s_textureIndexes.diffuse = settings.textures.size(); if (textureIndexes)
textureIndexes->diffuse = settings.textures.size();
settings.textures.push_back({ settings.textures.push_back({
1, 1,
"Diffuse", "Diffuse",
ImageType::E2D ImageType::E2D
}); });
s_uniformBlockIndex = settings.uniformBlocks.size(); if (uniformBlockIndex)
*uniformBlockIndex = settings.uniformBlocks.size();
settings.uniformBlocks.push_back({ settings.uniformBlocks.push_back({
0, 0,
"BasicSettings", "BasicSettings",
fieldOffsets.GetSize(), offsets.totalSize,
std::move(variables), std::move(variables),
std::move(defaultValues) std::move(defaultValues)
}); });
// Shaders settings.shaders = std::move(uberShaders);
ShaderAst::StatementPtr shaderAst = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto uberShader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, shaderAst);
settings.shaders.emplace_back(uberShader);
// Options // Options
// HasDiffuseMap // HasDiffuseMap
{ if (optionIndexes)
std::array<UInt64, ShaderStageTypeCount> shaderOptions; optionIndexes->hasDiffuseMap = settings.options.size();
shaderOptions.fill(0);
shaderOptions[UnderlyingCast(ShaderStageType::Fragment)] = uberShader->GetOptionFlagByName("HAS_DIFFUSE_TEXTURE");
shaderOptions[UnderlyingCast(ShaderStageType::Vertex)] = uberShader->GetOptionFlagByName("HAS_DIFFUSE_TEXTURE");
s_optionIndexes.hasDiffuseMap = settings.options.size(); MaterialSettings::BuildOption(settings.options, settings.shaders, "HasDiffuseMap", "HAS_DIFFUSE_TEXTURE");
settings.options.push_back({
"HasDiffuseMap",
shaderOptions
});
}
// HasAlphaMap // HasAlphaMap
{ if (optionIndexes)
std::array<UInt64, ShaderStageTypeCount> shaderOptions; optionIndexes->hasAlphaMap = settings.options.size();
shaderOptions.fill(0);
shaderOptions[UnderlyingCast(ShaderStageType::Fragment)] = uberShader->GetOptionFlagByName("HAS_ALPHA_TEXTURE");
shaderOptions[UnderlyingCast(ShaderStageType::Vertex)] = uberShader->GetOptionFlagByName("HAS_ALPHA_TEXTURE");
s_optionIndexes.hasAlphaMap = settings.options.size(); MaterialSettings::BuildOption(settings.options, settings.shaders, "HasAlphaMap", "HAS_ALPHA_TEXTURE");
settings.options.push_back({
"HasAlphaMap",
shaderOptions
});
}
// AlphaTest // AlphaTest
{ if (optionIndexes)
std::array<UInt64, ShaderStageTypeCount> shaderOptions; optionIndexes->alphaTest = settings.options.size();
shaderOptions.fill(0);
shaderOptions[UnderlyingCast(ShaderStageType::Fragment)] = uberShader->GetOptionFlagByName("ALPHA_TEST");
s_optionIndexes.alphaTest = settings.options.size(); MaterialSettings::BuildOption(settings.options, settings.shaders, "AlphaTest", "ALPHA_TEST");
settings.options.push_back({
"AlphaTest",
shaderOptions
});
}
s_materialSettings = std::make_shared<MaterialSettings>(std::move(settings)); return settings;
}
std::vector<std::shared_ptr<UberShader>> BasicMaterial::BuildShaders()
{
ShaderAst::StatementPtr shaderAst = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, shaderAst);
return { std::move(shader) };
}
auto BasicMaterial::BuildUniformOffsets() -> std::pair<UniformOffsets, FieldOffsets>
{
FieldOffsets fieldOffsets(StructLayout::Std140);
UniformOffsets uniformOffsets;
uniformOffsets.alphaThreshold = fieldOffsets.AddField(StructFieldType::Float1);
uniformOffsets.diffuseColor = fieldOffsets.AddField(StructFieldType::Float4);
uniformOffsets.totalSize = fieldOffsets.GetSize();
return std::make_pair(std::move(uniformOffsets), std::move(fieldOffsets));
}
bool BasicMaterial::Initialize()
{
std::tie(s_uniformOffsets, std::ignore) = BuildUniformOffsets();
std::vector<UInt8> defaultValues(s_uniformOffsets.totalSize);
s_materialSettings = std::make_shared<MaterialSettings>(Build(s_uniformOffsets, std::move(defaultValues), BuildShaders(), &s_uniformBlockIndex, &s_optionIndexes, &s_textureIndexes));
return true; return true;
} }

View File

@ -0,0 +1,44 @@
// 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/DepthMaterial.hpp>
#include <Nazara/Shader/ShaderLangParser.hpp>
#include <Nazara/Utility/FieldOffsets.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
namespace
{
const UInt8 r_shader[] = {
#include <Nazara/Graphics/Resources/Shaders/depth_material.nzsl.h>
};
}
std::vector<std::shared_ptr<UberShader>> DepthMaterial::BuildShaders()
{
ShaderAst::StatementPtr shaderAst = ShaderLang::Parse(std::string_view(reinterpret_cast<const char*>(r_shader), sizeof(r_shader)));
auto shader = std::make_shared<UberShader>(ShaderStageType::Fragment | ShaderStageType::Vertex, shaderAst);
return { std::move(shader) };
}
bool DepthMaterial::Initialize()
{
UniformOffsets offsets;
std::tie(offsets, std::ignore) = BuildUniformOffsets();
std::vector<UInt8> defaultValues(offsets.totalSize);
s_materialSettings = std::make_shared<MaterialSettings>(Build(offsets, std::move(defaultValues), BuildShaders()));
return true;
}
void DepthMaterial::Uninitialize()
{
s_materialSettings.reset();
}
std::shared_ptr<MaterialSettings> DepthMaterial::s_materialSettings;
}

View File

@ -6,6 +6,7 @@
#include <Nazara/Core/File.hpp> #include <Nazara/Core/File.hpp>
#include <Nazara/Core/Log.hpp> #include <Nazara/Core/Log.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp> #include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/DepthMaterial.hpp>
#include <Nazara/Graphics/MaterialPass.hpp> #include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Graphics/MaterialSettings.hpp> #include <Nazara/Graphics/MaterialSettings.hpp>
#include <Nazara/Graphics/PhongLightingMaterial.hpp> #include <Nazara/Graphics/PhongLightingMaterial.hpp>
@ -78,6 +79,7 @@ namespace Nz
bool MaterialPipeline::Initialize() bool MaterialPipeline::Initialize()
{ {
BasicMaterial::Initialize(); BasicMaterial::Initialize();
DepthMaterial::Initialize();
PhongLightingMaterial::Initialize(); PhongLightingMaterial::Initialize();
return true; return true;
@ -87,6 +89,7 @@ namespace Nz
{ {
s_pipelineCache.clear(); s_pipelineCache.clear();
PhongLightingMaterial::Uninitialize(); PhongLightingMaterial::Uninitialize();
DepthMaterial::Uninitialize();
BasicMaterial::Uninitialize(); BasicMaterial::Uninitialize();
} }

View File

@ -38,6 +38,7 @@ external
[set(1), binding(0)] instanceData: uniform<InstanceData>, [set(1), binding(0)] instanceData: uniform<InstanceData>,
[set(2), binding(0)] settings: uniform<BasicSettings>, [set(2), binding(0)] settings: uniform<BasicSettings>,
[set(2), binding(2)] MaterialAlphaMap: sampler2D<f32>, [set(2), binding(2)] MaterialAlphaMap: sampler2D<f32>,
[set(2), binding(1)] MaterialDiffuseMap: sampler2D<f32>
} }
// Fragment stage // Fragment stage
@ -58,11 +59,11 @@ fn main(input: FragIn)
// TODO: alpha *= MaterialAlphaMap.Sample(input.uv).x // TODO: alpha *= MaterialAlphaMap.Sample(input.uv).x
alpha = alpha * MaterialAlphaMap.Sample(input.uv).x; alpha = alpha * MaterialAlphaMap.Sample(input.uv).x;
// TODO: const assert?
if (alpha < settings.AlphaThreshold) if (alpha < settings.AlphaThreshold)
discard; discard;
} }
// Dummy fragment shader (TODO: Add a way to delete stage?)
[entry(frag), cond(!ALPHA_TEST)] [entry(frag), cond(!ALPHA_TEST)]
fn main() {} fn main() {}