- Correctly redid commit 34d551fe1f.

This commit is contained in:
Rachael Alexanderson 2016-12-09 08:41:59 -05:00
parent 15cf9d36a9
commit 2b5f9eebba

View file

@ -50,21 +50,28 @@ namespace swrenderer
extern FTexture *rw_pic; extern FTexture *rw_pic;
extern int wallshade; extern int wallshade;
struct WallscanSampler struct WallscanSampler
{ {
WallscanSampler() { } WallscanSampler() { }
WallscanSampler(int y1, float swal, double yrepeat, fixed_t xoffset, FTexture *texture, const BYTE*(*getcol)(FTexture *texture, int x)); WallscanSampler(int y1, float swal, double yrepeat, fixed_t xoffset, double xmagnitude, FTexture *texture, const BYTE*(*getcol)(FTexture *texture, int x));
uint32_t uv_pos; uint32_t uv_pos;
uint32_t uv_step; uint32_t uv_step;
uint32_t uv_max; uint32_t uv_max;
const BYTE *source; const BYTE *source;
const BYTE *source2;
uint32_t texturefracx;
uint32_t height; uint32_t height;
}; };
WallscanSampler::WallscanSampler(int y1, float swal, double yrepeat, fixed_t xoffset, FTexture *texture, const BYTE*(*getcol)(FTexture *texture, int x)) WallscanSampler::WallscanSampler(int y1, float swal, double yrepeat, fixed_t xoffset, double xmagnitude, FTexture *texture, const BYTE*(*getcol)(FTexture *texture, int x))
{ {
xoffset += FLOAT2FIXED(xmagnitude * 0.5);
if (!r_swtruecolor)
{
height = texture->GetHeight(); height = texture->GetHeight();
int uv_fracbits = 32 - texture->HeightBits; int uv_fracbits = 32 - texture->HeightBits;
@ -93,16 +100,122 @@ WallscanSampler::WallscanSampler(int y1, float swal, double yrepeat, fixed_t xof
} }
source = getcol(texture, xoffset >> FRACBITS); source = getcol(texture, xoffset >> FRACBITS);
source2 = nullptr;
texturefracx = 0;
}
else
{
// Normalize to 0-1 range:
double uv_stepd = swal * yrepeat;
double v = (dc_texturemid + uv_stepd * (y1 - CenterY + 0.5)) / texture->GetHeight();
v = v - floor(v);
double v_step = uv_stepd / texture->GetHeight();
if (isnan(v) || isnan(v_step)) // this should never happen, but it apparently does..
{
uv_stepd = 0.0;
v = 0.0;
v_step = 0.0;
}
// Convert to uint32:
uv_pos = (uint32_t)(v * 0x100000000LL);
uv_step = (uint32_t)(v_step * 0x100000000LL);
uv_max = 0;
// Texture mipmap and filter selection:
if (getcol != R_GetColumn)
{
source = getcol(texture, xoffset >> FRACBITS);
source2 = nullptr;
height = texture->GetHeight();
texturefracx = 0;
}
else
{
double ymagnitude = fabs(uv_stepd);
double magnitude = MAX(ymagnitude, xmagnitude);
double min_lod = -1000.0;
double lod = MAX(log2(magnitude) + r_lod_bias, min_lod);
bool magnifying = lod < 0.0f;
int mipmap_offset = 0;
int mip_width = texture->GetWidth();
int mip_height = texture->GetHeight();
if (r_mipmap && texture->Mipmapped() && mip_width > 1 && mip_height > 1)
{
uint32_t xpos = (uint32_t)((((uint64_t)xoffset) << FRACBITS) / mip_width);
int level = (int)lod;
while (level > 0 && mip_width > 1 && mip_height > 1)
{
mipmap_offset += mip_width * mip_height;
level--;
mip_width = MAX(mip_width >> 1, 1);
mip_height = MAX(mip_height >> 1, 1);
}
xoffset = (xpos >> FRACBITS) * mip_width;
}
const uint32_t *pixels = texture->GetPixelsBgra() + mipmap_offset;
bool filter_nearest = (magnifying && !r_magfilter) || (!magnifying && !r_minfilter);
if (filter_nearest)
{
int tx = (xoffset >> FRACBITS) % mip_width;
if (tx < 0)
tx += mip_width;
source = (BYTE*)(pixels + tx * mip_height);
source2 = nullptr;
height = mip_height;
texturefracx = 0;
}
else
{
xoffset -= FRACUNIT / 2;
int tx0 = (xoffset >> FRACBITS) % mip_width;
if (tx0 < 0)
tx0 += mip_width;
int tx1 = (tx0 + 1) % mip_width;
source = (BYTE*)(pixels + tx0 * mip_height);
source2 = (BYTE*)(pixels + tx1 * mip_height);
height = mip_height;
texturefracx = (xoffset >> (FRACBITS - 4)) & 15;
}
}
}
} }
// Draw a column with support for non-power-of-two ranges // Draw a column with support for non-power-of-two ranges
void wallscan_drawcol1(int x, int y1, int y2, WallscanSampler &sampler, DWORD(*draw1column)()) void wallscan_drawcol1(int x, int y1, int y2, WallscanSampler &sampler, DWORD(*draw1column)())
{ {
if (r_swtruecolor)
{
int count = y2 - y1;
dc_source = sampler.source;
dc_source2 = sampler.source2;
dc_texturefracx = sampler.texturefracx;
dc_dest = (ylookup[y1] + x) * 4 + dc_destorg;
dc_count = count;
dc_iscale = sampler.uv_step;
dc_texturefrac = sampler.uv_pos;
dc_textureheight = sampler.height;
draw1column();
uint64_t step64 = sampler.uv_step;
uint64_t pos64 = sampler.uv_pos;
sampler.uv_pos = (uint32_t)(pos64 + step64 * count);
}
else
{
if (sampler.uv_max == 0 || sampler.uv_step == 0) // power of two if (sampler.uv_max == 0 || sampler.uv_step == 0) // power of two
{ {
int count = y2 - y1; int count = y2 - y1;
dc_source = sampler.source; dc_source = sampler.source;
dc_source2 = sampler.source2;
dc_texturefracx = sampler.texturefracx;
dc_dest = (ylookup[y1] + x) + dc_destorg; dc_dest = (ylookup[y1] + x) + dc_destorg;
dc_count = count; dc_count = count;
dc_iscale = sampler.uv_step; dc_iscale = sampler.uv_step;
@ -127,6 +240,8 @@ void wallscan_drawcol1(int x, int y1, int y2, WallscanSampler &sampler, DWORD(*d
uint32_t count = MIN(left, next_uv_wrap); uint32_t count = MIN(left, next_uv_wrap);
dc_source = sampler.source; dc_source = sampler.source;
dc_source2 = sampler.source2;
dc_texturefracx = sampler.texturefracx;
dc_dest = (ylookup[y1] + x) + dc_destorg; dc_dest = (ylookup[y1] + x) + dc_destorg;
dc_count = count; dc_count = count;
dc_iscale = sampler.uv_step; dc_iscale = sampler.uv_step;
@ -141,17 +256,42 @@ void wallscan_drawcol1(int x, int y1, int y2, WallscanSampler &sampler, DWORD(*d
sampler.uv_pos = uv_pos; sampler.uv_pos = uv_pos;
} }
}
} }
// Draw four columns with support for non-power-of-two ranges // Draw four columns with support for non-power-of-two ranges
void wallscan_drawcol4(int x, int y1, int y2, WallscanSampler *sampler, void(*draw4columns)()) void wallscan_drawcol4(int x, int y1, int y2, WallscanSampler *sampler, void(*draw4columns)())
{ {
if (r_swtruecolor)
{
int count = y2 - y1;
for (int i = 0; i < 4; i++)
{
bufplce[i] = sampler[i].source;
bufplce2[i] = sampler[i].source2;
buftexturefracx[i] = sampler[i].texturefracx;
bufheight[i] = sampler[i].height;
vplce[i] = sampler[i].uv_pos;
vince[i] = sampler[i].uv_step;
uint64_t step64 = sampler[i].uv_step;
uint64_t pos64 = sampler[i].uv_pos;
sampler[i].uv_pos = (uint32_t)(pos64 + step64 * count);
}
dc_dest = (ylookup[y1] + x) * 4 + dc_destorg;
dc_count = count;
draw4columns();
}
else
{
if (sampler[0].uv_max == 0 || sampler[0].uv_step == 0) // power of two, no wrap handling needed if (sampler[0].uv_max == 0 || sampler[0].uv_step == 0) // power of two, no wrap handling needed
{ {
int count = y2 - y1; int count = y2 - y1;
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
bufplce[i] = sampler[i].source; bufplce[i] = sampler[i].source;
bufplce2[i] = sampler[i].source2;
buftexturefracx[i] = sampler[i].texturefracx;
vplce[i] = sampler[i].uv_pos; vplce[i] = sampler[i].uv_pos;
vince[i] = sampler[i].uv_step; vince[i] = sampler[i].uv_step;
@ -169,6 +309,8 @@ void wallscan_drawcol4(int x, int y1, int y2, WallscanSampler *sampler, void(*dr
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
bufplce[i] = sampler[i].source; bufplce[i] = sampler[i].source;
bufplce2[i] = sampler[i].source2;
buftexturefracx[i] = sampler[i].texturefracx;
} }
uint32_t left = y2 - y1; uint32_t left = y2 - y1;
@ -205,6 +347,7 @@ void wallscan_drawcol4(int x, int y1, int y2, WallscanSampler *sampler, void(*dr
left -= count; left -= count;
} }
} }
}
} }
typedef DWORD(*Draw1ColumnFuncPtr)(); typedef DWORD(*Draw1ColumnFuncPtr)();
@ -231,7 +374,7 @@ void wallscan_any(
DWORD(*draw1column)(); DWORD(*draw1column)();
void(*draw4columns)(); void(*draw4columns)();
setupwallscan(fracbits, draw1column, draw4columns); setupwallscan(r_swtruecolor ? FRACBITS : fracbits, draw1column, draw4columns);
bool fixed = (fixedcolormap != NULL || fixedlightlev >= 0); bool fixed = (fixedcolormap != NULL || fixedlightlev >= 0);
if (fixed) if (fixed)
@ -240,12 +383,16 @@ void wallscan_any(
palookupoffse[1] = dc_colormap; palookupoffse[1] = dc_colormap;
palookupoffse[2] = dc_colormap; palookupoffse[2] = dc_colormap;
palookupoffse[3] = dc_colormap; palookupoffse[3] = dc_colormap;
palookuplight[0] = 0;
palookuplight[1] = 0;
palookuplight[2] = 0;
palookuplight[3] = 0;
} }
if (fixedcolormap) if (fixedcolormap)
dc_colormap = fixedcolormap; R_SetColorMapLight(fixedcolormap, 0, 0);
else else
dc_colormap = basecolormap->Maps; R_SetColorMapLight(basecolormap, 0, 0);
float light = rw_light; float light = rw_light;
@ -253,6 +400,8 @@ void wallscan_any(
int aligned_x1 = clamp((x1 + 3) / 4 * 4, x1, x2); int aligned_x1 = clamp((x1 + 3) / 4 * 4, x1, x2);
int aligned_x2 = clamp(x2 / 4 * 4, x1, x2); int aligned_x2 = clamp(x2 / 4 * 4, x1, x2);
double xmagnitude = 1.0;
// First unaligned columns: // First unaligned columns:
for (int x = x1; x < aligned_x1; x++, light += rw_lightstep) for (int x = x1; x < aligned_x1; x++, light += rw_lightstep)
{ {
@ -262,9 +411,11 @@ void wallscan_any(
continue; continue;
if (!fixed) if (!fixed)
dc_colormap = basecolormap->Maps + (GETPALOOKUP(light, wallshade) << COLORMAPSHIFT); R_SetColorMapLight(basecolormap, light, wallshade);
WallscanSampler sampler(y1, swal[x], yrepeat, lwal[x] + xoffset, rw_pic, getcol); if (x + 1 < x2) xmagnitude = fabs(FIXED2DBL(lwal[x + 1]) - FIXED2DBL(lwal[x]));
WallscanSampler sampler(y1, swal[x], yrepeat, lwal[x] + xoffset, xmagnitude, rw_pic, getcol);
wallscan_drawcol1(x, y1, y2, sampler, draw1column); wallscan_drawcol1(x, y1, y2, sampler, draw1column);
} }
@ -284,7 +435,10 @@ void wallscan_any(
WallscanSampler sampler[4]; WallscanSampler sampler[4];
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
sampler[i] = WallscanSampler(y1[i], swal[x + i], yrepeat, lwal[x + i] + xoffset, rw_pic, getcol); {
if (x + i + 1 < x2) xmagnitude = fabs(FIXED2DBL(lwal[x + i + 1]) - FIXED2DBL(lwal[x + i]));
sampler[i] = WallscanSampler(y1[i], swal[x + i], yrepeat, lwal[x + i] + xoffset, xmagnitude, rw_pic, getcol);
}
// Figure out where we vertically can start and stop drawing 4 columns in one go // Figure out where we vertically can start and stop drawing 4 columns in one go
int middle_y1 = y1[0]; int middle_y1 = y1[0];
@ -297,13 +451,16 @@ void wallscan_any(
// If we got an empty column in our set we cannot draw 4 columns in one go: // If we got an empty column in our set we cannot draw 4 columns in one go:
bool empty_column_in_set = false; bool empty_column_in_set = false;
int bilinear_count = 0;
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
if (y2[i] <= y1[i]) if (y2[i] <= y1[i])
empty_column_in_set = true; empty_column_in_set = true;
if (sampler[i].source2)
bilinear_count++;
} }
if (empty_column_in_set || middle_y2 <= middle_y1) if (empty_column_in_set || middle_y2 <= middle_y1 || (bilinear_count > 0 && bilinear_count < 4))
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
@ -311,7 +468,7 @@ void wallscan_any(
continue; continue;
if (!fixed) if (!fixed)
dc_colormap = basecolormap->Maps + (GETPALOOKUP(lights[i], wallshade) << COLORMAPSHIFT); R_SetColorMapLight(basecolormap, lights[i], wallshade);
wallscan_drawcol1(x + i, y1[i], y2[i], sampler[i], draw1column); wallscan_drawcol1(x + i, y1[i], y2[i], sampler[i], draw1column);
} }
continue; continue;
@ -321,7 +478,7 @@ void wallscan_any(
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
if (!fixed) if (!fixed)
dc_colormap = basecolormap->Maps + (GETPALOOKUP(lights[i], wallshade) << COLORMAPSHIFT); R_SetColorMapLight(basecolormap, lights[i], wallshade);
if (y1[i] < middle_y1) if (y1[i] < middle_y1)
wallscan_drawcol1(x + i, y1[i], middle_y1, sampler[i], draw1column); wallscan_drawcol1(x + i, y1[i], middle_y1, sampler[i], draw1column);
@ -331,8 +488,17 @@ void wallscan_any(
if (!fixed) if (!fixed)
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{
if (r_swtruecolor)
{
palookupoffse[i] = basecolormap->Maps;
palookuplight[i] = LIGHTSCALE(lights[i], wallshade);
}
else
{ {
palookupoffse[i] = basecolormap->Maps + (GETPALOOKUP(lights[i], wallshade) << COLORMAPSHIFT); palookupoffse[i] = basecolormap->Maps + (GETPALOOKUP(lights[i], wallshade) << COLORMAPSHIFT);
palookuplight[i] = 0;
}
} }
} }
wallscan_drawcol4(x, middle_y1, middle_y2, sampler, draw4columns); wallscan_drawcol4(x, middle_y1, middle_y2, sampler, draw4columns);
@ -341,7 +507,7 @@ void wallscan_any(
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
if (!fixed) if (!fixed)
dc_colormap = basecolormap->Maps + (GETPALOOKUP(lights[i], wallshade) << COLORMAPSHIFT); R_SetColorMapLight(basecolormap, lights[i], wallshade);
if (middle_y2 < y2[i]) if (middle_y2 < y2[i])
wallscan_drawcol1(x + i, middle_y2, y2[i], sampler[i], draw1column); wallscan_drawcol1(x + i, middle_y2, y2[i], sampler[i], draw1column);
@ -357,13 +523,15 @@ void wallscan_any(
continue; continue;
if (!fixed) if (!fixed)
dc_colormap = basecolormap->Maps + (GETPALOOKUP(light, wallshade) << COLORMAPSHIFT); R_SetColorMapLight(basecolormap, light, wallshade);
WallscanSampler sampler(y1, swal[x], yrepeat, lwal[x] + xoffset, rw_pic, getcol); if (x + 1 < x2) xmagnitude = fabs(FIXED2DBL(lwal[x + 1]) - FIXED2DBL(lwal[x]));
WallscanSampler sampler(y1, swal[x], yrepeat, lwal[x] + xoffset, xmagnitude, rw_pic, getcol);
wallscan_drawcol1(x, y1, y2, sampler, draw1column); wallscan_drawcol1(x, y1, y2, sampler, draw1column);
} }
NetUpdate(); NetUpdate ();
} }
void wallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, const BYTE *(*getcol)(FTexture *tex, int x)) void wallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, const BYTE *(*getcol)(FTexture *tex, int x))
@ -376,7 +544,7 @@ void wallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lw
}); });
} }
void maskwallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, const BYTE *(*getcol)(FTexture *tex, int x) = R_GetColumn) void maskwallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, const BYTE *(*getcol)(FTexture *tex, int x))
{ {
if (!rw_pic->bMasked) // Textures that aren't masked can use the faster wallscan. if (!rw_pic->bMasked) // Textures that aren't masked can use the faster wallscan.
{ {
@ -393,7 +561,7 @@ void maskwallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t
} }
} }
void transmaskwallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, const BYTE *(*getcol)(FTexture *tex, int x) = R_GetColumn) void transmaskwallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, const BYTE *(*getcol)(FTexture *tex, int x))
{ {
static fixed_t(*tmvline1)(); static fixed_t(*tmvline1)();
static void(*tmvline4)(); static void(*tmvline4)();
@ -454,7 +622,7 @@ void wallscan_striped (int x1, int x2, short *uwal, short *dwal, float *swal, fi
wallshade = startshade; wallshade = startshade;
} }
void call_wallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, bool mask) static void call_wallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, bool mask)
{ {
if (mask) if (mask)
{ {
@ -493,6 +661,12 @@ void call_wallscan(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_
void wallscan_np2(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, double top, double bot, bool mask) void wallscan_np2(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat, double top, double bot, bool mask)
{ {
if (!r_np2)
{
call_wallscan(x1, x2, uwal, dwal, swal, lwal, yrepeat, mask);
}
else
{
short most1[MAXWIDTH], most2[MAXWIDTH], most3[MAXWIDTH]; short most1[MAXWIDTH], most2[MAXWIDTH], most3[MAXWIDTH];
short *up, *down; short *up, *down;
double texheight = rw_pic->GetHeight(); double texheight = rw_pic->GetHeight();
@ -551,9 +725,10 @@ void wallscan_np2(int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t
} }
call_wallscan(x1, x2, uwal, down, swal, lwal, yrepeat, mask); call_wallscan(x1, x2, uwal, down, swal, lwal, yrepeat, mask);
} }
}
} }
void wallscan_np2_ds(drawseg_t *ds, int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat) static void wallscan_np2_ds(drawseg_t *ds, int x1, int x2, short *uwal, short *dwal, float *swal, fixed_t *lwal, double yrepeat)
{ {
if (rw_pic->GetHeight() != 1 << rw_pic->HeightBits) if (rw_pic->GetHeight() != 1 << rw_pic->HeightBits)
{ {
@ -579,5 +754,4 @@ void wallscan_np2_ds(drawseg_t *ds, int x1, int x2, short *uwal, short *dwal, fl
} }
} }
} }