Skip to content
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
20 changes: 19 additions & 1 deletion features/IBL/Shaders/IBL/IBL.hlsli
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Common/Color.hlsli"
#include "Common/Math.hlsli"
#include "Common/Random.hlsli"
#include "Common/SharedData.hlsli"
Expand All @@ -20,6 +21,23 @@ namespace ImageBasedLighting
float colorR = SphericalHarmonics::SHHallucinateZH3Irradiance(shR, rayDir);
float colorG = SphericalHarmonics::SHHallucinateZH3Irradiance(shG, rayDir);
float colorB = SphericalHarmonics::SHHallucinateZH3Irradiance(shB, rayDir);
return float3(colorR, colorG, colorB);
return float3(colorR, colorG, colorB) / Math::PI;
}

float3 GetFogIBLColor(float3 fogColor)
{
float3 directionalAmbientColor = max(0, mul(SharedData::DirectionalAmbient, float4(float3(0, 0, 0), 1.0))).xyz;
float3 iblColor = directionalAmbientColor * SharedData::iblSettings.DALCAmount + Color::Saturation(GetDiffuseIBL(float3(0, 0, 0)), SharedData::iblSettings.IBLSaturation) * SharedData::iblSettings.DiffuseIBLScale;
if (SharedData::iblSettings.PreserveFogLuminance) {
const float fogLuminance = Color::RGBToLuminance2(fogColor);
const float iblLuminance = Color::RGBToLuminance2(iblColor);
if (iblLuminance > 0) {
const float scale = fogLuminance / iblLuminance;
iblColor *= scale;
} else {
iblColor = fogColor;
}
}
return lerp(fogColor, iblColor, SharedData::iblSettings.FogAmount);
}
}
6 changes: 4 additions & 2 deletions package/Shaders/Common/SharedData.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,13 @@ namespace SharedData
struct IBLSettings
{
uint EnableDiffuseIBL;
uint SampleUnderHorizonFromDynCube;
uint PreserveFogLuminance;
uint pad;
float DiffuseIBLScale;
float DALCAmount;
float IBLSaturation;
uint SampleUnderHorizonFromDynCube;
uint3 pad;
float FogAmount;
};

struct ExtendedTranslucencySettings
Expand Down
8 changes: 7 additions & 1 deletion package/Shaders/Effect.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,13 @@ PS_OUTPUT main(PS_INPUT input)
# elif defined(MULTBLEND) || defined(MULTBLEND_DECAL)
float3 blendedColor = lerp(lightColor, 1.0.xxx, saturate(1.5 * input.FogParam.w).xxx);
# else
float3 blendedColor = lerp(lightColor, input.FogParam.xyz, input.FogParam.www);
float3 fogColor = input.FogParam.xyz;
# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
fogColor = ImageBasedLighting::GetFogIBLColor(fogColor);
}
# endif
float3 blendedColor = lerp(lightColor, fogColor, input.FogParam.www);
# endif
# else
float3 blendedColor = lightColor.xyz;
Expand Down
9 changes: 9 additions & 0 deletions package/Shaders/ISSAOComposite.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ float SimplexNoise(float3 v)
dot(p2, x2), dot(p3, x3)));
}

# if defined(IBL)
# include "IBL/IBL.hlsli"
# endif

PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT psout;
Expand Down Expand Up @@ -170,6 +174,11 @@ PS_OUTPUT main(PS_INPUT input)
float fogDistanceFactor = (2 * CameraNearFar.x * CameraNearFar.y) / ((CameraNearFar.y + CameraNearFar.x) - (2 * (1.01 * depth - 0.01) - 1) * (CameraNearFar.y - CameraNearFar.x));
float fogFactor = min(FogParam.w, pow(saturate(fogDistanceFactor * FogParam.y - FogParam.x), FogParam.z));
float3 fogColor = lerp(FogNearColor.xyz, FogFarColor.xyz, fogFactor);
# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
fogColor = ImageBasedLighting::GetFogIBLColor(fogColor);
}
# endif
if (depth < 0.999999) {
composedColor.xyz = FogNearColor.w * lerp(composedColor.xyz, fogColor, fogFactor);
}
Expand Down
8 changes: 7 additions & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3112,8 +3112,14 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)

# if !defined(DEFERRED)
color.xyz = Color::LinearToGamma(Color::GammaToLinear(color.xyz) + specularColor);
float3 fogColor = input.FogParam.xyz;
# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
fogColor = ImageBasedLighting::GetFogIBLColor(fogColor);
}
# endif
if (FrameBuffer::FrameParams.y && FrameBuffer::FrameParams.z)
color.xyz = lerp(color.xyz, input.FogParam.xyz, input.FogParam.w);
color.xyz = lerp(color.xyz, fogColor, input.FogParam.w);
# endif

# if defined(TESTCUBEMAP) && defined(ENVMAP) && defined(DYNAMIC_CUBEMAPS)
Expand Down
25 changes: 23 additions & 2 deletions package/Shaders/Water.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ float3 GetSunColor(float3 normal, float3 viewDirection)
# include "InverseSquareLighting/InverseSquareLighting.hlsli"
# endif

# if defined(IBL)
# include "IBL/IBL.hlsli"
# endif

PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT psout;
Expand Down Expand Up @@ -1140,7 +1144,13 @@ PS_OUTPUT main(PS_INPUT input)
# if defined(VC)
float specularFraction = lerp(1, fresnel * diffuseOutput.refractionMul, distanceFactor);
float3 finalColorPreFog = lerp(diffuseColor, specularColor, specularFraction) + sunColor * depthControl.w;
float3 finalColor = lerp(finalColorPreFog, input.FogParam.xyz * PosAdjust[eyeIndex].w, input.FogParam.w);
float3 fogColor = input.FogParam.xyz;
# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
fogColor = ImageBasedLighting::GetFogIBLColor(fogColor);
}
# endif
float3 finalColor = lerp(finalColorPreFog, fogColor * PosAdjust[eyeIndex].w, input.FogParam.w);
# if defined(WETNESS_EFFECTS) && defined(DEBUG_WETNESS_EFFECTS)
// DEBUG MODE: Override water color with debug visualization
float3 debugColor = WetnessEffects::GetDebugWetnessColorStandard(waterData.rippleInfo, 2.0, 3.0);
Expand All @@ -1152,12 +1162,23 @@ PS_OUTPUT main(PS_INPUT input)
# else
float specularFraction = lerp(1, fresnel, distanceFactor);
float3 finalColorPreFog = lerp(diffuseOutput.refractionDiffuseColor, specularColor, specularFraction) + sunColor * depthControl.w;
finalColorPreFog = lerp(finalColorPreFog, input.FogParam.xyz * PosAdjust[eyeIndex].w, input.FogParam.w);
float3 preFogColor = input.FogParam.xyz;
# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
preFogColor = ImageBasedLighting::GetFogIBLColor(preFogColor);
}
# endif
finalColorPreFog = lerp(finalColorPreFog, preFogColor * PosAdjust[eyeIndex].w, input.FogParam.w);

float3 refractionColor = diffuseOutput.refractionColor;

float fogFactor = min(FogParam.w, pow(saturate(-diffuseOutput.depth * FogParam.y - FogParam.x), FogParam.z));
float3 fogColor = lerp(FogNearColor.xyz, FogFarColor.xyz, fogFactor);
# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
fogColor = ImageBasedLighting::GetFogIBLColor(fogColor);
}
# endif
refractionColor = lerp(refractionColor, fogColor, fogFactor);

float3 finalColor = lerp(refractionColor, finalColorPreFog, diffuseOutput.refractionMul);
Expand Down
6 changes: 5 additions & 1 deletion src/Features/IBL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
IBL::Settings,
EnableDiffuseIBL,
SampleUnderHorizonFromDynCube,
PreserveFogLuminance,
DiffuseIBLScale,
DALCAmount,
IBLSaturation,
SampleUnderHorizonFromDynCube)
FogAmount)

void IBL::DrawSettings()
{
ImGui::Checkbox("Enable Diffuse IBL", (bool*)&settings.EnableDiffuseIBL);
ImGui::SliderFloat("Diffuse IBL Scale", &settings.DiffuseIBLScale, 0.0f, 10.0f, "%.2f");
ImGui::SliderFloat("Diffuse IBL Saturation", &settings.IBLSaturation, 0.0f, 2.0f, "%.2f");
ImGui::SliderFloat("DALC Amount", &settings.DALCAmount, 0.0f, 1.0f, "%.2f");
ImGui::SliderFloat("Fog Mix", &settings.FogAmount, 0.0f, 1.0f, "%.2f");
ImGui::Checkbox("Preserve Fog Luminance", (bool*)&settings.PreserveFogLuminance);
ImGui::Checkbox("[EXP] Sample Under Horizon From Dynamic Cubemaps", (bool*)&settings.SampleUnderHorizonFromDynCube);
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text(
Expand Down
10 changes: 6 additions & 4 deletions src/Features/IBL.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ struct IBL : Feature
struct alignas(16) Settings
{
uint EnableDiffuseIBL = 1;
float DiffuseIBLScale = 0.5f;
float DALCAmount = 0.5f;
float IBLSaturation = 0.65f;
uint SampleUnderHorizonFromDynCube = 0;
uint pad[3];
uint PreserveFogLuminance = 0;
uint pad;
float DiffuseIBLScale = 1.0f;
float DALCAmount = 0.33f;
float IBLSaturation = 1.0f;
float FogAmount = 0.0f;
} settings;
Comment on lines +48 to 54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Initialize pad and guard layout with a static_assert

Pad currently has no in-class initializer; while RestoreDefaultSettings zeroes it, it may be indeterminate before that call. Initialize it to 0 and add a static_assert to lock in the intended size/alignment.

 		uint PreserveFogLuminance = 0;
-		uint pad;
+		uint pad = 0;
 		float DiffuseIBLScale = 1.0f;
 		float DALCAmount = 0.33f;
 		float IBLSaturation = 1.0f;
 		float FogAmount = 0.0f;
 	} settings;

Outside this hunk, consider adding:

static_assert(sizeof(Settings) == 32, "IBL::Settings size must match HLSL IBLSettings packing");
static_assert(alignof(Settings) == 16, "IBL::Settings must remain 16-byte aligned");
🤖 Prompt for AI Agents
In src/Features/IBL.h around lines 48 to 54, the 'pad' member is not in-class
initialized and can be indeterminate prior to RestoreDefaultSettings; initialize
'pad' to 0 in the declaration and add static_asserts to enforce layout: check
sizeof(Settings) equals the expected packed size and alignof(Settings) equals 16
to match HLSL packing. Ensure the static_assert messages clearly reference
"IBL::Settings size must match HLSL IBLSettings packing" and "IBL::Settings must
remain 16-byte aligned".


ID3D11ComputeShader* GetDiffuseIBLCS();
Expand Down
Loading