Skip to content

feat(terrainblending): restore enable setting#1755

Merged
doodlum merged 3 commits into
community-shaders:devfrom
alandtse:terrain-blending-enable
Jan 24, 2026
Merged

feat(terrainblending): restore enable setting#1755
doodlum merged 3 commits into
community-shaders:devfrom
alandtse:terrain-blending-enable

Conversation

@alandtse
Copy link
Copy Markdown
Collaborator

@alandtse alandtse commented Jan 24, 2026

Summary by CodeRabbit

  • New Features

    • Added a terrain blending setting with a UI checkbox and save/load support.
  • Bug Fixes / Reliability

    • Gate all terrain-blending rendering and texture usage on the new setting to ensure correct behavior when disabled.
  • Performance

    • Skip terrain-blending computations and related texture sampling when the feature is turned off, reducing unnecessary work.

✏️ Tip: You can customize this high-level summary in your review settings.

Copilot AI review requested due to automatic review settings January 24, 2026 11:08
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 24, 2026

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Walkthrough

Adds a runtime toggle for terrain blending and wires it through shader shared data, shader logic, feature serialization, deferred rendering, and dependent feature bindings so terrain blending work and resources are skipped when disabled.

Changes

Cohort / File(s) Summary
Shader Data Structures
package/Shaders/Common/SharedData.hlsli
Added TerrainBlendingSettings (uint Enabled; uint3 _padding;) and terrainBlendingSettings member to cbuffer FeatureData.
Shader Implementations
package/Shaders/Lighting.hlsl
Wrapped depth sampling, linearization, and blend-factor computation/assignment in [flatten] if (SharedData::terrainBlendingSettings.Enabled) { ... } so terrain blending math runs only when enabled.
Feature API & UI
src/Features/TerrainBlending.h, src/Features/TerrainBlending.cpp
Added Settings (with Enabled), serialization (LoadSettings/SaveSettings), and DrawSettings() UI checkbox; gated shader usage behind singleton.settings.Enabled.
Feature Buffer Integration
src/FeatureBuffer.cpp
Included TerrainBlending.h and appended terrainBlending.settings into the feature buffer data layout.
Deferred & Runtime Binding
src/Deferred.cpp, src/State.cpp, src/Features/SubsurfaceScattering.cpp
Require terrainBlending.loaded && terrainBlending.settings.Enabled when selecting blended depth SRV and when invoking terrain-blending render passes; fall back to depthSRV when disabled.

Sequence Diagram(s)

sequenceDiagram
  participant UI as UI
  participant Settings as TerrainBlending.Settings
  participant FeatureBuf as FeatureBuffer
  participant Runtime as Renderer/State
  participant Shader as Lighting.hlsl
  participant Deferred as DeferredPasses

  UI->>Settings: Toggle Enabled
  Settings->>FeatureBuf: Serialize settings
  FeatureBuf->>Runtime: Provide feature buffer (includes Enabled)
  Runtime->>Shader: Update SharedData::terrainBlendingSettings
  Runtime->>Deferred: Bind SRVs (choose blendedDepth or depthSRV based on Enabled)
  Deferred->>Shader: Execute shading path
  alt Enabled == true
    Shader->>Shader: sample blended depth, compute blendFactor
  else Enabled == false
    Shader->>Shader: skip terrain blend computations
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • doodlum
  • davo0411

Poem

🐰 A tiny toggle, snug and bright,
I hop and flip the blending light,
When Enabled’s on the depths entwine,
When off — the shader skips the line,
A checkbox hop, and all is right. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: restoring an enable/disable setting for the terrain blending feature that controls whether terrain blending operations execute.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

Using provided base ref: 44eb788
Using base ref: 44eb788
Base commit date: 2026-01-19T23:55:15Z (Monday, January 19, 2026 11:55 PM)
No actionable suggestions for changed features.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/Features/TerrainBlending.h`:
- Around line 23-30: The TerrainBlending::Settings struct is 4 bytes in C++ but
HLSL expects a 16-byte layout (uint + padding); update the struct used by
Settings settings to include explicit padding so it becomes 16 bytes (e.g., add
three 4-byte padding members or a uint32_t pad[3]) and apply the same alignment
assertion (static_assert(sizeof(Settings) == 16) and/or alignas(16)) to
guarantee C++/HLSL layout match; update any code touching Settings (e.g.,
DrawSettings) if it reads/writes padding fields.
🧹 Nitpick comments (1)
package/Shaders/Lighting.hlsl (1)

993-1006: Redundant saturate call on line 1005.

The value blendFactorTerrain is already saturated on line 1000, and the only other assignment sets it to 1 (line 1003), which is within the [0,1] range. The final saturate on line 1005 has no effect.

♻️ Suggested simplification
 		blendFactorTerrain = saturate((depthSampledLinear - depthPixelLinear) / 10.0);

 		if (input.Position.z == depthSampled)
 			blendFactorTerrain = 1;
-
-		blendFactorTerrain = saturate(blendFactorTerrain);
 	}

Comment thread src/Features/TerrainBlending.h
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR reintroduces an explicit enable/disable setting for the Terrain Blending feature and threads that state through the C++ feature system and HLSL shaders so that disabling the feature cleanly restores vanilla behavior.

Changes:

  • Add a TerrainBlending::Settings struct with an ImGui checkbox, and expose it as part of the shared feature buffer so terrain blending can be toggled at runtime.
  • Gate all uses of the blended terrain depth buffers (in State, Deferred, SubsurfaceScattering, and TerrainBlending hooks) on both the feature being loaded and settings.Enabled.
  • Extend SharedData::FeatureData and Lighting.hlsl to carry terrainBlendingSettings.Enabled into shaders and skip terrain blending mask computations and outputs when disabled.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/State.cpp Uses terrainBlending.settings.Enabled to conditionally apply terrain shader hacks and to choose the appropriate depth SRV for shared data.
src/Features/TerrainBlending.h Introduces the Settings struct and wires it into the TerrainBlending feature, enabling per-feature configuration.
src/Features/TerrainBlending.cpp Defines JSON serialization for Settings, adds the ImGui checkbox UI, and gates depth redirection logic in hooks on settings.Enabled.
src/Features/SubsurfaceScattering.cpp Switches SSS depth input to use blended terrain depth only when Terrain Blending is both loaded and enabled.
src/FeatureBuffer.cpp Appends terrainBlending.settings to the feature buffer to match the new TerrainBlendingSettings entry in the shader constant buffer.
src/Deferred.cpp Uses settings.Enabled to decide when to feed blended depth into deferred passes and when to run the terrain blending passes.
package/Shaders/Lighting.hlsl Adds a runtime enable flag around terrain blending mask computation and writing of the blend factor into the G-buffer.
package/Shaders/Common/SharedData.hlsli Adds TerrainBlendingSettings and extends the FeatureData cbuffer to carry the terrain blending enable flag into shaders.

Comment thread src/Features/TerrainBlending.h
Comment thread src/Features/TerrainBlending.cpp
alandtse and others added 2 commits January 24, 2026 03:18
- Add 16-byte alignment and padding to Settings struct to match HLSL layout
- Add STATIC_ASSERT_ALIGNAS_16 macro to verify struct size
- Implement LoadSettings/SaveSettings for JSON persistence
- Remove redundant saturate() call in shader

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/Features/TerrainBlending.cpp`:
- Around line 11-16: In TerrainBlending::DrawSettings the call to
ImGui::Checkbox is passing a uint32_t (settings.Enabled) cast to bool* which
corrupts memory; change this to either use ImGui::CheckboxFlags with a uint32_t
mask referencing settings.Enabled or use a temporary bool (e.g., local bool
enabled = settings.Enabled != 0), pass &enabled to ImGui::Checkbox, then after
the UI call assign settings.Enabled = enabled ? 1u : 0u so the shader-aligned
uint32_t remains intact.
♻️ Duplicate comments (1)
src/Features/TerrainBlending.h (1)

23-27: Zero‑initialize Settings padding to avoid nondeterministic CBuffer bytes.

Uninitialized padding can leak stack garbage into the feature buffer and cause noisy updates. Initialize it explicitly.

🛠️ Proposed fix
 struct Settings
 {
-	uint32_t Enabled = true;
-	uint32_t pad[3];
+	uint32_t Enabled = 1;
+	uint32_t pad[3] = { 0, 0, 0 };
 };

Comment thread src/Features/TerrainBlending.cpp
@github-actions
Copy link
Copy Markdown

✅ A pre-release build is available for this PR:
Download

@doodlum doodlum merged commit 61a13bb into community-shaders:dev Jan 24, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants