Add PBR rendering (WIP)

This commit is contained in:
SirLynix
2022-04-02 17:21:27 +02:00
committed by Jérôme Leclercq
parent e63bb072da
commit 3e21b4bea6
7 changed files with 1141 additions and 0 deletions

View File

@@ -59,6 +59,7 @@
#include <Nazara/Graphics/MaterialSettings.hpp>
#include <Nazara/Graphics/Model.hpp>
#include <Nazara/Graphics/PhongLightingMaterial.hpp>
#include <Nazara/Graphics/PhysicallyBasedMaterial.hpp>
#include <Nazara/Graphics/PointLight.hpp>
#include <Nazara/Graphics/PredefinedShaderStructs.hpp>
#include <Nazara/Graphics/RenderElement.hpp>

View File

@@ -0,0 +1,125 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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_GRAPHICS_PHYSICALLYBASEDMATERIAL_HPP
#define NAZARA_GRAPHICS_PHYSICALLYBASEDMATERIAL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
namespace Nz
{
class NAZARA_GRAPHICS_API PhysicallyBasedMaterial : public BasicMaterial
{
friend class MaterialPipeline;
public:
PhysicallyBasedMaterial(MaterialPass& material);
Color GetAmbientColor() const;
inline const std::shared_ptr<Texture>& GetEmissiveMap() const;
inline const TextureSamplerInfo& GetEmissiveSampler() const;
inline const std::shared_ptr<Texture>& GetHeightMap() const;
inline const TextureSamplerInfo& GetHeightSampler() const;
inline const std::shared_ptr<Texture>& GetMetallicMap() const;
inline const TextureSamplerInfo& GetMetallicSampler() const;
inline const std::shared_ptr<Texture>& GetNormalMap() const;
inline const TextureSamplerInfo& GetNormalSampler() const;
inline const std::shared_ptr<Texture>& GetRoughnessMap() const;
inline const TextureSamplerInfo& GetRoughnessSampler() const;
float GetShininess() const;
Color GetSpecularColor() const;
inline const std::shared_ptr<Texture>& GetSpecularMap() const;
inline const TextureSamplerInfo& GetSpecularSampler() const;
inline bool HasAmbientColor() const;
inline bool HasEmissiveMap() const;
inline bool HasHeightMap() const;
inline bool HasMetallicMap() const;
inline bool HasNormalMap() const;
inline bool HasRoughnessMap() const;
inline bool HasShininess() const;
inline bool HasSpecularColor() const;
inline bool HasSpecularMap() const;
void SetAmbientColor(const Color& ambient);
inline void SetEmissiveMap(std::shared_ptr<Texture> emissiveMap);
inline void SetEmissiveSampler(TextureSamplerInfo emissiveSampler);
inline void SetHeightMap(std::shared_ptr<Texture> heightMap);
inline void SetHeightSampler(TextureSamplerInfo heightSampler);
inline void SetMetallicMap(std::shared_ptr<Texture> metallicMap);
inline void SetMetallicSampler(TextureSamplerInfo metallicSampler);
inline void SetNormalMap(std::shared_ptr<Texture> normalMap);
inline void SetNormalSampler(TextureSamplerInfo normalSampler);
inline void SetRoughnessMap(std::shared_ptr<Texture> roughnessMap);
inline void SetRoughnessSampler(TextureSamplerInfo roughnessSampler);
void SetShininess(float shininess);
void SetSpecularColor(const Color& specular);
inline void SetSpecularMap(std::shared_ptr<Texture> specularMap);
inline void SetSpecularSampler(TextureSamplerInfo specularSampler);
static const std::shared_ptr<MaterialSettings>& GetSettings();
protected:
struct PbrOptionIndexes
{
std::size_t hasEmissiveMap;
std::size_t hasHeightMap;
std::size_t hasMetallicMap;
std::size_t hasNormalMap;
std::size_t hasRoughnessMap;
std::size_t hasSpecularMap;
};
struct PbrUniformOffsets
{
std::size_t ambientColor;
std::size_t shininess;
std::size_t totalSize;
std::size_t specularColor;
};
struct PbrTextureIndexes
{
std::size_t emissive;
std::size_t height;
std::size_t metallic;
std::size_t roughness;
std::size_t normal;
std::size_t specular;
};
struct PbrBuildOptions : BasicBuildOptions
{
PbrUniformOffsets pbrOffsets;
PbrOptionIndexes* pbrOptionIndexes = nullptr;
PbrTextureIndexes* pbrTextureIndexes = nullptr;
};
PbrOptionIndexes m_pbrOptionIndexes;
PbrTextureIndexes m_pbrTextureIndexes;
PbrUniformOffsets m_pbrUniformOffsets;
static MaterialSettings::Builder Build(PbrBuildOptions& options);
static std::vector<std::shared_ptr<UberShader>> BuildShaders();
static std::pair<PbrUniformOffsets, FieldOffsets> BuildUniformOffsets();
private:
static bool Initialize();
static void Uninitialize();
static std::shared_ptr<MaterialSettings> s_pbrMaterialSettings;
static std::size_t s_pbrUniformBlockIndex;
static PbrOptionIndexes s_pbrOptionIndexes;
static PbrTextureIndexes s_pbrTextureIndexes;
static PbrUniformOffsets s_pbrUniformOffsets;
};
}
#include <Nazara/Graphics/PhysicallyBasedMaterial.inl>
#endif // NAZARA_GRAPHICS_PHYSICALLYBASEDMATERIAL_HPP

View File

@@ -0,0 +1,226 @@
// Copyright (C) 2022 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// 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/PhysicallyBasedMaterial.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <memory>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
{
inline const std::shared_ptr<Texture>& PhysicallyBasedMaterial::GetEmissiveMap() const
{
NazaraAssert(HasEmissiveMap(), "Material has no emissive map slot");
return GetMaterial().GetTexture(m_pbrTextureIndexes.emissive);
}
inline const TextureSamplerInfo& PhysicallyBasedMaterial::GetEmissiveSampler() const
{
NazaraAssert(HasSpecularMap(), "Material has no emissive map slot");
return GetMaterial().GetTextureSampler(m_pbrTextureIndexes.emissive);
}
inline const std::shared_ptr<Texture>& PhysicallyBasedMaterial::GetHeightMap() const
{
NazaraAssert(HasHeightMap(), "Material has no height map slot");
return GetMaterial().GetTexture(m_pbrTextureIndexes.height);
}
inline const TextureSamplerInfo& PhysicallyBasedMaterial::GetHeightSampler() const
{
NazaraAssert(HasSpecularMap(), "Material has no height map slot");
return GetMaterial().GetTextureSampler(m_pbrTextureIndexes.height);
}
inline const std::shared_ptr<Texture>& PhysicallyBasedMaterial::GetMetallicMap() const
{
NazaraAssert(HasMetallicMap(), "Material has no metallic map slot");
return GetMaterial().GetTexture(m_pbrTextureIndexes.metallic);
}
inline const TextureSamplerInfo& PhysicallyBasedMaterial::GetMetallicSampler() const
{
NazaraAssert(HasMetallicMap(), "Material has no metallic map slot");
return GetMaterial().GetTextureSampler(m_pbrTextureIndexes.metallic);
}
inline const std::shared_ptr<Texture>& PhysicallyBasedMaterial::GetNormalMap() const
{
NazaraAssert(HasNormalMap(), "Material has no normal map slot");
return GetMaterial().GetTexture(m_pbrTextureIndexes.normal);
}
inline const TextureSamplerInfo& PhysicallyBasedMaterial::GetNormalSampler() const
{
NazaraAssert(HasSpecularMap(), "Material has no normal map slot");
return GetMaterial().GetTextureSampler(m_pbrTextureIndexes.normal);
}
inline const std::shared_ptr<Texture>& PhysicallyBasedMaterial::GetRoughnessMap() const
{
NazaraAssert(HasRoughnessMap(), "Material has no roughness map slot");
return GetMaterial().GetTexture(m_pbrTextureIndexes.roughness);
}
inline const TextureSamplerInfo& PhysicallyBasedMaterial::GetRoughnessSampler() const
{
NazaraAssert(HasRoughnessMap(), "Material has no roughness map slot");
return GetMaterial().GetTextureSampler(m_pbrTextureIndexes.roughness);
}
inline const std::shared_ptr<Texture>& PhysicallyBasedMaterial::GetSpecularMap() const
{
NazaraAssert(HasSpecularMap(), "Material has no specular map slot");
return GetMaterial().GetTexture(m_pbrTextureIndexes.specular);
}
inline const TextureSamplerInfo& PhysicallyBasedMaterial::GetSpecularSampler() const
{
NazaraAssert(HasSpecularMap(), "Material has no specular map slot");
return GetMaterial().GetTextureSampler(m_pbrTextureIndexes.specular);
}
inline bool PhysicallyBasedMaterial::HasAmbientColor() const
{
return m_pbrUniformOffsets.ambientColor != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasEmissiveMap() const
{
return m_pbrTextureIndexes.emissive != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasHeightMap() const
{
return m_pbrTextureIndexes.height != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasMetallicMap() const
{
return m_pbrTextureIndexes.metallic != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasNormalMap() const
{
return m_pbrTextureIndexes.normal != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasRoughnessMap() const
{
return m_pbrTextureIndexes.roughness != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasShininess() const
{
return m_pbrUniformOffsets.shininess != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasSpecularColor() const
{
return m_pbrUniformOffsets.specularColor != MaterialSettings::InvalidIndex;
}
inline bool PhysicallyBasedMaterial::HasSpecularMap() const
{
return m_pbrTextureIndexes.specular != MaterialSettings::InvalidIndex;
}
inline void PhysicallyBasedMaterial::SetEmissiveMap(std::shared_ptr<Texture> emissiveMap)
{
NazaraAssert(HasEmissiveMap(), "Material has no emissive map slot");
bool hasEmissiveMap = (emissiveMap != nullptr);
GetMaterial().SetTexture(m_pbrTextureIndexes.emissive, std::move(emissiveMap));
if (m_pbrOptionIndexes.hasEmissiveMap != MaterialSettings::InvalidIndex)
GetMaterial().SetOptionValue(m_pbrOptionIndexes.hasEmissiveMap, hasEmissiveMap);
}
inline void PhysicallyBasedMaterial::SetEmissiveSampler(TextureSamplerInfo emissiveSampler)
{
NazaraAssert(HasEmissiveMap(), "Material has no emissive map slot");
GetMaterial().SetTextureSampler(m_pbrTextureIndexes.emissive, std::move(emissiveSampler));
}
inline void PhysicallyBasedMaterial::SetHeightMap(std::shared_ptr<Texture> heightMap)
{
NazaraAssert(HasHeightMap(), "Material has no specular map slot");
bool hasHeightMap = (heightMap != nullptr);
GetMaterial().SetTexture(m_pbrTextureIndexes.height, std::move(heightMap));
if (m_pbrOptionIndexes.hasHeightMap != MaterialSettings::InvalidIndex)
GetMaterial().SetOptionValue(m_pbrOptionIndexes.hasHeightMap, hasHeightMap);
}
inline void PhysicallyBasedMaterial::SetHeightSampler(TextureSamplerInfo heightSampler)
{
NazaraAssert(HasHeightMap(), "Material has no height map slot");
GetMaterial().SetTextureSampler(m_pbrTextureIndexes.height, std::move(heightSampler));
}
inline void PhysicallyBasedMaterial::SetMetallicMap(std::shared_ptr<Texture> metallicMap)
{
NazaraAssert(HasMetallicMap(), "Material has no metallic map slot");
bool hasMetallicMap = (metallicMap != nullptr);
GetMaterial().SetTexture(m_pbrTextureIndexes.metallic, std::move(metallicMap));
if (m_pbrOptionIndexes.hasMetallicMap != MaterialSettings::InvalidIndex)
GetMaterial().SetOptionValue(m_pbrOptionIndexes.hasMetallicMap, hasMetallicMap);
}
inline void PhysicallyBasedMaterial::SetMetallicSampler(TextureSamplerInfo metallicSampler)
{
NazaraAssert(HasMetallicMap(), "Material has no metallic map slot");
GetMaterial().SetTextureSampler(m_pbrTextureIndexes.metallic, std::move(metallicSampler));
}
inline void PhysicallyBasedMaterial::SetNormalMap(std::shared_ptr<Texture> normalMap)
{
NazaraAssert(HasNormalMap(), "Material has no normal map slot");
bool hasNormalMap = (normalMap != nullptr);
GetMaterial().SetTexture(m_pbrTextureIndexes.normal, std::move(normalMap));
if (m_pbrOptionIndexes.hasNormalMap != MaterialSettings::InvalidIndex)
GetMaterial().SetOptionValue(m_pbrOptionIndexes.hasNormalMap, hasNormalMap);
}
inline void PhysicallyBasedMaterial::SetNormalSampler(TextureSamplerInfo normalSampler)
{
NazaraAssert(HasNormalMap(), "Material has no normal map slot");
GetMaterial().SetTextureSampler(m_pbrTextureIndexes.normal, std::move(normalSampler));
}
inline void PhysicallyBasedMaterial::SetRoughnessMap(std::shared_ptr<Texture> roughnessMap)
{
NazaraAssert(HasRoughnessMap(), "Material has no roughness map slot");
bool hasRoughnessMap = (roughnessMap != nullptr);
GetMaterial().SetTexture(m_pbrTextureIndexes.roughness, std::move(roughnessMap));
if (m_pbrOptionIndexes.hasRoughnessMap != MaterialSettings::InvalidIndex)
GetMaterial().SetOptionValue(m_pbrOptionIndexes.hasRoughnessMap, hasRoughnessMap);
}
inline void PhysicallyBasedMaterial::SetRoughnessSampler(TextureSamplerInfo metallicSampler)
{
NazaraAssert(HasRoughnessMap(), "Material has no roughness map slot");
GetMaterial().SetTextureSampler(m_pbrTextureIndexes.roughness, std::move(metallicSampler));
}
inline void PhysicallyBasedMaterial::SetSpecularMap(std::shared_ptr<Texture> specularMap)
{
NazaraAssert(HasNormalMap(), "Material has no specular map slot");
bool hasSpecularMap = (specularMap != nullptr);
GetMaterial().SetTexture(m_pbrTextureIndexes.specular, std::move(specularMap));
if (m_pbrOptionIndexes.hasSpecularMap != MaterialSettings::InvalidIndex)
GetMaterial().SetOptionValue(m_pbrOptionIndexes.hasSpecularMap, hasSpecularMap);
}
inline void PhysicallyBasedMaterial::SetSpecularSampler(TextureSamplerInfo specularSampler)
{
NazaraAssert(HasSpecularMap(), "Material has no specular map slot");
GetMaterial().SetTextureSampler(m_pbrTextureIndexes.specular, std::move(specularSampler));
}
}
#include <Nazara/Graphics/DebugOff.hpp>