From e2ec2ff4de6946ba7a5c16d4f8f53b0c2fb822bd Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 8 Feb 2025 23:17:43 +0100 Subject: [PATCH] optimize ogl drawer functions by avoiding excessive interger to float conversions --- src/hardware/hw_draw.c | 146 +++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 64 deletions(-) diff --git a/src/hardware/hw_draw.c b/src/hardware/hw_draw.c index 0c3fd3d73..d00ce9e02 100644 --- a/src/hardware/hw_draw.c +++ b/src/hardware/hw_draw.c @@ -62,6 +62,9 @@ static UINT8 softwaretranstogl[11] = { 0, 25, 51, 76,102,127,153,178,204,229 static UINT8 softwaretranstogl_hi[11] = { 0, 51,102,153,204,255,255,255,255,255,255}; static UINT8 softwaretranstogl_lo[11] = { 0, 12, 24, 36, 48, 60, 71, 83, 95,111,127}; +static const float FLOATBASEVIDWIDTH = (float)BASEVIDWIDTH; +static const float FLOATBASEVIDHEIGHT = (float)BASEVIDHEIGHT; + // // -----------------+ // HWR_DrawPatch : Draw a 'tile' graphic @@ -144,6 +147,9 @@ void HWR_DrawStretchyFixedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t if (alphalevel >= 10 && alphalevel < 13) return; + const float fvw = (float)vid.width; + const float fvh = (float)vid.height; + // make patch ready in hardware cache if (!colormap) HWR_GetPatch(gpatch); @@ -227,21 +233,21 @@ void HWR_DrawStretchyFixedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t Z_Free(realpatch); } // centre screen - if (fabsf((float)vid.width - (float)BASEVIDWIDTH * dupx) > 1.0E-36f) + if (fabsf(fvw - FLOATBASEVIDWIDTH * dupx) > 1.0E-36f) { if (option & V_SNAPTORIGHT) - cx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)); + cx += (fvw - (FLOATBASEVIDWIDTH * dupx)); else if (!(option & V_SNAPTOLEFT)) - cx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx))/2; + cx += (fvw - (FLOATBASEVIDWIDTH * dupx))/2; } - if (fabsf((float)vid.height - (float)BASEVIDHEIGHT * dupy) > 1.0E-36f) + if (fabsf(fvh - FLOATBASEVIDHEIGHT * dupy) > 1.0E-36f) { if ((option & (V_SPLITSCREEN|V_SNAPTOTOP)) == (V_SPLITSCREEN|V_SNAPTOTOP)) - cy += ((float)vid.height/2 - ((float)BASEVIDHEIGHT/2 * dupy)); + cy += (fvh/2 - (FLOATBASEVIDHEIGHT/2 * dupy)); else if (option & V_SNAPTOBOTTOM) - cy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)); + cy += (fvh - (FLOATBASEVIDHEIGHT * dupy)); else if (!(option & V_SNAPTOTOP)) - cy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy))/2; + cy += (fvh - (FLOATBASEVIDHEIGHT * dupy))/2; } } } @@ -258,12 +264,12 @@ void HWR_DrawStretchyFixedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t } // positions of the cx, cy, are between 0 and vid.width/vid.height now, we need them to be between -1 and 1 - cx = -1 + (cx / (vid.width/2)); - cy = 1 - (cy / (vid.height/2)); + cx = -1.0f + (cx / (vid.width / 2.0f)); + cy = 1.0f - (cy / (vid.height / 2.0f)); // fwidth and fheight are similar - fwidth /= vid.width / 2; - fheight /= vid.height / 2; + fwidth /= fvw / 2; + fheight /= fvh / 2; // set the polygon vertices to the right positions v[0].x = v[3].x = cx; @@ -333,6 +339,9 @@ void HWR_DrawCroppedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t pscal if (alphalevel >= 10 && alphalevel < 13) return; + const float fvw = (float)vid.width; + const float fvh = (float)vid.height; + // make patch ready in hardware cache HWR_GetPatch(gpatch); @@ -383,21 +392,21 @@ void HWR_DrawCroppedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t pscal Z_Free(realpatch); } // centre screen - if (fabsf((float)vid.width - (float)BASEVIDWIDTH * dupx) > 1.0E-36f) + if (fabsf(fvw - FLOATBASEVIDWIDTH * dupx) > 1.0E-36f) { if (option & V_SNAPTORIGHT) - cx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)); + cx += (fvw - (FLOATBASEVIDWIDTH * dupx)); else if (!(option & V_SNAPTOLEFT)) - cx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx))/2; + cx += (fvw - (FLOATBASEVIDWIDTH * dupx))/2; } - if (fabsf((float)vid.height - (float)BASEVIDHEIGHT * dupy) > 1.0E-36f) + if (fabsf(fvh - FLOATBASEVIDHEIGHT * dupy) > 1.0E-36f) { if ((option & (V_SPLITSCREEN|V_SNAPTOTOP)) == (V_SPLITSCREEN|V_SNAPTOTOP)) - cy += ((float)vid.height/2 - ((float)BASEVIDHEIGHT/2 * dupy)); + cy += (fvh/2 - (FLOATBASEVIDHEIGHT/2 * dupy)); else if (option & V_SNAPTOBOTTOM) - cy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)); + cy += (fvh - (FLOATBASEVIDHEIGHT * dupy)); else if (!(option & V_SNAPTOTOP)) - cy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy))/2; + cy += (fvh - (FLOATBASEVIDHEIGHT * dupy))/2; } } } @@ -429,12 +438,12 @@ void HWR_DrawCroppedPatch(GLPatch_t *gpatch, fixed_t x, fixed_t y, fixed_t pscal } // positions of the cx, cy, are between 0 and vid.width/vid.height now, we need them to be between -1 and 1 - cx = -1 + (cx / (vid.width/2)); - cy = 1 - (cy / (vid.height/2)); + cx = -1.0f + (cx / (vid.width / 2.0f)); + cy = 1.0f - (cy / (vid.height / 2.0f)); // fwidth and fheight are similar - fwidth /= vid.width / 2; - fheight /= vid.height / 2; + fwidth /= fvw / 2; + fheight /= fvh / 2; // set the polygon vertices to the right positions v[0].x = v[3].x = cx; @@ -823,6 +832,9 @@ void HWR_DrawDiag(INT32 x, INT32 y, INT32 wh, INT32 color) fy = (float)y; fw = fh = (float)wh; + const float fvw = (float)vid.width; + const float fvh = (float)vid.height; + if (!(color & V_NOSCALESTART)) { float dupx = (float)vid.dupx, dupy = (float)vid.dupy; @@ -832,20 +844,20 @@ void HWR_DrawDiag(INT32 x, INT32 y, INT32 wh, INT32 color) fw *= dupx; fh *= dupy; - if (fabsf((float)vid.width - ((float)BASEVIDWIDTH * dupx)) > 1.0E-36f) + if (fabsf(fvw - (FLOATBASEVIDWIDTH * dupx)) > 1.0E-36f) { if (color & V_SNAPTORIGHT) - fx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)); + fx += (fvw - (FLOATBASEVIDWIDTH * dupx)); else if (!(color & V_SNAPTOLEFT)) - fx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)) / 2; + fx += (fvw - (FLOATBASEVIDWIDTH * dupx)) / 2; } - if (fabsf((float)vid.height - ((float)BASEVIDHEIGHT * dupy)) > 1.0E-36f) + if (fabsf(fvh - (FLOATBASEVIDHEIGHT * dupy)) > 1.0E-36f) { // same thing here if (color & V_SNAPTOBOTTOM) - fy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)); + fy += (fvh - (FLOATBASEVIDHEIGHT * dupy)); else if (!(color & V_SNAPTOTOP)) - fy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)) / 2; + fy += (fvh - (FLOATBASEVIDHEIGHT * dupy)) / 2; } } @@ -866,16 +878,16 @@ void HWR_DrawDiag(INT32 x, INT32 y, INT32 wh, INT32 color) return; if (fx + fw > vid.width) { - fwait = fw - ((float)vid.width - fx); - fw = (float)vid.width - fx; + fwait = fw - (fvw- fx); + fw = fvw - fx; } if (fy + fh > vid.height) - fh = (float)vid.height - fy; + fh = fvh - fy; - fx = -1 + fx / (vid.width / 2); - fy = 1 - fy / (vid.height / 2); - fw = fw / (vid.width / 2); - fh = fh / (vid.height / 2); + fx = -1.0f + fx / (vid.width / 2.0f); + fy = 1.0f - fy / (vid.height / 2.0f); + fw = fw / (fvw / 2); + fh = fh / (fvh / 2); v[0].x = v[3].x = fx; v[2].x = v[1].x = fx + fw; @@ -919,6 +931,9 @@ void HWR_DrawConsoleFill(INT32 x, INT32 y, INT32 w, INT32 h, UINT32 color, INT32 fw = (float)w; fh = (float)h; + const float fvw = (float)vid.width; + const float fvh = (float)vid.height; + if (!(options & V_NOSCALESTART)) { float dupx = (float)vid.dupx, dupy = (float)vid.dupy; @@ -940,25 +955,25 @@ void HWR_DrawConsoleFill(INT32 x, INT32 y, INT32 w, INT32 h, UINT32 color, INT32 fw *= dupx; fh *= dupy; - if (fabsf((float)vid.width - ((float)BASEVIDWIDTH * dupx)) > 1.0E-36f) + if (fabsf(fvw - (FLOATBASEVIDWIDTH * dupx)) > 1.0E-36f) { if (options & V_SNAPTORIGHT) - fx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)); + fx += (fvw - (FLOATBASEVIDWIDTH * dupx)); else if (!(options & V_SNAPTOLEFT)) - fx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)) / 2; + fx += (fvw - (FLOATBASEVIDWIDTH * dupx)) / 2; } - if (fabsf((float)vid.height - ((float)BASEVIDHEIGHT * dupy)) > 1.0E-36f) + if (fabsf(fvh - (FLOATBASEVIDHEIGHT * dupy)) > 1.0E-36f) { // same thing here if (options & V_SNAPTOBOTTOM) - fy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)); + fy += (fvh - (FLOATBASEVIDHEIGHT * dupy)); else if (!(options & V_SNAPTOTOP)) - fy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)) / 2; + fy += (fvh - (FLOATBASEVIDHEIGHT * dupy)) / 2; } if (options & V_SPLITSCREEN) - fy += ((float)BASEVIDHEIGHT * dupy)/2; + fy += (FLOATBASEVIDHEIGHT * dupy)/2; if (options & V_HORZSCREEN) - fx += ((float)BASEVIDWIDTH * dupx)/2; + fx += (FLOATBASEVIDWIDTH * dupx)/2; } @@ -978,14 +993,14 @@ void HWR_DrawConsoleFill(INT32 x, INT32 y, INT32 w, INT32 h, UINT32 color, INT32 if (fw <= 0 || fh <= 0) return; if (fx + fw > vid.width) - fw = (float)vid.width - fx; + fw = fvw - fx; if (fy + fh > vid.height) - fh = (float)vid.height - fy; + fh = fvh - fy; - fx = -1 + fx / (vid.width / 2); - fy = 1 - fy / (vid.height / 2); - fw = fw / (vid.width / 2); - fh = fh / (vid.height / 2); + fx = -1.0f + fx / (vid.width / 2.0f); + fy = 1.0f - fy / (vid.height / 2.0f); + fw = fw / (fvw / 2); + fh = fh / (fvh / 2); v[0].x = v[3].x = fx; v[2].x = v[1].x = fx + fw; @@ -1030,6 +1045,9 @@ void HWR_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color) fw = (float)w; fh = (float)h; + const float fvw = (float)vid.width; + const float fvh = (float)vid.height; + if (!(color & V_NOSCALESTART)) { float dupx = (float)vid.dupx, dupy = (float)vid.dupy; @@ -1051,25 +1069,25 @@ void HWR_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color) fw *= dupx; fh *= dupy; - if (fabsf((float)vid.width - (float)BASEVIDWIDTH * dupx) > 1.0E-36f) + if (fabsf(fvw - FLOATBASEVIDWIDTH * dupx) > 1.0E-36f) { if (color & V_SNAPTORIGHT) - fx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)); + fx += (fvw - (FLOATBASEVIDWIDTH * dupx)); else if (!(color & V_SNAPTOLEFT)) - fx += ((float)vid.width - ((float)BASEVIDWIDTH * dupx)) / 2; + fx += (fvw - (FLOATBASEVIDWIDTH * dupx)) / 2; } - if (fabsf((float)vid.height - (float)BASEVIDHEIGHT * dupy) > 1.0E-36f) + if (fabsf(fvh - FLOATBASEVIDHEIGHT * dupy) > 1.0E-36f) { // same thing here if (color & V_SNAPTOBOTTOM) - fy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)); + fy += (fvh - (FLOATBASEVIDHEIGHT * dupy)); else if (!(color & V_SNAPTOTOP)) - fy += ((float)vid.height - ((float)BASEVIDHEIGHT * dupy)) / 2; + fy += (fvh - (FLOATBASEVIDHEIGHT * dupy)) / 2; } if (color & V_SPLITSCREEN) - fy += ((float)BASEVIDHEIGHT * dupy)/2; + fy += (FLOATBASEVIDHEIGHT * dupy)/2; if (color & V_HORZSCREEN) - fx += ((float)BASEVIDWIDTH * dupx)/2; + fx += (FLOATBASEVIDWIDTH * dupx)/2; } @@ -1089,14 +1107,14 @@ void HWR_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color) if (fw <= 0 || fh <= 0) return; if (fx + fw > vid.width) - fw = (float)vid.width - fx; + fw = fvw - fx; if (fy + fh > vid.height) - fh = (float)vid.height - fy; + fh = fvh - fy; - fx = -1 + fx / (vid.width / 2); - fy = 1 - fy / (vid.height / 2); - fw = fw / (vid.width / 2); - fh = fh / (vid.height / 2); + fx = -1.0f + fx / (vid.width / 2.0f); + fy = 1.0f - fy / (vid.height / 2.0f); + fw = fw / (fvw / 2); + fh = fh / (fvh / 2); v[0].x = v[3].x = fx; v[2].x = v[1].x = fx + fw; @@ -1139,7 +1157,7 @@ void HWR_DrawFill(INT32 x, INT32 y, INT32 w, INT32 h, INT32 color) #define _FILE_OFFSET_BITS 0 #endif - #include "png.h" +#include "png.h" #ifdef PNG_WRITE_SUPPORTED #define USE_PNG // PNG is only used if write is supported (see ../m_misc.c) #endif