Skip to content

FIx transparency in drawing programs#254

Open
cwervo wants to merge 2 commits intomainfrom
ac/discard-draw-calls
Open

FIx transparency in drawing programs#254
cwervo wants to merge 2 commits intomainfrom
ac/discard-draw-calls

Conversation

@cwervo
Copy link
Copy Markdown
Collaborator

@cwervo cwervo commented Apr 22, 2026

We aren't alpha-blending the shaders in the final compositing step so even sections of the shaders in the /draw directory that use vec4(0, 0, 0, 0) for transparency end up with black rectangles that bound the shapes they're drawing (e.g. the photo on the left). Using discard we get the behavior we expect, where overlapping shapes with transparent regions don't block each other.

Before | After

before photo with shapes overlapping each other with black areas after photo with shapes correctly overlapping each other

Also works on images:
IMG_5230

@cwervo cwervo requested a review from osnr April 22, 2026 21:26
@osnr
Copy link
Copy Markdown
Collaborator

osnr commented Apr 22, 2026

Oh, this is a nice change.

How hard is it to just do the actual alpha blending in our compositor so we can return rgba(0,0,0,0) and it does the same thing? Or do you feel that would be weird?

Copy link
Copy Markdown
Collaborator

@ppkn ppkn left a comment

Choose a reason for hiding this comment

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

I haven't tested this. But I love it. Thanks for fixing this.

@cwervo
Copy link
Copy Markdown
Collaborator Author

cwervo commented Apr 22, 2026

I didn't investigate turning on alpha blending, let me take a look and see if that would be better!

- Add composite-canvas pipeline that un-premultiplies the textu
  color before blending, to prevent darkening of semi-transpare
  pixels.
- Enabled by the above, use vec4(0.0) instead of discard in
  all draw pipelines, to allow proper alpha blending of anti-al
  edges and semi-transparent textures.
@cwervo cwervo force-pushed the ac/discard-draw-calls branch from 3d07d1f to fd02db8 Compare April 22, 2026 23:15
@cwervo
Copy link
Copy Markdown
Collaborator Author

cwervo commented Apr 22, 2026

Okay, so it turned out we were alpha blending but we were crushing the "alpha blend factor" (VK_BLEND_FACTOR_ZERO, changed to VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA), resulting in the black bounding boxes.

Now that alpha blending is correctly turned on there's a funny problem where our canvases could stack up transparencies and produce an unintended halo effect. We fix this with a new GPU pipeline, canvas-composite that we use instead of the image pipeline. It's almost identical except it reverses the alpha premultiplication, fixing the potential haloing bug!

Now we get nicely overlapping partially transparent elements:

IMG_5235 2

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