diff --git a/GPU/Software/Clipper.cpp b/GPU/Software/Clipper.cpp index a7ecd03451..63c0b313f8 100644 --- a/GPU/Software/Clipper.cpp +++ b/GPU/Software/Clipper.cpp @@ -226,24 +226,24 @@ void ProcessQuad(VertexData* data) Rasterizer::DrawTriangle(*bottomright, *bottomleft, *topleft); } -void ProcessTriangle(VertexData* data) +void ProcessTriangle(VertexData& v0, VertexData& v1, VertexData& v2) { if (gstate.isModeThrough()) { - Rasterizer::DrawTriangle(data[0], data[1], data[2]); + Rasterizer::DrawTriangle(v0, v1, v2); return; } enum { NUM_CLIPPED_VERTICES = 33, NUM_INDICES = NUM_CLIPPED_VERTICES + 3 }; - VertexData* Vertices[NUM_CLIPPED_VERTICES]; + VertexData* Vertices[NUM_INDICES]; VertexData ClippedVertices[NUM_CLIPPED_VERTICES]; for (int i = 0; i < NUM_CLIPPED_VERTICES; ++i) Vertices[i+3] = &ClippedVertices[i]; // TODO: Change logic when it's a backface - Vertices[0] = &data[0]; - Vertices[1] = &data[1]; - Vertices[2] = &data[2]; + Vertices[0] = &v0; + Vertices[1] = &v1; + Vertices[2] = &v2; int indices[NUM_INDICES] = { 0, 1, 2, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, SKIP_FLAG, @@ -251,9 +251,9 @@ void ProcessTriangle(VertexData* data) int numIndices = 3; int mask = 0; - mask |= CalcClipMask(data[0].clippos); - mask |= CalcClipMask(data[1].clippos); - mask |= CalcClipMask(data[2].clippos); + mask |= CalcClipMask(v0.clippos); + mask |= CalcClipMask(v1.clippos); + mask |= CalcClipMask(v2.clippos); if (mask) { for(int i = 0; i < 3; i += 3) { diff --git a/GPU/Software/Clipper.h b/GPU/Software/Clipper.h index 3cf3148269..c8e5634161 100644 --- a/GPU/Software/Clipper.h +++ b/GPU/Software/Clipper.h @@ -21,7 +21,7 @@ namespace Clipper { -void ProcessTriangle(VertexData* data); +void ProcessTriangle(VertexData& v0, VertexData& v1, VertexData& v2); void ProcessQuad(VertexData* data); } diff --git a/GPU/Software/Rasterizer.cpp b/GPU/Software/Rasterizer.cpp index 763237bf1f..0082587383 100644 --- a/GPU/Software/Rasterizer.cpp +++ b/GPU/Software/Rasterizer.cpp @@ -396,6 +396,7 @@ void DrawTriangle(const VertexData& v0, const VertexData& v1, const VertexData& float s = (v0.texturecoords.s() * w0 / v0.clippos.w + v1.texturecoords.s() * w1 / v1.clippos.w + v2.texturecoords.s() * w2 / v2.clippos.w) / den; float t = (v0.texturecoords.t() * w0 / v0.clippos.w + v1.texturecoords.t() * w1 / v1.clippos.w + v2.texturecoords.t() * w2 / v2.clippos.w) / den; + Vec3 prim_color_rgb(0, 0, 0); int prim_color_a = 0; Vec3 sec_color(0, 0, 0); diff --git a/GPU/Software/TransformUnit.cpp b/GPU/Software/TransformUnit.cpp index 919ebbacba..ee608cdb9b 100644 --- a/GPU/Software/TransformUnit.cpp +++ b/GPU/Software/TransformUnit.cpp @@ -41,20 +41,33 @@ ClipCoords TransformUnit::ViewToClip(const ViewCoords& coords) return ClipCoords(projection_matrix * coords4); } -ScreenCoords TransformUnit::ClipToScreen(const ClipCoords& coords) +static bool outside_range_flag = false; + +// TODO: This is ugly +static inline ScreenCoords ClipToScreenInternal(const ClipCoords& coords, bool set_flag = true) { ScreenCoords ret; + // TODO: Check for invalid parameters (x2 < x1, etc) float vpx1 = getFloat24(gstate.viewportx1); float vpx2 = getFloat24(gstate.viewportx2); float vpy1 = getFloat24(gstate.viewporty1); float vpy2 = getFloat24(gstate.viewporty2); float vpz1 = getFloat24(gstate.viewportz1); float vpz2 = getFloat24(gstate.viewportz2); - // TODO: Check for invalid parameters (x2 < x1, etc) - ret.x = (coords.x * vpx1 / coords.w + vpx2) * 16; // 16 = 0xFFFF / 4095.9375; - ret.y = (coords.y * vpy1 / coords.w + vpy2) * 16; // 16 = 0xFFFF / 4095.9375; - ret.z = (coords.z * vpz1 / coords.w + vpz2) * 16; // 16 = 0xFFFF / 4095.9375; - return ret; + + float retx = coords.x * vpx1 / coords.w + vpx2; + float rety = coords.y * vpy1 / coords.w + vpy2; + float retz = coords.z * vpz1 / coords.w + vpz2; + if (set_flag && (retx > 4095.9375f || rety > 4096.9375f || retz > 65535.f || retx < 0 || rety < 0 || retz < 0)) + outside_range_flag = true; + + // 16 = 0xFFFF / 4095.9375 + return ScreenCoords(retx * 16, rety * 16, retz); +} + +ScreenCoords TransformUnit::ClipToScreen(const ClipCoords& coords) +{ + return ClipToScreenInternal(coords, false); } DrawingCoords TransformUnit::ScreenToDrawing(const ScreenCoords& coords) @@ -106,7 +119,7 @@ static VertexData ReadVertex(VertexReader& vreader) ModelCoords mcoords(pos[0], pos[1], pos[2]); vertex.worldpos = WorldCoords(TransformUnit::ModelToWorld(mcoords)); vertex.clippos = ClipCoords(TransformUnit::ViewToClip(TransformUnit::WorldToView(vertex.worldpos))); - vertex.drawpos = DrawingCoords(TransformUnit::ScreenToDrawing(TransformUnit::ClipToScreen(vertex.clippos))); + vertex.drawpos = DrawingCoords(TransformUnit::ScreenToDrawing(ClipToScreenInternal(vertex.clippos))); if (vreader.hasNormal()) { vertex.worldnormal = TransformUnit::ModelToWorld(vertex.normal) - Vec3(gstate.worldMatrix[9], gstate.worldMatrix[10], gstate.worldMatrix[11]); @@ -158,26 +171,31 @@ void TransformUnit::SubmitPrimitive(void* vertices, void* indices, u32 prim_type for (int vtx = 0; vtx < vertex_count; vtx += vtcs_per_prim) { VertexData data[max_vtcs_per_prim]; - for (unsigned int i = 0; i < vtcs_per_prim; ++i) { + for (int i = 0; i < vtcs_per_prim; ++i) { if (indices) vreader.Goto(indices_16bit ? indices16[vtx+i] : indices8[vtx+i]); else vreader.Goto(vtx+i); data[i] = ReadVertex(vreader); + if (outside_range_flag) + break; + } + if (outside_range_flag) { + outside_range_flag = false; + continue; } switch (prim_type) { - case GE_PRIM_TRIANGLES: { - VertexData temp; -if (!gstate.getCullMode()) { - temp = data[2]; - data[2] = data[1]; - data[1] = temp; -} - Clipper::ProcessTriangle(data); - break;} + case GE_PRIM_TRIANGLES: + { + if (!gstate.getCullMode()) + Clipper::ProcessTriangle(data[2], data[1], data[0]); + else + Clipper::ProcessTriangle(data[0], data[1], data[2]); + break; + } case GE_PRIM_RECTANGLES: Clipper::ProcessQuad(data); @@ -186,6 +204,7 @@ if (!gstate.getCullMode()) { } } else if (prim_type == GE_PRIM_TRIANGLE_STRIP) { VertexData data[3]; + unsigned int skip_count = 2; // Don't draw a triangle when loading the first two vertices for (int vtx = 0; vtx < vertex_count; ++vtx) { if (indices) @@ -194,12 +213,24 @@ if (!gstate.getCullMode()) { vreader.Goto(vtx); data[vtx % 3] = ReadVertex(vreader); - - if (vtx < 2) + if (outside_range_flag) { + // Drop all primitives containing the current vertex + skip_count = 2; + outside_range_flag = false; continue; + } - // TODO: Should make sure to draw the vertices in the correct order! - Clipper::ProcessTriangle(data); + if (skip_count) { + --skip_count; + continue; + } + + // We need to reverse the vertex order for each second primitive, + // but we additionally need to do that for every primitive if CCW cullmode is used. + if ((!gstate.getCullMode()) ^ (vtx % 2)) + Clipper::ProcessTriangle(data[2], data[1], data[0]); + else + Clipper::ProcessTriangle(data[0], data[1], data[2]); } } }