Make culling a bit faster

This commit is contained in:
Miloslav Ciz 2025-02-04 01:04:36 +01:00
parent 2c2f1a4c9f
commit f1b80bf328
2 changed files with 30 additions and 24 deletions

View file

@ -1,5 +1,6 @@
=========== GENERAL ==============
- countdown and finish text sometimes seem to jump horizontally somehow
- try to speed up the slow culling
- make a small txt game menual
- test:

View file

@ -583,25 +583,38 @@ int _LCR_rendererQuadCoversTri(const S3L_Unit quad[8], const S3L_Unit tri[6])
*/
uint8_t _LCR_rendererCheckMapTriCover(const S3L_Index *t1, const S3L_Index *t2)
{
if ((t1[0] == t2[0] || t1[0] == t2[1] || t1[0] == t2[2]) &&
(t1[1] == t2[0] || t1[1] == t2[1] || t1[1] == t2[2]) &&
(t1[2] == t2[0] || t1[2] == t2[1] || t1[2] == t2[2]))
S3L_Unit *vertices[6];
// critical place, manually unrolled loop:
vertices[0] = LCR_renderer.mapVerts + 3 * t1[0];
vertices[3] = LCR_renderer.mapVerts + 3 * t2[0];
if ( // quick manhattan distance bailout condition
S3L_abs(vertices[0][0] - vertices[3][0]) +
S3L_abs(vertices[0][1] - vertices[3][1]) +
S3L_abs(vertices[0][2] - vertices[3][2]) >
(3 * LCR_RENDERER_UNIT / 2))
return 0;
vertices[1] = LCR_renderer.mapVerts + 3 * t1[1];
vertices[4] = LCR_renderer.mapVerts + 3 * t2[1];
vertices[2] = LCR_renderer.mapVerts + 3 * t1[2];
vertices[5] = LCR_renderer.mapVerts + 3 * t2[2];
if ( // same vert indices?
(((t1[0] == t2[0]) | (t1[0] == t2[1]) | (t1[0] == t2[2]))) &
(((t1[1] == t2[0]) | (t1[1] == t2[1]) | (t1[1] == t2[2]))) &
(((t1[2] == t2[0]) | (t1[2] == t2[1]) | (t1[2] == t2[2]))))
return 0x03;
uint8_t result = 0;
int plane = -1;
S3L_Unit *vertices[6];
for (int i = 0; i < 3; ++i)
{
vertices[i] = LCR_renderer.mapVerts + 3 * t1[i];
vertices[3 + i] = LCR_renderer.mapVerts + 3 * t2[i];
}
for (int i = 0; i < 3; ++i)
if (vertices[0][i] == vertices[1][i] && vertices[1][i] == vertices[2][i] &&
vertices[2][i] == vertices[3][i] && vertices[3][i] == vertices[4][i] &&
vertices[4][i] == vertices[5][i])
vertices[2][i] == vertices[3][i] && vertices[3][i] == vertices[4][i] &&
vertices[4][i] == vertices[5][i])
{
plane = i;
break;
@ -609,11 +622,6 @@ uint8_t _LCR_rendererCheckMapTriCover(const S3L_Index *t1, const S3L_Index *t2)
if (plane >= 0) // both triangles in the same plane => then do more checks
{
if (S3L_abs(vertices[0][0] - vertices[3][0]) +
S3L_abs(vertices[0][1] - vertices[3][1]) +
S3L_abs(vertices[0][2] - vertices[3][2]) > 2 * LCR_RENDERER_UNIT)
return 0; // quick manhattan distance bailout condition
for (int j = 0; j < 2; ++j)
{
S3L_Unit points2D[14]; // tri1, quad (tri2 + 1 extra vert)
@ -738,13 +746,11 @@ void _LCR_cullHiddenMapTris(void)
if ((cover & 0x02) && (j < LCR_renderer.mapModel.triangleCount - n))
{
_LCR_rendererSwapMapTris(j,
LCR_renderer.mapModel.triangleCount - 1 - n);
_LCR_rendererSwapMapTris(j,LCR_renderer.mapModel.triangleCount - 1 - n);
n++;
}
t2 += 3; // check next triangle
t2 += 3; // move to the next triangle
}
if (t1Covered)
@ -863,7 +869,8 @@ uint8_t _LCR_buildMapModel(void)
for (int j = 0; j < LCR_currentMap.blockCount; ++j)
{
if ((j + 1) % LCR_SETTING_TRIANGLE_CULLING_PERIOD == 0)
if (((j + 1) % LCR_SETTING_TRIANGLE_CULLING_PERIOD == 0) ||
(LCR_renderer.mapModel.triangleCount == LCR_SETTING_MAX_MAP_TRIANGLES))
_LCR_cullHiddenMapTris();
S3L_Unit originOffset = -1 * LCR_MAP_SIZE_BLOCKS / 2 * LCR_RENDERER_UNIT;
@ -995,9 +1002,7 @@ uint8_t _LCR_buildMapModel(void)
}
_LCR_cullHiddenMapTris();
LCR_LOG1("map model built");
return 1;
}