Skip to content

Commit b7f0c25

Browse files
authored
Merge pull request #3435 from Starbuck5/surface-sdl3-comp
Surface module compiling in SDL3
2 parents 10bc90e + 20440c3 commit b7f0c25

12 files changed

+329
-272
lines changed

src_c/_surface.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,28 @@
2727
#include "_pygame.h"
2828
#include "surface.h"
2929

30+
// Some simd compat stuff going here for now.
31+
#if PG_SDL3
32+
// SDL3 no longer includes intrinsics by default, we need to do it explicitly
33+
#include <SDL3/SDL_intrin.h>
34+
35+
/* If SDL_AVX2_INTRINSICS is defined by SDL3, we need to set macros that our
36+
* code checks for avx2 build time support */
37+
#ifdef SDL_AVX2_INTRINSICS
38+
#ifndef HAVE_IMMINTRIN_H
39+
#define HAVE_IMMINTRIN_H 1
40+
#endif /* HAVE_IMMINTRIN_H*/
41+
#ifndef __AVX2__
42+
#define __AVX2__
43+
#endif /* __AVX2__*/
44+
#endif /* SDL_AVX2_INTRINSICS*/
45+
46+
// TODO reenable this to test best
47+
#ifdef SDL_SSE2_INTRINSICS
48+
#ifndef __SSE2__
49+
#define __SSE2__
50+
#endif /* __SSE2__*/
51+
#endif /* SDL_SSE2_INTRINSICS*/
52+
#endif /* PG_SDL3 */
53+
3054
#endif

src_c/alphablit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,18 +1715,18 @@ premul_surf_color_by_alpha(SDL_Surface *src, SDL_Surface *dst)
17151715
#if !defined(__EMSCRIPTEN__)
17161716
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
17171717
if ((PG_SURF_BytesPerPixel(src) == 4) && pg_has_avx2()) {
1718-
premul_surf_color_by_alpha_avx2(src, dst);
1718+
premul_surf_color_by_alpha_avx2(src, src_format, dst);
17191719
return 0;
17201720
}
17211721
#if defined(__SSE2__)
17221722
if ((PG_SURF_BytesPerPixel(src) == 4) && SDL_HasSSE2()) {
1723-
premul_surf_color_by_alpha_sse2(src, dst);
1723+
premul_surf_color_by_alpha_sse2(src, src_format, dst);
17241724
return 0;
17251725
}
17261726
#endif /* __SSE2__*/
17271727
#if PG_ENABLE_ARM_NEON
17281728
if ((PG_SURF_BytesPerPixel(src) == 4) && SDL_HasNEON()) {
1729-
premul_surf_color_by_alpha_sse2(src, dst);
1729+
premul_surf_color_by_alpha_sse2(src, src_format, dst);
17301730
return 0;
17311731
}
17321732
#endif /* PG_ENABLE_ARM_NEON */

src_c/meson.build

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ rwobject = py.extension_module(
8484
subdir: pg,
8585
)
8686

87-
# TODO: support SDL3
88-
if sdl_api != 3
8987
simd_blitters_avx2 = static_library(
9088
'simd_blitters_avx2',
9189
'simd_blitters_avx2.c',
@@ -132,7 +130,6 @@ surface = py.extension_module(
132130
install: true,
133131
subdir: pg,
134132
)
135-
endif
136133

137134
surflock = py.extension_module(
138135
'surflock',

src_c/simd_blitters.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ premul_surf_color_by_alpha_non_simd(SDL_Surface *src,
5555
PG_PixelFormat *dst_format,
5656
SDL_Palette *dst_palette);
5757
void
58-
premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst);
58+
premul_surf_color_by_alpha_sse2(SDL_Surface *src, PG_PixelFormat *srcfmt,
59+
SDL_Surface *dst);
5960

6061
void
6162
alphablit_alpha_avx2_argb_no_surf_alpha_opaque_dst(SDL_BlitInfo *info);
@@ -86,4 +87,5 @@ blit_blend_rgb_min_avx2(SDL_BlitInfo *info);
8687
void
8788
blit_blend_premultiplied_avx2(SDL_BlitInfo *info);
8889
void
89-
premul_surf_color_by_alpha_avx2(SDL_Surface *src, SDL_Surface *dst);
90+
premul_surf_color_by_alpha_avx2(SDL_Surface *src, PG_PixelFormat *src_format,
91+
SDL_Surface *dst);

src_c/simd_blitters_avx2.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,8 @@ blit_blend_premultiplied_avx2(SDL_BlitInfo *info)
15611561
#if defined(__AVX2__) && defined(HAVE_IMMINTRIN_H) && \
15621562
!defined(SDL_DISABLE_IMMINTRIN_H)
15631563
void
1564-
premul_surf_color_by_alpha_avx2(SDL_Surface *src, SDL_Surface *dst)
1564+
premul_surf_color_by_alpha_avx2(SDL_Surface *src, PG_PixelFormat *src_format,
1565+
SDL_Surface *dst)
15651566
{
15661567
int i, height = src->h;
15671568
const int width = src->w;
@@ -1578,7 +1579,7 @@ premul_surf_color_by_alpha_avx2(SDL_Surface *src, SDL_Surface *dst)
15781579
__m256i mm_src, mm_dst, alphaA, alphaB, mm_alpha_in;
15791580
__m256i mm_srcA, mm_srcB;
15801581

1581-
const __m256i mm256_amask = _mm256_set1_epi32(src->format->Amask);
1582+
const __m256i mm256_amask = _mm256_set1_epi32(src_format->Amask);
15821583
const __m256i mm_zero = _mm256_setzero_si256();
15831584
const __m256i partial_mask =
15841585
_mm256_set_epi32(0, pxl_excess > 6 ? -1 : 0, pxl_excess > 5 ? -1 : 0,
@@ -1587,10 +1588,10 @@ premul_surf_color_by_alpha_avx2(SDL_Surface *src, SDL_Surface *dst)
15871588
pxl_excess > 0 ? -1 : 0);
15881589
const __m256i mm256_ones = _mm256_set1_epi16(0x0001);
15891590

1590-
char _a_off = ((src->format->Amask >> 8) == 0) ? 0
1591-
: ((src->format->Amask >> 16) == 0) ? 1
1592-
: ((src->format->Amask >> 24) == 0) ? 2
1593-
: 3;
1591+
char _a_off = ((src_format->Amask >> 8) == 0) ? 0
1592+
: ((src_format->Amask >> 16) == 0) ? 1
1593+
: ((src_format->Amask >> 24) == 0) ? 2
1594+
: 3;
15941595

15951596
/* masks for shuffling the alpha to the RGB channels for multiplication */
15961597
const __m256i shuffle_maskA = _mm256_set_epi8(
@@ -1637,7 +1638,8 @@ premul_surf_color_by_alpha_avx2(SDL_Surface *src, SDL_Surface *dst)
16371638
}
16381639
#else
16391640
void
1640-
premul_surf_color_by_alpha_avx2(SDL_Surface *src, SDL_Surface *dst)
1641+
premul_surf_color_by_alpha_avx2(SDL_Surface *src, PG_PixelFormat *src_format,
1642+
SDL_Surface *dst)
16411643
{
16421644
BAD_AVX2_FUNCTION_CALL;
16431645
}

src_c/simd_blitters_sse2.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ alphablit_alpha_sse2_argb_surf_alpha(SDL_BlitInfo *info)
151151
Uint32 *dstp = (Uint32 *)info->d_pixels;
152152
int dstskip = info->d_skip >> 2;
153153

154-
SDL_PixelFormat *srcfmt = info->src;
155-
SDL_PixelFormat *dstfmt = info->dst;
154+
PG_PixelFormat *srcfmt = info->src;
155+
PG_PixelFormat *dstfmt = info->dst;
156156

157157
// int srcbpp = PG_FORMAT_BytesPerPixel(srcfmt);
158158
// int dstbpp = PG_FORMAT_BytesPerPixel(dstfmt);
@@ -293,8 +293,8 @@ alphablit_alpha_sse2_argb_no_surf_alpha(SDL_BlitInfo *info)
293293
int height = info->height;
294294
int srcskip = info->s_skip >> 2;
295295
int dstskip = info->d_skip >> 2;
296-
SDL_PixelFormat *srcfmt = info->src;
297-
SDL_PixelFormat *dstfmt = info->dst;
296+
PG_PixelFormat *srcfmt = info->src;
297+
PG_PixelFormat *dstfmt = info->dst;
298298

299299
/* Original 'Straight Alpha' blending equation:
300300
--------------------------------------------
@@ -719,7 +719,7 @@ blit_blend_premultiplied_sse2(SDL_BlitInfo *info)
719719
int srcskip = info->s_skip >> 2;
720720
Uint32 *dstp = (Uint32 *)info->d_pixels;
721721
int dstskip = info->d_skip >> 2;
722-
SDL_PixelFormat *srcfmt = info->src;
722+
PG_PixelFormat *srcfmt = info->src;
723723
Uint32 amask = srcfmt->Amask;
724724
// Uint64 multmask;
725725
Uint64 ones;
@@ -787,15 +787,14 @@ blit_blend_premultiplied_sse2(SDL_BlitInfo *info)
787787
}
788788

789789
void
790-
premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst)
790+
premul_surf_color_by_alpha_sse2(SDL_Surface *src, PG_PixelFormat *srcfmt,
791+
SDL_Surface *dst)
791792
{
792793
int n;
793794
int width = src->w;
794795
int height = src->h;
795796
Uint32 *srcp = (Uint32 *)src->pixels;
796797
Uint32 *dstp = (Uint32 *)dst->pixels;
797-
798-
SDL_PixelFormat *srcfmt = src->format;
799798
Uint32 amask = srcfmt->Amask;
800799
Uint64 ones;
801800

src_c/simd_fill.h

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,64 +40,84 @@ _pg_HasSSE_NEON();
4040

4141
// AVX2 functions
4242
int
43-
surface_fill_blend_add_avx2(SDL_Surface *surface, SDL_Rect *rect,
43+
surface_fill_blend_add_avx2(SDL_Surface *surface,
44+
PG_PixelFormat *surface_format, SDL_Rect *rect,
4445
Uint32 color);
4546
int
46-
surface_fill_blend_rgba_add_avx2(SDL_Surface *surface, SDL_Rect *rect,
47-
Uint32 color);
47+
surface_fill_blend_rgba_add_avx2(SDL_Surface *surface,
48+
PG_PixelFormat *surface_format,
49+
SDL_Rect *rect, Uint32 color);
4850

4951
int
50-
surface_fill_blend_sub_avx2(SDL_Surface *surface, SDL_Rect *rect,
52+
surface_fill_blend_sub_avx2(SDL_Surface *surface,
53+
PG_PixelFormat *surface_format, SDL_Rect *rect,
5154
Uint32 color);
5255
int
53-
surface_fill_blend_rgba_sub_avx2(SDL_Surface *surface, SDL_Rect *rect,
54-
Uint32 color);
56+
surface_fill_blend_rgba_sub_avx2(SDL_Surface *surface,
57+
PG_PixelFormat *surface_format,
58+
SDL_Rect *rect, Uint32 color);
5559
int
56-
surface_fill_blend_mult_avx2(SDL_Surface *surface, SDL_Rect *rect,
60+
surface_fill_blend_mult_avx2(SDL_Surface *surface,
61+
PG_PixelFormat *surface_format, SDL_Rect *rect,
5762
Uint32 color);
5863
int
59-
surface_fill_blend_rgba_mult_avx2(SDL_Surface *surface, SDL_Rect *rect,
60-
Uint32 color);
64+
surface_fill_blend_rgba_mult_avx2(SDL_Surface *surface,
65+
PG_PixelFormat *surface_format,
66+
SDL_Rect *rect, Uint32 color);
6167
int
62-
surface_fill_blend_min_avx2(SDL_Surface *surface, SDL_Rect *rect,
68+
surface_fill_blend_min_avx2(SDL_Surface *surface,
69+
PG_PixelFormat *surface_format, SDL_Rect *rect,
6370
Uint32 color);
6471
int
65-
surface_fill_blend_rgba_min_avx2(SDL_Surface *surface, SDL_Rect *rect,
66-
Uint32 color);
72+
surface_fill_blend_rgba_min_avx2(SDL_Surface *surface,
73+
PG_PixelFormat *surface_format,
74+
SDL_Rect *rect, Uint32 color);
6775
int
68-
surface_fill_blend_max_avx2(SDL_Surface *surface, SDL_Rect *rect,
76+
surface_fill_blend_max_avx2(SDL_Surface *surface,
77+
PG_PixelFormat *surface_format, SDL_Rect *rect,
6978
Uint32 color);
7079
int
71-
surface_fill_blend_rgba_max_avx2(SDL_Surface *surface, SDL_Rect *rect,
72-
Uint32 color);
80+
surface_fill_blend_rgba_max_avx2(SDL_Surface *surface,
81+
PG_PixelFormat *surface_format,
82+
SDL_Rect *rect, Uint32 color);
7383
// SSE2 functions
7484
int
75-
surface_fill_blend_add_sse2(SDL_Surface *surface, SDL_Rect *rect,
85+
surface_fill_blend_add_sse2(SDL_Surface *surface,
86+
PG_PixelFormat *surface_format, SDL_Rect *rect,
7687
Uint32 color);
7788
int
78-
surface_fill_blend_rgba_add_sse2(SDL_Surface *surface, SDL_Rect *rect,
79-
Uint32 color);
89+
surface_fill_blend_rgba_add_sse2(SDL_Surface *surface,
90+
PG_PixelFormat *surface_format,
91+
SDL_Rect *rect, Uint32 color);
8092
int
81-
surface_fill_blend_sub_sse2(SDL_Surface *surface, SDL_Rect *rect,
93+
surface_fill_blend_sub_sse2(SDL_Surface *surface,
94+
PG_PixelFormat *surface_format, SDL_Rect *rect,
8295
Uint32 color);
8396
int
84-
surface_fill_blend_rgba_sub_sse2(SDL_Surface *surface, SDL_Rect *rect,
85-
Uint32 color);
97+
surface_fill_blend_rgba_sub_sse2(SDL_Surface *surface,
98+
PG_PixelFormat *surface_format,
99+
SDL_Rect *rect, Uint32 color);
86100
int
87-
surface_fill_blend_mult_sse2(SDL_Surface *surface, SDL_Rect *rect,
101+
surface_fill_blend_mult_sse2(SDL_Surface *surface,
102+
PG_PixelFormat *surface_format, SDL_Rect *rect,
88103
Uint32 color);
89104
int
90-
surface_fill_blend_rgba_mult_sse2(SDL_Surface *surface, SDL_Rect *rect,
91-
Uint32 color);
105+
surface_fill_blend_rgba_mult_sse2(SDL_Surface *surface,
106+
PG_PixelFormat *surface_format,
107+
SDL_Rect *rect, Uint32 color);
92108
int
93-
surface_fill_blend_min_sse2(SDL_Surface *surface, SDL_Rect *rect,
109+
surface_fill_blend_min_sse2(SDL_Surface *surface,
110+
PG_PixelFormat *surface_format, SDL_Rect *rect,
94111
Uint32 color);
95112
int
96-
surface_fill_blend_rgba_min_sse2(SDL_Surface *surface, SDL_Rect *rect,
97-
Uint32 color);
113+
surface_fill_blend_rgba_min_sse2(SDL_Surface *surface,
114+
PG_PixelFormat *surface_format,
115+
SDL_Rect *rect, Uint32 color);
98116
int
99-
surface_fill_blend_max_sse2(SDL_Surface *surface, SDL_Rect *rect,
117+
surface_fill_blend_max_sse2(SDL_Surface *surface,
118+
PG_PixelFormat *surface_format, SDL_Rect *rect,
100119
Uint32 color);
101120
int
102-
surface_fill_blend_rgba_max_sse2(SDL_Surface *surface, SDL_Rect *rect,
103-
Uint32 color);
121+
surface_fill_blend_rgba_max_sse2(SDL_Surface *surface,
122+
PG_PixelFormat *surface_format,
123+
SDL_Rect *rect, Uint32 color);

0 commit comments

Comments
 (0)