Skip to content

Commit 900d76d

Browse files
Avoid a per-pixel branch in SoftMin GPU renderer
1 parent 01ff9ec commit 900d76d

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/Graphics_SoftMin.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -480,32 +480,24 @@ static void DrawTriangle3D(Vertex* V0, Vertex* V1, Vertex* V2) {
480480
}
481481

482482
if (gfx_alphaTest && A == 0) return;
483-
color = BitmapCol_Make(R, G, B, 0xFF);
484-
485-
for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01)
486-
{
487-
int bc0 = bc0_start;
488-
int bc1 = bc1_start;
489-
int bc2 = bc2_start;
490-
491-
for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01)
492-
{
493-
if ((bc0 | bc1 | bc2) < 0) continue;
494-
int cb_index = y * cb_stride + x;
495-
496-
if (!gfx_alphaBlend) {
497-
colorBuffer[cb_index] = color;
498-
continue;
499-
}
483+
500484

501-
// Hardcode for alpha of 128
502-
BitmapCol dst = colorBuffer[cb_index];
503-
int finR = (R + BitmapCol_R(dst)) >> 1;
504-
int finG = (G + BitmapCol_G(dst)) >> 1;
505-
int finB = (B + BitmapCol_B(dst)) >> 1;
485+
if (!gfx_alphaBlend) {
486+
#define PIXEL_PLOT_FUNC(index, x, y) \
487+
colorBuffer[index] = color;
506488

507-
colorBuffer[cb_index] = BitmapCol_Make(finR, finG, finB, 0xFF);
508-
}
489+
color = BitmapCol_Make(R, G, B, 0xFF);
490+
#include "Graphics_SoftMin.tri.i"
491+
} else {
492+
// Hardcode for alpha of 128
493+
#define PIXEL_PLOT_FUNC(index, x, y) \
494+
BitmapCol dst = colorBuffer[index]; \
495+
int finR = (R + BitmapCol_R(dst)) >> 1; \
496+
int finG = (G + BitmapCol_G(dst)) >> 1; \
497+
int finB = (B + BitmapCol_B(dst)) >> 1; \
498+
colorBuffer[index] = BitmapCol_Make(finR, finG, finB, 0xFF);
499+
500+
#include "Graphics_SoftMin.tri.i"
509501
}
510502
}
511503

src/Graphics_SoftMin.tri.i

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
for (y = minY; y <= maxY; y++, bc0_start += dy12, bc1_start += dy20, bc2_start += dy01)
2+
{
3+
int bc0 = bc0_start;
4+
int bc1 = bc1_start;
5+
int bc2 = bc2_start;
6+
7+
for (x = minX; x <= maxX; x++, bc0 += dx12, bc1 += dx20, bc2 += dx01)
8+
{
9+
if ((bc0 | bc1 | bc2) < 0) continue;
10+
11+
int cb_index = y * cb_stride + x;
12+
PIXEL_PLOT_FUNC(cb_index, x, y);
13+
}
14+
}

0 commit comments

Comments
 (0)