Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add disable_alpha render mode to shaders #51844

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented Aug 18, 2021

3.x version of #51711.

This can be used by multipass shaders to write to a texture's alpha channel without causing the material to become transparent.

Preview

Default render mode (GLES3)

2021-08-16_05 08 20

disable_alpha render mode (GLES3)

2021-08-16_05 08 29

Default render mode (GLES2)

2021-08-16_05 09 06

disable_alpha render mode (GLES2)

2021-08-16_05 09 15

This can be used by multipass shaders to write to a texture's
alpha channel without causing the material to become transparent.
@Calinou Calinou requested a review from a team as a code owner August 18, 2021 15:26
@Calinou Calinou added this to the 3.4 milestone Aug 18, 2021
@Calinou Calinou changed the title Add disable_alpha render mode to shaders Add disable_alpha render mode to shaders (3.x) Aug 18, 2021
@Lauson1ex
Copy link
Contributor

@Calinou I made a test scene with a shader which renders the final colors to the RGB components of a Viewport, and depth to the alpha component. That Viewport's color and depth is then accessed by a 2D ColorRect elsewhere.

  • When transparent_bg is disabled on the Viewport, reading the alpha from that Viewport always results in 0.0;
  • When transparent_bg is enabled on the Viewport, reading the alpha from that Viewport works as expected, but RGB then always get premultiplied by the alpha.

transparent_bg = false, RGB components (after linear -> sRGB conversion):
image

transparent_bg = false, ALPHA component (depth, after screen-space -> view-space conversion):
image
(black screen because transparent_bg is disabled)

transparent_bg = true, RGB components (after linear -> sRGB conversion):
image
(black screen because ALPHA =< 0.0 due to depth being between 0.0 and -1.0 after screen-space to view-space transform)

transparent_bg = true, ALPHA component (depth, after screen-space -> view-space conversion):
image

And finally:
We can verify that RGB is being premultiplied by ALPHA by manually setting its value.

transparent_bg = true, ALPHA = 1.0, RGB components:
image

transparent_bg = true, ALPHA = 0.5, RGB components:
image

ALPHA is being correctly written to the Viewport, and it is being accessed by other shaders as expected. The problem is that the RGB components of a Viewport should not be premultiplied by ALPHA, especially when keep_3d_linear is enabled, which implies that the Viewport is being used as a data render target.

@clayjohn
Copy link
Member

@Calinou to add to @Lauson1ex's comment above, you likely need to adjust the GLBlendFunc to GL_ONE somewhere.

@akien-mga akien-mga modified the milestones: 3.4, 3.5 Nov 8, 2021
@akien-mga akien-mga modified the milestones: 3.5, 3.x Jul 3, 2022
@Calinou Calinou changed the title Add disable_alpha render mode to shaders (3.x) [3.x] Add disable_alpha render mode to shaders Jun 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants