Skip to content

feat: add transparency handling in GBuffer and enhance ColorTargetState with blend state management#465

Merged
Miou-zora merged 8 commits into
mainfrom
add-transparency-when-using-textures
Feb 11, 2026
Merged

feat: add transparency handling in GBuffer and enhance ColorTargetState with blend state management#465
Miou-zora merged 8 commits into
mainfrom
add-transparency-when-using-textures

Conversation

@Miou-zora

@Miou-zora Miou-zora commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

This pull request introduces improvements to the G-buffer rendering pipeline, focusing on alpha transparency handling and blending. The main changes ensure that fragments with low alpha are discarded and that the albedo output supports alpha blending. Additionally, the blend state management is refactored for better configurability and clarity.

Alpha transparency and blending improvements:

  • Fragments with alpha below 0.05 are now discarded in the fs_main shader function, preventing nearly transparent pixels from being rendered.
  • The albedo output in the G-buffer now uses a properly configured blend state to support alpha blending, ensuring correct compositing of semi-transparent surfaces.

Blend state management refactoring:

  • The ColorTargetState utility now includes methods to get and set the blend state, allowing for more flexible and explicit blend state configuration per render target.
  • The internal representation of ColorTargetState is updated to store a wgpu::BlendState, improving encapsulation and clarity.
  • The Shader class now retrieves the blend state from the ColorTargetState instead of using a separate variable, streamlining the pipeline state setup.

Summary by CodeRabbit

  • Refactor
    • Per-target blend configuration now uses each target's blend settings and exposes blend-state accessors for finer control of compositing in the UI render pass.
  • Bug Fixes / Rendering
    • Fragment shader now discards very-low-alpha texels to reduce artifacts and improve appearance of semi-transparent textures.
  • Chores
    • CI workflow configured to use a dedicated build cache path for faster builds.

@Miou-zora Miou-zora requested a review from a team January 31, 2026 22:27
@Miou-zora Miou-zora self-assigned this Jan 31, 2026
@Miou-zora Miou-zora added the enhancement New feature or request label Jan 31, 2026
@coderabbitai

coderabbitai Bot commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

GBuffer fragment shader now samples into textureColor, discards fragments with textureColor.a < 0.05, and computes albedo from textureColor.rgb * material.diffuse.rgb. Graphics code now uses per-format/per-target blend states via ColorTargetState additions and removes a previous RMLUI special-case.

Changes

Cohort / File(s) Summary
GBuffer Fragment Shader
src/plugin/default-pipeline/src/resource/pass/GBuffer.hpp
Fragment shader now samples into textureColor, early-discards fragments when textureColor.a < 0.05, and computes albedo as textureColor.rgb * material.diffuse.rgb.
ColorTargetState API
src/plugin/graphic/src/utils/shader/ColorTargetState.hpp
Added private wgpu::BlendState _blendState, getBlendState() accessor and fluent setBlendState() mutator to hold per-target blend configuration.
Shader Creation Logic
src/plugin/graphic/src/resource/Shader.hpp
Removed RMLUI special-case shared blendState; colorTarget.blend now uses format.getBlendState() (per-format/per-target blend state).
Rmlui Render Pass
src/plugin/rmlui/src/utils/RmluiRenderPass.cpp
Creates and applies explicit BlendState for END_RENDER_TEXTURE (color: src=One, dst=OneMinusSrcAlpha, op=Add; alpha: src=One, dst=OneMinusSrcAlpha, op=Add).
CI workflow
.github/workflows/ci.yml
Added build-cache-path: 'build/.build_cache' to xmake action invocations in several jobs.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • ripel2
  • Divengerss

Poem

🐰
I hop through pixels, light and bright,
I hush the faint and keep the sight,
I stitch the blends with nimble paw,
Shaders sing and render law,
A carrot compile — hops of delight! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% 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 accurately summarizes the two main changes in the PR: transparency handling in GBuffer (alpha discard) and ColorTargetState enhancement with blend state accessors/mutators.

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

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-transparency-when-using-textures

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.

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/plugin/graphic/src/resource/Shader.hpp (1)

68-85: ⚠️ Potential issue | 🟠 Major

RMLUI premultiplied alpha blend is lost due to descriptor not setting blend state.

The local blendState configured for RMLUI_RENDER_PASS_SHADER (lines 68–78) is unused because line 84 always applies &format.getBlendState() from the descriptor. Since RmluiRenderPass::CreateShader() never calls .setBlendState() on the descriptor's ColorTargetState, it defaults to wgpu::Default instead of the required premultiplied alpha settings (srcFactor=One, dstFactor=OneMinusSrcAlpha).

Fix by setting the blend state in the descriptor:

Suggested fix (set blend state in RMLUI descriptor)
    auto colorOutput =
        Graphic::Utils::ColorTargetState("END_RENDER_TEXTURE").setFormat(wgpu::TextureFormat::BGRA8UnormSrgb);
+   wgpu::BlendState premultipliedAlpha(wgpu::Default);
+   premultipliedAlpha.color.srcFactor = wgpu::BlendFactor::One;
+   premultipliedAlpha.color.dstFactor = wgpu::BlendFactor::OneMinusSrcAlpha;
+   premultipliedAlpha.color.operation = wgpu::BlendOperation::Add;
+   premultipliedAlpha.alpha.srcFactor = wgpu::BlendFactor::One;
+   premultipliedAlpha.alpha.dstFactor = wgpu::BlendFactor::OneMinusSrcAlpha;
+   premultipliedAlpha.alpha.operation = wgpu::BlendOperation::Add;
+   colorOutput.setBlendState(premultipliedAlpha);

Then remove the now-unused local blendState override from Shader.hpp.

🤖 Fix all issues with AI agents
In `@src/plugin/default-pipeline/src/resource/pass/GBuffer.hpp`:
- Around line 98-103: The shader currently discards low-alpha fragments but then
forces output.alpha to 1.0, breaking premultiplied blending; update the write to
output.albedo to preserve the sampled alpha and premultiply RGB by that alpha
(use textureColor and material.diffuse): e.g. compute premultipliedRGB =
textureColor.rgb * material.diffuse.rgb * textureColor.a and set output.albedo =
vec4(premultipliedRGB, textureColor.a) (or multiply textureColor.a by
material.diffuse.a if your material encodes alpha), ensuring you keep the
existing discard and textureSample usage.

Comment thread src/plugin/default-pipeline/src/resource/pass/GBuffer.hpp

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/plugin/graphic/src/resource/Shader.hpp`:
- Line 68: The local variable declaration `wgpu::BlendState
blendState(wgpu::Default);` in Shader.hpp is dead code after the refactor that
now uses `format.getBlendState()`; remove this unused `blendState` declaration
so there are no unused locals in the function (ensure no other references to
`blendState` remain and rely on `format.getBlendState()` where needed).

Comment thread src/plugin/graphic/src/resource/Shader.hpp Outdated
@sonarqubecloud

Copy link
Copy Markdown

@Miou-zora Miou-zora merged commit 2660c3d into main Feb 11, 2026
16 checks passed
@Miou-zora Miou-zora deleted the add-transparency-when-using-textures branch February 11, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant