Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little fix for Terrain shader compile #478

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Engine/BuiltInShaders/common/LightSource.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//-----------------------------------------------------------------------------------------//
// @brief Calculates the contribution of all light sources. //
// //
// vec3 CalculateLights(Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF); //
//-----------------------------------------------------------------------------------------//
//--------------------------------------------------------------------------------------------------------//
// @brief Calculates the contribution of all light sources. //
// //
// vec3 CalculateLights(Material material, vec3 worldPos, vec3 viewDir, vec3 diffuseBRDF, float csmDepth) //
//--------------------------------------------------------------------------------------------------------//

#include "../UniformDefines/U_Light.sh"
#include "../UniformDefines/U_Shadow.sh"
Expand Down
4 changes: 2 additions & 2 deletions Engine/BuiltInShaders/shaders/fs_PBR.sc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void main()
vec3 cameraPos = GetCamera().position.xyz;
vec3 viewDir = normalize(cameraPos - v_worldPos);

// Directional Light
float csmDepth = (v_color0.z - u_cameraNearFarPlane.x)/(u_cameraNearFarPlane.y - u_cameraNearFarPlane.x);
// Directional Light
float csmDepth = (v_color0.z - u_cameraNearFarPlane.x) / (u_cameraNearFarPlane.y - u_cameraNearFarPlane.x);
vec3 dirColor = GetDirectional(material, v_worldPos, viewDir, csmDepth);

// Environment Light
Expand Down
10 changes: 6 additions & 4 deletions Engine/BuiltInShaders/shaders/fs_terrain.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define IBL
$input v_worldPos, v_normal, v_texcoord0, v_TBN
$input v_worldPos, v_normal, v_texcoord0, v_TBN, v_color0

#include "../common/common.sh"
#include "../common/BRDF.sh"
Expand All @@ -12,14 +12,15 @@ $input v_worldPos, v_normal, v_texcoord0, v_TBN
#include "../UniformDefines/U_Terrain.sh"

uniform vec4 u_emissiveColor;
uniform vec4 u_cameraNearFarPlane;

SAMPLER2D(s_texSnow, TERRAIN_TOP_ALBEDO_MAP_SLOT);
SAMPLER2D(s_texRock, TERRAIN_MEDIUM_ALBEDO_MAP_SLOT);
SAMPLER2D(s_texGrass, TERRAIN_BOTTOM_ALBEDO_MAP_SLOT);

vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir) {
vec3 GetDirectional(Material material, vec3 worldPos, vec3 viewDir, float csmDepth) {
vec3 diffuseBRDF = material.albedo * CD_PI_INV;
return CalculateLights(material, worldPos, viewDir, diffuseBRDF);
return CalculateLights(material, worldPos, viewDir, diffuseBRDF, csmDepth);
}

vec3 GetEnvironment(Material material, vec3 normal, vec3 viewDir) {
Expand Down Expand Up @@ -74,7 +75,8 @@ void main()
vec3 viewDir = normalize(cameraPos - v_worldPos);

// Directional Light
vec3 dirColor = GetDirectional(material, v_worldPos, viewDir);
float csmDepth = (v_color0.z - u_cameraNearFarPlane.x) / (u_cameraNearFarPlane.y - u_cameraNearFarPlane.x);
vec3 dirColor = GetDirectional(material, v_worldPos, viewDir, csmDepth);

// Environment Light
vec3 envColor = GetEnvironment(material, v_normal, viewDir);
Expand Down
3 changes: 2 additions & 1 deletion Engine/BuiltInShaders/shaders/vs_terrain.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$input a_position, a_normal, a_tangent, a_texcoord0
$output v_worldPos, v_normal, v_texcoord0, v_TBN
$output v_worldPos, v_normal, v_texcoord0, v_TBN, v_color0

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

Expand Down Expand Up @@ -32,6 +32,7 @@ void main()

gl_Position = mul(u_modelViewProj, vec4(a_position.x, elevation, a_position.z, 1.0));
v_worldPos = mul(u_model[0], vec4(a_position.x, elevation, a_position.z, 1.0)).xyz;
v_color0 = mul(u_modelView, vec4(a_position, 1.0));

v_normal = -normalize(N1 + N2 + N3 +N4);
vec3 tangent = normalize(mul(u_modelInvTrans, vec4(a_tangent, 0.0)).xyz);
Expand Down
15 changes: 10 additions & 5 deletions Engine/Source/Runtime/Rendering/TerrainRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ constexpr const char* cubeRadianceSampler = "s_texCubeRad";
constexpr const char* lutTexture = "Textures/lut/ibl_brdf_lut.dds";

constexpr const char* cameraPos = "u_cameraPos";
constexpr const char* cameraNearFarPlane = "u_cameraNearFarPlane";

constexpr const char* albedoColor = "u_albedoColor";
constexpr const char* metallicRoughnessFactor = "u_metallicRoughnessFactor";
Expand Down Expand Up @@ -76,6 +77,8 @@ void TerrainRenderer::Warmup()
GetRenderContext()->CreateTexture(pSkyComponent->GetRadianceTexturePath().c_str(), samplerFlags);

GetRenderContext()->CreateUniform(cameraPos, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateUniform(cameraNearFarPlane, bgfx::UniformType::Vec4, 1);

GetRenderContext()->CreateUniform(albedoColor, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateUniform(emissiveColor, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateUniform(metallicRoughnessFactor, bgfx::UniformType::Vec4, 1);
Expand All @@ -97,7 +100,10 @@ void TerrainRenderer::UpdateView(const float* pViewMatrix, const float* pProject
void TerrainRenderer::Render(float deltaTime)
{
// TODO : Remove it. If every renderer need to submit camera related uniform, it should be done not inside Renderer class.
const CameraComponent* pMainCameraComponent = m_pCurrentSceneWorld->GetCameraComponent(m_pCurrentSceneWorld->GetMainCameraEntity());
const cd::Transform& cameraTransform = m_pCurrentSceneWorld->GetTransformComponent(m_pCurrentSceneWorld->GetMainCameraEntity())->GetTransform();
SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());

for (Entity entity : m_pCurrentSceneWorld->GetTerrainEntities())
{
MaterialComponent* pMaterialComponent = m_pCurrentSceneWorld->GetMaterialComponent(entity);
Expand Down Expand Up @@ -145,14 +151,9 @@ void TerrainRenderer::Render(float deltaTime)
GetRenderContext()->GetTexture(StringCrc(elevationTexture)));

// Sky
SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());
SkyType crtSkyType = pSkyComponent->GetSkyType();

if (crtSkyType == SkyType::SkyBox)
{
// Create a new TextureHandle each frame if the skybox texture path has been updated,
// otherwise RenderContext::CreateTexture will automatically skip it.

constexpr StringCrc irrSamplerCrc(cubeIrradianceSampler);
GetRenderContext()->CreateTexture(pSkyComponent->GetIrradianceTexturePath().c_str(), samplerFlags);
bgfx::setTexture(IBL_IRRADIANCE_SLOT,
Expand All @@ -174,6 +175,10 @@ void TerrainRenderer::Render(float deltaTime)
constexpr StringCrc cameraPosCrc(cameraPos);
GetRenderContext()->FillUniform(cameraPosCrc, &cameraTransform.GetTranslation().x(), 1);

constexpr StringCrc cameraNearFarPlaneCrc(cameraNearFarPlane);
float cameraNearFarPlanedata[2] { pMainCameraComponent->GetNearPlane(), pMainCameraComponent->GetFarPlane() };
GetRenderContext()->FillUniform(cameraNearFarPlaneCrc, cameraNearFarPlanedata, 1);

// Submit uniform values : material settings
constexpr StringCrc albedoColorCrc(albedoColor);
GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor<cd::Vec3f>(cd::MaterialPropertyGroup::BaseColor), 1);
Expand Down
15 changes: 7 additions & 8 deletions Engine/Source/Runtime/Rendering/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void WorldRenderer::Warmup()
GetRenderContext()->CreateUniform(LightDir, bgfx::UniformType::Vec4, 1);
GetRenderContext()->CreateUniform(HeightOffsetAndshadowLength, bgfx::UniformType::Vec4, 1);

GetRenderContext()->CreateUniform(lightViewProjs, bgfx::UniformType::Mat4, 12); //
GetRenderContext()->CreateUniform(lightViewProjs, bgfx::UniformType::Mat4, 12);
GetRenderContext()->CreateUniform(cubeShadowMapSamplers[0], bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform(cubeShadowMapSamplers[1], bgfx::UniformType::Sampler);
GetRenderContext()->CreateUniform(cubeShadowMapSamplers[2], bgfx::UniformType::Sampler);
Expand All @@ -107,6 +107,7 @@ void WorldRenderer::UpdateView(const float* pViewMatrix, const float* pProjectio
void WorldRenderer::Render(float deltaTime)
{
// TODO : Remove it. If every renderer need to submit camera related uniform, it should be done not inside Renderer class.
const CameraComponent* pMainCameraComponent = m_pCurrentSceneWorld->GetCameraComponent(m_pCurrentSceneWorld->GetMainCameraEntity());
const cd::Transform& cameraTransform = m_pCurrentSceneWorld->GetTransformComponent(m_pCurrentSceneWorld->GetMainCameraEntity())->GetTransform();
SkyComponent* pSkyComponent = m_pCurrentSceneWorld->GetSkyComponent(m_pCurrentSceneWorld->GetSkyEntity());

Expand Down Expand Up @@ -231,7 +232,7 @@ void WorldRenderer::Render(float deltaTime)
if (SkyType::SkyBox == crtSkyType)
{
// Create a new TextureHandle each frame if the skybox texture path has been updated,
// otherwise RenderContext::CreateTexture will automatically skip it.
// otherwise RenderContext::CreateTexture will skip it automatically.

constexpr StringCrc irrSamplerCrc(cubeIrradianceSampler);
GetRenderContext()->CreateTexture(pSkyComponent->GetIrradianceTexturePath().c_str(), samplerFlags);
Expand Down Expand Up @@ -267,6 +268,10 @@ void WorldRenderer::Render(float deltaTime)
constexpr StringCrc cameraPosCrc(cameraPos);
GetRenderContext()->FillUniform(cameraPosCrc, &cameraTransform.GetTranslation().x(), 1);

constexpr StringCrc cameraNearFarPlaneCrc(cameraNearFarPlane);
float cameraNearFarPlanedata[2]{ pMainCameraComponent->GetNearPlane(), pMainCameraComponent->GetFarPlane() };
GetRenderContext()->FillUniform(cameraNearFarPlaneCrc, cameraNearFarPlanedata, 1);

// Submit uniform values : material settings
constexpr StringCrc albedoColorCrc(albedoColor);
GetRenderContext()->FillUniform(albedoColorCrc, pMaterialComponent->GetFactor<cd::Vec3f>(cd::MaterialPropertyGroup::BaseColor), 1);
Expand Down Expand Up @@ -320,12 +325,6 @@ void WorldRenderer::Render(float deltaTime)
constexpr engine::StringCrc lightViewProjsCrc(lightViewProjs);
GetRenderContext()->FillUniform(lightViewProjsCrc, lightViewProjsData.data(), totalLightViewProjOffset);

// Submit shared uniform (related to camera)
constexpr StringCrc cameraNearFarPlaneCrc(cameraNearFarPlane);
CameraComponent* pMainCameraComponent = m_pCurrentSceneWorld->GetCameraComponent(m_pCurrentSceneWorld->GetMainCameraEntity());
float cameraNearFarPlanedata[2] = { pMainCameraComponent->GetNearPlane(), pMainCameraComponent->GetFarPlane() };
GetRenderContext()->FillUniform(cameraNearFarPlaneCrc, &cameraNearFarPlanedata[0], 1);

// Submit shadow map and settings of each light
constexpr StringCrc shadowMapSamplerCrcs[3] = { StringCrc(cubeShadowMapSamplers[0]), StringCrc(cubeShadowMapSamplers[1]), StringCrc(cubeShadowMapSamplers[2]) };
for (int lightIndex = 0; lightIndex < lightEntityCount; lightIndex++)
Expand Down
Loading