Optimize HWR_RenderBatches polygon sorting

This commit is contained in:
alufolie91 committed 2023-10-01 02:31:38 +02:00
1 parent 74a6f76621
commit 5201ccd6d2
1 file changed
+27 -31
+27 -31
View File
@@ -2152,7 +2152,7 @@ int finalVertexArrayAllocSize = 65536;
PolygonArrayEntry* polygonArray = NULL;// contains the polygon data from DrawPolygon, waiting to be processed
int polygonArraySize = 0;
UINT32* polygonIndexArray = NULL;// contains sorting pointers for polygonArray
PolygonArrayEntry **polygonArraySorted = NULL;// contains sorted pointers to polygonArray
int polygonArrayAllocSize = 65536;
FOutVector* unsortedVertexArray = NULL;// contains unsorted vertices and texture coordinates from DrawPolygon
@@ -2168,7 +2168,7 @@ EXPORT void HWRAPI(StartBatching) (void)
finalVertexArray = malloc(finalVertexArrayAllocSize * sizeof(FOutVector));
finalVertexIndexArray = malloc(finalVertexArrayAllocSize * 3 * sizeof(UINT32));
polygonArray = malloc(polygonArrayAllocSize * sizeof(PolygonArrayEntry));
polygonIndexArray = malloc(polygonArrayAllocSize * sizeof(UINT32));
polygonArraySorted = malloc(polygonArrayAllocSize * sizeof(PolygonArrayEntry *));
unsortedVertexArray = malloc(unsortedVertexArrayAllocSize * sizeof(FOutVector));
}
// drawing functions will now collect the drawing data instead of passing it to opengl
@@ -2178,10 +2178,8 @@ EXPORT void HWRAPI(StartBatching) (void)
static int comparePolygons(const void *p1, const void *p2)
{
unsigned int index1 = *(const unsigned int*)p1;
unsigned int index2 = *(const unsigned int*)p2;
PolygonArrayEntry* poly1 = &polygonArray[index1];
PolygonArrayEntry* poly2 = &polygonArray[index2];
PolygonArrayEntry *poly1 = *(PolygonArrayEntry *const *)p1;
PolygonArrayEntry *poly2 = *(PolygonArrayEntry *const *)p2;
int diff;
INT64 diff64;
@@ -2197,7 +2195,7 @@ static int comparePolygons(const void *p1, const void *p2)
// skywalls and horizon lines must retain their order for horizon lines to work
if (shader1 == -1 && shader2 == -1)
return index1 - index2;
return poly1 - poly2;
diff = poly1->texNum - poly2->texNum;
if (diff != 0) return diff;
@@ -2228,10 +2226,8 @@ static int comparePolygons(const void *p1, const void *p2)
static int comparePolygonsNoShaders(const void *p1, const void *p2)
{
unsigned int index1 = *(const unsigned int*)p1;
unsigned int index2 = *(const unsigned int*)p2;
PolygonArrayEntry* poly1 = &polygonArray[index1];
PolygonArrayEntry* poly2 = &polygonArray[index2];
PolygonArrayEntry *poly1 = *(PolygonArrayEntry *const *)p1;
PolygonArrayEntry *poly2 = *(PolygonArrayEntry *const *)p2;
int diff;
INT64 diff64;
@@ -2246,7 +2242,7 @@ static int comparePolygonsNoShaders(const void *p1, const void *p2)
// skywalls and horizon lines must retain their order for horizon lines to work
if (texNum1 == 0 && texNum2 == 0)
return index1 - index2;
return poly1 - poly2;
diff = poly1->polyFlags - poly2->polyFlags;
if (diff != 0) return diff;
@@ -2263,7 +2259,7 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
int finalVertexWritePos = 0;// position in finalVertexArray
int finalIndexWritePos = 0;// position in finalVertexIndexArray
int polygonReadPos = 0;// position in polygonIndexArray
int polygonReadPos = 0;// position in polygonArraySorted
GLuint currentShader;
GLuint currentTexture;
@@ -2289,19 +2285,19 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
*sNumPolys = polygonArraySize;
*sNumCalls = *sNumVerts = 0;
*sNumShaders = *sNumTextures = *sNumPolyFlags = *sNumColors = 1;
// init polygonIndexArray
// init polygonArraySorted
for (i = 0; i < polygonArraySize; i++)
{
polygonIndexArray[i] = i;
polygonArraySorted[i] = &polygonArray[i];
}
// sort polygons
//CONS_Printf("qsort polys\n");
*sSortTime = I_GetPreciseTime();
if (gl_allowshaders)
qsort(polygonIndexArray, polygonArraySize, sizeof(unsigned int), comparePolygons);
qsort(polygonArraySorted, polygonArraySize, sizeof(PolygonArrayEntry *), comparePolygons);
else
qsort(polygonIndexArray, polygonArraySize, sizeof(unsigned int), comparePolygonsNoShaders);
qsort(polygonArraySorted, polygonArraySize, sizeof(PolygonArrayEntry *), comparePolygonsNoShaders);
*sSortTime = I_GetPreciseTime() - *sSortTime;
//CONS_Printf("sort done\n");
// sort order
@@ -2313,10 +2309,10 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
*sDrawTime = I_GetPreciseTime();
currentShader = polygonArray[polygonIndexArray[0]].shader;
currentTexture = polygonArray[polygonIndexArray[0]].texNum;
currentPolyFlags = polygonArray[polygonIndexArray[0]].polyFlags;
currentSurfaceInfo = polygonArray[polygonIndexArray[0]].surf;
currentShader = polygonArraySorted[0]->shader;
currentTexture = polygonArraySorted[0]->texNum;
currentPolyFlags = polygonArraySorted[0]->polyFlags;
currentSurfaceInfo = polygonArraySorted[0]->surf;
// For now, will sort and track the colors. Vertex attributes could be used instead of uniforms
// and a color array could replace the color calls.
@@ -2394,8 +2390,8 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
// reset write pos
// repeat loop
int index = polygonIndexArray[polygonReadPos++];
int numVerts = polygonArray[index].numVerts;
PolygonArrayEntry *entry = polygonArraySorted[polygonReadPos++];
int numVerts = entry->numVerts;
// before writing, check if there is enough room
// using 'while' instead of 'if' here makes sure that there will *always* be enough room.
// probably never will this loop run more than once though
@@ -2420,7 +2416,7 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
}
//CONS_Printf("write verts to final\n");
// write the vertices of the polygon
memcpy(&finalVertexArray[finalVertexWritePos], &unsortedVertexArray[polygonArray[index].vertsIndex],
memcpy(&finalVertexArray[finalVertexWritePos], &unsortedVertexArray[entry->vertsIndex],
numVerts * sizeof(FOutVector));
// write the indexes, pointing to the fan vertexes but in triangles format
firstIndex = finalVertexWritePos;
@@ -2442,11 +2438,11 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
{
//CONS_Printf("state change check\n");
// check if a state change is required, set the change bools and next vars
int nextIndex = polygonIndexArray[polygonReadPos];
nextShader = polygonArray[nextIndex].shader;
nextTexture = polygonArray[nextIndex].texNum;
nextPolyFlags = polygonArray[nextIndex].polyFlags;
nextSurfaceInfo = polygonArray[nextIndex].surf;
PolygonArrayEntry *nextEntry = polygonArraySorted[polygonReadPos];
nextShader = nextEntry->shader;
nextTexture = nextEntry->texNum;
nextPolyFlags = nextEntry->polyFlags;
nextSurfaceInfo = nextEntry->surf;
if (nextPolyFlags & PF_NoTexture)
nextTexture = 0;
if (currentShader != nextShader)
@@ -2651,8 +2647,8 @@ EXPORT void HWRAPI(DrawPolygon) (FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUI
free(polygonArray);
polygonArray = new_array;
// also need to redo the index array, dont need to copy it though
free(polygonIndexArray);
polygonIndexArray = malloc(polygonArrayAllocSize * sizeof(unsigned int));
free(polygonArraySorted);
polygonArraySorted = malloc(polygonArrayAllocSize * sizeof(PolygonArrayEntry *));
}
while (unsortedVertexArraySize + (int)iNumPts > unsortedVertexArrayAllocSize)