Skip to content
Closed
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
4 changes: 3 additions & 1 deletion package/Shaders/Common/SharedData.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ namespace SharedData
struct TerrainBlendingSettings
{
uint Enabled;
uint3 _padding;
float BlendStrength;
float TerrainDepthCullingDistance;
float pad0;
};

cbuffer FeatureData : register(b6)
Expand Down
3 changes: 2 additions & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,8 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
float depthSampledLinear = SharedData::GetScreenDepth(depthSampled);
float depthPixelLinear = SharedData::GetScreenDepth(input.Position.z);

blendFactorTerrain = saturate((depthSampledLinear - depthPixelLinear) / 10.0);
float blendStrength = max(SharedData::terrainBlendingSettings.BlendStrength * 10.0f, 0.001f);
blendFactorTerrain = saturate((depthSampledLinear - depthPixelLinear) / blendStrength);

if (input.Position.z == depthSampled)
blendFactorTerrain = 1;
Expand Down
23 changes: 20 additions & 3 deletions src/Features/TerrainBlending.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
TerrainBlending::Settings,
Enabled)
Enabled,
BlendStrength,
TerrainDepthCullingDistance)

void TerrainBlending::DrawSettings()
{
ImGui::Checkbox("Enable Terrain Blending", (bool*)&settings.Enabled);
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text("Enable seamless blending between terrain and objects.");
}

ImGui::SliderFloat("Blend Strength", &settings.BlendStrength, 0.125f, 1.25f, "%.3f");
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text(
"Controls degree of blending.\n"
"Lower values create a tighter transition.");
}

ImGui::SeparatorText("Performance Option");
ImGui::SliderFloat("Terrain Depth Culling Distance", &settings.TerrainDepthCullingDistance, 256.0f, 8192.0f, "%.0f units");
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text(
"Skips terrain depth blending beyond this distance.\n"
"Decrease to just before blending stops to save performance.");
}
Comment on lines +20 to +33
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.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

# First, check if the file exists and get basic info
find . -name "TerrainBlending.cpp" -o -name "TerrainBlending.h" -o -name "TerrainBlending.hpp" | head -20

Repository: doodlum/skyrim-community-shaders

Length of output: 143


🏁 Script executed:

# Also search for any terrain blending related files
fd -e cpp -e h -e hpp | grep -i terrain

Repository: doodlum/skyrim-community-shaders

Length of output: 327


🏁 Script executed:

# Check the actual code at lines 20-33 in TerrainBlending.cpp
cat -n src/Features/TerrainBlending.cpp | head -50

Repository: doodlum/skyrim-community-shaders

Length of output: 1910


🏁 Script executed:

# Also search for LoadSettings function
rg "LoadSettings" src/Features/TerrainBlending.cpp -A 15 -B 2

Repository: doodlum/skyrim-community-shaders

Length of output: 501


🏁 Script executed:

# Check the header file for settings struct
cat -n src/Features/TerrainBlending.h

Repository: doodlum/skyrim-community-shaders

Length of output: 4486


🏁 Script executed:

# Search for shader usage of BlendStrength and TerrainDepthCullingDistance
rg "BlendStrength|TerrainDepthCullingDistance" src/Features/ -t cpp -t hpp -B 2 -A 2

Repository: doodlum/skyrim-community-shaders

Length of output: 103


🏁 Script executed:

# Check shader files for how these settings are used (divisor concerns)
rg "BlendStrength|TerrainDepthCullingDistance" src/ -t hlsl -B 2 -A 2

Repository: doodlum/skyrim-community-shaders

Length of output: 104


🏁 Script executed:

# Look for other features that validate JSON settings to see common patterns
rg "LoadSettings" src/Features/ -A 8 | head -100

Repository: doodlum/skyrim-community-shaders

Length of output: 5668


🏁 Script executed:

# Search for shader files that might use TerrainBlending settings
find . -name "*.hlsl" -o -name "*.cs" | xargs grep -l "BlendStrength\|TerrainDepthCulling" 2>/dev/null

Repository: doodlum/skyrim-community-shaders

Length of output: 109


🏁 Script executed:

# Look for where settings are passed to shaders
rg "BlendStrength|TerrainDepthCulling" src/Features/ -B 3 -A 3

Repository: doodlum/skyrim-community-shaders

Length of output: 2588


🏁 Script executed:

# Check VR.cpp to see the ClampToValidRanges pattern
cat -n src/Features/VR.cpp | head -100

Repository: doodlum/skyrim-community-shaders

Length of output: 3244


🏁 Script executed:

# Search for Constant Buffer or cbuffer usage in TerrainBlending
rg "cbuffer\|ConstantBuffer\|settings\." src/Features/TerrainBlending.cpp -B 2 -A 2

Repository: doodlum/skyrim-community-shaders

Length of output: 58


🏁 Script executed:

# Find VR Settings struct to see ClampToValidRanges
rg "struct Settings" src/Features/VR.h -A 50 | head -100

Repository: doodlum/skyrim-community-shaders

Length of output: 2969


🏁 Script executed:

# Search for ClampToValidRanges implementation
rg "ClampToValidRanges" src/Features/ -B 2 -A 15

Repository: doodlum/skyrim-community-shaders

Length of output: 1818


🏁 Script executed:

# Check if std::isfinite is used anywhere in codebase
rg "isfinite|std::clamp" src/Features/ -B 2 -A 2

Repository: doodlum/skyrim-community-shaders

Length of output: 7955


🏁 Script executed:

# Look at how settings are used in compute shaders (constant buffer binding)
rg "constant buffer\|cbuffer.*Settings" src/ -i -A 5

Repository: doodlum/skyrim-community-shaders

Length of output: 58


Validate and clamp JSON-loaded settings to safe ranges

Settings loaded from JSON bypass the ImGui slider bounds. Invalid or NaN values could cause logic errors in distance comparisons and culling. Add validation in LoadSettings().

Follow the established pattern used in VR.h's ClampToValidRanges(): directly clamp to the same ranges as the UI sliders without explicit isfinite checks.

void TerrainBlending::LoadSettings(json& o_json)
{
	settings = o_json;
+	settings.BlendStrength = std::clamp(settings.BlendStrength, 0.125f, 1.25f);
+	settings.TerrainDepthCullingDistance = std::clamp(settings.TerrainDepthCullingDistance, 256.0f, 8192.0f);
}
🤖 Prompt for AI Agents
In `@src/Features/TerrainBlending.cpp` around lines 20 - 33, The JSON-loaded
settings must be clamped in LoadSettings(): after deserializing, call the same
clamping logic as VR.h's ClampToValidRanges() to enforce BlendStrength ∈
[0.125f, 1.25f] and TerrainDepthCullingDistance ∈ [256.0f, 8192.0f]; update
LoadSettings() to directly clamp settings.BlendStrength and
settings.TerrainDepthCullingDistance to those exact bounds (no isfinite checks
required) so values from JSON cannot bypass the ImGui slider ranges used in the
UI.

}

void TerrainBlending::LoadSettings(json& o_json)
Expand Down Expand Up @@ -271,7 +288,7 @@ void TerrainBlending::Hooks::BSBatchRenderer__RenderPassImmediately::thunk(RE::B
bool inTerrain = a_pass->shaderProperty && a_pass->shaderProperty->flags.all(RE::BSShaderProperty::EShaderPropertyFlag::kMultiTextureLandscape);

if (inTerrain) {
if ((a_pass->geometry->worldBound.center.GetDistance(singleton.averageEyePosition) - a_pass->geometry->worldBound.radius) > 2048.0f) {
if ((a_pass->geometry->worldBound.center.GetDistance(singleton.averageEyePosition) - a_pass->geometry->worldBound.radius) > singleton.settings.TerrainDepthCullingDistance) {
inTerrain = false;
}
}
Expand Down Expand Up @@ -349,4 +366,4 @@ void TerrainBlending::RenderTerrainBlendingPasses()

auto& mainDepth = renderer->GetDepthStencilData().depthStencils[RE::RENDER_TARGETS_DEPTHSTENCIL::kMAIN];
mainDepth.depthSRV = depthSRVBackup;
}
}
4 changes: 3 additions & 1 deletion src/Features/TerrainBlending.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ struct TerrainBlending : Feature
struct Settings
{
uint32_t Enabled = true;
uint32_t pad[3];
float BlendStrength = 1.0f;
float TerrainDepthCullingDistance = 2048.0f;
float pad0 = 0.0f;
};
STATIC_ASSERT_ALIGNAS_16(Settings);

Expand Down