Skip to content

Commit

Permalink
Merge branch 'main' into Ribbon
Browse files Browse the repository at this point in the history
  • Loading branch information
OVOAOVO committed May 18, 2024
2 parents 2af93aa + 9c27e06 commit c9db3b3
Show file tree
Hide file tree
Showing 49 changed files with 1,230 additions and 291 deletions.
3 changes: 1 addition & 2 deletions Engine/Auto/Scripts/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ project("Editor")

if ENABLE_SPDLOG then
defines {
-- TODO : Remove _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING after spdlog updates the format to the right version.
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "FMT_USE_NONTYPE_TEMPLATE_ARGS=0", "_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING",
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "SPDLOG_USE_STD_FORMAT",
}

includedirs {
Expand Down
3 changes: 1 addition & 2 deletions Engine/Auto/Scripts/engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ project("Engine")
}

defines {
-- TODO : Remove _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING after spdlog updates the format to the right version.
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "FMT_USE_NONTYPE_TEMPLATE_ARGS=0", "_SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING",
"SPDLOG_ENABLE", "SPDLOG_NO_EXCEPTIONS", "SPDLOG_USE_STD_FORMAT",
}
end

Expand Down
43 changes: 31 additions & 12 deletions Engine/BuiltInShaders/common/BRDF.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,36 @@ float DistributionGGX(float NdotH, float rough) {
return a2 * CD_PI_INV * denom_inv * denom_inv;
}

// Geometry
float Visibility(float NdotV, float NdotL, float rough) {
// Specular BRDF = (F * D * G) / (4 * NdotV * NdotL)
// = (F * D * (NdotV / (NdotV * (1 - K) + K)) * (NdotL / (NdotL * (1 - K) + K))) / (4 * NdotV * NdotL)
// = (F * D * (1 / (NdotV * (1 - K) + K)) * (1 / (NdotL * (1 - K) + K))) / 4
// = F * D * Vis
// Vis = 1 / (NdotV * (1 - K) + K) / (NdotL * (1 - K) + K) / 4
float Visibility_HighQuality(float NdotV, float NdotL, float rough) {
// BRDF = (F * D * G) / (4 * NdotV * NdotL) = F * D * V
// V = G / (4 * NdotV * NdotL)
// = 0.5 / (NdotL * sqrt(a2 + (1 - a2) * NdotV^2) + NdotV * sqrt(a2 + (1 - a2) * NdotL^2))

// rough = (rough + 1) / 2, by Disney
// a = rough^2
float a2 = pow((rough + 1.0) * 0.5, 4);
float lambda_v = NdotL * sqrt(a2 + (1.0 - a2) * NdotV * NdotV);
float lambda_l = NdotV * sqrt(a2 + (1.0 - a2) * NdotL * NdotL);
return 0.5 / (lambda_v + lambda_l);
}
float Visibility_LowQuality(float NdotV, float NdotL, float rough) {
// BRDF = (F * D * G) / (4 * NdotV * NdotL) = F * D * V
// V = G / (4 * NdotV * NdotL)
// = 1 / ((NdotV * (2 - a) + a) * (NdotL * (2 - a) + a))

// rough = (rough + 1) / 2, by Disney
// a = rough^2
float a = (rough + 1.0) * (rough + 1.0) * 0.25;
float g1_v_inv = NdotV * (2.0 - a) + a;
float g1_l_inv = NdotL * (2.0 - a) + a;

float f = rough + 1.0;
float k = f * f * 0.125;
float ggxV = 1.0 / (NdotV * (1.0 - k) + k);
float ggxL = 1.0 / (NdotL * (1.0 - k) + k);
return ggxV * ggxL * 0.25;
return 1.0 / (g1_v_inv * g1_l_inv);
}

// Geometry
float Visibility(float NdotV, float NdotL, float rough) {
// TODO : Wrap them in macros after we've collected enough approximate / exact formulas.
return Visibility_LowQuality(NdotV, NdotL, rough);
}
13 changes: 5 additions & 8 deletions Engine/BuiltInShaders/common/Envirnoment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ SAMPLER2D(s_texLUT, BRDF_LUT_SLOT);

uniform vec4 u_iblStrength;

vec3 SampleEnvIrradiance(vec3 normal, float mip) {
vec3 SampleEnvIrradiance(vec3 normal) {
// We use the HDR texture which in linear space.
vec3 cubeNormalDir = normalize(fixCubeLookup(normal, mip, 256.0));
return textureCube(s_texCubeIrr, cubeNormalDir).xyz;
return textureCube(s_texCubeIrr, normal).xyz;
}

vec3 SampleEnvRadiance(vec3 reflectDir, float mip) {
// We use the HDR texture which in linear space.
vec3 cubeReflectDir = normalize(fixCubeLookup(reflectDir, mip, 256.0));
return textureCubeLod(s_texCubeRad, cubeReflectDir, mip).xyz;
return textureCubeLod(s_texCubeRad, reflectDir, mip).xyz;
}

vec2 SampleIBLSpecularBRDFLUT(float NdotV, float roughness) {
Expand All @@ -57,11 +55,10 @@ vec3 GetIBL(Material material, vec3 vertexNormal, vec3 viewDir) {
horizonOcclusion *= horizonOcclusion;
float finalSpecularOcclusion = min(specularOcclusion, horizonOcclusion);

float mip = clamp(6.0 * material.roughness, 0.1, 6.0);

// Environment Prefiltered Irradiance
vec3 envIrradiance = SampleEnvIrradiance(material.normal, 0.0);
vec3 envIrradiance = SampleEnvIrradiance(material.normal);
// Environment Specular Radiance
float mip = material.roughness * 6.0;
vec3 envRadiance = SampleEnvRadiance(reflectDir, mip);

// Environment Specular BRDF
Expand Down
7 changes: 4 additions & 3 deletions Engine/BuiltInShaders/common/LightSource.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ uniform vec4 u_lightParams[LIGHT_LENGTH];
uniform mat4 u_lightViewProjs[4*3];
uniform vec4 u_clipFrustumDepth;
uniform vec4 u_bias[3]; // [LIGHT_NUM]
uniform vec4 u_isCastShadow;

SAMPLERCUBE(s_texCubeShadowMap_1, SHADOW_MAP_CUBE_FIRST_SLOT);
SAMPLERCUBE(s_texCubeShadowMap_2, SHADOW_MAP_CUBE_SECOND_SLOT);
Expand Down Expand Up @@ -185,7 +186,7 @@ vec3 CalculatePointLight(U_Light light, Material material, vec3 worldPos, vec3 v
vec3 specularBRDF = Fre * NDF * Vis;

vec3 KD = mix(vec3_splat(1.0) - Fre, vec3_splat(0.0), material.metallic);
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex);
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex) * u_isCastShadow.x;
return (1 - shadow) * (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
}

Expand Down Expand Up @@ -237,7 +238,7 @@ vec3 CalculateSpotLight(U_Light light, Material material, vec3 worldPos, vec3 vi
vec3 specularBRDF = Fre * NDF * Vis;

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
float shadow = CalculateSpotShadow(worldPos, material.normal, lightDir, light.lightViewProjOffset, lightIndex);
float shadow = CalculatePointShadow(worldPos, light.position, light.range, lightIndex) * u_isCastShadow.x;
return (1.0 - shadow) * (KD * diffuseBRDF + specularBRDF) * radiance * NdotL;
}

Expand Down Expand Up @@ -310,7 +311,7 @@ vec3 CalculateDirectionalLight(U_Light light, Material material, vec3 worldPos,

vec3 KD = mix(1.0 - Fre, vec3_splat(0.0), material.metallic);
vec3 irradiance = light.color * light.intensity;
float shadow = CalculateCascadedDirectionalShadow(worldPos, material.normal, lightDir, csmDepth, light.lightViewProjOffset, lightIndex);
float shadow = CalculateCascadedDirectionalShadow(worldPos, material.normal, lightDir, csmDepth, light.lightViewProjOffset, lightIndex) * u_isCastShadow.x;
return (1.0 - shadow) * (KD * diffuseBRDF + specularBRDF) * irradiance * NdotL;
}

Expand Down
9 changes: 5 additions & 4 deletions Engine/BuiltInShaders/common/Material.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Material {
};
uniform vec4 u_albedoColor;
uniform vec4 u_metallicRoughnessFactor;
uniform vec4 u_metallicRoughnessRefectanceFactor;
uniform vec4 u_albedoUVOffsetAndScale;
uniform vec4 u_alphaCutOff;
Expand Down Expand Up @@ -110,15 +110,16 @@ Material GetMaterial(vec2 uv, vec3 normal, mat3 TBN) {
material.roughness = orm.y;
material.metallic = orm.z;
#else
material.roughness = u_metallicRoughnessFactor.y;
material.metallic = u_metallicRoughnessFactor.x;
material.roughness = u_metallicRoughnessRefectanceFactor.y;
material.metallic = u_metallicRoughnessRefectanceFactor.x;
#endif
#if defined(EMISSIVEMAP)
material.emissive = SampleEmissiveTexture(uv);
#endif
material.F0 = mix(vec3_splat(0.04), material.albedo, material.metallic);
float refectance = u_metallicRoughnessRefectanceFactor.z;
material.F0 = mix(0.16 * refectance * refectance, material.albedo, material.metallic);
return material;
}
10 changes: 9 additions & 1 deletion Engine/BuiltInShaders/common/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
#define CD_PI 3.1415926536
#define CD_PI2 9.8696044011
#define CD_PI_INV 0.3183098862
#define CD_PI_INV2 0.1013211836
#define CD_PI_INV2 0.1013211836

// https://baddogzz.github.io/2020/03/04/SGA-Pow-Opt/
float pow_low(float x, float n)
{
// 1.4427f --> 1/ln(2)
n = n * 1.4427 + 1.4427;
return exp2(x * n - n);
}
12 changes: 12 additions & 0 deletions Engine/BuiltInShaders/shaders/vs_skeleton.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$input a_position, a_indices

#include "../common/common.sh"

uniform mat4 u_boneMatrices[128];

void main()
{
mat4 boneTransform = u_boneMatrices[a_indices[0]];
vec4 localPosition = mul(boneTransform, vec4(a_position, 1.0));
gl_Position = mul(u_modelViewProj, localPosition);
}
64 changes: 57 additions & 7 deletions Engine/Source/Editor/ECWorld/ECWorldConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#include "Rendering/RenderContext.h"
#include "Rendering/Resources/MeshResource.h"
#include "Rendering/Resources/ResourceContext.h"
#include "Rendering/Resources/SkeletonResource.h"
#include "Rendering/Resources/TextureResource.h"
#include "Rendering/ShaderFeature.h"
#include "Resources/ResourceBuilder.h"
#include "Resources/ResourceLoader.h"
#include "Resources/ShaderBuilder.h"
#include "Rendering/Utility/VertexLayoutUtility.h"
#include "Scene/SceneDatabase.h"

#include <algorithm>
Expand Down Expand Up @@ -57,13 +59,17 @@ void ECWorldConsumer::Execute(const cd::SceneDatabase* pSceneDatabase)
bool hasSkin = mesh.GetSkinIDCount() > 0U;
if (hasSkin)
{
engine::MaterialType* pMaterialType = m_pSceneWorld->GetAnimationMaterialType();
AddSkinMesh(meshEntity, mesh, pMaterialType->GetRequiredVertexFormat());

// TODO : Use a standalone .cdanim file to play animation.
// Currently, we assume that imported SkinMesh will play animation automatically for testing.
AddAnimation(meshEntity, pSceneDatabase->GetAnimation(0), pSceneDatabase);
AddMaterial(meshEntity, nullptr, pMaterialType, pSceneDatabase);
//TODO : depens on skeleton to match animation
if (pSceneDatabase->GetAnimationCount() <= 1U)
{
engine::MaterialType* pMaterialType = m_pSceneWorld->GetAnimationMaterialType();
AddSkinMesh(meshEntity, mesh, pMaterialType->GetRequiredVertexFormat(), pSceneDatabase);
AddAnimation(meshEntity, pSceneDatabase->GetAnimation(0), pSceneDatabase);
AddMaterial(meshEntity, nullptr, pMaterialType, pSceneDatabase);
AddSkeleton(meshEntity, pSceneDatabase);
}
}
else
{
Expand Down Expand Up @@ -185,9 +191,39 @@ void ECWorldConsumer::AddStaticMesh(engine::Entity entity, const cd::Mesh& mesh,
staticMeshComponent.SetMeshResource(pMeshResource);
}

void ECWorldConsumer::AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat)
void ECWorldConsumer::AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat, const cd::SceneDatabase* pSceneDatabase)
{
AddStaticMesh(entity, mesh, vertexFormat);
assert(mesh.GetVertexCount() > 0 && mesh.GetPolygonCount() > 0);

engine::World* pWorld = m_pSceneWorld->GetWorld();
auto& nameComponent = pWorld->CreateComponent<engine::NameComponent>(entity);
std::string meshName(mesh.GetName());
engine::StringCrc meshNameCrc(meshName);
nameComponent.SetName(cd::MoveTemp(meshName));

auto& collisionMeshComponent = pWorld->CreateComponent<engine::CollisionMeshComponent>(entity);
collisionMeshComponent.SetType(engine::CollisonMeshType::AABB);
collisionMeshComponent.SetAABB(mesh.GetAABB());
collisionMeshComponent.Build();

auto& staticMeshComponent = pWorld->CreateComponent<engine::StaticMeshComponent>(entity);
engine::MeshResource* pMeshResource = m_pResourceContext->AddMeshResource(meshNameCrc);
pMeshResource->SetMeshAsset(&mesh);

//auto& skinMeshComponent = pWorld->CreateComponent<engine::SkinMeshComponent>(entity);
for (auto skinID : mesh.GetSkinIDs())
{
pMeshResource->SetSkinAsset(&pSceneDatabase->GetSkin(skinID.Data()));
auto skeletonID = pSceneDatabase->GetSkin(skinID.Data()).GetSkeletonID();
auto& skeleton = pSceneDatabase->GetSkeleton(skeletonID.Data());
for (auto boneID : skeleton.GetBoneIDs())
{
pMeshResource->AddBonesAsset(pSceneDatabase->GetBone(boneID.Data()));
}
}

pMeshResource->UpdateVertexFormat(vertexFormat);
staticMeshComponent.SetMeshResource(pMeshResource);
}

void ECWorldConsumer::AddAnimation(engine::Entity entity, const cd::Animation& animation, const cd::SceneDatabase* pSceneDatabase)
Expand Down Expand Up @@ -435,4 +471,18 @@ void ECWorldConsumer::AddParticleEmitter(engine::Entity entity, const cd::Mesh&
particleEmitterComponent.Build();
}

void ECWorldConsumer::AddSkeleton(engine::Entity entity, const cd::SceneDatabase* pSceneDatabase)
{
engine::World* pWorld = m_pSceneWorld->GetWorld();
auto& SkeletonComponent = pWorld->CreateComponent<engine::SkeletonComponent>(entity);

auto& skeleton = pSceneDatabase->GetSkeleton(0);
std::string meshName(skeleton.GetName());
engine::StringCrc meshNameCrc(meshName);
engine::SkeletonResource* pSkeletonResource = m_pResourceContext->AddSkeletonResource(meshNameCrc);
pSkeletonResource->SetSceneDataBase(pSceneDatabase);
SkeletonComponent.SetSkeletonAsset(pSkeletonResource);

}

}
3 changes: 2 additions & 1 deletion Engine/Source/Editor/ECWorld/ECWorldConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class ECWorldConsumer final : public cdtools::IConsumer
void AddLight(engine::Entity entity, const cd::Light& light);
void AddTransform(engine::Entity entity, const cd::Transform& transform);
void AddStaticMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat);
void AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat);
void AddSkinMesh(engine::Entity entity, const cd::Mesh& mesh, const cd::VertexFormat& vertexFormat, const cd::SceneDatabase* pSceneDatabase);
void AddSkeleton(engine::Entity entity, const cd::SceneDatabase* pSceneDatabase);
void AddAnimation(engine::Entity entity, const cd::Animation& animation, const cd::SceneDatabase* pSceneDatabase);
void AddMaterial(engine::Entity entity, const cd::Material* pMaterial, engine::MaterialType* pMaterialType, const cd::SceneDatabase* pSceneDatabase);
void AddBlendShape(engine::Entity entity, const cd::Mesh* pMesh, const cd::BlendShape& blendShape, const cd::SceneDatabase* pSceneDatabase);
Expand Down
20 changes: 11 additions & 9 deletions Engine/Source/Editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ void EditorApp::Init(engine::EngineInitArgs initArgs)

InitECWorld();
m_pEditorImGuiContext->SetSceneWorld(m_pSceneWorld.get());
// In order to avoid temporary handling of bugs
m_pSceneWorld->GetSceneDatabase()->GetMeshes().reserve(100);

InitEngineRenderers();

Expand Down Expand Up @@ -382,7 +384,7 @@ void EditorApp::OnShaderHotModifiedCallback(const char* rootDir, const char* fil
// Do nothing when a non-shader file is detected.
return;
}
m_pRenderContext->OnShaderHotModified(engine::StringCrc{ engine::Path::GetFileNameWithoutExtension(filePath) });
m_pRenderContext->OnShaderHotModified(engine::Path::GetFileNameWithoutExtension(filePath));
}

void EditorApp::UpdateMaterials()
Expand Down Expand Up @@ -492,6 +494,14 @@ void EditorApp::InitEngineRenderers()
AddEngineRenderer(cd::MoveTemp(pPBRSkyRenderer));
}

auto pSkeletonRenderer = std::make_unique<engine::SkeletonRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pSkeletonRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pSkeletonRenderer));

auto pAnimationRenderer = std::make_unique<engine::AnimationRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pAnimationRenderer));

auto pSceneRenderer = std::make_unique<engine::WorldRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pSceneRenderer = pSceneRenderer.get();
pSceneRenderer->SetSceneWorld(m_pSceneWorld.get());
Expand All @@ -516,14 +526,6 @@ void EditorApp::InitEngineRenderers()
pTerrainRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pTerrainRenderer));

auto pSkeletonRenderer = std::make_unique<engine::SkeletonRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pSkeletonRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pSkeletonRenderer));

auto pAnimationRenderer = std::make_unique<engine::AnimationRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
pAnimationRenderer->SetSceneWorld(m_pSceneWorld.get());
AddEngineRenderer(cd::MoveTemp(pAnimationRenderer));

auto pWhiteModelRenderer = std::make_unique<engine::WhiteModelRenderer>(m_pRenderContext->CreateView(), pSceneRenderTarget);
m_pWhiteModelRenderer = pWhiteModelRenderer.get();
pWhiteModelRenderer->SetSceneWorld(m_pSceneWorld.get());
Expand Down
1 change: 1 addition & 0 deletions Engine/Source/Editor/Resources/ResourceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ TaskHandle ResourceBuilder::AddRadianceCubeMapBuildTask(const char* pInputFilePa
}

std::string pathWithoutExtension = std::filesystem::path(pOutputFilePath).replace_extension().generic_string();
// TODO : mipCount should be affected by dstFaceSize, need to parameterize them in the future.
std::vector<std::string> radianceCommandArguments{"--input", pInputFilePath,
"--filter", "radiance", "--lightingModel", "phongbrdf", "--excludeBase", "true", "--mipCount", "7",
"--dstFaceSize", "256",
Expand Down
3 changes: 1 addition & 2 deletions Engine/Source/Editor/UILayers/AssetBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ void AssetBrowser::ImportAssetFile(const char* pFilePath)
std::filesystem::path absolutePath = CDPROJECT_RESOURCES_ROOT_PATH;
absolutePath /= relativePath;

CD_INFO("Compile skybox textures to {0}.", absolutePath);
CD_INFO("Compile skybox textures to {0}.", absolutePath.generic_string());

std::string irrdianceOutput = absolutePath.generic_string() + "_irr.dds";
ResourceBuilder::Get().AddIrradianceCubeMapBuildTask(pFilePath, irrdianceOutput.c_str());
Expand Down Expand Up @@ -997,7 +997,6 @@ void AssetBrowser::ImportModelFile(const char* pFilePath)
// Step 2 : Process generated cd::SceneDatabase
ProcessSceneDatabase(pSceneDatabase, m_importOptions.ImportMesh, m_importOptions.ImportMaterial, m_importOptions.ImportTexture,
m_importOptions.ImportCamera, m_importOptions.ImportLight);

// Step 3 : Convert cd::SceneDatabase to entities and components
{
ECWorldConsumer ecConsumer(pSceneWorld, pCurrentRenderContext);
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Editor/UILayers/AssetBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct AssetImportOptions
bool ImportMaterial = true;
bool ImportMesh = true;
bool ImportTexture = true;
bool ImportAnimation = false;
bool ImportAnimation = true;
};

struct AssetExportOptions
Expand Down
Loading

0 comments on commit c9db3b3

Please sign in to comment.