tweak the pipeline flow regarding the face culling

avoids misprediction, improves vectorization if possible
This commit is contained in:
Bigfoot71 2025-05-15 02:54:32 +02:00
parent fd37d4f528
commit 515e894076

78
src/external/rlsw.h vendored
View File

@ -2291,14 +2291,8 @@ static inline bool sw_polygon_clip(sw_vertex_t polygon[SW_MAX_CLIPPED_POLYGON_VE
/* === Triangle Rendering Part === */
static inline void sw_triangle_clip_and_project(void)
static inline bool sw_triangle_face_culling(void)
{
sw_vertex_t* polygon = RLSW.vertexBuffer;
int* vertexCounter = &RLSW.vertexCounter;
// Step 1: Face culling - discard triangles facing away
if (RLSW.stateFlags & SW_STATE_CULL_FACE) {
// NOTE: Face culling is done before clipping to avoid unnecessary computations.
// However, culling requires NDC coordinates, while clipping must be done
// in homogeneous space to correctly interpolate newly generated vertices.
@ -2306,24 +2300,32 @@ static inline void sw_triangle_clip_and_project(void)
// - Once before clipping for face culling.
// - Again after clipping for the new vertices.
const float invW0 = 1.0f / polygon[0].homogeneous[3];
const float invW1 = 1.0f / polygon[1].homogeneous[3];
const float invW2 = 1.0f / polygon[2].homogeneous[3];
// Preload homogeneous coordinates into local variables
const float* h0 = RLSW.vertexBuffer[0].homogeneous;
const float* h1 = RLSW.vertexBuffer[1].homogeneous;
const float* h2 = RLSW.vertexBuffer[2].homogeneous;
// Compute 1/w once and delay divisions
const float invW0 = 1.0f / h0[3];
const float invW1 = 1.0f / h1[3];
const float invW2 = 1.0f / h2[3];
// Compute the signed 2D area (cross product in Z)
const float x0 = polygon[0].homogeneous[0] * invW0, y0 = polygon[0].homogeneous[1] * invW0;
const float x1 = polygon[1].homogeneous[0] * invW1, y1 = polygon[1].homogeneous[1] * invW1;
const float x2 = polygon[2].homogeneous[0] * invW2, y2 = polygon[2].homogeneous[1] * invW2;
const float x0 = h0[0] * invW0, y0 = h0[1] * invW0;
const float x1 = h1[0] * invW1, y1 = h1[1] * invW1;
const float x2 = h2[0] * invW2, y2 = h2[1] * invW2;
const float sgnArea = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
// Discard the triangle if it faces the culled direction
if ((RLSW.cullFace == SW_FRONT) ? (sgnArea >= 0) : (sgnArea <= 0)) {
*vertexCounter = 0;
return;
}
return (RLSW.cullFace == SW_FRONT)
? (sgnArea < 0) : (sgnArea > 0);
}
// Step 2: Clipping and perspective projection
static inline void sw_triangle_clip_and_project(void)
{
sw_vertex_t* polygon = RLSW.vertexBuffer;
int* vertexCounter = &RLSW.vertexCounter;
if (sw_polygon_clip(polygon, vertexCounter) && *vertexCounter >= 3) {
// Transformation to screen space and normalization
@ -2628,6 +2630,12 @@ DEFINE_TRIANGLE_RASTER(sw_triangle_raster_TEX_DEPTH_BLEND, sw_triangle_raster_sc
static inline void sw_triangle_render(void)
{
if (RLSW.stateFlags & SW_STATE_CULL_FACE) {
if (!sw_triangle_face_culling()) {
return;
}
}
sw_triangle_clip_and_project();
if (RLSW.vertexCounter < 3) {
@ -2677,23 +2685,17 @@ static inline void sw_triangle_render(void)
/* === Quad Rendering Part === */
static inline void sw_quad_clip_and_project()
static inline bool sw_quad_face_culling(void)
{
sw_vertex_t* polygon = RLSW.vertexBuffer;
int* vertexCounter = &RLSW.vertexCounter;
// Step 1: Face culling - discard quads facing away
if (RLSW.stateFlags & SW_STATE_CULL_FACE) {
// NOTE: We use Green's theorem (signed polygon area) instead of triangulation.
// This is faster but only reliable if the quad is convex and not self-intersecting.
// For face culling purposes, this approximation is acceptable.
// Preload homogeneous coordinates into local variables
const float* h0 = polygon[0].homogeneous;
const float* h1 = polygon[1].homogeneous;
const float* h2 = polygon[2].homogeneous;
const float* h3 = polygon[3].homogeneous;
const float* h0 = RLSW.vertexBuffer[0].homogeneous;
const float* h1 = RLSW.vertexBuffer[1].homogeneous;
const float* h2 = RLSW.vertexBuffer[2].homogeneous;
const float* h3 = RLSW.vertexBuffer[3].homogeneous;
// Compute 1/w once and delay divisions
const float invW0 = 1.0f / h0[3];
@ -2717,13 +2719,15 @@ static inline void sw_quad_clip_and_project()
+ (x3 * y0 - x0 * y3);
// Perform face culling based on area sign
if ((RLSW.cullFace == SW_FRONT) ? (sgnArea >= 0.0f) : (sgnArea <= 0.0f)) {
*vertexCounter = 0;
return;
}
return (RLSW.cullFace == SW_FRONT)
? (sgnArea < 0.0f) : (sgnArea > 0.0f);
}
// Step 2: Clipping and perspective projection
static inline void sw_quad_clip_and_project(void)
{
sw_vertex_t* polygon = RLSW.vertexBuffer;
int* vertexCounter = &RLSW.vertexCounter;
if (sw_polygon_clip(polygon, vertexCounter) && *vertexCounter >= 4) {
// Transformation to screen space and normalization
@ -2758,6 +2762,12 @@ static inline void sw_quad_clip_and_project()
static inline void sw_quad_render(void)
{
if (RLSW.stateFlags & SW_STATE_CULL_FACE) {
if (!sw_quad_face_culling()) {
return;
}
}
sw_quad_clip_and_project();
if (RLSW.vertexCounter < 4) {