diff --git a/DMA.cpp b/DMA.cpp index 19711ae3..dbedc010 100644 --- a/DMA.cpp +++ b/DMA.cpp @@ -113,7 +113,7 @@ void DMA::Start() if ((Cnt & 0x00600000) == 0x00600000) CurDstAddr = DstAddr; - //printf("ARM%d DMA%d %08X %08X->%08X %d bytes %dbit\n", CPU?7:9, Num, Cnt, CurSrcAddr, CurDstAddr, RemCount*((Cnt&0x04000000)?4:2), (Cnt&0x04000000)?32:16); + //printf("ARM%d DMA%d %08X %02X %08X->%08X %d bytes %dbit\n", CPU?7:9, Num, Cnt, StartMode, CurSrcAddr, CurDstAddr, RemCount*((Cnt&0x04000000)?4:2), (Cnt&0x04000000)?32:16); // special path for cart DMA. this is a gross hack. // emulating it properly requires emulating cart transfer delays, so uh... TODO diff --git a/GPU3D.cpp b/GPU3D.cpp index edc9f127..01fdfb82 100644 --- a/GPU3D.cpp +++ b/GPU3D.cpp @@ -171,6 +171,8 @@ Polygon* CurPolygonRAM; u32 NumVertices, NumPolygons; u32 CurRAMBank; +u32 FlushRequest; + bool Init() @@ -234,6 +236,8 @@ void Reset() NumVertices = 0; NumPolygons = 0; + FlushRequest = 0; + SoftRenderer::Reset(); } @@ -372,32 +376,39 @@ void UpdateClipMatrix() +template +void ClipSegment(Vertex* outbuf, int num, Vertex* vout, Vertex* vin) +{ + s32 factor = ((vin->Position[3] - (plane*vin->Position[comp])) << 12) / + ((vin->Position[3] - (plane*vin->Position[comp])) - (vout->Position[3] - (plane*vout->Position[comp]))); + + Vertex mid; +#define INTERPOLATE(var) mid.var = vin->var + (((vout->var - vin->var) * factor) >> 12); + + INTERPOLATE(Position[0]); + INTERPOLATE(Position[1]); + INTERPOLATE(Position[2]); + INTERPOLATE(Position[3]); + + INTERPOLATE(Color[0]); + INTERPOLATE(Color[1]); + INTERPOLATE(Color[2]); + +#undef INTERPOLATE + outbuf[num] = mid; +} + void SubmitPolygon() { // clip. // for each vertex: // if it's outside, check if the previous and next vertices are inside, if so, fixor - Vertex clippedvertices[20]; + Vertex clippedvertices[2][10]; u32 numclipped; int nverts = PolygonMode & 0x1 ? 4:3; int nvisible = 0; - - /*for (int i = 0; i < nverts; i++) - { - s32* v = TempVertexBuffer[i].Position; - - if ((u32)(v[0]+0x1000) <= 0x2000 && - (u32)(v[1]+0x1000) <= 0x2000 && - (u32)(v[2]+0x1000) <= 0x2000) - { - nvisible++; - } - } - - if (!nvisible) return;*/ - int prev, next; int c; @@ -407,76 +418,51 @@ void SubmitPolygon() for (int i = 0; i < nverts; i++) { Vertex vtx = TempVertexBuffer[i]; - if (vtx.Position[0] > 0x1000) + if (vtx.Position[0] > vtx.Position[3]) { Vertex* vprev = &TempVertexBuffer[prev]; - if (vprev->Position[0] <= 0x1000) + if (vprev->Position[0] <= vprev->Position[3]) { - s32 factor = ((0x1000 - vprev->Position[0]) << 12) / (vtx.Position[0] - vprev->Position[0]); - - Vertex mid; - mid.Position[0] = 0x1000; - mid.Position[1] = vprev->Position[1] + (((vtx.Position[1] - vprev->Position[1]) * factor) >> 12); - mid.Position[2] = vprev->Position[2] + (((vtx.Position[2] - vprev->Position[2]) * factor) >> 12); - mid.Color[0] = vprev->Color[0] + (((vtx.Color[0] - vprev->Color[0]) * factor) >> 12); - mid.Color[1] = vprev->Color[1] + (((vtx.Color[1] - vprev->Color[1]) * factor) >> 12); - mid.Color[2] = vprev->Color[2] + (((vtx.Color[2] - vprev->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; + ClipSegment<0, 1>(clippedvertices[0], c, &vtx, vprev); + c++; } Vertex* vnext = &TempVertexBuffer[next]; - if (vnext->Position[0] <= 0x1000) + if (vnext->Position[0] <= vnext->Position[3]) { - s32 factor = ((0x1000 - vnext->Position[0]) << 12) / (vtx.Position[0] - vnext->Position[0]); - - Vertex mid; - mid.Position[0] = 0x1000; - mid.Position[1] = vnext->Position[1] + (((vtx.Position[1] - vnext->Position[1]) * factor) >> 12); - mid.Position[2] = vnext->Position[2] + (((vtx.Position[2] - vnext->Position[2]) * factor) >> 12); - mid.Color[0] = vnext->Color[0] + (((vtx.Color[0] - vnext->Color[0]) * factor) >> 12); - mid.Color[1] = vnext->Color[1] + (((vtx.Color[1] - vnext->Color[1]) * factor) >> 12); - mid.Color[2] = vnext->Color[2] + (((vtx.Color[2] - vnext->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; - } - } - else if (vtx.Position[0] < -0x1000) - { - Vertex* vprev = &TempVertexBuffer[prev]; - if (vprev->Position[0] >= -0x1000) - { - s32 factor = ((-0x1000 - vprev->Position[0]) << 12) / (vtx.Position[0] - vprev->Position[0]); - - Vertex mid; - mid.Position[0] = -0x1000; - mid.Position[1] = vprev->Position[1] + (((vtx.Position[1] - vprev->Position[1]) * factor) >> 12); - mid.Position[2] = vprev->Position[2] + (((vtx.Position[2] - vprev->Position[2]) * factor) >> 12); - mid.Color[0] = vprev->Color[0] + (((vtx.Color[0] - vprev->Color[0]) * factor) >> 12); - mid.Color[1] = vprev->Color[1] + (((vtx.Color[1] - vprev->Color[1]) * factor) >> 12); - mid.Color[2] = vprev->Color[2] + (((vtx.Color[2] - vprev->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; - } - - Vertex* vnext = &TempVertexBuffer[next]; - if (vnext->Position[0] >= -0x1000) - { - s32 factor = ((-0x1000 - vnext->Position[0]) << 12) / (vtx.Position[0] - vnext->Position[0]); - - Vertex mid; - mid.Position[0] = -0x1000; - mid.Position[1] = vnext->Position[1] + (((vtx.Position[1] - vnext->Position[1]) * factor) >> 12); - mid.Position[2] = vnext->Position[2] + (((vtx.Position[2] - vnext->Position[2]) * factor) >> 12); - mid.Color[0] = vnext->Color[0] + (((vtx.Color[0] - vnext->Color[0]) * factor) >> 12); - mid.Color[1] = vnext->Color[1] + (((vtx.Color[1] - vnext->Color[1]) * factor) >> 12); - mid.Color[2] = vnext->Color[2] + (((vtx.Color[2] - vnext->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; + ClipSegment<0, 1>(clippedvertices[0], c, &vtx, vnext); + c++; } } else - clippedvertices[c++] = vtx; + clippedvertices[0][c++] = vtx; + + prev++; if (prev >= nverts) prev = 0; + next++; if (next >= nverts) next = 0; + } + + nverts = c; prev = nverts-1; next = 1; c = 0; + for (int i = 0; i < nverts; i++) + { + Vertex vtx = clippedvertices[0][i]; + if (vtx.Position[0] < -vtx.Position[3]) + { + Vertex* vprev = &clippedvertices[0][prev]; + if (vprev->Position[0] >= -vprev->Position[3]) + { + ClipSegment<0, -1>(clippedvertices[1], c, &vtx, vprev); + c++; + } + + Vertex* vnext = &clippedvertices[0][next]; + if (vnext->Position[0] >= -vnext->Position[3]) + { + ClipSegment<0, -1>(clippedvertices[1], c, &vtx, vnext); + c++; + } + } + else + clippedvertices[1][c++] = vtx; prev++; if (prev >= nverts) prev = 0; next++; if (next >= nverts) next = 0; @@ -484,81 +470,55 @@ void SubmitPolygon() // Y clipping - nverts = c; - prev = nverts-1; next = 1; c = 10; + nverts = c; prev = nverts-1; next = 1; c = 0; for (int i = 0; i < nverts; i++) { - Vertex vtx = clippedvertices[i]; - if (vtx.Position[1] > 0x1000) + Vertex vtx = clippedvertices[1][i]; + if (vtx.Position[1] > vtx.Position[3]) { - Vertex* vprev = &clippedvertices[prev]; - if (vprev->Position[1] <= 0x1000) + Vertex* vprev = &clippedvertices[1][prev]; + if (vprev->Position[1] <= vprev->Position[3]) { - s32 factor = ((0x1000 - vprev->Position[1]) << 12) / (vtx.Position[1] - vprev->Position[1]); - - Vertex mid; - mid.Position[0] = vprev->Position[0] + (((vtx.Position[0] - vprev->Position[0]) * factor) >> 12); - mid.Position[1] = 0x1000; - mid.Position[2] = vprev->Position[2] + (((vtx.Position[2] - vprev->Position[2]) * factor) >> 12); - mid.Color[0] = vprev->Color[0] + (((vtx.Color[0] - vprev->Color[0]) * factor) >> 12); - mid.Color[1] = vprev->Color[1] + (((vtx.Color[1] - vprev->Color[1]) * factor) >> 12); - mid.Color[2] = vprev->Color[2] + (((vtx.Color[2] - vprev->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; + ClipSegment<1, 1>(clippedvertices[0], c, &vtx, vprev); + c++; } - Vertex* vnext = &clippedvertices[next]; - if (vnext->Position[1] <= 0x1000) + Vertex* vnext = &clippedvertices[1][next]; + if (vnext->Position[1] <= vnext->Position[3]) { - s32 factor = ((0x1000 - vnext->Position[1]) << 12) / (vtx.Position[1] - vnext->Position[1]); - - Vertex mid; - mid.Position[0] = vnext->Position[0] + (((vtx.Position[0] - vnext->Position[0]) * factor) >> 12); - mid.Position[1] = 0x1000; - mid.Position[2] = vnext->Position[2] + (((vtx.Position[2] - vnext->Position[2]) * factor) >> 12); - mid.Color[0] = vnext->Color[0] + (((vtx.Color[0] - vnext->Color[0]) * factor) >> 12); - mid.Color[1] = vnext->Color[1] + (((vtx.Color[1] - vnext->Color[1]) * factor) >> 12); - mid.Color[2] = vnext->Color[2] + (((vtx.Color[2] - vnext->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; - } - } - else if (vtx.Position[1] < -0x1000) - { - Vertex* vprev = &clippedvertices[prev]; - if (vprev->Position[1] >= -0x1000) - { - s32 factor = ((-0x1000 - vprev->Position[1]) << 12) / (vtx.Position[1] - vprev->Position[1]); - - Vertex mid; - mid.Position[0] = vprev->Position[0] + (((vtx.Position[0] - vprev->Position[0]) * factor) >> 12); - mid.Position[1] = -0x1000; - mid.Position[2] = vprev->Position[2] + (((vtx.Position[2] - vprev->Position[2]) * factor) >> 12); - mid.Color[0] = vprev->Color[0] + (((vtx.Color[0] - vprev->Color[0]) * factor) >> 12); - mid.Color[1] = vprev->Color[1] + (((vtx.Color[1] - vprev->Color[1]) * factor) >> 12); - mid.Color[2] = vprev->Color[2] + (((vtx.Color[2] - vprev->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; - } - - Vertex* vnext = &clippedvertices[next]; - if (vnext->Position[1] >= -0x1000) - { - s32 factor = ((-0x1000 - vnext->Position[1]) << 12) / (vtx.Position[1] - vnext->Position[1]); - - Vertex mid; - mid.Position[0] = vnext->Position[0] + (((vtx.Position[0] - vnext->Position[0]) * factor) >> 12); - mid.Position[1] = -0x1000; - mid.Position[2] = vnext->Position[2] + (((vtx.Position[2] - vnext->Position[2]) * factor) >> 12); - mid.Color[0] = vnext->Color[0] + (((vtx.Color[0] - vnext->Color[0]) * factor) >> 12); - mid.Color[1] = vnext->Color[1] + (((vtx.Color[1] - vnext->Color[1]) * factor) >> 12); - mid.Color[2] = vnext->Color[2] + (((vtx.Color[2] - vnext->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; + ClipSegment<1, 1>(clippedvertices[0], c, &vtx, vnext); + c++; } } else - clippedvertices[c++] = vtx; + clippedvertices[0][c++] = vtx; + + prev++; if (prev >= nverts) prev = 0; + next++; if (next >= nverts) next = 0; + } + + nverts = c; prev = nverts-1; next = 1; c = 0; + for (int i = 0; i < nverts; i++) + { + Vertex vtx = clippedvertices[0][i]; + if (vtx.Position[1] < -vtx.Position[3]) + { + Vertex* vprev = &clippedvertices[0][prev]; + if (vprev->Position[1] >= -vprev->Position[3]) + { + ClipSegment<1, -1>(clippedvertices[1], c, &vtx, vprev); + c++; + } + + Vertex* vnext = &clippedvertices[0][next]; + if (vnext->Position[1] >= -vnext->Position[3]) + { + ClipSegment<1, -1>(clippedvertices[1], c, &vtx, vnext); + c++; + } + } + else + clippedvertices[1][c++] = vtx; prev++; if (prev >= nverts) prev = 0; next++; if (next >= nverts) next = 0; @@ -566,81 +526,55 @@ void SubmitPolygon() // Z clipping - nverts = c-10; - prev = nverts-1; next = 1; c = 0; + nverts = c; prev = nverts-1; next = 1; c = 0; for (int i = 0; i < nverts; i++) { - Vertex vtx = clippedvertices[10+i]; - if (vtx.Position[2] > 0x1000) + Vertex vtx = clippedvertices[1][i]; + if (vtx.Position[2] > vtx.Position[3]) { - Vertex* vprev = &clippedvertices[10+prev]; - if (vprev->Position[2] <= 0x1000) + Vertex* vprev = &clippedvertices[1][prev]; + if (vprev->Position[2] <= vprev->Position[3]) { - s32 factor = ((0x1000 - vprev->Position[2]) << 12) / (vtx.Position[2] - vprev->Position[2]); - - Vertex mid; - mid.Position[0] = vprev->Position[0] + (((vtx.Position[0] - vprev->Position[0]) * factor) >> 12); - mid.Position[1] = vprev->Position[1] + (((vtx.Position[1] - vprev->Position[1]) * factor) >> 12); - mid.Position[2] = 0x1000; - mid.Color[0] = vprev->Color[0] + (((vtx.Color[0] - vprev->Color[0]) * factor) >> 12); - mid.Color[1] = vprev->Color[1] + (((vtx.Color[1] - vprev->Color[1]) * factor) >> 12); - mid.Color[2] = vprev->Color[2] + (((vtx.Color[2] - vprev->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; + ClipSegment<2, 1>(clippedvertices[0], c, &vtx, vprev); + c++; } - Vertex* vnext = &clippedvertices[10+next]; - if (vnext->Position[2] <= 0x1000) + Vertex* vnext = &clippedvertices[1][next]; + if (vnext->Position[2] <= vnext->Position[3]) { - s32 factor = ((0x1000 - vnext->Position[2]) << 12) / (vtx.Position[2] - vnext->Position[2]); - - Vertex mid; - mid.Position[0] = vnext->Position[0] + (((vtx.Position[0] - vnext->Position[0]) * factor) >> 12); - mid.Position[1] = vnext->Position[1] + (((vtx.Position[1] - vnext->Position[1]) * factor) >> 12); - mid.Position[2] = 0x1000; - mid.Color[0] = vnext->Color[0] + (((vtx.Color[0] - vnext->Color[0]) * factor) >> 12); - mid.Color[1] = vnext->Color[1] + (((vtx.Color[1] - vnext->Color[1]) * factor) >> 12); - mid.Color[2] = vnext->Color[2] + (((vtx.Color[2] - vnext->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; - } - } - else if (vtx.Position[2] < -0x1000) - { - Vertex* vprev = &clippedvertices[10+prev]; - if (vprev->Position[2] >= -0x1000) - { - s32 factor = ((-0x1000 - vprev->Position[2]) << 12) / (vtx.Position[2] - vprev->Position[2]); - - Vertex mid; - mid.Position[0] = vprev->Position[0] + (((vtx.Position[0] - vprev->Position[0]) * factor) >> 12); - mid.Position[1] = vprev->Position[1] + (((vtx.Position[1] - vprev->Position[1]) * factor) >> 12); - mid.Position[2] = -0x1000; - mid.Color[0] = vprev->Color[0] + (((vtx.Color[0] - vprev->Color[0]) * factor) >> 12); - mid.Color[1] = vprev->Color[1] + (((vtx.Color[1] - vprev->Color[1]) * factor) >> 12); - mid.Color[2] = vprev->Color[2] + (((vtx.Color[2] - vprev->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; - } - - Vertex* vnext = &clippedvertices[10+next]; - if (vnext->Position[2] >= -0x1000) - { - s32 factor = ((-0x1000 - vnext->Position[2]) << 12) / (vtx.Position[2] - vnext->Position[2]); - - Vertex mid; - mid.Position[0] = vnext->Position[0] + (((vtx.Position[0] - vnext->Position[0]) * factor) >> 12); - mid.Position[1] = vnext->Position[1] + (((vtx.Position[1] - vnext->Position[1]) * factor) >> 12); - mid.Position[2] = -0x1000; - mid.Color[0] = vnext->Color[0] + (((vtx.Color[0] - vnext->Color[0]) * factor) >> 12); - mid.Color[1] = vnext->Color[1] + (((vtx.Color[1] - vnext->Color[1]) * factor) >> 12); - mid.Color[2] = vnext->Color[2] + (((vtx.Color[2] - vnext->Color[2]) * factor) >> 12); - - clippedvertices[c++] = mid; + ClipSegment<2, 1>(clippedvertices[0], c, &vtx, vnext); + c++; } } else - clippedvertices[c++] = vtx; + clippedvertices[0][c++] = vtx; + + prev++; if (prev >= nverts) prev = 0; + next++; if (next >= nverts) next = 0; + } + + nverts = c; prev = nverts-1; next = 1; c = 0; + for (int i = 0; i < nverts; i++) + { + Vertex vtx = clippedvertices[0][i]; + if (vtx.Position[2] < -vtx.Position[3]) + { + Vertex* vprev = &clippedvertices[0][prev]; + if (vprev->Position[2] >= -vprev->Position[3]) + { + ClipSegment<2, -1>(clippedvertices[1], c, &vtx, vprev); + c++; + } + + Vertex* vnext = &clippedvertices[0][next]; + if (vnext->Position[2] >= -vnext->Position[3]) + { + ClipSegment<2, -1>(clippedvertices[1], c, &vtx, vnext); + c++; + } + } + else + clippedvertices[1][c++] = vtx; prev++; if (prev >= nverts) prev = 0; next++; if (next >= nverts) next = 0; @@ -659,7 +593,7 @@ void SubmitPolygon() for (int i = 0; i < c; i++) { - CurVertexRAM[NumVertices] = clippedvertices[i]; + CurVertexRAM[NumVertices] = clippedvertices[1][i]; poly->Vertices[i] = &CurVertexRAM[NumVertices]; NumVertices++; @@ -673,6 +607,8 @@ void SubmitVertex() //s32 vertextrans[4]; Vertex* vertextrans = &TempVertexBuffer[VertexNumInPoly]; + if (PolygonMode & 0x2) return; + //printf("vertex: %f %f %f\n", vertex[0]/4096.0f, vertex[1]/4096.0f, vertex[2]/4096.0f); UpdateClipMatrix(); @@ -681,15 +617,23 @@ void SubmitVertex() vertextrans->Position[2] = (vertex[0]*ClipMatrix[2] + vertex[1]*ClipMatrix[6] + vertex[2]*ClipMatrix[10] + vertex[3]*ClipMatrix[14]) >> 12; vertextrans->Position[3] = (vertex[0]*ClipMatrix[3] + vertex[1]*ClipMatrix[7] + vertex[2]*ClipMatrix[11] + vertex[3]*ClipMatrix[15]) >> 12; - s32 w_inv; + /*printf("vertex fart: %f %f %f %f\n", + vertextrans->Position[0]/4096.0f, + vertextrans->Position[1]/4096.0f, + vertextrans->Position[2]/4096.0f, + vertextrans->Position[3]/4096.0f);*/ + + /*s32 w_inv; if (vertextrans->Position[3] == 0) w_inv = 0x1000; // checkme + else if(vertextrans->Position[3] < 0) + w_inv = 0x1000000 / -vertextrans->Position[3]; else w_inv = 0x1000000 / vertextrans->Position[3]; vertextrans->Position[0] = (vertextrans->Position[0] * w_inv) >> 12; vertextrans->Position[1] = (vertextrans->Position[1] * w_inv) >> 12; - vertextrans->Position[2] = (vertextrans->Position[2] * w_inv) >> 12; + vertextrans->Position[2] = (vertextrans->Position[2] * w_inv) >> 12;*/ vertextrans->Color[0] = VertexColor[0]; vertextrans->Color[1] = VertexColor[1]; @@ -699,7 +643,12 @@ void SubmitVertex() vertextrans->Position[0]/4096.0f, vertextrans->Position[1]/4096.0f, vertextrans->Position[2]/4096.0f, - vertextrans->Position[3]/4096.0f);*/ + vertextrans->Position[3]/4096.0f); + printf("clip: %f %f %f %f\n", + ClipMatrix[3]/4096.0f, + ClipMatrix[7]/4096.0f, + ClipMatrix[11]/4096.0f, + ClipMatrix[15]/4096.0f);*/ /*if (vertextrans[3] == 0) { @@ -724,10 +673,48 @@ void SubmitVertex() VertexNum++; VertexNumInPoly++; - if (VertexNumInPoly >= (PolygonMode & 0x1 ? 4:3)) + + switch (PolygonMode) { - VertexNumInPoly = (PolygonMode & 0x2 ? 2:0); - SubmitPolygon(); + case 0: // triangle + if (VertexNumInPoly == 3) + { + VertexNumInPoly = 0; + SubmitPolygon(); + } + break; + + case 1: // quad + if (VertexNumInPoly == 4) + { + VertexNumInPoly = 0; + SubmitPolygon(); + } + break; + + /*case 2: // triangle strip + if (VertexNum > 3) + { + if (VertexNumInPoly == 1) + { + VertexNumInPoly = 0; + // reorder + } + else + VertexNumInPoly = 0; + + SubmitPolygon(); + } + else if (VertexNum == 3) + { + VertexNumInPoly = 2; + SubmitPolygon(); + + TempVertexBuffer[0] = TempVertexBuffer[1]; + TempVertexBuffer[1] = TempVertexBuffer[2]; + } + break;*/ + default: VertexNumInPoly = 0; break; } } @@ -738,12 +725,14 @@ void CmdFIFOWrite(CmdFIFOEntry& entry) if (CmdFIFO->IsEmpty() && !CmdPIPE->IsFull()) { CmdPIPE->Write(entry); + GXStat |= (1<<27); } else { if (CmdFIFO->IsFull()) { printf("!!! GX FIFO FULL\n"); + //NDS::debug(0); return; } @@ -775,6 +764,8 @@ void ExecuteCommand() { CmdFIFOEntry entry = CmdFIFORead(); + //printf("FIFO: %02X %08X\n", entry.Command, entry.Param); + ExecParams[ExecParamCount] = entry.Param; ExecParamCount++; @@ -784,6 +775,8 @@ void ExecuteCommand() ExecParamCount = 0; GXStat &= ~(1<<14); + //if (CycleCount > 0) + // GXStat |= (1<<27); //printf("3D CMD %02X\n", entry.Command); @@ -1121,7 +1114,7 @@ void ExecuteCommand() break; case 0x50: - // TODO: make it happen upon VBlank, not right now + FlushRequest = 1;//0x80000000 | (ExecParams[0] & 0x3); break; case 0x60: // viewport x1,y1,x2,y2 @@ -1145,7 +1138,11 @@ void Run(s32 cycles) CycleCount -= cycles; if (CycleCount <= 0 && CmdPIPE->IsEmpty()) + { CycleCount = 0; + if (!FlushRequest) + GXStat &= ~(1<<27); + } } @@ -1170,15 +1167,20 @@ void CheckFIFODMA() void VBlank() { - // TODO: only do this if a SwapBuffers command was issued - SoftRenderer::RenderFrame(CurVertexRAM, CurPolygonRAM, NumPolygons); + if (FlushRequest) + { + SoftRenderer::RenderFrame(CurVertexRAM, CurPolygonRAM, NumPolygons); - CurRAMBank = CurRAMBank?0:1; - CurVertexRAM = &VertexRAM[CurRAMBank ? 6144 : 0]; - CurPolygonRAM = &PolygonRAM[CurRAMBank ? 2048 : 0]; + CurRAMBank = CurRAMBank?0:1; + CurVertexRAM = &VertexRAM[CurRAMBank ? 6144 : 0]; + CurPolygonRAM = &PolygonRAM[CurRAMBank ? 2048 : 0]; - NumVertices = 0; - NumPolygons = 0; + NumVertices = 0; + NumPolygons = 0; + + FlushRequest = 0; + GXStat &= ~(1<<27); + } } u8* GetLine(int line) @@ -1213,8 +1215,7 @@ u32 Read32(u32 addr) ((ProjMatrixStackPointer & 0x1) << 13) | (fifolevel << 16) | (fifolevel < 128 ? (1<<25) : 0) | - (fifolevel == 0 ? (1<<26) : 0) | - (CycleCount > 0 ? (1<<27) : 0); + (fifolevel == 0 ? (1<<26) : 0); } } @@ -1262,23 +1263,30 @@ void Write32(u32 addr, u32 val) CurCommand = val; ParamCount = 0; TotalParams = CmdNumParams[CurCommand & 0xFF]; + + if (TotalParams > 0) return; } else ParamCount++; - while (ParamCount == TotalParams) + for (;;) { CmdFIFOEntry entry; entry.Command = CurCommand & 0xFF; entry.Param = val; CmdFIFOWrite(entry); - CurCommand >>= 8; - NumCommands--; - if (NumCommands == 0) break; + if (ParamCount >= TotalParams) + { + CurCommand >>= 8; + NumCommands--; + if (NumCommands == 0) break; - ParamCount = 0; - TotalParams = CmdNumParams[CurCommand & 0xFF]; + ParamCount = 0; + TotalParams = CmdNumParams[CurCommand & 0xFF]; + } + if (ParamCount < TotalParams) + break; } return; diff --git a/GPU3D_Soft.cpp b/GPU3D_Soft.cpp index 4155751b..d47975df 100644 --- a/GPU3D_Soft.cpp +++ b/GPU3D_Soft.cpp @@ -50,9 +50,9 @@ void RenderPolygon(Polygon* polygon) { int nverts = polygon->NumVertices; - int vtop, vbot; + int vtop = 0, vbot = 0; s32 ytop = 191, ybot = 0; - s32 scrcoords[10][3]; + s32 scrcoords[10][4]; // find the topmost and bottommost vertices of the polygon @@ -60,14 +60,36 @@ void RenderPolygon(Polygon* polygon) { Vertex* vtx = polygon->Vertices[i]; - s32 scrX = (((vtx->Position[0] + 0x1000) * Viewport[2]) >> 13) + Viewport[0]; - s32 scrY = (((vtx->Position[1] + 0x1000) * Viewport[3]) >> 13) + Viewport[1]; + s32 w_inv; + if (vtx->Position[3] == 0) + { + w_inv = 0x1000; // checkme + printf("!! W=0\n"); + } + else + w_inv = 0x1000000 / vtx->Position[3]; + + if (vtx->Position[3] < 0) printf("!!! W=%d\n", vtx->Position[3]); + + s32 posX = (vtx->Position[0] * w_inv) >> 12; + s32 posY = (vtx->Position[1] * w_inv) >> 12; + s32 posZ = (vtx->Position[2] * w_inv) >> 12; + //s32 posX = vtx->Position[0]; + //s32 posY = vtx->Position[1]; + + s32 scrX = (((posX + 0x1000) * Viewport[2]) >> 13) + Viewport[0]; + s32 scrY = (((posY + 0x1000) * Viewport[3]) >> 13) + Viewport[1]; + s32 scrZ = (vtx->Position[2] + 0x1000) >> 1; if (scrX > 255) scrX = 255; if (scrY > 191) scrY = 191; + if (scrZ > 0xFFF) scrZ = 0xFFF; + if (scrX < 0) { printf("!! bad X %d\n", scrX); scrX = 0;} + if (scrY < 0) { printf("!! bad Y %d\n", scrY); scrY = 0;} scrcoords[i][0] = scrX; scrcoords[i][1] = 191 - scrY; - scrcoords[i][2] = 0; // TODO: Z + scrcoords[i][2] = scrZ; + scrcoords[i][3] = vtx->Position[3]; if (scrcoords[i][1] < ytop) { @@ -79,6 +101,8 @@ void RenderPolygon(Polygon* polygon) ybot = scrcoords[i][1]; vbot = i; } + //if (vtx->Color[0]==63 && vtx->Color[1]==0 && vtx->Color[2]==0) + //printf("v%d: %d,%d W=%d\n", i, scrX, 191-scrY, vtx->Position[3]); } // draw, line per line @@ -103,28 +127,33 @@ void RenderPolygon(Polygon* polygon) for (s32 y = ytop; y <= ybot; y++) { - if (y == scrcoords[lnext][1] && y < ybot) + if (y < ybot) { - lcur++; - if (lcur >= nverts) lcur = 0; + while (y == scrcoords[lnext][1]) + { + lcur++; + if (lcur >= nverts) lcur = 0; - lnext = lcur + 1; - if (lnext >= nverts) lnext = 0; + lnext = lcur + 1; + if (lnext >= nverts) lnext = 0; - //lstep = ((scrcoords[lnext][0] - scrcoords[lcur][0]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]); - //xmin = scrcoords[lcur][0] << 12; - } + //lstep = ((scrcoords[lnext][0] - scrcoords[lcur][0]) << 12) / (scrcoords[lnext][1] - scrcoords[lcur][1]); + //xmin = scrcoords[lcur][0] << 12; + if (lcur == vbot) break; + } - if (y == scrcoords[rnext][1] && y < ybot) - { - rcur--; - if (rcur < 0) rcur = nverts - 1; + while (y == scrcoords[rnext][1]) + { + rcur--; + if (rcur < 0) rcur = nverts - 1; - rnext = rcur - 1; - if (rnext < 0) rnext = nverts - 1; + rnext = rcur - 1; + if (rnext < 0) rnext = nverts - 1; - //rstep = ((scrcoords[rnext][0] - scrcoords[rcur][0]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]); - //xmax = scrcoords[rcur][0] << 12; + //rstep = ((scrcoords[rnext][0] - scrcoords[rcur][0]) << 12) / (scrcoords[rnext][1] - scrcoords[rcur][1]); + //xmax = scrcoords[rcur][0] << 12; + if (rcur == vbot) break; + } } Vertex* vlcur = polygon->Vertices[lcur]; @@ -147,6 +176,12 @@ void RenderPolygon(Polygon* polygon) s32 xl = scrcoords[lcur][0] + (((scrcoords[lnext][0] - scrcoords[lcur][0]) * lfactor) >> 12); s32 xr = scrcoords[rcur][0] + (((scrcoords[rnext][0] - scrcoords[rcur][0]) * rfactor) >> 12); + //if (vlcur->Color[0]==0 && vlcur->Color[1]==63 && vlcur->Color[2]==0) + // printf("y:%d xleft:%d xright:%d %d,%d %d,%d\n", y, xl, xr, lcur, rcur, vtop, vbot); + + //s32 zl = scrcoords[lcur][3] + (((scrcoords[lnext][3] - scrcoords[lcur][3]) * lfactor) >> 12); + //s32 zr = scrcoords[rcur][3] + (((scrcoords[rnext][3] - scrcoords[rcur][3]) * rfactor) >> 12); + u8 rl = vlcur->Color[0] + (((vlnext->Color[0] - vlcur->Color[0]) * lfactor) >> 12); u8 gl = vlcur->Color[1] + (((vlnext->Color[1] - vlcur->Color[1]) * lfactor) >> 12); u8 bl = vlcur->Color[2] + (((vlnext->Color[2] - vlcur->Color[2]) * lfactor) >> 12); @@ -165,11 +200,31 @@ void RenderPolygon(Polygon* polygon) { s32 xfactor = (x - xl) * xdiv; + //s32 z = (zl << 12) + ((zr - zl) * xfactor); + //z = zl + (((zr - zl) * xfactor) >> 12); + + //s32 z_inv = ((z>>12)==0) ? 0x1000 : 0x1000000 / (z >> 12); + //xfactor = (xfactor * z_inv) >> 12; + //xfactor = (xfactor << 12) / z; + + // TODO: get rid of this shit + if (x<0 || x>255 || y<0 || y>191) + { + //printf("BAD COORDS!! %d %d\n", x, y); + x = 0; y = 0; + } + u8* pixel = &ColorBuffer[((256*y) + x) * 4]; pixel[0] = rl + (((rr - rl) * xfactor) >> 12); pixel[1] = gl + (((gr - gl) * xfactor) >> 12); pixel[2] = bl + (((br - bl) * xfactor) >> 12); - pixel[3] = 31; + pixel[3] = 31; // TODO: alpha + + // Z debug + /*u8 zerp = (z * 63) / 0xFFFFFF; + pixel[0] = zerp; + pixel[1] = zerp; + pixel[2] = zerp;*/ } } } @@ -180,11 +235,19 @@ void RenderFrame(Vertex* vertices, Polygon* polygons, int npolys) for (int i = 0; i < 256*192; i++) { - ((u32*)ColorBuffer)[i] = 0x1F000000; + ((u32*)ColorBuffer)[i] = 0x00000000; } for (int i = 0; i < npolys; i++) { + /*printf("polygon %d: %d %d %d\n", i, polygons[i].Vertices[0]->Color[0], polygons[i].Vertices[0]->Color[1], polygons[i].Vertices[0]->Color[2]); + for (int j = 0; j < polygons[i].NumVertices; j++) + printf(" %d: %f %f %f\n", + j, + polygons[i].Vertices[j]->Position[0]/4096.0f, + polygons[i].Vertices[j]->Position[1]/4096.0f, + polygons[i].Vertices[j]->Position[2]/4096.0f); +*/ RenderPolygon(&polygons[i]); } } diff --git a/README.md b/README.md index f2545aab..9b2103e8 100644 --- a/README.md +++ b/README.md @@ -35,4 +35,21 @@ TODO LIST * 3D engine * sound * wifi - * other non-core shit (debugger, graphics viewers, cheat crapo, etc) \ No newline at end of file + * other non-core shit (debugger, graphics viewers, cheat crapo, etc) + + +IMMEDIATE TODO LIST (prior release 1.0) + + * UI + * 3D engine that atleast supports texturing + * emulate DMA timings + * make timers suck less + + +TODO LIST FOR LATER + + * sound + * more 3D engine features + * hardware renderer for 3D + * wifi + * maybe emulate flashcarts or other fancy hardware diff --git a/main.cpp b/main.cpp index 600cdbfe..7600102f 100644 --- a/main.cpp +++ b/main.cpp @@ -85,6 +85,8 @@ LRESULT CALLBACK derpo(HWND window, UINT msg, WPARAM wparam, LPARAM lparam) case VK_RIGHT: NDS::PressKey(4); break; case 'A': NDS::PressKey(0); break; case 'B': NDS::PressKey(1); break; + case 'L': NDS::PressKey(9); break; + case 'R': NDS::PressKey(8); break; case 'D': NDS::debug(0); break; } return 0; @@ -100,6 +102,8 @@ LRESULT CALLBACK derpo(HWND window, UINT msg, WPARAM wparam, LPARAM lparam) case VK_RIGHT: NDS::ReleaseKey(4); break; case 'A': NDS::ReleaseKey(0); break; case 'B': NDS::ReleaseKey(1); break; + case 'L': NDS::ReleaseKey(9); break; + case 'R': NDS::ReleaseKey(8); break; } return 0; diff --git a/melonDS.depend b/melonDS.depend index 8fffc74e..08e52b74 100644 --- a/melonDS.depend +++ b/melonDS.depend @@ -1,16 +1,16 @@ # depslib dependency file v1.0 -1486502416 source:c:\documents\sources\melonds\main.cpp +1486824787 source:c:\documents\sources\melonds\main.cpp "NDS.h" "GPU.h" -1486502049 c:\documents\sources\melonds\nds.h +1486822548 c:\documents\sources\melonds\nds.h "types.h" 1481161027 c:\documents\sources\melonds\types.h -1486778178 source:c:\documents\sources\melonds\nds.cpp +1486947856 source:c:\documents\sources\melonds\nds.cpp "NDS.h" @@ -109,7 +109,7 @@ 1486511075 c:\documents\sources\melonds\fifo.h "types.h" -1486589927 source:c:\documents\sources\melonds\dma.cpp +1486823366 source:c:\documents\sources\melonds\dma.cpp "NDS.h" "DMA.h" @@ -148,14 +148,14 @@ 1486777933 c:\documents\sources\melonds\gpu3d.h -1486782030 source:c:\documents\sources\melonds\gpu3d.cpp +1486947978 source:c:\documents\sources\melonds\gpu3d.cpp "NDS.h" "GPU.h" "FIFO.h" -1486781263 source:c:\documents\sources\melonds\gpu3d_soft.cpp +1486947027 source:c:\documents\sources\melonds\gpu3d_soft.cpp "NDS.h"