Some video SW clean up and bug fixes. Did a hacky fix for bad light data. Add fog rendering. Add some offsets to make rasterized pixel locations and texture coordinates more accurate.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5361 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
donkopunchstania 2010-04-14 03:27:45 +00:00
parent bfe6e92c15
commit c2ac9be9b2
14 changed files with 179 additions and 78 deletions

View File

@ -339,6 +339,9 @@ namespace Clipper
Vertices[0] = lineV0; Vertices[0] = lineV0;
Vertices[1] = lineV1; Vertices[1] = lineV1;
// point to a valid vertex to store to when clipping
Vertices[2] = &ClippedVertices[17];
ClipLine(indices); ClipLine(indices);
if(indices[0] != SKIP_FLAG) if(indices[0] != SKIP_FLAG)

View File

@ -105,7 +105,7 @@ void DumpActiveTextures()
u32 texmap = bpmem.tevindref.getTexMap(stageNum); u32 texmap = bpmem.tevindref.getTexMap(stageNum);
s32 maxLod = GetMaxTextureLod(texmap); s32 maxLod = GetMaxTextureLod(texmap);
for (s32 mip = 0; mip < maxLod; ++mip) for (s32 mip = 0; mip <= maxLod; ++mip)
{ {
SaveTexture(StringFromFormat("%star%i_ind%i_map%i_mip%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX), stats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), texmap, mip); SaveTexture(StringFromFormat("%star%i_ind%i_map%i_mip%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX), stats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), texmap, mip);
} }
@ -120,7 +120,7 @@ void DumpActiveTextures()
int texmap = order.getTexMap(stageOdd); int texmap = order.getTexMap(stageOdd);
s32 maxLod = GetMaxTextureLod(texmap); s32 maxLod = GetMaxTextureLod(texmap);
for (s32 mip = 0; mip < maxLod; ++mip) for (s32 mip = 0; mip <= maxLod; ++mip)
{ {
SaveTexture(StringFromFormat("%star%i_stage%i_map%i_mip%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX), stats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), texmap, mip); SaveTexture(StringFromFormat("%star%i_stage%i_map%i_mip%i.tga", File::GetUserPath(D_DUMPTEXTURES_IDX), stats.thisFrame.numDrawnObjects, stageNum, texmap, mip).c_str(), texmap, mip);
} }

View File

@ -85,13 +85,13 @@ namespace HwRasterizer
void DrawColorVertex(OutputVertexData *v) void DrawColorVertex(OutputVertexData *v)
{ {
glColor3ub(v->color[0][0], v->color[0][1], v->color[0][2]); glColor3ub(v->color[0][0], v->color[0][1], v->color[0][2]);
glVertex3f(v->screenPosition[0] / efbHalfWidth - 1.0f, 1.0f - v->screenPosition[1] / efbHalfHeight, v->screenPosition[2]); glVertex3f(v->screenPosition.x / efbHalfWidth - 1.0f, 1.0f - v->screenPosition.y / efbHalfHeight, v->screenPosition.z);
} }
void DrawTextureVertex(OutputVertexData *v) void DrawTextureVertex(OutputVertexData *v)
{ {
glTexCoord2f(v->texCoords[0][0] * texWidth, v->texCoords[0][1] * texHeight); glTexCoord2f(v->texCoords[0].x * texWidth, v->texCoords[0].y * texHeight);
glVertex3f(v->screenPosition[0] / efbHalfWidth - 1.0f, 1.0f - v->screenPosition[1] / efbHalfHeight, v->screenPosition[2]); glVertex3f(v->screenPosition.x / efbHalfWidth - 1.0f, 1.0f - v->screenPosition.y / efbHalfHeight, v->screenPosition.z);
} }
void DrawTriangleFrontFace(OutputVertexData *v0, OutputVertexData *v1, OutputVertexData *v2) void DrawTriangleFrontFace(OutputVertexData *v0, OutputVertexData *v1, OutputVertexData *v2)

View File

@ -49,6 +49,11 @@ Slope WSlope;
Slope ColorSlopes[2][4]; Slope ColorSlopes[2][4];
Slope TexSlopes[8][3]; Slope TexSlopes[8][3];
s32 vertex0X;
s32 vertex0Y;
float vertexOffsetX;
float vertexOffsetY;
s32 scissorLeft = 0; s32 scissorLeft = 0;
s32 scissorTop = 0; s32 scissorTop = 0;
s32 scissorRight = 0; s32 scissorRight = 0;
@ -108,7 +113,10 @@ inline void Draw(s32 x, s32 y, s32 xi, s32 yi)
{ {
INCSTAT(stats.thisFrame.rasterizedPixels); INCSTAT(stats.thisFrame.rasterizedPixels);
float zFloat = 1.0f + ZSlope.GetValue(x, y); float dx = vertexOffsetX + (float)(x - vertex0X);
float dy = vertexOffsetY + (float)(y - vertex0Y);
float zFloat = 1.0f + ZSlope.GetValue(dx, dy);
if (zFloat < 0.0f || zFloat > 1.0f) if (zFloat < 0.0f || zFloat > 1.0f)
return; return;
@ -133,7 +141,7 @@ inline void Draw(s32 x, s32 y, s32 xi, s32 yi)
for (unsigned int i = 0; i < bpmem.genMode.numcolchans; i++) for (unsigned int i = 0; i < bpmem.genMode.numcolchans; i++)
{ {
for(int comp = 0; comp < 4; comp++) for(int comp = 0; comp < 4; comp++)
tev.Color[i][comp] = (u8)ColorSlopes[i][comp].GetValue(x, y); tev.Color[i][comp] = (u8)ColorSlopes[i][comp].GetValue(dx, dy);
} }
// tex coords // tex coords
@ -159,7 +167,19 @@ inline void Draw(s32 x, s32 y, s32 xi, s32 yi)
tev.Draw(); tev.Draw();
} }
void InitSlope(Slope *slope, float f1, float f2, float f3, float DX31, float DX12, float DY12, float DY31, float X1, float Y1) void InitTriangle(float X1, float Y1, s32 xi, s32 yi)
{
vertex0X = xi;
vertex0Y = yi;
// adjust a little less than 0.5
const float adjust = 0.495f;
vertexOffsetX = ((float)xi - X1) + adjust;
vertexOffsetY = ((float)yi - Y1) + adjust;
}
void InitSlope(Slope *slope, float f1, float f2, float f3, float DX31, float DX12, float DY12, float DY31)
{ {
float DF31 = f3 - f1; float DF31 = f3 - f1;
float DF21 = f2 - f1; float DF21 = f2 - f1;
@ -169,8 +189,6 @@ void InitSlope(Slope *slope, float f1, float f2, float f3, float DX31, float DX1
slope->dfdx = -a / c; slope->dfdx = -a / c;
slope->dfdy = -b / c; slope->dfdy = -b / c;
slope->f0 = f1; slope->f0 = f1;
slope->x0 = X1;
slope->y0 = Y1;
} }
inline void CalculateLOD(s32 &lod, bool &linear, u32 texmap, u32 texcoord) inline void CalculateLOD(s32 &lod, bool &linear, u32 texmap, u32 texcoord)
@ -210,7 +228,7 @@ inline void CalculateLOD(s32 &lod, bool &linear, u32 texmap, u32 texcoord)
bias >>= 1; bias >>= 1;
lod += bias; lod += bias;
linear = (lod >= 0 && (tm0.min_filter & 4) || lod < 0 && tm0.mag_filter); linear = (lod > 0 && (tm0.min_filter & 4) || lod <= 0 && tm0.mag_filter);
// order of checks matters // order of checks matters
// should be: // should be:
@ -227,10 +245,10 @@ void BuildBlock(s32 blockX, s32 blockY)
{ {
RasterBlockPixel& pixel = rasterBlock.Pixel[xi][yi]; RasterBlockPixel& pixel = rasterBlock.Pixel[xi][yi];
s32 x = xi + blockX; float dx = vertexOffsetX + (float)(xi + blockX - vertex0X);
s32 y = yi + blockY; float dy = vertexOffsetY + (float)(yi + blockY - vertex0Y);
float invW = 1.0f / WSlope.GetValue(x, y); float invW = 1.0f / WSlope.GetValue(dx, dy);
pixel.InvW = invW; pixel.InvW = invW;
// tex coords // tex coords
@ -239,14 +257,14 @@ void BuildBlock(s32 blockX, s32 blockY)
float projection; float projection;
if (xfregs.texMtxInfo[i].projection) if (xfregs.texMtxInfo[i].projection)
{ {
float q = TexSlopes[i][2].GetValue(x, y) * invW; float q = TexSlopes[i][2].GetValue(dx, dy) * invW;
projection = invW / q; projection = invW / q;
} }
else else
projection = invW; projection = invW;
pixel.Uv[i][0] = TexSlopes[i][0].GetValue(x, y) * projection; pixel.Uv[i][0] = TexSlopes[i][0].GetValue(dx, dy) * projection;
pixel.Uv[i][1] = TexSlopes[i][1].GetValue(x, y) * projection; pixel.Uv[i][1] = TexSlopes[i][1].GetValue(dx, dy) * projection;
} }
} }
} }
@ -288,14 +306,15 @@ void DrawTriangleFrontFace(OutputVertexData *v0, OutputVertexData *v1, OutputVer
// adapted from http://www.devmaster.net/forums/showthread.php?t=1884 // adapted from http://www.devmaster.net/forums/showthread.php?t=1884
// 28.4 fixed-pou32 coordinates. rounded to nearest // 28.4 fixed-pou32 coordinates. rounded to nearest and adjusted to match hardware output
const s32 Y1 = iround(16.0f * v0->screenPosition[1]); // could also take floor and adjust -8
const s32 Y2 = iround(16.0f * v1->screenPosition[1]); const s32 Y1 = iround(16.0f * v0->screenPosition[1]) - 9;
const s32 Y3 = iround(16.0f * v2->screenPosition[1]); const s32 Y2 = iround(16.0f * v1->screenPosition[1]) - 9;
const s32 Y3 = iround(16.0f * v2->screenPosition[1]) - 9;
const s32 X1 = iround(16.0f * v0->screenPosition[0]); const s32 X1 = iround(16.0f * v0->screenPosition[0]) - 9;
const s32 X2 = iround(16.0f * v1->screenPosition[0]); const s32 X2 = iround(16.0f * v1->screenPosition[0]) - 9;
const s32 X3 = iround(16.0f * v2->screenPosition[0]); const s32 X3 = iround(16.0f * v2->screenPosition[0]) - 9;
// Deltas // Deltas
const s32 DX12 = X1 - X2; const s32 DX12 = X1 - X2;
@ -331,28 +350,30 @@ void DrawTriangleFrontFace(OutputVertexData *v0, OutputVertexData *v1, OutputVer
return; return;
// Setup slopes // Setup slopes
float fltx1 = v0->screenPosition[0]; float fltx1 = v0->screenPosition.x;
float flty1 = v0->screenPosition[1]; float flty1 = v0->screenPosition.y;
float fltdx31 = v2->screenPosition[0] - fltx1; float fltdx31 = v2->screenPosition.x - fltx1;
float fltdx12 = fltx1 - v1->screenPosition[0]; float fltdx12 = fltx1 - v1->screenPosition.x;
float fltdy12 = flty1 - v1->screenPosition[1]; float fltdy12 = flty1 - v1->screenPosition.y;
float fltdy31 = v2->screenPosition[1] - flty1; float fltdy31 = v2->screenPosition.y - flty1;
InitTriangle(fltx1, flty1, (X1 + 0xF) >> 4, (Y1 + 0xF) >> 4);
float w[3] = { 1.0f / v0->projectedPosition.w, 1.0f / v1->projectedPosition.w, 1.0f / v2->projectedPosition.w }; float w[3] = { 1.0f / v0->projectedPosition.w, 1.0f / v1->projectedPosition.w, 1.0f / v2->projectedPosition.w };
InitSlope(&WSlope, w[0], w[1], w[2], fltdx31, fltdx12, fltdy12, fltdy31, fltx1, flty1); InitSlope(&WSlope, w[0], w[1], w[2], fltdx31, fltdx12, fltdy12, fltdy31);
InitSlope(&ZSlope, v0->screenPosition[2], v1->screenPosition[2], v2->screenPosition[2], fltdx31, fltdx12, fltdy12, fltdy31, fltx1, flty1); InitSlope(&ZSlope, v0->screenPosition[2], v1->screenPosition[2], v2->screenPosition[2], fltdx31, fltdx12, fltdy12, fltdy31);
for(unsigned int i = 0; i < bpmem.genMode.numcolchans; i++) for(unsigned int i = 0; i < bpmem.genMode.numcolchans; i++)
{ {
for(int comp = 0; comp < 4; comp++) for(int comp = 0; comp < 4; comp++)
InitSlope(&ColorSlopes[i][comp], v0->color[i][comp], v1->color[i][comp], v2->color[i][comp], fltdx31, fltdx12, fltdy12, fltdy31, fltx1, flty1); InitSlope(&ColorSlopes[i][comp], v0->color[i][comp], v1->color[i][comp], v2->color[i][comp], fltdx31, fltdx12, fltdy12, fltdy31);
} }
for(unsigned int i = 0; i < bpmem.genMode.numtexgens; i++) for(unsigned int i = 0; i < bpmem.genMode.numtexgens; i++)
{ {
for(int comp = 0; comp < 3; comp++) for(int comp = 0; comp < 3; comp++)
InitSlope(&TexSlopes[i][comp], v0->texCoords[i][comp] * w[0], v1->texCoords[i][comp] * w[1], v2->texCoords[i][comp] * w[2], fltdx31, fltdx12, fltdy12, fltdy31, fltx1, flty1); InitSlope(&TexSlopes[i][comp], v0->texCoords[i][comp] * w[0], v1->texCoords[i][comp] * w[1], v2->texCoords[i][comp] * w[2], fltdx31, fltdx12, fltdy12, fltdy31);
} }
// Start in corner of 8x8 block // Start in corner of 8x8 block

View File

@ -35,9 +35,8 @@ namespace Rasterizer
float dfdx; float dfdx;
float dfdy; float dfdy;
float f0; float f0;
float x0;
float y0; float GetValue(float dx, float dy) { return f0 + (dfdx * dx) + (dfdy * dy); }
float GetValue(s32 x, s32 y) { return f0 + (dfdx * (x - x0)) + (dfdy * (y - y0)); }
}; };
struct RasterBlockPixel struct RasterBlockPixel

View File

@ -705,6 +705,57 @@ void Tev::Draw()
Position[2] = ztex & 0x00ffffff; Position[2] = ztex & 0x00ffffff;
} }
// fog
if (bpmem.fog.c_proj_fsel.fsel)
{
float ze;
if (bpmem.fog.c_proj_fsel.proj == 0)
{
// perspective
// ze = A/(B - Zs)
s32 denom = bpmem.fog.b_magnitude - (Position[2] >> bpmem.fog.b_shift);
ze = bpmem.fog.a.GetA() / (float)denom;
}
else
{
// orthographic
// ze = a*Zs
ze = bpmem.fog.a.GetA() / (float)Position[2];
}
ze = (ze * (float)0xffffff) - bpmem.fog.c_proj_fsel.GetC();
// clamp 0 to 1
float fog = (ze<0.0f) ? 0.0f : ((ze>1.0f) ? 1.0f : ze);
switch (bpmem.fog.c_proj_fsel.fsel)
{
case 4: // exp
fog = 1.0f - pow(2.0f, -8.0f * fog);
break;
case 5: // exp2
fog = 1.0f - pow(2.0f, -8.0f * fog * fog);
break;
case 6: // backward exp
fog = 1.0f - fog;
fog = 1.0f - pow(2.0f, -8.0f * fog);
break;
case 7: // backward exp2
fog = 1.0f - fog;
fog = 1.0f - pow(2.0f, -8.0f * fog * fog);
break;
}
// lerp from output to fog color
u32 fogInt = (u32)(fog * 256);
u32 invFog = 256 - fogInt;
output[RED_C] = (output[RED_C] * invFog + fogInt * bpmem.fog.color.r) >> 8;
output[GRN_C] = (output[GRN_C] * invFog + fogInt * bpmem.fog.color.g) >> 8;
output[BLU_C] = (output[BLU_C] * invFog + fogInt * bpmem.fog.color.b) >> 8;
}
if (!bpmem.zcontrol.zcomploc && bpmem.zmode.testenable) if (!bpmem.zcontrol.zcomploc && bpmem.zmode.testenable)
{ {
if (!EfbInterface::ZCompare(Position[0], Position[1], Position[2])) if (!EfbInterface::ZCompare(Position[0], Position[1], Position[2]))

View File

@ -157,12 +157,16 @@ void SampleMip(s32 s, s32 t, s32 mip, bool linear, u8 texmap, u8 *sample)
} }
} }
if (linear)
{
// offset linear sampling
s -= 64;
t -= 64;
// integer part of sample location // integer part of sample location
int imageS = s >> 7; int imageS = s >> 7;
int imageT = t >> 7; int imageT = t >> 7;
if (linear)
{
// linear sampling // linear sampling
int imageSPlus1 = imageS + 1; int imageSPlus1 = imageS + 1;
int fractS = s & 0x7f; int fractS = s & 0x7f;
@ -197,6 +201,10 @@ void SampleMip(s32 s, s32 t, s32 mip, bool linear, u8 texmap, u8 *sample)
} }
else else
{ {
// integer part of sample location
int imageS = s >> 7;
int imageT = t >> 7;
// nearest neighbor sampling // nearest neighbor sampling
WrapCoord(imageS, tm0.wrap_s, imageWidth); WrapCoord(imageS, tm0.wrap_s, imageWidth);
WrapCoord(imageT, tm0.wrap_t, imageHeight); WrapCoord(imageT, tm0.wrap_t, imageHeight);

View File

@ -166,15 +166,9 @@ inline void TransformTexCoordRegular(const TexMtxInfo &texinfo, int coordNum, bo
else else
{ {
if (postInfo.normalize) if (postInfo.normalize)
{ tempCoord = dst->normalized();
float length = sqrtf(dst[0] * dst[0] + dst[1] * dst[1] + dst[2] * dst[2]);
float invL = 1.0f / length;
tempCoord = *dst * invL;
}
else else
{
tempCoord = *dst; tempCoord = *dst;
}
MultiplyVec3Mat34(tempCoord, postMat, *dst); MultiplyVec3Mat34(tempCoord, postMat, *dst);
} }
@ -463,29 +457,32 @@ void TransformTexCoord(const InputVertexData *src, OutputVertexData *dst, bool s
float d1 = ldir * dst->normal[1]; float d1 = ldir * dst->normal[1];
float d2 = ldir * dst->normal[2]; float d2 = ldir * dst->normal[2];
dst->texCoords[coordNum][0] = dst->texCoords[texinfo.embosssourceshift][0] + d1; dst->texCoords[coordNum].x = dst->texCoords[texinfo.embosssourceshift].x + d1;
dst->texCoords[coordNum][1] = dst->texCoords[texinfo.embosssourceshift][1] + d2; dst->texCoords[coordNum].y = dst->texCoords[texinfo.embosssourceshift].y + d2;
dst->texCoords[coordNum][2] = dst->texCoords[texinfo.embosssourceshift][2]; dst->texCoords[coordNum].z = dst->texCoords[texinfo.embosssourceshift].z;
} }
break; break;
case XF_TEXGEN_COLOR_STRGBC0: case XF_TEXGEN_COLOR_STRGBC0:
_assert_(texinfo.sourcerow == XF_SRCCOLORS_INROW); _assert_(texinfo.sourcerow == XF_SRCCOLORS_INROW);
_assert_(texinfo.inputform == XF_TEXINPUT_AB11); _assert_(texinfo.inputform == XF_TEXINPUT_AB11);
dst->texCoords[coordNum][0] = (float)dst->color[0][0] / 255.0f; dst->texCoords[coordNum].x = (float)dst->color[0][0] / 255.0f;
dst->texCoords[coordNum][1] = (float)dst->color[0][1] / 255.0f; dst->texCoords[coordNum].y = (float)dst->color[0][1] / 255.0f;
dst->texCoords[coordNum][2] = 1.0f; dst->texCoords[coordNum].z = 1.0f;
break; break;
case XF_TEXGEN_COLOR_STRGBC1: case XF_TEXGEN_COLOR_STRGBC1:
_assert_(texinfo.sourcerow == XF_SRCCOLORS_INROW); _assert_(texinfo.sourcerow == XF_SRCCOLORS_INROW);
_assert_(texinfo.inputform == XF_TEXINPUT_AB11); _assert_(texinfo.inputform == XF_TEXINPUT_AB11);
dst->texCoords[coordNum][0] = (float)dst->color[1][0] / 255.0f; dst->texCoords[coordNum].x = (float)dst->color[1][0] / 255.0f;
dst->texCoords[coordNum][1] = (float)dst->color[1][1] / 255.0f; dst->texCoords[coordNum].y = (float)dst->color[1][1] / 255.0f;
dst->texCoords[coordNum][2] = 1.0f; dst->texCoords[coordNum].z = 1.0f;
break; break;
default: default:
ERROR_LOG(VIDEO, "Bad tex gen type %i", texinfo.texgentype); ERROR_LOG(VIDEO, "Bad tex gen type %i", texinfo.texgentype);
} }
}
for (u32 coordNum = 0; coordNum < xfregs.numTexGens; coordNum++)
{
dst->texCoords[coordNum][0] *= (bpmem.texcoords[coordNum].s.scale_minus_1 + 1); dst->texCoords[coordNum][0] *= (bpmem.texcoords[coordNum].s.scale_minus_1 + 1);
dst->texCoords[coordNum][1] *= (bpmem.texcoords[coordNum].t.scale_minus_1 + 1); dst->texCoords[coordNum][1] *= (bpmem.texcoords[coordNum].t.scale_minus_1 + 1);
} }

View File

@ -35,6 +35,30 @@ void XFWritten(u32 transferSize, u32 baseAddress)
if (baseAddress <= 0x1026 && topAddress >= 0x1020) if (baseAddress <= 0x1026 && topAddress >= 0x1020)
Clipper::SetViewOffset(); Clipper::SetViewOffset();
// fix lights so invalid values don't trash the lighting computations
if (baseAddress <= 0x067f && topAddress >= 0x0604)
{
u32* x = xfregs.lights;
// go through all lights
for (int light = 0; light < 8; light++)
{
// skip to floating point values
x += 4;
for (int i = 0; i < 12; i++)
{
u32 xVal = *x;
// if the exponent is 255 then the number is inf or nan
if ((xVal & 0x7f800000) == 0x7f800000)
*x = 0;
x++;
}
}
}
} }
void LoadXFReg(u32 transferSize, u32 baseAddress, u32 *pData) void LoadXFReg(u32 transferSize, u32 baseAddress, u32 *pData)
@ -66,8 +90,7 @@ void LoadIndexedXF(u32 val, int array)
int size = ((val >> 12) & 0xF) + 1; int size = ((val >> 12) & 0xF) + 1;
//load stuff from array to address in xf mem //load stuff from array to address in xf mem
u32* xfmem = (u32*)&xfregs; u32 *pData = (u32*)g_VideoInitialize.pGetMemoryPointer(arraybases[array] + arraystrides[array]*index);
for (int i = 0; i < size; i++) LoadXFReg(size, address, pData);
xfmem[address + i] = Memory_Read_U32(arraybases[array] + arraystrides[array]*index + i*4);
} }

View File

@ -113,7 +113,6 @@ void Video_Prepare(void)
// Run from the CPU thread (from VideoInterface.cpp) // Run from the CPU thread (from VideoInterface.cpp)
void Video_BeginField(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight) void Video_BeginField(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight)
{ {
g_VideoInitialize.pCopiedToXFB(true);
} }
// Run from the CPU thread (from VideoInterface.cpp) // Run from the CPU thread (from VideoInterface.cpp)