From 96f5a8adb3ac1511bef81c19deaddd913e251f1a Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 7 Jul 2025 21:47:48 +0200 Subject: [PATCH] fix for non pow2 width textures in software from doom-legacy --- src/r_data.c | 33 ++++++++++++++++++++++++++++++++- src/r_data.h | 1 - src/r_segs.cpp | 10 +++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/r_data.c b/src/r_data.c index 7ac8ffe2d..058177307 100644 --- a/src/r_data.c +++ b/src/r_data.c @@ -473,11 +473,28 @@ UINT8 *R_GetColumn(fixed_t tex, INT32 col) { UINT8 *data; - col &= texturewidthmask[tex]; data = texturecache[tex]; + const INT16 texwidth = textures[tex]->width; + + if (texturewidthmask[tex]) + col &= texturewidthmask[tex]; // set by load textures + else + { + // Odd width texture, cannot just mask. + // Sometime gets colnum = -1 or = width, even without tiling. + // Test LostCiv, Map 20, crates. + col = ( col < 0 ) ? + texwidth - (((-col - 1) % texwidth) + 1) + : col % texwidth; + } + if (!data) + { + // This must be here because cache can be freed by other operations. + // To prevent must lock individual texture cache on every draw. data = R_GenerateTexture(tex); + } return data + LONG(texturecolumnofs[tex][col]); } @@ -662,9 +679,23 @@ void R_LoadTextures(void) Z_Free(patchlump); + // determine width power of 2 +#if 1 + // [WDJ] only need to determine if exact power of 2. + k = 1; + while (k < texture->width) + k<<=1; +#else + // Largest power of 2 that fits within width. k = 1; while (k << 1 <= texture->width) k <<= 1; +#endif + if (k != texture->width) + { + // Odd width + k = 1; // make texturewidthmask = 0 + } texturewidthmask[i] = k - 1; textureheight[i] = texture->height << FRACBITS; diff --git a/src/r_data.h b/src/r_data.h index c3abab0c1..575866478 100644 --- a/src/r_data.h +++ b/src/r_data.h @@ -55,7 +55,6 @@ typedef struct UINT8 type; // TEXTURETYPE_ INT16 width, height; boolean holes; - // All the patches[patchcount] are drawn back to front into the cached texture. INT16 patchcount; texpatch_t patches[0]; diff --git a/src/r_segs.cpp b/src/r_segs.cpp index 452d1eb8e..6bc139a2d 100644 --- a/src/r_segs.cpp +++ b/src/r_segs.cpp @@ -306,8 +306,10 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 else dc->texturemid -= (textureheight[texnum])*times; + const INT16 colnum = maskedtexturecol[dc->x]; + // calculate lighting - if (maskedtexturecol[dc->x] != INT16_MAX) + if (colnum != INT16_MAX) { // Check for overflows first if (R_OverflowTest(dc)) @@ -337,7 +339,7 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 dc->iscale = 0xffffffffu / (unsigned)spryscale; // draw the texture - col = (column_t *)((UINT8 *)R_GetColumn(texnum, maskedtexturecol[dc->x]) - 3); + col = (column_t *)((UINT8 *)R_GetColumn(texnum, colnum) - 3); auto set_light_vars = [&](INT32 k) { @@ -434,7 +436,7 @@ static void R_RenderMaskedSegLoop(drawcolumndata_t* dc, drawseg_t *drawseg, INT3 dc->iscale = 0xffffffffu / (unsigned)spryscale; // draw the texture - col = (column_t *)((UINT8 *)R_GetColumn(texnum, maskedtexturecol[dc->x]) - 3); + col = (column_t *)((UINT8 *)R_GetColumn(texnum, colnum) - 3); colfunc_2s(dc, col); } @@ -1190,6 +1192,7 @@ static void R_RenderSegLoop(drawcolumndata_t* dc) INT32 bottom; INT32 i; + // Loop over width for (; rw_x < rw_stopx; rw_x++) { // mark floor / ceiling areas @@ -1563,6 +1566,7 @@ static void R_MarkSegBounds(void) INT32 top, bottom; INT16 topclip, bottomclip; + // Loop over width for (; rw_x < rw_stopx; rw_x++) { // mark floor / ceiling areas