From cb044389610ad1d2b9d2360b247246747fd63b1c Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 16 Aug 2025 22:00:58 +0200 Subject: [PATCH] =?UTF-8?q?port=20srb2classic=C2=B4s=20sdl=20blitting=20st?= =?UTF-8?q?uff=20for=20software?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit all credits to hanicef mostly from https://codeberg.org/srb2classic/srb2classic/commit/a1fa0b89dbf75ef8be17c9ca5c728cc9c1848582 and https://codeberg.org/srb2classic/srb2classic/commit/a1cf54e66ad1cb66af99056bc940fc68dce24224 boosts my fps around 20-25 fps --- src/sdl/i_video.c | 95 +++++++++++------------------------------------ src/v_video.c | 38 ++++++++++++++++--- src/v_video.h | 12 ++++++ 3 files changed, 67 insertions(+), 78 deletions(-) diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 06b6e2dde..47465b870 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -170,13 +170,8 @@ static INT32 mousemovex = 0, mousemovey = 0; // SDL vars static SDL_Surface *vidSurface = NULL; -static SDL_Surface *bufSurface = NULL; static SDL_Surface *icoSurface = NULL; -static SDL_Color localPalette[256]; -#if 0 -static SDL_Rect **modeList = NULL; -static Uint8 BitsPerPixel = 16; -#endif +static UINT32 localPalette[256]; Uint16 realwidth = BASEVIDWIDTH; Uint16 realheight = BASEVIDHEIGHT; static SDL_bool mousegrabok = SDL_TRUE; @@ -225,7 +220,6 @@ static INT32 windowedModes[MAXWINMODES][2] = static INT32 custom_width = 0; static INT32 custom_height = 0; -static void Impl_VideoSetupSDLBuffer(void); static void Impl_VideoSetupBuffer(void); static SDL_bool Impl_CreateWindow(SDL_bool fullscreen); static void Impl_SetWindowIcon(void); @@ -526,7 +520,6 @@ static void VID_Command_Info_f (void) if (!M_CheckParm("-noblit")) videoblitok = SDL_TRUE; - SurfaceInfo(bufSurface, M_GetText("Current Engine Mode")); SurfaceInfo(vidSurface, M_GetText("Current Video Mode")); } @@ -1360,7 +1353,7 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen) { static SDL_bool wasfullscreen = SDL_FALSE; Uint32 rmask, gmask, bmask, amask; - int bpp = 16; + int bpp; int sw_texture_format = SDL_PIXELFORMAT_ABGR8888; realwidth = vid.width; @@ -1429,16 +1422,6 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen) SDL_DestroyTexture(texture); } - if (!usesdl2soft) - { - sw_texture_format = SDL_PIXELFORMAT_RGB565; - } - else - { - bpp = 32; - sw_texture_format = SDL_PIXELFORMAT_RGBA8888; - } - texture = SDL_CreateTexture(renderer, sw_texture_format, SDL_TEXTUREACCESS_STREAMING, width, height); // Set up SW surface @@ -1513,19 +1496,18 @@ void I_FinishUpdate(void) if (rendermode == render_soft && vid.screens[0]) { - if (!bufSurface) //Double-Check - { - Impl_VideoSetupSDLBuffer(); - } - if (bufSurface) - { - SDL_BlitSurface(bufSurface, &src_rect, vidSurface, &src_rect); - // Fury -- there's no way around UpdateTexture, the GL backend uses it anyway - SDL_LockSurface(vidSurface); - SDL_UpdateTexture(texture, &src_rect, vidSurface->pixels, vidSurface->pitch); - SDL_UnlockSurface(vidSurface); - } + SDL_LockSurface(vidSurface); + // copy pixels ourselves to the video surface (prevents a crash in libsdl) + UINT32 *dst = vidSurface->pixels; + UINT8 *src = vid.screens[0]; + for (int32_t i = 0; i < vid.width * vid.height; i++) + *dst++ = localPalette[*src++]; + SDL_UnlockSurface(vidSurface); + // Fury -- there's no way around UpdateTexture, the GL backend uses it anyway + SDL_LockSurface(vidSurface); + SDL_UpdateTexture(texture, &src_rect, vidSurface->pixels, vidSurface->pitch); + SDL_UnlockSurface(vidSurface); SDL_RenderClear(renderer); SDL_RenderCopy(renderer, texture, &src_rect, NULL); @@ -1572,14 +1554,11 @@ void I_SetPalette(RGBA_t *palette) for (i = 0; i < 256; i++) { - localPalette[i].r = palette[i].s.red; - localPalette[i].g = palette[i].s.green; - localPalette[i].b = palette[i].s.blue; + localPalette[i] = 0xff000000; + localPalette[i] |= palette[i].s.red << 0; + localPalette[i] |= palette[i].s.green << 8; + localPalette[i] |= palette[i].s.blue << 16; } - - //if (vidSurface) SDL_SetPaletteColors(vidSurface->format->palette, localPalette, 0, 256); - // Fury -- SDL2 vidSurface is a 32-bit surface buffer copied to the texture. It's not palletized, like bufSurface. - if (bufSurface) SDL_SetPaletteColors(bufSurface->format->palette, localPalette, 0, 256); } // return number of fullscreen + X11 modes @@ -1701,15 +1680,6 @@ INT32 VID_SetMode(INT32 modeNum) SDLSetMode(vid.width, vid.height, USE_FULLSCREEN); Impl_VideoSetupBuffer(); - if (rendermode == render_soft) - { - if (bufSurface) - { - SDL_FreeSurface(bufSurface); - bufSurface = NULL; - } - } - src_rect.w = vid.width; src_rect.h = vid.height; @@ -1837,36 +1807,19 @@ static void Impl_SetWindowIcon(void) SDL_SetWindowIcon(window, icoSurface); } -static void Impl_VideoSetupSDLBuffer(void) -{ - if (bufSurface != NULL) - { - SDL_FreeSurface(bufSurface); - bufSurface = NULL; - } - // Set up the SDL palletized buffer (copied to vidbuffer before being rendered to texture) - bufSurface = SDL_CreateRGBSurfaceFrom(vid.screens[0], vid.width, vid.height, 8, - (int)vid.rowbytes, 0x00000000, 0x00000000, 0x00000000, 0x00000000); // 256 mode - - if (bufSurface) - { - SDL_SetPaletteColors(bufSurface->format->palette, localPalette, 0, 256); - } - else - { - I_Error("%s", M_GetText("No system memory for SDL buffer surface\n")); - } -} - static void Impl_VideoSetupBuffer(void) { // Set up game's software render buffer + size_t size; + vid.rowbytes = vid.width; if (vid.buffer) free(vid.buffer); - vid.buffer = calloc(vid.rowbytes*vid.height, NUMSCREENS); + size = vid.rowbytes*vid.height * NUMSCREENS; + + vid.buffer = calloc(size, NUMSCREENS); if (!vid.buffer) { @@ -2127,10 +2080,6 @@ void I_ShutdownGraphics(void) if (vid.buffer) free(vid.buffer); vid.buffer = NULL; - - if (bufSurface) - SDL_FreeSurface(bufSurface); - bufSurface = NULL; } I_OutputMsg("I_ShutdownGraphics(): "); diff --git a/src/v_video.c b/src/v_video.c index a4c1d24f1..a20cdb626 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -615,8 +615,21 @@ static void CV_constextsize_OnChange(void) void VID_BlitLinearScreen(const UINT8 *srcptr, UINT8 *destptr, INT32 width, INT32 height, size_t srcrowbytes, size_t destrowbytes) { - if ((srcrowbytes == destrowbytes) && (srcrowbytes == (size_t)width)) - M_Memcpy(destptr, srcptr, srcrowbytes * height); + if (srcrowbytes == destrowbytes) + { + size_t i = srcrowbytes * height; +#if defined(__SSE__) + while (i >= 16) + { + // TODO: find where the buffer is misaligned at times and align it + _mm_storeu_ps((float *)destptr, _mm_loadu_ps((const float *)srcptr)); + srcptr += 16; + destptr += 16; + i -= 16; + } +#endif + M_Memcpy(destptr, srcptr, i); + } else { while (height--) @@ -3779,17 +3792,32 @@ UINT8 GetColorLUTDirect(colorlookup_t *lut, UINT8 r, UINT8 g, UINT8 b) void V_Init(void) { INT32 i; - UINT8 *base = vid.buffer; const INT32 screensize = vid.rowbytes * vid.height; for (i = 0; i < NUMSCREENS; i++) + { +#if defined(__SSE__) + aligned_free(vid.screens[i]); +#else + free(vid.screens[i]); +#endif vid.screens[i] = NULL; + } // start address of NUMSCREENS * width*height vidbuffers - if (base) + if (screensize > 0) { for (i = 0; i < NUMSCREENS; i++) - vid.screens[i] = base + i*screensize; + { + // we need to allocate these relative to their cpu restrictions to not trigger segfaults + // TODO: add support for sve and neon +#if defined(__SSE__) + vid.screens[i] = aligned_alloc(128, screensize); +#else + vid.screens[i] = malloc(screensize); +#endif + memset(vid.screens[i], 0, screensize); + } } #ifdef DEBUG diff --git a/src/v_video.h b/src/v_video.h index d5a4adf5e..1032082be 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -23,6 +23,18 @@ extern "C" { #include "r_defs.h" #include "r_main.h" +#if defined(__SSE__) +#ifdef _WIN32 +#include +#define aligned_alloc(align, size) _aligned_malloc(size, align) +#define aligned_free(ptr) _aligned_free(ptr) +#else +#define aligned_free(ptr) free(ptr) +#endif + +#include +#endif + // // VIDEO //