review line raster

This commit is contained in:
Bigfoot71 2025-05-18 21:17:23 +02:00
parent dd48df432e
commit e75329ab85

153
src/external/rlsw.h vendored
View file

@ -3376,133 +3376,140 @@ static inline void FUNC_NAME(const sw_vertex_t* v0, const sw_vertex_t* v1) \
int x2 = (int)(v1->screen[0] + 0.5f); \ int x2 = (int)(v1->screen[0] + 0.5f); \
int y2 = (int)(v1->screen[1] + 0.5f); \ int y2 = (int)(v1->screen[1] + 0.5f); \
\ \
float z1 = v0->homogeneous[2]; \ int dx = x2 - x1; \
float z2 = v1->homogeneous[2]; \ int dy = y2 - y1; \
\ \
int shortLen = y2 - y1; \ /* Handling of lines that are more horizontal or vertical */ \
int longLen = x2 - x1; \
bool yLonger = 0; \
\ \
if (abs(shortLen) > abs(longLen)) { \ if (dx == 0 && dy == 0) { \
int tmp = shortLen; \ /* TODO: A point should be rendered here */ \
shortLen = longLen; \ return; \
longLen = tmp; \
yLonger = 1; \
} \ } \
\ \
float invEndVal = 1.0f / longLen; \ bool yLonger = (abs(dy) > abs(dx)); \
int endVal = longLen; \ int longLen, shortLen; \
int sgnInc = 1; \
\ \
if (longLen < 0) { \ if (yLonger) { \
longLen = -longLen; \ longLen = dy; \
sgnInc = -1; \ shortLen = dx; \
} else { \
longLen = dx; \
shortLen = dy; \
} \ } \
\ \
int decInc = (longLen == 0) ? 0 \ /* Handling of traversal direction */ \
: (shortLen << 16) / longLen; \ \
int sgnInc = (longLen < 0) ? -1 : 1; \
int abslongLen = abs(longLen); \
\
/* Calculation of the increment step for the shorter coordinate */ \
\
int decInc = 0; \
if (abslongLen != 0) { \
decInc = (shortLen << 16) / abslongLen; \
} \
\
float longLenRcp = (abslongLen != 0) ? (1.0f / abslongLen) : 0.0f; \
\
/* Calculation of interpolation steps */ \
\
const float zStep = (v1->homogeneous[2] - v0->homogeneous[2]) * longLenRcp; \
const float rStep = (v1->color[0] - v0->color[0]) * longLenRcp; \
const float gStep = (v1->color[1] - v0->color[1]) * longLenRcp; \
const float bStep = (v1->color[2] - v0->color[2]) * longLenRcp; \
const float aStep = (v1->color[3] - v0->color[3]) * longLenRcp; \
\
float z = v0->homogeneous[2]; \
\
float color[4] = { \
v0->color[0], \
v0->color[1], \
v0->color[2], \
v0->color[3] \
}; \
\ \
const int fbWidth = RLSW.framebuffer.width; \ const int fbWidth = RLSW.framebuffer.width; \
const float zDiff = z2 - z1; \ void* cBuffer = RLSW.framebuffer.color; \
\ void* dBuffer = RLSW.framebuffer.depth; \
uint8_t* colorBuffer = RLSW.framebuffer.color; \
uint16_t* depthBuffer = RLSW.framebuffer.depth; \
\ \
int j = 0; \ int j = 0; \
if (yLonger) { \
for (int i = 0; i != endVal; i += sgnInc, j += decInc) { \
float t = (float)i * invEndVal; \
\ \
int x = x1 + (j >> 16); \ if (yLonger) \
int y = y1 + i; \ { \
float z = z1 + t * zDiff; \ for (int i = 0; i != longLen; i += sgnInc) \
int offset = y * fbWidth + x; \ { \
int offset = (y1 + i) * fbWidth + (x1 + (j >> 16)); \
\ \
void* dptr = sw_framebuffer_get_depth_addr( \ void* dptr = sw_framebuffer_get_depth_addr(dBuffer, offset); \
depthBuffer, offset \
); \
\ \
if (ENABLE_DEPTH_TEST) { \ if (ENABLE_DEPTH_TEST) { \
float depth = sw_framebuffer_read_depth(dptr); \ float depth = sw_framebuffer_read_depth(dptr); \
if (z > depth) continue; \ if (z > depth) goto discardA; \
} \ } \
\ \
sw_framebuffer_write_depth(dptr, z); \ sw_framebuffer_write_depth(dptr, z); \
\ \
void* cptr = sw_framebuffer_get_color_addr( \ void* cptr = sw_framebuffer_get_color_addr(cBuffer, offset); \
colorBuffer, offset \
); \
\ \
if (ENABLE_COLOR_BLEND) \ if (ENABLE_COLOR_BLEND) \
{ \ { \
float dstColor[4]; \ float dstColor[4]; \
sw_framebuffer_read_color(dstColor, cptr); \ sw_framebuffer_read_color(dstColor, cptr); \
\ \
float srcColor[4]; \ sw_blend_colors(dstColor, color); \
srcColor[0] = sw_lerp(v0->color[0], v1->color[0], t); \
srcColor[1] = sw_lerp(v0->color[1], v1->color[1], t); \
srcColor[2] = sw_lerp(v0->color[2], v1->color[2], t); \
srcColor[3] = sw_lerp(v0->color[3], v1->color[3], t); \
\
sw_blend_colors(dstColor, srcColor); \
sw_framebuffer_write_color(cptr, dstColor); \ sw_framebuffer_write_color(cptr, dstColor); \
} \ } \
else \ else \
{ \ { \
float color[3]; \
color[0] = sw_lerp(v0->color[0], v1->color[0], t); \
color[1] = sw_lerp(v0->color[1], v1->color[1], t); \
color[2] = sw_lerp(v0->color[2], v1->color[2], t); \
sw_framebuffer_write_color(cptr, color); \ sw_framebuffer_write_color(cptr, color); \
} \ } \
\
discardA: \
j += decInc; \
z += zStep; \
color[0] += rStep; \
color[1] += gStep; \
color[2] += bStep; \
color[3] += aStep; \
} \ } \
} \ } \
else { \ else \
for (int i = 0; i != endVal; i += sgnInc, j += decInc) { \ { \
float t = (float)i * invEndVal; \ for (int i = 0; i != longLen; i += sgnInc) \
{ \
int offset = (y1 + (j >> 16)) * fbWidth + (x1 + i); \
\ \
int x = x1 + i; \ void* dptr = sw_framebuffer_get_depth_addr(dBuffer, offset); \
int y = y1 + (j >> 16); \
float z = z1 + t * zDiff; \
int offset = y * fbWidth + x; \
\
void* dptr = sw_framebuffer_get_depth_addr( \
depthBuffer, offset \
); \
\ \
if (ENABLE_DEPTH_TEST) { \ if (ENABLE_DEPTH_TEST) { \
float depth = sw_framebuffer_read_depth(dptr); \ float depth = sw_framebuffer_read_depth(dptr); \
if (z > depth) continue; \ if (z > depth) goto discardB; \
} \ } \
\ \
sw_framebuffer_write_depth(dptr, z); \ sw_framebuffer_write_depth(dptr, z); \
\ \
void* cptr = sw_framebuffer_get_color_addr( \ void* cptr = sw_framebuffer_get_color_addr(cBuffer, offset); \
colorBuffer, offset \
); \
\ \
if (ENABLE_COLOR_BLEND) \ if (ENABLE_COLOR_BLEND) \
{ \ { \
float dstColor[4]; \ float dstColor[4]; \
sw_framebuffer_read_color(dstColor, cptr); \ sw_framebuffer_read_color(dstColor, cptr); \
\ \
float srcColor[4]; \ sw_blend_colors(dstColor, color); \
srcColor[0] = sw_lerp(v0->color[0], v1->color[0], t); \
srcColor[1] = sw_lerp(v0->color[1], v1->color[1], t); \
srcColor[2] = sw_lerp(v0->color[2], v1->color[2], t); \
srcColor[3] = sw_lerp(v0->color[3], v1->color[3], t); \
\
sw_blend_colors(dstColor, srcColor); \
sw_framebuffer_write_color(cptr, dstColor); \ sw_framebuffer_write_color(cptr, dstColor); \
} \ } \
else \ else \
{ \ { \
float color[3]; \
color[0] = sw_lerp(v0->color[0], v1->color[0], t); \
color[1] = sw_lerp(v0->color[1], v1->color[1], t); \
color[2] = sw_lerp(v0->color[2], v1->color[2], t); \
sw_framebuffer_write_color(cptr, color); \ sw_framebuffer_write_color(cptr, color); \
} \ } \
\
discardB: \
j += decInc; \
z += zStep; \
color[0] += rStep; \
color[1] += gStep; \
color[2] += bStep; \
color[3] += aStep; \
} \ } \
} \ } \
} }