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

BLEND_OVERLAY blend mode #2377

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

itzpr3d4t0r
Copy link
Member

@itzpr3d4t0r itzpr3d4t0r commented Aug 4, 2023

Closes #2373, featuring:
BLEND_OVERLAY and BLEND_RGB_OVERLAY flags and algorithm with:

  • 1 pixel at a time implementation that's hardware agnostic
  • 8 pixels at a time with AVX2
  • 4 pixels at a time with SSE2

For a visual test i'm using the following image and program:
base

import pygame

pygame.init()
TEXT_COLOR = "black"
screen = pygame.display.set_mode((1000, 500))

img = pygame.image.load("base.png").convert()
img = pygame.transform.scale(img, (250, 250))

background = pygame.Surface(img.get_size())
background.fill((255, 100, 50))

background2 = pygame.Surface(img.get_size())
background2.fill((255, 0, 100))

font = pygame.font.SysFont("Arial", 28, True)
BLEND_OVERLAY = font.render("OVERLAY", True, TEXT_COLOR)
BLEND_ADD = font.render("ADD", True, TEXT_COLOR)
BLEND_MULT = font.render("MULT", True, TEXT_COLOR)
BASE = font.render("BASE", True, TEXT_COLOR)
MIN = font.render("MIN", True, TEXT_COLOR)
MAX = font.render("MAX", True, TEXT_COLOR)
SUB = font.render("SUB", True, TEXT_COLOR)

keep = True
clock = pygame.Clock()

while keep:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep = False

    screen.blits(
        [
            (img, (0, 250)),
            (img, (250, 250)),
            (img, (500, 250)),
            (img, (750, 250)),
            (img, (0, 0)),
            (img, (250, 0)),
            (img, (500, 0)),
            (img, (750, 0)),
            (background, (0, 0), None, pygame.BLEND_OVERLAY),
            (background, (250, 0), None, pygame.BLEND_ADD),
            (background, (500, 0), None, pygame.BLEND_MULT),
            (background, (250, 250), None, pygame.BLEND_MIN),
            (background, (500, 250), None, pygame.BLEND_MAX),
            (background, (750, 250), None, pygame.BLEND_SUB),
            (BLEND_OVERLAY, BLEND_OVERLAY.get_rect(center=(125, 20))),
            (BLEND_ADD, BLEND_ADD.get_rect(center=(375, 20))),
            (BLEND_MULT, BLEND_MULT.get_rect(center=(625, 20))),
            (BASE, BASE.get_rect(center=(125, 270))),
            (MIN, MIN.get_rect(center=(375, 270))),
            (MAX, MAX.get_rect(center=(625, 270))),
            (SUB, SUB.get_rect(center=(875, 270))),
        ]
    )

    pygame.display.flip()

@itzpr3d4t0r itzpr3d4t0r marked this pull request as ready for review January 17, 2024 17:57
@itzpr3d4t0r itzpr3d4t0r requested a review from a team as a code owner January 17, 2024 17:57
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

OK, this all seems to work OK, the tests make sense and I'm convinced that it has value as a blend mode.

Only two things lacking really:

  • An SSE2/NEON version for decent performance on macs/arm.
  • Documentation on the special flags page - ideally we should include the formula in this one as it is more complicated than the existing set of MUL, ADD & SUB blend modes. Something like the description on the wikipedia page:
    image

Would help I think.

I think we could have the SSE2 version in another PR - because there will at least be a version for all platforms after this one, but we definitely need the docs or nobody will know it exists.

@Starbuck5 Starbuck5 marked this pull request as draft March 22, 2024 08:23
@itzpr3d4t0r itzpr3d4t0r marked this pull request as ready for review April 5, 2024 15:30
@itzpr3d4t0r itzpr3d4t0r requested a review from MyreMylar April 5, 2024 15:46
@itzpr3d4t0r
Copy link
Member Author

Since this has been up for a long time I've also added the SSE2 RGB blitter.

Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

Great!

The new docs look lovely:

image

And the blend mode still works locally, as well as passing the unit tests on all platforms now.

Approved!

@itzpr3d4t0r itzpr3d4t0r added SIMD New API This pull request may need extra debate as it adds a new class or function to pygame and removed enhancement labels May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New API This pull request may need extra debate as it adds a new class or function to pygame SIMD Surface pygame.Surface
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BLEND_OVERLAY flag
2 participants