GBA: Now down to ~0.94 seconds a frame

This commit is contained in:
UnknownShadow200 2025-08-17 18:46:17 +10:00
parent cbba51ddce
commit 4fb5cec968

View File

@ -409,6 +409,23 @@ 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))) #define edgeFunction(ax,ay, bx,by, cx,cy) (((bx) - (ax)) * ((cy) - (ay)) - ((by) - (ay)) * ((cx) - (ax)))
#define MultiplyColors(vColor, tColor) \
a1 = PackedCol_A(vColor); \
a2 = BitmapCol_A(tColor); \
A = ( a1 * a2 ) >> 8; \
\
r1 = PackedCol_R(vColor); \
r2 = BitmapCol_R(tColor); \
R = ( r1 * r2 ) >> 8; \
\
g1 = PackedCol_G(vColor); \
g2 = BitmapCol_G(tColor); \
G = ( g1 * g2 ) >> 8; \
\
b1 = PackedCol_B(vColor); \
b2 = BitmapCol_B(tColor); \
B = ( b1 * b2 ) >> 8; \
static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) { static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) {
int x0 = (int)V0->x, y0 = (int)V0->y; int x0 = (int)V0->x, y0 = (int)V0->y;
int x1 = (int)V1->x, y1 = (int)V1->y; int x1 = (int)V1->x, y1 = (int)V1->y;
@ -426,24 +443,40 @@ static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) {
minX = max(minX, 0); maxX = min(maxX, fb_maxX); minX = max(minX, 0); maxX = min(maxX, fb_maxX);
minY = max(minY, 0); maxY = min(maxY, fb_maxY); 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/ // 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) // 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 dx01 = y0 - y1, dy01 = x1 - x0;
int dx12 = y1 - y2, dy12 = x2 - x1; int dx12 = y1 - y2, dy12 = x2 - x1;
int dx20 = y2 - y0, dy20 = x0 - x2; 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 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 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); 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) for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01)
{ {
float bc0 = bc0_start; float bc0 = bc0_start;
@ -452,73 +485,14 @@ static void DrawTriangle2D(Vertex* V0, Vertex* V1, Vertex* V2) {
for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01) for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01)
{ {
float ic0 = bc0 * factor; if (bc0 < 0 || bc1 < 0 || bc2 < 0) continue;
float ic1 = bc1 * factor;
float ic2 = bc2 * factor;
if (ic0 < 0 || ic1 < 0 || ic2 < 0) continue;
int cb_index = y * cb_stride + x; int cb_index = y * cb_stride + x;
int R, G, B, A; colorBuffer[cb_index] = color;
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); \
A = ( a1 * a2 ) >> 8; \
\
r1 = PackedCol_R(vColor); \
r2 = BitmapCol_R(tColor); \
R = ( r1 * r2 ) >> 8; \
\
g1 = PackedCol_G(vColor); \
g2 = BitmapCol_G(tColor); \
G = ( g1 * g2 ) >> 8; \
\
b1 = PackedCol_B(vColor); \
b2 = BitmapCol_B(tColor); \
B = ( b1 * b2 ) >> 8; \
static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) { static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) {
int x0 = (int)V0->x, y0 = (int)V0->y; int x0 = (int)V0->x, y0 = (int)V0->y;
int x1 = (int)V1->x, y1 = (int)V1->y; int x1 = (int)V1->x, y1 = (int)V1->y;
@ -542,18 +516,6 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) {
minX = max(minX, 0); maxX = min(maxX, fb_maxX); minX = max(minX, 0); maxX = min(maxX, fb_maxX);
minY = max(minY, 0); maxY = min(maxY, fb_maxY); 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/ // 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) // 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 dx01 = y0 - y1, dy01 = x1 - x0;
@ -564,29 +526,27 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) {
int bc1_start = edgeFunction(x2,y2, x0,y0, minX, minY); int bc1_start = edgeFunction(x2,y2, x0,y0, minX, minY);
int bc2_start = edgeFunction(x0,y0, x1,y1, minX, minY); int bc2_start = edgeFunction(x0,y0, x1,y1, minX, minY);
PackedCol color = V0->c;
int R, G, B, A, x, y; int R, G, B, A, x, y;
int a1, r1, g1, b1; int a1, r1, g1, b1;
int a2, r2, g2, b2; 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); R = PackedCol_R(color);
G = PackedCol_G(color); G = PackedCol_G(color);
B = PackedCol_B(color); B = PackedCol_B(color);
A = PackedCol_A(color); A = PackedCol_A(color);
} else { } else {
/* Always use a single pixel */ /* Always use a single pixel */
float rawY0 = v0; float rawY0 = V0->v * curTexHeight;
float rawY1 = v1; float rawY1 = V1->v * curTexHeight;
float rawY = min(rawY0, rawY1); float rawY = min(rawY0, rawY1);
int texY = (int)(rawY + 0.01f) & texHeightMask; int texY = (int)(rawY + 0.01f) & texHeightMask;
MultiplyColors(color, curTexPixels[texY * curTexWidth]); MultiplyColors(color, curTexPixels[texY * curTexWidth]);
texturing = false;
} }
if (!colWrite) return; if (gfx_alphaTest && A == 0) return;
if (gfx_alphaTest && A < 0x80) return;
color = BitmapCol_Make(R, G, B, 0xFF); color = BitmapCol_Make(R, G, B, 0xFF);
for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01) 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) for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01)
{ {
if ((bc0 | bc1 | bc2) < 0) continue; if ((bc0 | bc1 | bc2) < 0) continue;
int cb_index = y * cb_stride + x; int cb_index = y * cb_stride + x;
if (!gfx_alphaBlend) { if (!gfx_alphaBlend) {
@ -606,15 +565,12 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) {
continue; 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 // Hardcode for alpha of 128
int finR = (R + dstR) >> 1; BitmapCol dst = colorBuffer[cb_index];
int finG = (G + dstG) >> 1; int finR = (R + BitmapCol_R(dst)) >> 1;
int finB = (B + dstB) >> 1; int finG = (G + BitmapCol_G(dst)) >> 1;
int finB = (B + BitmapCol_B(dst)) >> 1;
colorBuffer[cb_index] = BitmapCol_Make(finR, finG, finB, 0xFF); 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[0], &vertices[2], &vertices[1]);
DrawTriangle2D(&vertices[2], &vertices[0], &vertices[3]); DrawTriangle2D(&vertices[2], &vertices[0], &vertices[3]);
} }
} else { } else if (colWrite) {
// 4 vertices = 1 quad = 2 triangles // 4 vertices = 1 quad = 2 triangles
for (i = 0; i < verticesCount / 4; i++, j += 4) for (i = 0; i < verticesCount / 4; i++, j += 4)
{ {