Browse Source

Small optimization to BSP tree traversal, by making the child node selection branchless

tess_experiment
Nico de Poel 3 years ago
parent
commit
59d838f501
  1. 3
      ps1bsp.h
  2. 15
      world.c

3
ps1bsp.h

@ -92,8 +92,7 @@ typedef struct
typedef struct
{
int planeId;
short front;
short back;
short children[2];
// TODO: add bounding box for frustum culling

15
world.c

@ -287,16 +287,9 @@ static void world_drawnode(const world_t *world, short nodeIdx, u_char *pvs)
short dist = world_pointPlaneDist(&cam_pos, plane);
// Draw child nodes in front-to-back order; adding faces to the OT will reverse the drawing order
if (dist > 0)
{
world_drawnode(world, node->front, pvs);
world_drawnode(world, node->back, pvs);
}
else
{
world_drawnode(world, node->back, pvs);
world_drawnode(world, node->front, pvs);
}
char order = dist < 0;
world_drawnode(world, node->children[order], pvs);
world_drawnode(world, node->children[order ^ 1], pvs);
}
// Decompress PVS data for the given leaf ID and store it in RAM at the given buffer pointer location.
@ -354,7 +347,7 @@ static u_short world_leafAtPoint(const world_t *world, const VECTOR *point)
const ps1bsp_plane_t *plane = &world->planes[node->planeId];
short dist = world_pointPlaneDist(point, plane);
nodeIdx = dist > 0 ? node->front : node->back; // TODO: this can be done branchless with (dist < 0)^1
nodeIdx = node->children[(dist > 0) ^ 1];
}
return ~nodeIdx;

Loading…
Cancel
Save