Graphics: Add DepthMaterial
This commit is contained in:
parent
7aafcfaae9
commit
a2a0e6bd54
|
|
@ -77,14 +77,9 @@ int main()
|
|||
|
||||
std::shared_ptr<Nz::Material> material = std::make_shared<Nz::Material>();
|
||||
|
||||
auto customSettings = Nz::BasicMaterial::GetSettings()->GetBuilderData();
|
||||
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);
|
||||
std::shared_ptr<Nz::MaterialPass> depthPass = std::make_shared<Nz::MaterialPass>(Nz::DepthMaterial::GetSettings());
|
||||
depthPass->EnableDepthBuffer(true);
|
||||
depthPass->EnableDepthClamp(true);
|
||||
depthPass->EnableFaceCulling(true);
|
||||
|
||||
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.SetDiffuseSampler(samplerInfo);
|
||||
|
||||
Nz::BasicMaterial basicMatDepth(*depthPass);
|
||||
Nz::DepthMaterial basicMatDepth(*depthPass);
|
||||
basicMatDepth.SetAlphaMap(Nz::Texture::LoadFromFile(resourceDir / "alphatile.png", texParams));
|
||||
|
||||
std::shared_ptr<Nz::Model> model = std::make_shared<Nz::Model>(std::move(gfxMesh));
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
#include <Nazara/Graphics/Camera.hpp>
|
||||
#include <Nazara/Graphics/Config.hpp>
|
||||
#include <Nazara/Graphics/CullingList.hpp>
|
||||
#include <Nazara/Graphics/DepthMaterial.hpp>
|
||||
#include <Nazara/Graphics/ElementRenderer.hpp>
|
||||
#include <Nazara/Graphics/Enums.hpp>
|
||||
#include <Nazara/Graphics/ForwardFramePipeline.hpp>
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class FieldOffsets;
|
||||
|
||||
class NAZARA_GRAPHICS_API BasicMaterial
|
||||
{
|
||||
friend class MaterialPipeline;
|
||||
|
|
@ -56,7 +58,7 @@ namespace Nz
|
|||
std::size_t totalSize;
|
||||
};
|
||||
|
||||
private:
|
||||
protected:
|
||||
struct OptionIndexes
|
||||
{
|
||||
std::size_t alphaTest;
|
||||
|
|
@ -70,6 +72,11 @@ namespace Nz
|
|||
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 void Uninitialize();
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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>
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Graphics/Enums.hpp>
|
||||
#include <Nazara/Graphics/UberShader.hpp>
|
||||
#include <Nazara/Renderer/RenderPipelineLayout.hpp>
|
||||
#include <Nazara/Renderer/ShaderModule.hpp>
|
||||
#include <Nazara/Utility/Enums.hpp>
|
||||
|
|
@ -19,8 +20,6 @@
|
|||
|
||||
namespace Nz
|
||||
{
|
||||
class UberShader;
|
||||
|
||||
class MaterialSettings
|
||||
{
|
||||
public:
|
||||
|
|
@ -56,6 +55,8 @@ namespace Nz
|
|||
|
||||
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
|
||||
{
|
||||
std::vector<std::shared_ptr<UberShader>> shaders;
|
||||
|
|
|
|||
|
|
@ -165,6 +165,32 @@ namespace Nz
|
|||
|
||||
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>
|
||||
|
|
|
|||
|
|
@ -94,107 +94,115 @@ namespace Nz
|
|||
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;
|
||||
|
||||
std::vector<MaterialSettings::UniformVariable> variables;
|
||||
variables.assign({
|
||||
if (offsets.alphaThreshold != std::numeric_limits<std::size_t>::max())
|
||||
{
|
||||
variables.push_back({
|
||||
"AlphaThreshold",
|
||||
s_uniformOffsets.alphaThreshold
|
||||
},
|
||||
{
|
||||
"DiffuseColor",
|
||||
s_uniformOffsets.diffuseColor
|
||||
}
|
||||
offsets.alphaThreshold
|
||||
});
|
||||
}
|
||||
|
||||
if (offsets.diffuseColor != std::numeric_limits<std::size_t>::max())
|
||||
{
|
||||
variables.push_back({
|
||||
"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");
|
||||
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);
|
||||
|
||||
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;
|
||||
// Textures
|
||||
if (textureIndexes)
|
||||
textureIndexes->alpha = settings.textures.size();
|
||||
|
||||
s_textureIndexes.alpha = settings.textures.size();
|
||||
settings.textures.push_back({
|
||||
2,
|
||||
"Alpha",
|
||||
ImageType::E2D
|
||||
});
|
||||
|
||||
s_textureIndexes.diffuse = settings.textures.size();
|
||||
if (textureIndexes)
|
||||
textureIndexes->diffuse = settings.textures.size();
|
||||
|
||||
settings.textures.push_back({
|
||||
1,
|
||||
"Diffuse",
|
||||
ImageType::E2D
|
||||
});
|
||||
|
||||
s_uniformBlockIndex = settings.uniformBlocks.size();
|
||||
if (uniformBlockIndex)
|
||||
*uniformBlockIndex = settings.uniformBlocks.size();
|
||||
|
||||
settings.uniformBlocks.push_back({
|
||||
0,
|
||||
"BasicSettings",
|
||||
fieldOffsets.GetSize(),
|
||||
offsets.totalSize,
|
||||
std::move(variables),
|
||||
std::move(defaultValues)
|
||||
});
|
||||
|
||||
// Shaders
|
||||
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);
|
||||
settings.shaders = std::move(uberShaders);
|
||||
|
||||
// Options
|
||||
|
||||
// HasDiffuseMap
|
||||
{
|
||||
std::array<UInt64, ShaderStageTypeCount> shaderOptions;
|
||||
shaderOptions.fill(0);
|
||||
shaderOptions[UnderlyingCast(ShaderStageType::Fragment)] = uberShader->GetOptionFlagByName("HAS_DIFFUSE_TEXTURE");
|
||||
shaderOptions[UnderlyingCast(ShaderStageType::Vertex)] = uberShader->GetOptionFlagByName("HAS_DIFFUSE_TEXTURE");
|
||||
if (optionIndexes)
|
||||
optionIndexes->hasDiffuseMap = settings.options.size();
|
||||
|
||||
s_optionIndexes.hasDiffuseMap = settings.options.size();
|
||||
settings.options.push_back({
|
||||
"HasDiffuseMap",
|
||||
shaderOptions
|
||||
});
|
||||
}
|
||||
MaterialSettings::BuildOption(settings.options, settings.shaders, "HasDiffuseMap", "HAS_DIFFUSE_TEXTURE");
|
||||
|
||||
// HasAlphaMap
|
||||
{
|
||||
std::array<UInt64, ShaderStageTypeCount> shaderOptions;
|
||||
shaderOptions.fill(0);
|
||||
shaderOptions[UnderlyingCast(ShaderStageType::Fragment)] = uberShader->GetOptionFlagByName("HAS_ALPHA_TEXTURE");
|
||||
shaderOptions[UnderlyingCast(ShaderStageType::Vertex)] = uberShader->GetOptionFlagByName("HAS_ALPHA_TEXTURE");
|
||||
if (optionIndexes)
|
||||
optionIndexes->hasAlphaMap = settings.options.size();
|
||||
|
||||
s_optionIndexes.hasAlphaMap = settings.options.size();
|
||||
settings.options.push_back({
|
||||
"HasAlphaMap",
|
||||
shaderOptions
|
||||
});
|
||||
}
|
||||
MaterialSettings::BuildOption(settings.options, settings.shaders, "HasAlphaMap", "HAS_ALPHA_TEXTURE");
|
||||
|
||||
// AlphaTest
|
||||
{
|
||||
std::array<UInt64, ShaderStageTypeCount> shaderOptions;
|
||||
shaderOptions.fill(0);
|
||||
shaderOptions[UnderlyingCast(ShaderStageType::Fragment)] = uberShader->GetOptionFlagByName("ALPHA_TEST");
|
||||
if (optionIndexes)
|
||||
optionIndexes->alphaTest = settings.options.size();
|
||||
|
||||
s_optionIndexes.alphaTest = settings.options.size();
|
||||
settings.options.push_back({
|
||||
"AlphaTest",
|
||||
shaderOptions
|
||||
});
|
||||
MaterialSettings::BuildOption(settings.options, settings.shaders, "AlphaTest", "ALPHA_TEST");
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
s_materialSettings = std::make_shared<MaterialSettings>(std::move(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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@
|
|||
#include <Nazara/Core/File.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/DepthMaterial.hpp>
|
||||
#include <Nazara/Graphics/MaterialPass.hpp>
|
||||
#include <Nazara/Graphics/MaterialSettings.hpp>
|
||||
#include <Nazara/Graphics/PhongLightingMaterial.hpp>
|
||||
|
|
@ -78,6 +79,7 @@ namespace Nz
|
|||
bool MaterialPipeline::Initialize()
|
||||
{
|
||||
BasicMaterial::Initialize();
|
||||
DepthMaterial::Initialize();
|
||||
PhongLightingMaterial::Initialize();
|
||||
|
||||
return true;
|
||||
|
|
@ -87,6 +89,7 @@ namespace Nz
|
|||
{
|
||||
s_pipelineCache.clear();
|
||||
PhongLightingMaterial::Uninitialize();
|
||||
DepthMaterial::Uninitialize();
|
||||
BasicMaterial::Uninitialize();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ external
|
|||
[set(1), binding(0)] instanceData: uniform<InstanceData>,
|
||||
[set(2), binding(0)] settings: uniform<BasicSettings>,
|
||||
[set(2), binding(2)] MaterialAlphaMap: sampler2D<f32>,
|
||||
[set(2), binding(1)] MaterialDiffuseMap: sampler2D<f32>
|
||||
}
|
||||
|
||||
// Fragment stage
|
||||
|
|
@ -58,11 +59,11 @@ fn main(input: FragIn)
|
|||
// TODO: alpha *= MaterialAlphaMap.Sample(input.uv).x
|
||||
alpha = alpha * MaterialAlphaMap.Sample(input.uv).x;
|
||||
|
||||
// TODO: const assert?
|
||||
if (alpha < settings.AlphaThreshold)
|
||||
discard;
|
||||
}
|
||||
|
||||
// Dummy fragment shader (TODO: Add a way to delete stage?)
|
||||
[entry(frag), cond(!ALPHA_TEST)]
|
||||
fn main() {}
|
||||
|
||||
Loading…
Reference in New Issue