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

Add SIMD versions of the greyscale transform (attempt #2) #2432

Merged
merged 19 commits into from
Nov 12, 2023

Conversation

MyreMylar
Copy link
Member

Original PR was here: #2214

The last attempt ended up a bit of a mess after it got split into four different PRs, then there was an attempt to rebase and git seemed to get completely bamboozled.

This PR requires #2421 to function, and serves as a demonstration, or proof of concept for that PR. The intent being that we could add SIMD versions of many more of the transform functions making some of them more practical for 'real-time' (i.e. once per frame) full-screen usage in a game.

I'm thinking here of having a live desaturation effect to indicate health loss (or some other game state) across the whole screen by multiply blending a grey scale version of the current full-display frame with a greyscale version of it. Lots of fun post process type effects become possible without having to delve into setting up shaders if you can make the transform functions fast enough for this kind of real-time usage.

So the other obvious question is, does this work? I think last time I gave it a test it was about 12x faster for the AVX2 version on a medium sized image but I may try out exactly the sort of example I talk about above with a test program to see what sort of practical frame-rate difference we end up with.

I think there was some feedback last time on the SIMD code that I still need to respond to before it all got messed up, so I'll try and get to that as well. This will also remain a draft until #2421 gets itself merged.

@MyreMylar MyreMylar changed the base branch from main to simd-transform-setup September 1, 2023 14:03
@itzpr3d4t0r

This comment was marked as outdated.

@MyreMylar
Copy link
Member Author

MyreMylar commented Sep 3, 2023

I created a testbed for 'real world' testing of many small blits a while ago, but it also serves for this.

import random
import pygame
from pygame import Rect, Font, Surface, Color, Vector2
from pygame.sprite import Group, Sprite


class Splash(Sprite):
    def __init__(self, splash_animation, position, splash_group: Group):
        super().__init__(splash_group)
        self.splash_anim = splash_animation
        self.splash_current_frame = 0
        self.image = self.splash_anim[self.splash_current_frame]
        self.splash_frame_time = 0.033
        self.splash_frame_time_acc = 0.0

        self.rect = Rect(0, 0, 16, 16)
        self.rect.midbottom = position

    def update(self, td: float):
        self.splash_frame_time_acc += td
        if self.splash_frame_time_acc >= self.splash_frame_time:
            self.splash_frame_time_acc = 0.0
            self.splash_current_frame += 1
            if self.splash_current_frame >= len(self.splash_anim):
                self.kill()
            else:
                self.image = self.splash_anim[self.splash_current_frame]


class RainDrop(Sprite):
    def __init__(self, drop_surf, position, screen_bottom,
                 rain_drop_group: Group, splash_animation, splash_group: Group):
        super().__init__(rain_drop_group)
        self.image = drop_surf

        self.position = Vector2(position[0], position[1])
        self.screen_bottom = screen_bottom

        self.splash_group = splash_group
        self.splash_anim = splash_animation

        self.fall_vec = Vector2(0.0, 1.0)
        self.fall_speed = random.normalvariate(400.0, 10.0)

        self.rect = Rect(0, 0, 1, 6)
        self.rect.midbottom = self.position

    def update(self, td: float, platforms: Group):

        self.position += self.fall_vec * self.fall_speed * td
        self.rect.midbottom = self.position

        for platform in platforms.sprites():
            if self.rect.colliderect(platform.rect):
                self.rect.bottom = platform.rect.top
                self.position.y = platform.rect.top
                self.kill()

        if self.alive() and self.rect.bottom >= self.screen_bottom:
            self.rect.bottom = self.screen_bottom
            self.position.y = self.screen_bottom
            self.kill()

        if not self.alive():
            Splash(self.splash_anim, self.position, self.splash_group)


class Platform(Sprite):
    def __init__(self, position, size, plat_group: Group):
        super().__init__(plat_group)
        self.image = Surface(size)
        self.image.fill((50, 50, 50))
        self.image.fill((30, 30, 30), Rect((1, 1), (size[0]-2, size[1]-2)))

        self.rect = Rect(position, size)


pygame.init()

desktop_size = pygame.display.get_desktop_sizes()[0]
display_surf = pygame.display.set_mode(desktop_size)
full_screen_surf = pygame.Surface(desktop_size)
greyscale_surf = pygame.Surface(desktop_size)

fifty_percent_surf = pygame.Surface(desktop_size)
fifty_percent_surf.fill((128, 128, 128))

seventy_five_percent_surf = pygame.Surface(desktop_size)
seventy_five_percent_surf.fill((191, 191, 191))

twenty_five_percent_surf = pygame.Surface(desktop_size)
twenty_five_percent_surf.fill((64, 64, 46))

background = pygame.image.load("images/background.png").convert()

splash_sheet = pygame.image.load("images/splash.png").convert_alpha()
splash_anim = [splash_sheet.subsurface(Rect(0, 0, 16, 16)),
               splash_sheet.subsurface(Rect(16, 0, 16, 16)),
               splash_sheet.subsurface(Rect(32, 0, 16, 16)),
               splash_sheet.subsurface(Rect(48, 0, 16, 16)),
               splash_sheet.subsurface(Rect(64, 0, 16, 16))]

splash_horiz_pos = random.uniform(0, 1904)

rain_drop_surf = Surface((1, 6), pygame.SRCALPHA)
rain_drop_surf.fill((40, 150, 170, 200), Rect(0, 5, 1, 1))
rain_drop_surf.fill((40, 150, 170, 175), Rect(0, 4, 1, 1))
rain_drop_surf.fill((40, 150, 170, 150), Rect(0, 3, 1, 1))
rain_drop_surf.fill((40, 150, 170, 100), Rect(0, 2, 1, 1))
rain_drop_surf.fill((40, 150, 170, 50), Rect(0, 1, 1, 1))
rain_drop_surf.fill((40, 100, 120, 50), Rect(0, 1, 1, 1))

splashes = Group()
drop_group = Group()
platform_group = Group()

Platform((700, 500), (150, 32), platform_group)
Platform((300, 700), (50, 32), platform_group)
Platform((100, 250), (100, 32), platform_group)
Platform((1200, 800), (150, 32), platform_group)
Platform((500, 900), (150, 32), platform_group)
Platform((900, 600), (150, 32), platform_group)

Platform((1400, 850), (120, 32), platform_group)
Platform((1600, 950), (175, 32), platform_group)
Platform((1750, 300), (100, 32), platform_group)

font = Font(size=48)

clock = pygame.time.Clock()
running = True

while running:

    time_delta = clock.tick()/1000.0

    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            running = False

    full_screen_surf.blit(background, (0, 0))

    for _ in range(int(random.uniform(0, 15))):
        RainDrop(rain_drop_surf, (int(random.uniform(0, 1920)), int(random.uniform(-100, -10))),
                 full_screen_surf.get_height(), drop_group, splash_anim, splashes)

    splashes.update(time_delta)
    drop_group.update(time_delta, platform_group)

    platform_group.draw(full_screen_surf)
    drop_group.draw(full_screen_surf)
    splashes.draw(full_screen_surf)

    pygame.transform.grayscale(full_screen_surf, greyscale_surf)

    # take 75% of greyscale & 25% of normal surf then add them together for a 75% desaturation effect, we could 
    # make this dynamic by filling the two surfaces based on some sliding variable between 0 and 255 
    greyscale_surf.blit(seventy_five_percent_surf, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
    full_screen_surf.blit(twenty_five_percent_surf, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
    display_surf.blit(full_screen_surf, (0, 0))
    display_surf.blit(greyscale_surf, (0, 0), special_flags=pygame.BLEND_RGBA_ADD)

    fps_text = font.render(f"{1.0/(max(time_delta, 0.0000000001)):.2f} FPS", True, Color("white"))
    display_surf.blit(fps_text, (desktop_size[0] - 200, 32))

    pygame.display.flip()

background.png:
background
splash.png (it's white so hard to see but should be click and saveable:
splash

If you stick them in an images/ subdirectory you should be able to run this program.

For me, I get 30ish FPS when running it without this PR on main. Then after deleting everything temporary and rebuilding build config and pip installing again (it's one of the pains with these PRs that add new .c files to Setup) I get approximately 60FPS (slightly below).

Seems like a pretty reasonable speedup in a believe-able use case.

The final demo looks like this in motion with the deasturation:

image

@yunline yunline added SIMD transform pygame.transform Performance Related to the speed or resource usage of the project labels Sep 4, 2023
Base automatically changed from simd-transform-setup to main September 9, 2023 04:26
@Starbuck5
Copy link
Member

@MyreMylar Now that the prereq PR is this ready to leave draft status?

@MyreMylar
Copy link
Member Author

MyreMylar commented Sep 9, 2023 via email

@MyreMylar MyreMylar marked this pull request as ready for review September 10, 2023 15:23
@MyreMylar MyreMylar requested a review from a team as a code owner September 10, 2023 15:23
@MyreMylar
Copy link
Member Author

OK, I've tried to address everything that was brought up in previous reviews now, so hopefully this is ready for fresh reviews if it passes on the CI tests.

Thanks for the patience with this,

src_c/transform.c Outdated Show resolved Hide resolved
src_c/transform.c Outdated Show resolved Hide resolved
@MyreMylar
Copy link
Member Author

In my testing:

SSE2/NEON old one-pixel method: 6 times faster than non-SIMD greyscale.
SSE2/NEON new two-pixel method: 7 times faster than non-SIMD greyscale.
AVX2 method: 11 times faster than non-SIMD greyscale.

Used this test program:

from sys import stdout
from pstats import Stats
from cProfile import Profile


import pygame

pygame.init()


def grayscale_test():
    dimensions = [
        (1, 1),
        (3, 3),
        (65, 65),
        (126, 127),
        (254, 255),
        (1024, 1024),
        (2047, 2048),
    ]
    for iterations in range(0, 1000):
        for w, h in dimensions:
            src_surf = pygame.Surface((w, h), pygame.SRCALPHA, 32)
            src_surf.fill((100, 255, 128, 200))

            result = pygame.transform.grayscale(src_surf)
            # res_color = result.get_at((0, 0))
            # if res_color == pygame.Color(195, 195, 195, 200):
            #     pass


if __name__ == "__main__":
    print("Grayscale")
    profiler = Profile()
    profiler.runcall(grayscale_test)
    stats = Stats(profiler, stream=stdout)
    stats.strip_dirs()
    stats.sort_stats("cumulative")
    stats.print_stats()

@MyreMylar MyreMylar self-assigned this Oct 7, 2023
Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

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

Pygame-ce w/ the SIMD be like 🚀

This looks good!

src_c/simd_transform_avx2.c Outdated Show resolved Hide resolved
src_c/simd_transform_avx2.c Outdated Show resolved Hide resolved
src_c/simd_transform_avx2.c Outdated Show resolved Hide resolved
src_c/simd_transform_avx2.c Outdated Show resolved Hide resolved
src_c/simd_transform_avx2.c Outdated Show resolved Hide resolved
src_c/simd_transform_sse2.c Outdated Show resolved Hide resolved
Copy link
Member

@itzpr3d4t0r itzpr3d4t0r left a comment

Choose a reason for hiding this comment

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

LGTM!

@MyreMylar MyreMylar merged commit acdb21f into main Nov 12, 2023
30 checks passed
@itzpr3d4t0r itzpr3d4t0r added this to the 2.4.0 milestone Nov 12, 2023
@MyreMylar MyreMylar deleted the simd_add_greyscale branch November 19, 2023 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance Related to the speed or resource usage of the project SIMD transform pygame.transform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants