diff --git a/src/Graphics_SoftMin.c b/src/Graphics_SoftMin.c index 64e6eda88..5d388558c 100644 --- a/src/Graphics_SoftMin.c +++ b/src/Graphics_SoftMin.c @@ -409,99 +409,6 @@ static void DrawSprite2D(Vertex* V0, Vertex* V1, Vertex* V2) { #define edgeFunction(ax,ay, bx,by, cx,cy) (((bx) - (ax)) * ((cy) - (ay)) - ((by) - (ay)) * ((cx) - (ax))) -static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) { - int x0 = (int)V0->x, y0 = (int)V0->y; - int x1 = (int)V1->x, y1 = (int)V1->y; - int x2 = (int)V2->x, y2 = (int)V2->y; - int minX = min(x0, min(x1, x2)); - int minY = min(y0, min(y1, y2)); - int maxX = max(x0, max(x1, x2)); - int maxY = max(y0, max(y1, y2)); - - // Reject triangles completely outside - if (maxX < 0 || minX > fb_maxX) return; - if (maxY < 0 || minY > fb_maxY) return; - - // Perform scissoring - minX = max(minX, 0); maxX = min(maxX, fb_maxX); - minY = max(minY, 0); maxY = min(maxY, fb_maxY); - - float u0 = V0->u * curTexWidth, u1 = V1->u * curTexWidth, u2 = V2->u * curTexWidth; - float v0 = V0->v * curTexHeight, v1 = V1->v * curTexHeight, v2 = V2->v * curTexHeight; - PackedCol color = V0->c; - - int area = edgeFunction(x0,y0, x1,y1, x2,y2); - float factor = 1.0f / area; - int x, y; - - // https://fgiesen.wordpress.com/2013/02/10/optimizing-the-basic-rasterizer/ - // Essentially these are the deltas of edge functions between X/Y and X/Y + 1 (i.e. one X/Y step) - int dx01 = y0 - y1, dy01 = x1 - x0; - int dx12 = y1 - y2, dy12 = x2 - x1; - int dx20 = y2 - y0, dy20 = x0 - x2; - - float bc0_start = edgeFunction(x1,y1, x2,y2, minX+0.5f,minY+0.5f); - float bc1_start = edgeFunction(x2,y2, x0,y0, minX+0.5f,minY+0.5f); - float bc2_start = edgeFunction(x0,y0, x1,y1, minX+0.5f,minY+0.5f); - - for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01) - { - float bc0 = bc0_start; - float bc1 = bc1_start; - float bc2 = bc2_start; - - for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01) - { - float ic0 = bc0 * factor; - float ic1 = bc1 * factor; - float ic2 = bc2 * factor; - - if (ic0 < 0 || ic1 < 0 || ic2 < 0) continue; - int cb_index = y * cb_stride + x; - - int R, G, B, A; - if (gfx_format == VERTEX_FORMAT_TEXTURED) { - float u = ic0 * u0 + ic1 * u1 + ic2 * u2; - float v = ic0 * v0 + ic1 * v1 + ic2 * v2; - int texX = ((int)u) & texWidthMask; - int texY = ((int)v) & texHeightMask; - int texIndex = texY * curTexWidth + texX; - - BitmapCol tColor = curTexPixels[texIndex]; - int a1 = PackedCol_A(color), a2 = BitmapCol_A(tColor); - A = ( a1 * a2 ) >> 8; - int r1 = PackedCol_R(color), r2 = BitmapCol_R(tColor); - R = ( r1 * r2 ) >> 8; - int g1 = PackedCol_G(color), g2 = BitmapCol_G(tColor); - G = ( g1 * g2 ) >> 8; - int b1 = PackedCol_B(color), b2 = BitmapCol_B(tColor); - B = ( b1 * b2 ) >> 8; - } else { - R = PackedCol_R(color); - G = PackedCol_G(color); - B = PackedCol_B(color); - A = PackedCol_A(color); - } - - if (gfx_alphaTest && A < 0x80) continue; - if (gfx_alphaBlend && A == 0) continue; - - if (gfx_alphaBlend && A != 255) { - BitmapCol dst = colorBuffer[cb_index]; - int dstR = BitmapCol_R(dst); - int dstG = BitmapCol_G(dst); - int dstB = BitmapCol_B(dst); - - R = (R * A + dstR * (255 - A)) >> 8; - G = (G * A + dstG * (255 - A)) >> 8; - B = (B * A + dstB * (255 - A)) >> 8; - } - - colorBuffer[cb_index] = BitmapCol_Make(R, G, B, 0xFF); - } - } -} - #define MultiplyColors(vColor, tColor) \ a1 = PackedCol_A(vColor); \ a2 = BitmapCol_A(tColor); \ @@ -519,6 +426,73 @@ static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) { b2 = BitmapCol_B(tColor); \ B = ( b1 * b2 ) >> 8; \ +static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) { + int x0 = (int)V0->x, y0 = (int)V0->y; + int x1 = (int)V1->x, y1 = (int)V1->y; + int x2 = (int)V2->x, y2 = (int)V2->y; + int minX = min(x0, min(x1, x2)); + int minY = min(y0, min(y1, y2)); + int maxX = max(x0, max(x1, x2)); + int maxY = max(y0, max(y1, y2)); + + // Reject triangles completely outside + if (maxX < 0 || minX > fb_maxX) return; + if (maxY < 0 || minY > fb_maxY) return; + + // Perform scissoring + minX = max(minX, 0); maxX = min(maxX, fb_maxX); + minY = max(minY, 0); maxY = min(maxY, fb_maxY); + + // https://fgiesen.wordpress.com/2013/02/10/optimizing-the-basic-rasterizer/ + // Essentially these are the deltas of edge functions between X/Y and X/Y + 1 (i.e. one X/Y step) + int dx01 = y0 - y1, dy01 = x1 - x0; + int dx12 = y1 - y2, dy12 = x2 - x1; + int dx20 = y2 - y0, dy20 = x0 - x2; + + // TODO still too slow, costs 300 ms/frame + float bc0_start = edgeFunction(x1,y1, x2,y2, minX+0.5f,minY+0.5f); + float bc1_start = edgeFunction(x2,y2, x0,y0, minX+0.5f,minY+0.5f); + float bc2_start = edgeFunction(x0,y0, x1,y1, minX+0.5f,minY+0.5f); + + PackedCol color = V0->c; + int R, G, B, A, x, y; + int a1, r1, g1, b1; + int a2, r2, g2, b2; + + if (gfx_format != VERTEX_FORMAT_TEXTURED) { + R = PackedCol_R(color); + G = PackedCol_G(color); + B = PackedCol_B(color); + A = PackedCol_A(color); + } else { + /* Always use a single pixel */ + float rawY0 = V0->v * curTexHeight; + float rawY1 = V1->v * curTexHeight; + + float rawY = min(rawY0, rawY1); + int texY = (int)(rawY + 0.01f) & texHeightMask; + MultiplyColors(color, curTexPixels[texY * curTexWidth]); + } + + if (A == 0) return; + color = BitmapCol_Make(R, G, B, 0xFF); + + for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01) + { + float bc0 = bc0_start; + float bc1 = bc1_start; + float bc2 = bc2_start; + + for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01) + { + if (bc0 < 0 || bc1 < 0 || bc2 < 0) continue; + int cb_index = y * cb_stride + x; + + colorBuffer[cb_index] = color; + } + } +} + static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) { int x0 = (int)V0->x, y0 = (int)V0->y; int x1 = (int)V1->x, y1 = (int)V1->y; @@ -541,52 +515,38 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) { // Perform scissoring minX = max(minX, 0); maxX = min(maxX, fb_maxX); minY = max(minY, 0); maxY = min(maxY, fb_maxY); - - // NOTE: W in frag variables below is actually 1/W - float w0 = V0->w, w1 = V1->w, w2 = V2->w; - - // TODO proper clipping - if (w0 <= 0 || w1 <= 0 || w2 <= 0) { - return; - } - - float z0 = V0->z, z1 = V1->z, z2 = V2->z; - PackedCol color = V0->c; - float v0 = V0->v * curTexHeight, v1 = V1->v * curTexHeight; // https://fgiesen.wordpress.com/2013/02/10/optimizing-the-basic-rasterizer/ // Essentially these are the deltas of edge functions between X/Y and X/Y + 1 (i.e. one X/Y step) - int dx01 = y0 - y1, dy01 = x1 - x0; - int dx12 = y1 - y2, dy12 = x2 - x1; - int dx20 = y2 - y0, dy20 = x0 - x2; + int dx01 = y0 - y1, dy01 = x1 - x0; + int dx12 = y1 - y2, dy12 = x2 - x1; + int dx20 = y2 - y0, dy20 = x0 - x2; int bc0_start = edgeFunction(x1,y1, x2,y2, minX, minY); int bc1_start = edgeFunction(x2,y2, x0,y0, minX, minY); int bc2_start = edgeFunction(x0,y0, x1,y1, minX, minY); + PackedCol color = V0->c; int R, G, B, A, x, y; int a1, r1, g1, b1; int a2, r2, g2, b2; - cc_bool texturing = gfx_format == VERTEX_FORMAT_TEXTURED; - if (!texturing) { + if (gfx_format != VERTEX_FORMAT_TEXTURED) { R = PackedCol_R(color); G = PackedCol_G(color); B = PackedCol_B(color); A = PackedCol_A(color); } else { /* Always use a single pixel */ - float rawY0 = v0; - float rawY1 = v1; + float rawY0 = V0->v * curTexHeight; + float rawY1 = V1->v * curTexHeight; float rawY = min(rawY0, rawY1); int texY = (int)(rawY + 0.01f) & texHeightMask; MultiplyColors(color, curTexPixels[texY * curTexWidth]); - texturing = false; } - if (!colWrite) return; - if (gfx_alphaTest && A < 0x80) return; + if (gfx_alphaTest && A == 0) return; color = BitmapCol_Make(R, G, B, 0xFF); for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01) @@ -598,7 +558,6 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) { for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01) { if ((bc0 | bc1 | bc2) < 0) continue; - int cb_index = y * cb_stride + x; if (!gfx_alphaBlend) { @@ -606,15 +565,12 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) { continue; } - BitmapCol dst = colorBuffer[cb_index]; - int dstR = BitmapCol_R(dst); - int dstG = BitmapCol_G(dst); - int dstB = BitmapCol_B(dst); - // Hardcode for alpha of 128 - int finR = (R + dstR) >> 1; - int finG = (G + dstG) >> 1; - int finB = (B + dstB) >> 1; + BitmapCol dst = colorBuffer[cb_index]; + int finR = (R + BitmapCol_R(dst)) >> 1; + int finG = (G + BitmapCol_G(dst)) >> 1; + int finB = (B + BitmapCol_B(dst)) >> 1; + colorBuffer[cb_index] = BitmapCol_Make(finR, finG, finB, 0xFF); } } @@ -909,7 +865,7 @@ void DrawQuads(int startVertex, int verticesCount, DrawHints hints) { DrawTriangle2D(&vertices[0], &vertices[2], &vertices[1]); DrawTriangle2D(&vertices[2], &vertices[0], &vertices[3]); } - } else { + } else if (colWrite) { // 4 vertices = 1 quad = 2 triangles for (i = 0; i < verticesCount / 4; i++, j += 4) {