Skip to content

gl: convert straight alpha to premultiplied alpha on texture upload#4046

Draft
wenjieshen wants to merge 1 commit into
mainfrom
jay/exp/cpu-premultiplied
Draft

gl: convert straight alpha to premultiplied alpha on texture upload#4046
wenjieshen wants to merge 1 commit into
mainfrom
jay/exp/cpu-premultiplied

Conversation

@wenjieshen
Copy link
Copy Markdown
Collaborator

@wenjieshen wenjieshen commented Dec 5, 2025

  • Move straight-alpha premultiplication from fragment shader to CPU during texture upload, using a buffered conversion path with OpenMP to keep upload O(n) but off the GPU hot path.
  • Set texture color space to premultiplied variants after conversion so downstream pipeline treats uploads consistently.
  • Update the GL image shader to indicate there is no long straight color space in the GL image texture.

The problematic file in #2863 performs correctly.
Screenshot 2025-12-05 161721

Screenshot 2025-12-05 162111
  • The boot time recorded for the example PicturePng shows a difference of less than 1%.
  • FPS in Lottie shows a difference of less than 0.05% after 2000 frames.
  • Running all examples, particularly Picture* and Image* is not observed significant regression.

Related issues #4041

@wenjieshen wenjieshen requested a review from hermet December 5, 2025 08:30
@wenjieshen wenjieshen self-assigned this Dec 5, 2025
@github-actions github-actions Bot added the gl OpenGL/WebGL render backend label Dec 5, 2025
@wenjieshen wenjieshen force-pushed the jay/exp/cpu-premultiplied branch 3 times, most recently from 6973178 to 6ea9cb2 Compare December 5, 2025 08:46
@wenjieshen wenjieshen marked this pull request as ready for review December 5, 2025 08:51
Copilot AI review requested due to automatic review settings December 5, 2025 08:51
Copy link
Copy Markdown

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 moves straight-alpha to premultiplied-alpha conversion from GPU fragment shaders to CPU during texture upload, improving rendering correctness for images with transparency. The conversion uses OpenMP parallelization to maintain performance while fixing the rendering issue described in #2863.

Key changes:

  • CPU-side alpha premultiplication during texture upload with parallel processing
  • Color space metadata update to reflect premultiplied format after conversion
  • Shader comments marking straight-alpha paths as forbidden in GL

Reviewed changes

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

File Description
src/renderer/gl_engine/tvgGlShaderSrc.cpp Added comments marking straight-alpha conversion paths as forbidden in GL implementation
src/renderer/gl_engine/tvgGlRenderer.cpp Implemented CPU-side alpha premultiplication with buffered conversion and color space mapping

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

Comment thread src/renderer/gl_engine/tvgGlRenderer.cpp
Comment thread src/renderer/gl_engine/tvgGlShaderSrc.cpp
Comment thread src/renderer/gl_engine/tvgGlRenderer.cpp
@wenjieshen wenjieshen added the showstopper Critical issues / Can not compile or build label Dec 5, 2025
@hermet hermet added bug Something isn't working and removed showstopper Critical issues / Can not compile or build labels Dec 8, 2025
@wenjieshen wenjieshen force-pushed the jay/exp/cpu-premultiplied branch 3 times, most recently from 8d0bf4d to ec940ce Compare December 8, 2025 19:05
Copilot AI review requested due to automatic review settings December 8, 2025 19:05
Copy link
Copy Markdown

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

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


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

Comment thread src/renderer/gl_engine/meson.build Outdated
Comment thread src/renderer/gl_engine/meson.build
@wenjieshen wenjieshen force-pushed the jay/exp/cpu-premultiplied branch from ec940ce to 0b91e0e Compare December 8, 2025 19:09
Copilot AI review requested due to automatic review settings December 8, 2025 19:25
@wenjieshen wenjieshen force-pushed the jay/exp/cpu-premultiplied branch from 0b91e0e to 2d9e273 Compare December 8, 2025 19:25
Copy link
Copy Markdown

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

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


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

Comment thread src/renderer/gl_engine/tvgGlRenderer.cpp
Comment thread src/renderer/gl_engine/tvgGlRenderer.cpp
Convert images with straight alpha to premultiplied alpha during texture upload instead of in the shader.

Moves the premultiplication from GPU (per-fragment) to CPU (once during upload),

Updates the shader to indicate straight alpha formats as already forbidden.

#4041
@wenjieshen wenjieshen force-pushed the jay/exp/cpu-premultiplied branch from 2d9e273 to 5017e23 Compare December 8, 2025 19:30
Copy link
Copy Markdown
Member

@SergeyLebedkin SergeyLebedkin left a comment

Choose a reason for hiding this comment

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

will grad the solution for wg
approved


if (!image->premultiplied && image->channelSize == sizeof(uint32_t)) {
auto buffer = (uint32_t*)malloc(image->w * image->h * sizeof(uint32_t));
auto h = static_cast<int32_t>(image->h);
Copy link
Copy Markdown
Member

@hermet hermet Dec 9, 2025

Choose a reason for hiding this comment

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

@wenjieshen openmp is not the best for this. can't use the gpu power?

@hermet hermet force-pushed the main branch 2 times, most recently from 882f621 to ee0ce46 Compare January 7, 2026 04:39
@wenjieshen wenjieshen marked this pull request as draft January 8, 2026 09:37
@wenjieshen
Copy link
Copy Markdown
Collaborator Author

Convert to draft until completion of the GPGPU version by referring to WebGPU engines.

@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 gl OpenGL/WebGL render backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants