From 1ab20814e97e977339ecf3736032af415f1c6609 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 11 Dec 2024 14:39:24 +0100 Subject: [PATCH 1/4] simplify GL_SetPalette can just copy the whole struct --- src/hardware/r_opengl/r_opengl.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index c42335d4d..d33dfd560 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1301,10 +1301,7 @@ void GL_SetPalette(RGBA_t *palette) for (i = 0; i < 256; i++) { - myPaletteData[i].s.red = palette[i].s.red; - myPaletteData[i].s.green = palette[i].s.green; - myPaletteData[i].s.blue = palette[i].s.blue; - myPaletteData[i].s.alpha = palette[i].s.alpha; + myPaletteData[i].s = palette[i].s; } GL_Flush(); From 8e273a1f19b3b3cbf5a7e1f3eb6f1417f6be9da2 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 11 Dec 2024 21:58:45 +0100 Subject: [PATCH 2/4] GL_UpdateTexture can be static --- src/hardware/hw_gl.h | 1 - src/hardware/r_opengl/r_opengl.c | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hardware/hw_gl.h b/src/hardware/hw_gl.h index aacb1b1fb..c2a88854f 100644 --- a/src/hardware/hw_gl.h +++ b/src/hardware/hw_gl.h @@ -41,7 +41,6 @@ void GL_DrawModelEx(model_t *model, INT32 frameIndex, float duration, float tics void GL_RenderSkyDome(gl_sky_t *sky); void GL_SetTexture(GLMipmap_t *pTexInfo); -void GL_UpdateTexture(GLMipmap_t *pTexInfo); void GL_DeleteTexture(GLMipmap_t *pTexInfo); void GL_Flush(void); diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index d33dfd560..061a1430e 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1562,7 +1562,7 @@ static void GL_AllocTextureBuffer(GLMipmap_t *pTexInfo) // -----------------+ // UpdateTexture : Updates texture data. // -----------------+ -void GL_UpdateTexture(GLMipmap_t *pTexInfo) +static void GL_UpdateTexture(GLMipmap_t *pTexInfo) { // Upload a texture GLuint num = pTexInfo->downloaded; @@ -2865,9 +2865,10 @@ void GL_PostImgRedraw(float points[SCREENVERTS][SCREENVERTS][2]) pglDrawArrays(GL_TRIANGLE_FAN, 0, 4); pglEnableClientState(GL_TEXTURE_COORD_ARRAY); - for(x = 0; x < SCREENVERTS-1;x ++) + + for (x = 0; x < SCREENVERTS-1;x ++) { - for(y = 0; y < SCREENVERTS-1; y++) + for (y = 0; y < SCREENVERTS-1; y++) { float stCoords[8]; float vertCoords[12]; From 3dec352245b1939b1479578e9fe4e3731446c371 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 11 Dec 2024 22:14:06 +0100 Subject: [PATCH 3/4] smol cleanup GL_SetBlend use early return since i likeit more --- src/hardware/r_opengl/r_opengl.c | 244 ++++++++++++++++--------------- 1 file changed, 126 insertions(+), 118 deletions(-) diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 061a1430e..989b4ed1e 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1417,139 +1417,148 @@ static void Clamp2D(GLenum pname) // is it faster when pixels are discarded ? void GL_SetBlend(FBITFIELD PolyFlags) { - FBITFIELD Xor; - Xor = CurrentPolyFlags^PolyFlags; - if (Xor & (PF_Blending|PF_RemoveYWrap|PF_ForceWrapX|PF_ForceWrapY|PF_Occlude|PF_NoTexture|PF_Modulated|PF_NoDepthTest|PF_Decal|PF_Invisible|PF_NoAlphaTest)) + const FBITFIELD Xor = (CurrentPolyFlags ^ PolyFlags); + + if (!(Xor & (PF_Blending|PF_RemoveYWrap|PF_ForceWrapX|PF_ForceWrapY|PF_Occlude|PF_NoTexture|PF_Modulated|PF_NoDepthTest|PF_Decal|PF_Invisible|PF_NoAlphaTest))) { - if (Xor&(PF_Blending)) // if blending mode must be changed - { - switch (PolyFlags & PF_Blending) { - case PF_Translucent & PF_Blending: - pglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // alpha = level of transparency - pglAlphaFunc(GL_NOTEQUAL, 0.0f); - break; - case PF_Masked & PF_Blending: - // Hurdler: does that mean lighting is only made by alpha src? - // it sounds ok, but not for polygonsmooth - pglBlendFunc(GL_SRC_ALPHA, GL_ZERO); // 0 alpha = holes in texture - pglAlphaFunc(GL_GREATER, 0.5f); - break; - case PF_Additive & PF_Blending: - pglBlendFunc(GL_SRC_ALPHA, GL_ONE); // src * alpha + dest - pglAlphaFunc(GL_NOTEQUAL, 0.0f); - break; - case PF_Environment & PF_Blending: - pglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - pglAlphaFunc(GL_NOTEQUAL, 0.0f); - break; - case PF_Substractive & PF_Blending: - // good for shadow - // not really but what else ? - pglBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR); - pglAlphaFunc(GL_NOTEQUAL, 0.0f); - break; - case PF_Fog & PF_Fog: - // Sryder: Fog - // multiplies input colour by input alpha, and destination colour by input colour, then adds them - pglBlendFunc(GL_SRC_ALPHA, GL_SRC_COLOR); - pglAlphaFunc(GL_ALWAYS, 0.0f); // Don't discard zero alpha fragments - break; - default : // must be 0, otherwise it's an error - // No blending - pglBlendFunc(GL_ONE, GL_ZERO); // the same as no blending - pglAlphaFunc(GL_GREATER, 0.5f); - break; - } - } - if (Xor & PF_NoAlphaTest) - { - if (PolyFlags & PF_NoAlphaTest) - pglDisable(GL_ALPHA_TEST); - else - pglEnable(GL_ALPHA_TEST); // discard 0 alpha pixels (holes in texture) - } + CurrentPolyFlags = PolyFlags; + return; + } - if (Xor & PF_Decal) + if (Xor & PF_Blending) // if blending mode must be changed + { + switch (PolyFlags & PF_Blending) { - if (PolyFlags & PF_Decal) - pglEnable(GL_POLYGON_OFFSET_FILL); - else - pglDisable(GL_POLYGON_OFFSET_FILL); - } - if (Xor&PF_NoDepthTest) - { - if (PolyFlags & PF_NoDepthTest) - pglDepthFunc(GL_ALWAYS); //pglDisable(GL_DEPTH_TEST); - else - pglDepthFunc(GL_LEQUAL); //pglEnable(GL_DEPTH_TEST); + case (PF_Translucent & PF_Blending): + pglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // alpha = level of transparency + pglAlphaFunc(GL_NOTEQUAL, 0.0f); + break; + case (PF_Masked & PF_Blending): + // Hurdler: does that mean lighting is only made by alpha src? + // it sounds ok, but not for polygonsmooth + pglBlendFunc(GL_SRC_ALPHA, GL_ZERO); // 0 alpha = holes in texture + pglAlphaFunc(GL_GREATER, 0.5f); + break; + case (PF_Additive & PF_Blending): + pglBlendFunc(GL_SRC_ALPHA, GL_ONE); // src * alpha + dest + pglAlphaFunc(GL_NOTEQUAL, 0.0f); + break; + case (PF_Environment & PF_Blending): + pglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + pglAlphaFunc(GL_NOTEQUAL, 0.0f); + break; + case (PF_Substractive & PF_Blending): + // good for shadow + // not really but what else ? + pglBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR); + pglAlphaFunc(GL_NOTEQUAL, 0.0f); + break; + case (PF_Fog & PF_Fog): + // Sryder: Fog + // multiplies input colour by input alpha, and destination colour by input colour, then adds them + pglBlendFunc(GL_SRC_ALPHA, GL_SRC_COLOR); + pglAlphaFunc(GL_ALWAYS, 0.0f); // Don't discard zero alpha fragments + break; + default : // must be 0, otherwise it's an error + // No blending + pglBlendFunc(GL_ONE, GL_ZERO); // the same as no blending + pglAlphaFunc(GL_GREATER, 0.5f); + break; } + } - if (Xor&PF_RemoveYWrap) - { - if (PolyFlags & PF_RemoveYWrap) - Clamp2D(GL_TEXTURE_WRAP_T); - } + if (Xor & PF_NoAlphaTest) + { + if (PolyFlags & PF_NoAlphaTest) + pglDisable(GL_ALPHA_TEST); + else + pglEnable(GL_ALPHA_TEST); // discard 0 alpha pixels (holes in texture) + } - if (Xor&PF_ForceWrapX) - { - if (PolyFlags & PF_ForceWrapX) - pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - } + if (Xor & PF_Decal) + { + if (PolyFlags & PF_Decal) + pglEnable(GL_POLYGON_OFFSET_FILL); + else + pglDisable(GL_POLYGON_OFFSET_FILL); + } + if (Xor & PF_NoDepthTest) + { + if (PolyFlags & PF_NoDepthTest) + pglDepthFunc(GL_ALWAYS); //pglDisable(GL_DEPTH_TEST); + else + pglDepthFunc(GL_LEQUAL); //pglEnable(GL_DEPTH_TEST); + } - if (Xor&PF_ForceWrapY) - { - if (PolyFlags & PF_ForceWrapY) - pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - } + if (Xor & PF_RemoveYWrap) + { + if (PolyFlags & PF_RemoveYWrap) + Clamp2D(GL_TEXTURE_WRAP_T); + } - if (Xor&PF_Modulated) - { + if (Xor & PF_ForceWrapX) + { + if (PolyFlags & PF_ForceWrapX) + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + } + + if (Xor & PF_ForceWrapY) + { + if (PolyFlags & PF_ForceWrapY) + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + } + + if (Xor & PF_Modulated) + { #if defined (__unix__) || defined (UNIXCOMMON) - if (oglflags & GLF_NOTEXENV) - { - if (!(PolyFlags & PF_Modulated)) - pglColor4ubv(white); - } - else + if (oglflags & GLF_NOTEXENV) + { + if (!(PolyFlags & PF_Modulated)) + pglColor4ubv(white); + } + else #endif - // mix texture colour with Surface->PolyColor - if (PolyFlags & PF_Modulated) - pglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - // colour from texture is unchanged before blending - else - pglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - } - - if (Xor & PF_Occlude) // depth test but (no) depth write - { - if (PolyFlags&PF_Occlude) - pglDepthMask(1); - else - pglDepthMask(0); - } - ////Hurdler: not used if we don't define POLYSKY - if (Xor & PF_Invisible) - { - if (PolyFlags&PF_Invisible) - pglBlendFunc(GL_ZERO, GL_ONE); // transparent blending - else - { // big hack: (TODO: manage that better) - // we test only for PF_Masked because PF_Invisible is only used - // (for now) with it (yeah, that's crappy, sorry) - if ((PolyFlags&PF_Blending)==PF_Masked) - pglBlendFunc(GL_SRC_ALPHA, GL_ZERO); - } - } - if (PolyFlags & PF_NoTexture) - GL_SetNoTexture(); + // mix texture colour with Surface->PolyColor + if (PolyFlags & PF_Modulated) + pglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + // colour from texture is unchanged before blending + else + pglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); } + + if (Xor & PF_Occlude) // depth test but (no) depth write + { + if (PolyFlags & PF_Occlude) + pglDepthMask(1); + else + pglDepthMask(0); + } + + ////Hurdler: not used if we don't define POLYSKY + if (Xor & PF_Invisible) + { + if (PolyFlags&PF_Invisible) + pglBlendFunc(GL_ZERO, GL_ONE); // transparent blending + else + { // big hack: (TODO: manage that better) + // we test only for PF_Masked because PF_Invisible is only used + // (for now) with it (yeah, that's crappy, sorry) + if ((PolyFlags & PF_Blending) == PF_Masked) + pglBlendFunc(GL_SRC_ALPHA, GL_ZERO); + } + } + + if (PolyFlags & PF_NoTexture) + GL_SetNoTexture(); + CurrentPolyFlags = PolyFlags; } static void GL_AllocTextureBuffer(GLMipmap_t *pTexInfo) { size_t size = pTexInfo->width * pTexInfo->height; + if (size > textureBufferSize) { textureBuffer = realloc(textureBuffer, size * sizeof(RGBA_t)); @@ -2865,10 +2874,9 @@ void GL_PostImgRedraw(float points[SCREENVERTS][SCREENVERTS][2]) pglDrawArrays(GL_TRIANGLE_FAN, 0, 4); pglEnableClientState(GL_TEXTURE_COORD_ARRAY); - - for (x = 0; x < SCREENVERTS-1;x ++) + for(x = 0; x < SCREENVERTS-1;x ++) { - for (y = 0; y < SCREENVERTS-1; y++) + for(y = 0; y < SCREENVERTS-1; y++) { float stCoords[8]; float vertCoords[12]; From 99e4870a524272f16218e33d6bd69ef90580e9ed Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 12 Dec 2024 02:31:44 +0100 Subject: [PATCH 4/4] check for the flag first --- src/hardware/r_opengl/r_opengl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 989b4ed1e..077bdbbbe 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1613,7 +1613,7 @@ static void GL_UpdateTexture(GLMipmap_t *pTexInfo) { int idx = (w*j+i); - if ((*pImgData == HWR_PATCHES_CHROMAKEY_COLORINDEX) && chromakeyed) + if (chromakeyed && (*pImgData == HWR_PATCHES_CHROMAKEY_COLORINDEX)) { tex[idx].s.red = 0; tex[idx].s.green = 0;