Skip to content

wg_engine: alpha premultiplied bug fix#4070

Open
SergeyLebedkin wants to merge 1 commit into
mainfrom
sergii/webgpu_alpha_premultiplied_bug
Open

wg_engine: alpha premultiplied bug fix#4070
SergeyLebedkin wants to merge 1 commit into
mainfrom
sergii/webgpu_alpha_premultiplied_bug

Conversation

@SergeyLebedkin

@SergeyLebedkin SergeyLebedkin commented Dec 23, 2025

Copy link
Copy Markdown
Member

Fixes an alpha premultiplication bug in the WebGPU engine by introducing
a texture preprocessing stage using compute shaders.

The preprocessing handles alpha premultiplication for non-premultiplied textures and
channel shuffling for ARGB format textures to convert them to the universal RGBA format.

Added compute shaders for texture preprocessing (premultiplication and channel shuffling)
Modified fragment shader to remove redundant alpha premultiplication
Updated texture handling to track premultiplication and shuffle state

image

#4041

@SergeyLebedkin SergeyLebedkin self-assigned this Dec 23, 2025
Copilot AI review requested due to automatic review settings December 23, 2025 22:01
@SergeyLebedkin SergeyLebedkin added bug Something isn't working webgpu WebGPU render backend labels Dec 23, 2025

Copilot AI left a comment

Copy link
Copy Markdown

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 fixes an alpha premultiplication bug in the WebGPU engine by introducing a texture preprocessing stage using compute shaders. The preprocessing handles alpha premultiplication for non-premultiplied textures and channel shuffling for ARGB format textures to convert them to the universal RGBA format.

Key changes:

  • Added compute shaders for texture preprocessing (premultiplication and channel shuffling)
  • Modified fragment shader to remove redundant alpha premultiplication
  • Updated texture handling to track premultiplication and shuffle state

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
tvgWgShaderSrc.h Added declaration for compute shader source
tvgWgShaderSrc.cpp Implemented three compute shader variants and fixed fragment shader alpha handling
tvgWgRenderData.h Added metadata fields to track texture state
tvgWgRenderData.cpp Updated texture initialization to detect format and premultiplication needs
tvgWgPipelines.h Added compute pipeline declarations and release method
tvgWgPipelines.cpp Implemented compute pipeline initialization and cleanup
tvgWgCompositor.h Added staging texture and preprocessing queue
tvgWgCompositor.cpp Implemented texture preprocessing logic in flush operation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/renderer/wg_engine/tvgWgCompositor.cpp Outdated
Comment thread src/renderer/wg_engine/tvgWgCompositor.cpp Outdated
@SergeyLebedkin SergeyLebedkin force-pushed the sergii/webgpu_alpha_premultiplied_bug branch from c4a61fe to c761414 Compare December 23, 2025 22:08
Fixes an alpha premultiplication bug in the WebGPU engine by introducing
a texture preprocessing stage using compute shaders.

The preprocessing handles alpha premultiplication for non-premultiplied textures and
channel shuffling for ARGB format textures to convert them to the universal RGBA format.

Added compute shaders for texture preprocessing (premultiplication and channel shuffling)
Modified fragment shader to remove redundant alpha premultiplication
Updated texture handling to track premultiplication and shuffle state

#4041
@SergeyLebedkin SergeyLebedkin force-pushed the sergii/webgpu_alpha_premultiplied_bug branch from c761414 to c6d4ad1 Compare December 23, 2025 22:17
@hermet

hermet commented Dec 24, 2025

Copy link
Copy Markdown
Member

channel shuffling for ARGB format textures to convert them to the universal RGBA format.

@SergeyLebedkin

  1. Canvas needs to clarify the demanded colorspace (You can see why WgCanvas::target() has a colorspace option) and it needs to bring the image with desired colorspace. That's the compatible result. Fixed to RGBA is not compatible. We need to confirm that why RGBA is universal and mandatory.

  2. What if big/little endian cases? You may regard them like a. ARGB(BGRA) / b. ABGR(RGBA)

  3. Why a certain png has issue and others have not? Can explain it?

@hermet hermet force-pushed the main branch 4 times, most recently from 882f621 to ee0ce46 Compare January 7, 2026 04:39
WGPURenderPipeline tint_effect{};
WGPURenderPipeline tritone_effect{};
// compute shaders
WGPUComputePipeline compute_alpha_premult_shuffle{};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: since we have multiple *_shuffle names, is it possible for you to consider a more descriptive suffix for these specific cases (like _bgr / _swizzle) to make the intent a bit clearer at call sites? Totally optional.

https://en.wikipedia.org/wiki/Swizzling_(computer_graphics)

@wenjieshen wenjieshen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for the contribution.
I tested this on Windows, and none of the examples showed any regressions. The Lottie example also runs at the same FPS as the previous version.

Screenshot 2026-01-07 235840 Screenshot 2026-01-07 235208

@wenjieshen wenjieshen self-requested a review January 7, 2026 16:35
@hermet hermet force-pushed the main branch 5 times, most recently from 1f2f79f to f2ae0e8 Compare January 14, 2026 07:18
@hermet hermet force-pushed the main branch 5 times, most recently from ed8518f to 21f0f8f Compare February 9, 2026 08:29
@hermet hermet force-pushed the main branch 3 times, most recently from 5cb1306 to f076d22 Compare March 4, 2026 05:01
@hermet hermet force-pushed the main branch 3 times, most recently from a00fdaa to 9bd7217 Compare April 7, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working webgpu WebGPU render backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants