From 56fb0046baa339478bb6dff773a51bbb64540255 Mon Sep 17 00:00:00 2001 From: bladeoner Date: Wed, 27 Mar 2019 22:17:24 +0100 Subject: [PATCH] Snes9x - Use for loops for DRAW_TILE. 3-4% speed benefit. Backport Select brightness function in tile renderer. (#828) - Use for loops for DRAW_TILE. 3-4% speed benefit. - Select brightness function in tile renderer. --- source/snes9x/gfx.h | 45 +++++++++++++++++--------- source/snes9x/tile.cpp | 73 ++++++++++++++++++++++-------------------- 2 files changed, 67 insertions(+), 51 deletions(-) diff --git a/source/snes9x/gfx.h b/source/snes9x/gfx.h index 01f788a..5b65924 100644 --- a/source/snes9x/gfx.h +++ b/source/snes9x/gfx.h @@ -131,8 +131,9 @@ extern struct SGFX GFX; ((((((C1) & RGB_REMOVE_LOW_BITS_MASK) + \ ((C2) & RGB_REMOVE_LOW_BITS_MASK)) >> 1) + \ ((C1) & (C2) & RGB_LOW_BITS_MASK)) | ALPHA_BITS_MASK) +#define COLOR_ADD_BRIGHTNESS1_2 COLOR_ADD1_2 -inline uint16 COLOR_ADD(uint16 C1, uint16 C2) +inline uint16 COLOR_ADD_BRIGHTNESS(uint16 C1, uint16 C2) { return ((brightness_cap[ (C1 >> RED_SHIFT_BITS) + (C2 >> RED_SHIFT_BITS) ] << RED_SHIFT_BITS) | (brightness_cap[((C1 >> GREEN_SHIFT_BITS) & 0x1f) + ((C2 >> GREEN_SHIFT_BITS) & 0x1f)] << GREEN_SHIFT_BITS) | @@ -143,27 +144,39 @@ inline uint16 COLOR_ADD(uint16 C1, uint16 C2) (brightness_cap[ (C1 & 0x1f) + (C2 & 0x1f)] )); } +inline uint16 COLOR_ADD(uint16 C1, uint16 C2) +{ + const int RED_MASK = 0x1F << RED_SHIFT_BITS; + const int GREEN_MASK = 0x1F << GREEN_SHIFT_BITS; + const int BLUE_MASK = 0x1F; + + int rb = C1 & (RED_MASK | BLUE_MASK); + rb += C2 & (RED_MASK | BLUE_MASK); + int rbcarry = rb & ((0x20 << RED_SHIFT_BITS) | (0x20 << 0)); + int g = (C1 & (GREEN_MASK)) + (C2 & (GREEN_MASK)); + int rgbsaturate = (((g & (0x20 << GREEN_SHIFT_BITS)) | rbcarry) >> 5) * 0x1f; + return (uint16)((rb & (RED_MASK | BLUE_MASK)) | (g & GREEN_MASK) | rgbsaturate); +} + + #define COLOR_SUB1_2(C1, C2) \ GFX.ZERO[(((C1) | RGB_HI_BITS_MASKx2) - \ ((C2) & RGB_REMOVE_LOW_BITS_MASK)) >> 1] inline uint16 COLOR_SUB (uint16 C1, uint16 C2) { - uint16 mC1, mC2, v = ALPHA_BITS_MASK; - - mC1 = C1 & FIRST_COLOR_MASK; - mC2 = C2 & FIRST_COLOR_MASK; - if (mC1 > mC2) v += (mC1 - mC2); - - mC1 = C1 & SECOND_COLOR_MASK; - mC2 = C2 & SECOND_COLOR_MASK; - if (mC1 > mC2) v += (mC1 - mC2); - - mC1 = C1 & THIRD_COLOR_MASK; - mC2 = C2 & THIRD_COLOR_MASK; - if (mC1 > mC2) v += (mC1 - mC2); - - return (v); + int a = (C1 & (THIRD_COLOR_MASK | FIRST_COLOR_MASK)) | ((0x20 << 0) | (0x20 << RED_SHIFT_BITS)); + int b = C2 & (THIRD_COLOR_MASK | FIRST_COLOR_MASK); + int c = a - b; + int d = c & ((0x20 << RED_SHIFT_BITS) | (0x20 << 0)); + int e = (C1 & (SECOND_COLOR_MASK)) | (0x20 << GREEN_SHIFT_BITS); + int f = C2 & (SECOND_COLOR_MASK); + int g = e - f; + int h = (g & (0x20 << GREEN_SHIFT_BITS)) | d; + int i = h >> 5; + int j = i * 0x1F; + int k = ((c & (THIRD_COLOR_MASK | FIRST_COLOR_MASK)) | (g & SECOND_COLOR_MASK)) & j; + return (uint16)k; } void S9xStartScreenRefresh (void); diff --git a/source/snes9x/tile.cpp b/source/snes9x/tile.cpp index 470bc57..524fd1d 100644 --- a/source/snes9x/tile.cpp +++ b/source/snes9x/tile.cpp @@ -21,7 +21,6 @@ static uint32 pixbit[8][16]; static uint8 hrbit_odd[256]; static uint8 hrbit_even[256]; - void S9xInitTileRenderer (void) { int i; @@ -400,6 +399,14 @@ void S9xSelectTileRenderers (int BGMode, bool8 sub, bool8 obj) if (Memory.FillRAM[0x2130] & 2) i++; } + if (IPPU.MaxBrightness != 0xf) + { + if (i == 1) + i = 7; + else if (i == 3) + i = 8; + } + } GFX.DrawTileMath = DT[i]; @@ -578,14 +585,9 @@ void S9xSelectTileConverter (int depth, bool8 hires, bool8 sub, bool8 mosaic) OFFSET_IN_LINE; \ for (l = LineCount; l > 0; l--, bp += 8 * PITCH, Offset += GFX.PPL) \ { \ - DRAW_PIXEL(0, Pix = bp[0]); \ - DRAW_PIXEL(1, Pix = bp[1]); \ - DRAW_PIXEL(2, Pix = bp[2]); \ - DRAW_PIXEL(3, Pix = bp[3]); \ - DRAW_PIXEL(4, Pix = bp[4]); \ - DRAW_PIXEL(5, Pix = bp[5]); \ - DRAW_PIXEL(6, Pix = bp[6]); \ - DRAW_PIXEL(7, Pix = bp[7]); \ + for (int x = 0; x < 8; x++) { \ + DRAW_PIXEL(x, Pix = bp[x]); \ + } \ } \ } \ else \ @@ -595,14 +597,9 @@ void S9xSelectTileConverter (int depth, bool8 hires, bool8 sub, bool8 mosaic) OFFSET_IN_LINE; \ for (l = LineCount; l > 0; l--, bp += 8 * PITCH, Offset += GFX.PPL) \ { \ - DRAW_PIXEL(0, Pix = bp[7]); \ - DRAW_PIXEL(1, Pix = bp[6]); \ - DRAW_PIXEL(2, Pix = bp[5]); \ - DRAW_PIXEL(3, Pix = bp[4]); \ - DRAW_PIXEL(4, Pix = bp[3]); \ - DRAW_PIXEL(5, Pix = bp[2]); \ - DRAW_PIXEL(6, Pix = bp[1]); \ - DRAW_PIXEL(7, Pix = bp[0]); \ + for (int x = 0; x < 8; x++) { \ + DRAW_PIXEL(x, Pix = bp[7 - x]); \ + } \ } \ } \ else \ @@ -612,14 +609,9 @@ void S9xSelectTileConverter (int depth, bool8 hires, bool8 sub, bool8 mosaic) OFFSET_IN_LINE; \ for (l = LineCount; l > 0; l--, bp -= 8 * PITCH, Offset += GFX.PPL) \ { \ - DRAW_PIXEL(0, Pix = bp[0]); \ - DRAW_PIXEL(1, Pix = bp[1]); \ - DRAW_PIXEL(2, Pix = bp[2]); \ - DRAW_PIXEL(3, Pix = bp[3]); \ - DRAW_PIXEL(4, Pix = bp[4]); \ - DRAW_PIXEL(5, Pix = bp[5]); \ - DRAW_PIXEL(6, Pix = bp[6]); \ - DRAW_PIXEL(7, Pix = bp[7]); \ + for (int x = 0; x < 8; x++) { \ + DRAW_PIXEL(x, Pix = bp[x]); \ + } \ } \ } \ else \ @@ -628,14 +620,9 @@ void S9xSelectTileConverter (int depth, bool8 hires, bool8 sub, bool8 mosaic) OFFSET_IN_LINE; \ for (l = LineCount; l > 0; l--, bp -= 8 * PITCH, Offset += GFX.PPL) \ { \ - DRAW_PIXEL(0, Pix = bp[7]); \ - DRAW_PIXEL(1, Pix = bp[6]); \ - DRAW_PIXEL(2, Pix = bp[5]); \ - DRAW_PIXEL(3, Pix = bp[4]); \ - DRAW_PIXEL(4, Pix = bp[3]); \ - DRAW_PIXEL(5, Pix = bp[2]); \ - DRAW_PIXEL(6, Pix = bp[1]); \ - DRAW_PIXEL(7, Pix = bp[0]); \ + for (int x = 0; x < 8; x++) { \ + DRAW_PIXEL(x, Pix = bp[7 - x]); \ + } \ } \ } @@ -1307,6 +1294,13 @@ static void MAKENAME(NAME1, Add_, NAME2) (ARGS) #undef MATH } +static void MAKENAME(NAME1, Add_Brightness_, NAME2) (ARGS) +{ +#define MATH(A, B, C) REGMATH(ADD_BRIGHTNESS, A, B, C) + DRAW_TILE(); +#undef MATH +} + static void MAKENAME(NAME1, AddF1_2_, NAME2) (ARGS) { #define MATH(A, B, C) MATHF1_2(ADD, A, B, C) @@ -1321,6 +1315,13 @@ static void MAKENAME(NAME1, AddS1_2_, NAME2) (ARGS) #undef MATH } +static void MAKENAME(NAME1, AddS1_2_Brightness_, NAME2) (ARGS) +{ +#define MATH(A, B, C) MATHS1_2(ADD_BRIGHTNESS, A, B, C) + DRAW_TILE(); +#undef MATH +} + static void MAKENAME(NAME1, Sub_, NAME2) (ARGS) { #define MATH(A, B, C) REGMATH(SUB, A, B, C) @@ -1342,7 +1343,7 @@ static void MAKENAME(NAME1, SubS1_2_, NAME2) (ARGS) #undef MATH } -static void (*MAKENAME(Renderers_, NAME1, NAME2)[7]) (ARGS) = +static void (*MAKENAME(Renderers_, NAME1, NAME2)[9]) (ARGS) = { MAKENAME(NAME1, _, NAME2), MAKENAME(NAME1, Add_, NAME2), @@ -1350,7 +1351,9 @@ static void (*MAKENAME(Renderers_, NAME1, NAME2)[7]) (ARGS) = MAKENAME(NAME1, AddS1_2_, NAME2), MAKENAME(NAME1, Sub_, NAME2), MAKENAME(NAME1, SubF1_2_, NAME2), - MAKENAME(NAME1, SubS1_2_, NAME2) + MAKENAME(NAME1, SubS1_2_, NAME2), + MAKENAME(NAME1, Add_Brightness_, NAME2), + MAKENAME(NAME1, AddS1_2_Brightness_, NAME2) }; #undef MAKENAME