From 4e2f1bcf2b1475e3cf2c5719d1ddd2d639990ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20Alh=C3=A4ll?= Date: Thu, 5 Jun 2025 19:23:20 +0200 Subject: [PATCH 1/4] Fix invalid formats being passed to interal texture formats The effects that this had on the engine is unknown, but the reference pages makes in very clear that the old behavior is wrong. --- src/hardware/r_opengl/r_opengl.c | 15 ++++++--------- src/sdl/ogl_sdl.c | 2 ++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index a8e9d6350..1fc319f0e 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -689,7 +689,7 @@ typedef enum gluniform_leveltime, gluniform_scr_resolution, - + gluniform_max, } gluniform_t; @@ -1803,7 +1803,7 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) case GL_TEXFMT_ALPHA_INTENSITY_88: GL_AllocTextureBuffer(pTexInfo); ptex = tex = textureBuffer; - texformat = GL_LUMINANCE_ALPHA; + texformat = GL_RGBA; for (idx = 0, j = 0; j < h; j++) { @@ -1821,7 +1821,7 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) case GL_TEXFMT_ALPHA_8: // Used for fade masks GL_AllocTextureBuffer(pTexInfo); ptex = tex = textureBuffer; - texformat = GL_ALPHA; + texformat = GL_RGBA; memset(&tex->s, 255, sizeof(byteColor_t)*w*h); // 255 because the fade mask is modulated with the screen texture, so alpha affects it while the colours don't @@ -1857,8 +1857,6 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) if (MipMap && !transparent) // No mipmaps on transparent stuff { - int maxlod = (texformat == GL_LUMINANCE_ALPHA || texformat == GL_ALPHA) ? 4 : 5; - pglTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); if (update) @@ -1868,7 +1866,7 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) // Control the mipmap level of detail pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0); - pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, maxlod); + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 5); } else { @@ -1892,7 +1890,6 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropic_filter); } - // -----------------+ // SetTexture : The mipmap becomes the current texture source // -----------------+ @@ -2735,7 +2732,7 @@ void GL_DrawModelEx(model_t *model, INT32 frameIndex, float duration, float tics poly.green = byte2float[Surface->PolyColor.s.green]; poly.blue = byte2float[Surface->PolyColor.s.blue]; poly.alpha = byte2float[Surface->PolyColor.s.alpha]; - + pglColor4ubv((GLubyte*)&Surface->PolyColor.s); GL_SetBlend(((poly.alpha < 1) ? Surface->PolyFlags : (PF_Masked|PF_Occlude))|PF_Modulated); @@ -2749,7 +2746,7 @@ void GL_DrawModelEx(model_t *model, INT32 frameIndex, float duration, float tics fade.green = byte2float[Surface->FadeColor.s.green]; fade.blue = byte2float[Surface->FadeColor.s.blue]; fade.alpha = byte2float[Surface->FadeColor.s.alpha]; - + if (Surface->LightTableId && Surface->LightTableId != lt_downloaded) { pglActiveTexture(GL_TEXTURE2); diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index c8701506e..93609685c 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -237,6 +237,8 @@ boolean OglSdlSurface(INT32 w, INT32 h) first_init = true; + HWR_Startup(); + return true; } From 22f50402fec4825649da01769d09cb7d2b0bbc2a Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 5 Jul 2025 15:51:15 +0200 Subject: [PATCH 2/4] remove this --- src/sdl/ogl_sdl.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index 93609685c..c8701506e 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -237,8 +237,6 @@ boolean OglSdlSurface(INT32 w, INT32 h) first_init = true; - HWR_Startup(); - return true; } From 2aa53242252867d7a94892db8f0c9e1f17cc5944 Mon Sep 17 00:00:00 2001 From: BlueStaggo Date: Mon, 31 Mar 2025 18:04:53 +0000 Subject: [PATCH 3/4] Manual mipmapping + Texture padding --- src/hardware/r_opengl/r_opengl.c | 127 +++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 1fc319f0e..12d46942d 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1729,6 +1729,114 @@ static void GL_AllocTextureBuffer(GLMipmap_t *pTexInfo) } } +#define PADDING_CHECK(offset, alphaCheck) { from = to + (offset); if ((alphaCheck)) from = NULL; else goto foundFrom; } + +static void PadRGBABitmap(RGBA_t *tex, UINT16 w, UINT16 h) +{ + INT32 i; + boolean notLeft, notRight, notTop, notBottom; + RGBA_t *to = tex - 1, *from; + + for (i = 0; i < w * h; i++) + { + to++; + if (to->rgba != 0) + continue; + from = NULL; + + notLeft = i % w != 0; + notRight = i % w != w - 1; + notTop = i / w != 0; + notBottom = i / w != h - 1; + + if (notRight) PADDING_CHECK(1, from->s.alpha == 0) // Check +X + if (notBottom) PADDING_CHECK(w, from->s.alpha == 0) // Check +Y + if (notLeft) PADDING_CHECK(-1, from->s.alpha == 0) // Check -X + if (notTop) PADDING_CHECK(-w, from->s.alpha == 0) // Check -Y + if (notRight && notBottom) PADDING_CHECK(1 + w, from->s.alpha == 0) // Check +X+Y + if (notLeft && notBottom) PADDING_CHECK(-1 + w, from->s.alpha == 0) // Check -X+Y + if (notLeft && notTop) PADDING_CHECK(-1 - w, from->s.alpha == 0) // Check -X-Y + if (notRight && notTop) PADDING_CHECK(1 - w, from->s.alpha == 0) // Check +X-Y + +foundFrom: + if (from != NULL) + { + *to = *from; + to->s.alpha = 0; + } + } +} + +#undef PADDING_CHECK + +static void GenerateMipmaps(INT32 w, INT32 h, RGBA_t *tex, INT32 maxLOD) +{ + if (tex == NULL) + { + GL_MSG_Warning("GenerateMipmaps: attempted to generate mipmaps without texture data"); + return; + } + + RGBA_t samplePoint[4]; + boolean padTexture; + INT32 pointsSampled = 0; + INT32 m, j, i, p; + UINT16 sumR, sumG, sumB, sumA; + + for (m = 0; m < maxLOD; m++) + { + if (w <= 1 || h <= 1) + return; + + padTexture = false; + + for (j = 0; j < h / 2; j++) + { + for (i = 0; i < w / 2; i++) + { + samplePoint[0] = tex[w*j*2 + i*2]; + samplePoint[1] = tex[w*j*2 + i*2+1]; + samplePoint[2] = tex[w*(j*2+1) + i*2]; + samplePoint[3] = tex[w*(j*2+1) + i*2+1]; + + pointsSampled = sumR = sumG = sumB = sumA = 0; + + for (p = 0; p < 4; p++) + { + if (samplePoint[p].s.alpha == 0) + continue; + sumR += samplePoint[p].s.red; + sumG += samplePoint[p].s.green; + sumB += samplePoint[p].s.blue; + sumA += samplePoint[p].s.alpha; + pointsSampled++; + } + + if (pointsSampled > 0) + { + tex[(w/2)*j+i].s.red = sumR / pointsSampled; + tex[(w/2)*j+i].s.green = sumG / pointsSampled; + tex[(w/2)*j+i].s.blue = sumB / pointsSampled; + tex[(w/2)*j+i].s.alpha = sumA / pointsSampled; + } + else + { + tex[(w/2)*j+i].rgba = 0; + padTexture = true; + } + } + } + + w /= 2; + h /= 2; + + if (padTexture) + PadRGBABitmap(tex, w, h); + + pglTexSubImage2D(GL_TEXTURE_2D, m + 1, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, tex); + } +} + // -----------------+ // UpdateTexture : Updates texture data. // -----------------+ @@ -1737,6 +1845,7 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) // Upload a texture GLuint num = pTexInfo->downloaded; boolean update = true; + const boolean applyPadding = mag_filter == GL_LINEAR || min_filter == GL_LINEAR; INT32 w = pTexInfo->width, h = pTexInfo->height; INT32 i, j; @@ -1795,10 +1904,27 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) pImgData++; } } + + if (applyPadding) + PadRGBABitmap(tex, w, h); + break; case GL_TEXFMT_RGBA: // Directly upload the texture data without any kind of conversion. ptex = pImgData; + + // However, it does need to be copied to a buffer for generating mipmaps and padding + if (MipMap || applyPadding) + { + AllocTextureBuffer(pTexInfo); + tex = textureBuffer; + memcpy(tex, ptex, w * h * 4); + ptex = tex; + + if (applyPadding) + PadRGBABitmap(tex, w, h); + } + break; case GL_TEXFMT_ALPHA_INTENSITY_88: GL_AllocTextureBuffer(pTexInfo); @@ -1867,6 +1993,7 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) // Control the mipmap level of detail pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0); pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 5); + GenerateMipmaps(w, h, tex, 5); } else { From fe5a0b7c64c6dedbcb19310224f686bd5d0f7a65 Mon Sep 17 00:00:00 2001 From: Alug Date: Sat, 5 Jul 2025 15:36:27 +0200 Subject: [PATCH 4/4] GL_ --- 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 12d46942d..497b1b08a 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -1916,7 +1916,7 @@ void GL_UpdateTexture(GLMipmap_t *pTexInfo) // However, it does need to be copied to a buffer for generating mipmaps and padding if (MipMap || applyPadding) { - AllocTextureBuffer(pTexInfo); + GL_AllocTextureBuffer(pTexInfo); tex = textureBuffer; memcpy(tex, ptex, w * h * 4); ptex = tex;