Revert "Optimize HWR_RenderBatches polygon sorting"

This reverts commit 5201ccd6d2.
This commit is contained in:
Alug committed 2024-01-14 23:49:48 +01:00
1 parent 79a594f6ee
commit a36e633555
1 file changed
+31 -27
+31 -27
View File
@@ -2390,7 +2390,7 @@ int finalVertexArrayAllocSize = 65536;
PolygonArrayEntry* polygonArray = NULL;// contains the polygon data from DrawPolygon, waiting to be processed
int polygonArraySize = 0;
PolygonArrayEntry **polygonArraySorted = NULL;// contains sorted pointers to polygonArray
UINT32* polygonIndexArray = NULL;// contains sorting pointers for polygonArray
int polygonArrayAllocSize = 65536;
FOutVector* unsortedVertexArray = NULL;// contains unsorted vertices and texture coordinates from DrawPolygon
@@ -2406,7 +2406,7 @@ EXPORT void HWRAPI(StartBatching) (void)
finalVertexArray = malloc(finalVertexArrayAllocSize * sizeof(FOutVector));
finalVertexIndexArray = malloc(finalVertexArrayAllocSize * 3 * sizeof(UINT32));
polygonArray = malloc(polygonArrayAllocSize * sizeof(PolygonArrayEntry));
polygonArraySorted = malloc(polygonArrayAllocSize * sizeof(PolygonArrayEntry *));
polygonIndexArray = malloc(polygonArrayAllocSize * sizeof(UINT32));
unsortedVertexArray = malloc(unsortedVertexArrayAllocSize * sizeof(FOutVector));
}
// drawing functions will now collect the drawing data instead of passing it to opengl
@@ -2416,8 +2416,10 @@ EXPORT void HWRAPI(StartBatching) (void)
static int comparePolygons(const void *p1, const void *p2)
{
PolygonArrayEntry *poly1 = *(PolygonArrayEntry *const *)p1;
PolygonArrayEntry *poly2 = *(PolygonArrayEntry *const *)p2;
unsigned int index1 = *(const unsigned int*)p1;
unsigned int index2 = *(const unsigned int*)p2;
PolygonArrayEntry* poly1 = &polygonArray[index1];
PolygonArrayEntry* poly2 = &polygonArray[index2];
int diff;
INT64 diff64;
@@ -2433,7 +2435,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 poly1 - poly2;
return index1 - index2;
diff = poly1->texNum - poly2->texNum;
if (diff != 0) return diff;
@@ -2464,8 +2466,10 @@ static int comparePolygons(const void *p1, const void *p2)
static int comparePolygonsNoShaders(const void *p1, const void *p2)
{
PolygonArrayEntry *poly1 = *(PolygonArrayEntry *const *)p1;
PolygonArrayEntry *poly2 = *(PolygonArrayEntry *const *)p2;
unsigned int index1 = *(const unsigned int*)p1;
unsigned int index2 = *(const unsigned int*)p2;
PolygonArrayEntry* poly1 = &polygonArray[index1];
PolygonArrayEntry* poly2 = &polygonArray[index2];
int diff;
INT64 diff64;
@@ -2480,7 +2484,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 poly1 - poly2;
return index1 - index2;
diff = poly1->polyFlags - poly2->polyFlags;
if (diff != 0) return diff;
@@ -2497,7 +2501,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 polygonArraySorted
int polygonReadPos = 0;// position in polygonIndexArray
GLuint currentShader;
GLuint currentTexture;
@@ -2523,19 +2527,19 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
*sNumPolys = polygonArraySize;
*sNumCalls = *sNumVerts = 0;
*sNumShaders = *sNumTextures = *sNumPolyFlags = *sNumColors = 1;
// init polygonArraySorted
// init polygonIndexArray
for (i = 0; i < polygonArraySize; i++)
{
polygonArraySorted[i] = &polygonArray[i];
polygonIndexArray[i] = i;
}
// sort polygons
//CONS_Printf("qsort polys\n");
*sSortTime = I_GetPreciseTime();
if (gl_allowshaders)
qsort(polygonArraySorted, polygonArraySize, sizeof(PolygonArrayEntry *), comparePolygons);
qsort(polygonIndexArray, polygonArraySize, sizeof(unsigned int), comparePolygons);
else
qsort(polygonArraySorted, polygonArraySize, sizeof(PolygonArrayEntry *), comparePolygonsNoShaders);
qsort(polygonIndexArray, polygonArraySize, sizeof(unsigned int), comparePolygonsNoShaders);
*sSortTime = I_GetPreciseTime() - *sSortTime;
//CONS_Printf("sort done\n");
// sort order
@@ -2547,10 +2551,10 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
*sDrawTime = I_GetPreciseTime();
currentShader = polygonArraySorted[0]->shader;
currentTexture = polygonArraySorted[0]->texNum;
currentPolyFlags = polygonArraySorted[0]->polyFlags;
currentSurfaceInfo = polygonArraySorted[0]->surf;
currentShader = polygonArray[polygonIndexArray[0]].shader;
currentTexture = polygonArray[polygonIndexArray[0]].texNum;
currentPolyFlags = polygonArray[polygonIndexArray[0]].polyFlags;
currentSurfaceInfo = polygonArray[polygonIndexArray[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.
@@ -2628,8 +2632,8 @@ EXPORT void HWRAPI(RenderBatches) (precise_t *sSortTime, precise_t *sDrawTime, i
// reset write pos
// repeat loop
PolygonArrayEntry *entry = polygonArraySorted[polygonReadPos++];
int numVerts = entry->numVerts;
int index = polygonIndexArray[polygonReadPos++];
int numVerts = polygonArray[index].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
@@ -2654,7 +2658,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[entry->vertsIndex],
memcpy(&finalVertexArray[finalVertexWritePos], &unsortedVertexArray[polygonArray[index].vertsIndex],
numVerts * sizeof(FOutVector));
// write the indexes, pointing to the fan vertexes but in triangles format
firstIndex = finalVertexWritePos;
@@ -2676,11 +2680,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
PolygonArrayEntry *nextEntry = polygonArraySorted[polygonReadPos];
nextShader = nextEntry->shader;
nextTexture = nextEntry->texNum;
nextPolyFlags = nextEntry->polyFlags;
nextSurfaceInfo = nextEntry->surf;
int nextIndex = polygonIndexArray[polygonReadPos];
nextShader = polygonArray[nextIndex].shader;
nextTexture = polygonArray[nextIndex].texNum;
nextPolyFlags = polygonArray[nextIndex].polyFlags;
nextSurfaceInfo = polygonArray[nextIndex].surf;
if (nextPolyFlags & PF_NoTexture)
nextTexture = 0;
if (currentShader != nextShader)
@@ -2885,8 +2889,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(polygonArraySorted);
polygonArraySorted = malloc(polygonArrayAllocSize * sizeof(PolygonArrayEntry *));
free(polygonIndexArray);
polygonIndexArray = malloc(polygonArrayAllocSize * sizeof(unsigned int));
}
while (unsortedVertexArraySize + (int)iNumPts > unsortedVertexArrayAllocSize)