Add batching support to OGL brightmaps

This commit is contained in:
Sally Coolatta 2023-09-18 01:05:36 -04:00 committed by James R
parent e4a2461faf
commit d03eb9e9cd
2 changed files with 72 additions and 30 deletions

View file

@ -17,6 +17,7 @@
// The texture for the next polygon given to HWR_ProcessPolygon. // The texture for the next polygon given to HWR_ProcessPolygon.
// Set with HWR_SetCurrentTexture. // Set with HWR_SetCurrentTexture.
GLMipmap_t *current_texture = NULL; GLMipmap_t *current_texture = NULL;
GLMipmap_t *current_brightmap = NULL;
boolean currently_batching = false; boolean currently_batching = false;
@ -42,10 +43,10 @@ int unsortedVertexArrayAllocSize = 65536;
// Call HWR_RenderBatches to render all the collected geometry. // Call HWR_RenderBatches to render all the collected geometry.
void HWR_StartBatching(void) void HWR_StartBatching(void)
{ {
if (currently_batching) if (currently_batching)
I_Error("Repeat call to HWR_StartBatching without HWR_RenderBatches"); I_Error("Repeat call to HWR_StartBatching without HWR_RenderBatches");
// init arrays if that has not been done yet // init arrays if that has not been done yet
if (!finalVertexArray) if (!finalVertexArray)
{ {
finalVertexArray = malloc(finalVertexArrayAllocSize * sizeof(FOutVector)); finalVertexArray = malloc(finalVertexArrayAllocSize * sizeof(FOutVector));
@ -55,7 +56,7 @@ void HWR_StartBatching(void)
unsortedVertexArray = malloc(unsortedVertexArrayAllocSize * sizeof(FOutVector)); unsortedVertexArray = malloc(unsortedVertexArrayAllocSize * sizeof(FOutVector));
} }
currently_batching = true; currently_batching = true;
} }
// This replaces the direct calls to pfnSetTexture in cases where batching is available. // This replaces the direct calls to pfnSetTexture in cases where batching is available.
@ -63,14 +64,29 @@ void HWR_StartBatching(void)
// Doing this was easier than getting a texture pointer to HWR_ProcessPolygon. // Doing this was easier than getting a texture pointer to HWR_ProcessPolygon.
void HWR_SetCurrentTexture(GLMipmap_t *texture) void HWR_SetCurrentTexture(GLMipmap_t *texture)
{ {
if (currently_batching) if (currently_batching)
{ {
current_texture = texture; if (texture != NULL)
} {
else if (texture->flags & TF_BRIGHTMAP)
{ {
HWD.pfnSetTexture(texture); current_brightmap = texture;
} }
else
{
current_texture = texture;
current_brightmap = NULL;
}
}
else
{
current_texture = current_brightmap = NULL;
}
}
else
{
HWD.pfnSetTexture(texture);
}
} }
// If batching is enabled, this function collects the polygon data and the chosen texture // If batching is enabled, this function collects the polygon data and the chosen texture
@ -78,7 +94,7 @@ void HWR_SetCurrentTexture(GLMipmap_t *texture)
// render the polygon immediately. // render the polygon immediately.
void HWR_ProcessPolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPts, FBITFIELD PolyFlags, int shader, boolean horizonSpecial) void HWR_ProcessPolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPts, FBITFIELD PolyFlags, int shader, boolean horizonSpecial)
{ {
if (currently_batching) if (currently_batching)
{ {
if (!pSurf) if (!pSurf)
I_Error("Got a null FSurfaceInfo in batching");// nulls should not come in the stuff that batching currently applies to I_Error("Got a null FSurfaceInfo in batching");// nulls should not come in the stuff that batching currently applies to
@ -114,6 +130,7 @@ void HWR_ProcessPolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPt
polygonArray[polygonArraySize].numVerts = iNumPts; polygonArray[polygonArraySize].numVerts = iNumPts;
polygonArray[polygonArraySize].polyFlags = PolyFlags; polygonArray[polygonArraySize].polyFlags = PolyFlags;
polygonArray[polygonArraySize].texture = current_texture; polygonArray[polygonArraySize].texture = current_texture;
polygonArray[polygonArraySize].brightmap = current_brightmap;
polygonArray[polygonArraySize].shader = shader; polygonArray[polygonArraySize].shader = shader;
polygonArray[polygonArraySize].horizonSpecial = horizonSpecial; polygonArray[polygonArraySize].horizonSpecial = horizonSpecial;
polygonArraySize++; polygonArraySize++;
@ -123,10 +140,10 @@ void HWR_ProcessPolygon(FSurfaceInfo *pSurf, FOutVector *pOutVerts, FUINT iNumPt
} }
else else
{ {
if (shader) if (shader)
HWD.pfnSetShader(shader); HWD.pfnSetShader(shader);
HWD.pfnDrawPolygon(pSurf, pOutVerts, iNumPts, PolyFlags); HWD.pfnDrawPolygon(pSurf, pOutVerts, iNumPts, PolyFlags);
} }
} }
static int comparePolygons(const void *p1, const void *p2) static int comparePolygons(const void *p1, const void *p2)
@ -161,6 +178,15 @@ static int comparePolygons(const void *p1, const void *p2)
diff64 = downloaded1 - downloaded2; diff64 = downloaded1 - downloaded2;
if (diff64 != 0) return diff64; if (diff64 != 0) return diff64;
downloaded1 = 0;
downloaded2 = 0;
if (poly1->brightmap)
downloaded1 = poly1->brightmap->downloaded; // there should be a opengl texture name here, usable for comparisons
if (poly2->brightmap)
downloaded2 = poly2->brightmap->downloaded;
diff64 = downloaded1 - downloaded2;
if (diff64 != 0) return diff64;
diff = poly1->polyFlags - poly2->polyFlags; diff = poly1->polyFlags - poly2->polyFlags;
if (diff != 0) return diff; if (diff != 0) return diff;
@ -219,15 +245,17 @@ static int comparePolygonsNoShaders(const void *p1, const void *p2)
// the rendering backend to draw them. // the rendering backend to draw them.
void HWR_RenderBatches(void) void HWR_RenderBatches(void)
{ {
int finalVertexWritePos = 0;// position in finalVertexArray int finalVertexWritePos = 0;// position in finalVertexArray
int finalIndexWritePos = 0;// position in finalVertexIndexArray int finalIndexWritePos = 0;// position in finalVertexIndexArray
int polygonReadPos = 0;// position in polygonIndexArray int polygonReadPos = 0;// position in polygonIndexArray
int currentShader; int currentShader;
int nextShader = 0; int nextShader = 0;
GLMipmap_t *currentTexture; GLMipmap_t *currentTexture = NULL;
GLMipmap_t *nextTexture = NULL; GLMipmap_t *nextTexture = NULL;
GLMipmap_t *currentBrightmap = NULL;
GLMipmap_t *nextBrightmap = NULL;
FBITFIELD currentPolyFlags = 0; FBITFIELD currentPolyFlags = 0;
FBITFIELD nextPolyFlags = 0; FBITFIELD nextPolyFlags = 0;
FSurfaceInfo currentSurfaceInfo; FSurfaceInfo currentSurfaceInfo;
@ -235,7 +263,7 @@ void HWR_RenderBatches(void)
int i; int i;
if (!currently_batching) if (!currently_batching)
I_Error("HWR_RenderBatches called without starting batching"); I_Error("HWR_RenderBatches called without starting batching");
nextSurfaceInfo.LightInfo.fade_end = 0; nextSurfaceInfo.LightInfo.fade_end = 0;
@ -268,14 +296,16 @@ void HWR_RenderBatches(void)
// sort order // sort order
// 1. shader // 1. shader
// 2. texture // 2. texture
// 3. polyflags // 3. brightmap
// 4. colors + light level // 4. polyflags
// 5. colors + light level
// not sure about what order of the last 2 should be, or if it even matters // not sure about what order of the last 2 should be, or if it even matters
ps_hw_batchdrawtime = I_GetPreciseTime(); ps_hw_batchdrawtime = I_GetPreciseTime();
currentShader = polygonArray[polygonIndexArray[0]].shader; currentShader = polygonArray[polygonIndexArray[0]].shader;
currentTexture = polygonArray[polygonIndexArray[0]].texture; currentTexture = polygonArray[polygonIndexArray[0]].texture;
currentBrightmap = polygonArray[polygonIndexArray[0]].brightmap;
currentPolyFlags = polygonArray[polygonIndexArray[0]].polyFlags; currentPolyFlags = polygonArray[polygonIndexArray[0]].polyFlags;
currentSurfaceInfo = polygonArray[polygonIndexArray[0]].surf; currentSurfaceInfo = polygonArray[polygonIndexArray[0]].surf;
// For now, will sort and track the colors. Vertex attributes could be used instead of uniforms // For now, will sort and track the colors. Vertex attributes could be used instead of uniforms
@ -289,9 +319,15 @@ void HWR_RenderBatches(void)
} }
if (currentPolyFlags & PF_NoTexture) if (currentPolyFlags & PF_NoTexture)
currentTexture = NULL; {
else currentTexture = currentBrightmap = NULL;
HWD.pfnSetTexture(currentTexture); }
else
{
HWD.pfnSetTexture(currentTexture);
if (currentBrightmap)
HWD.pfnSetTexture(currentBrightmap);
}
while (1)// note: remember handling notexture polyflag as having texture number 0 (also in comparePolygons) while (1)// note: remember handling notexture polyflag as having texture number 0 (also in comparePolygons)
{ {
@ -361,16 +397,17 @@ void HWR_RenderBatches(void)
int nextIndex = polygonIndexArray[polygonReadPos]; int nextIndex = polygonIndexArray[polygonReadPos];
nextShader = polygonArray[nextIndex].shader; nextShader = polygonArray[nextIndex].shader;
nextTexture = polygonArray[nextIndex].texture; nextTexture = polygonArray[nextIndex].texture;
nextBrightmap = polygonArray[nextIndex].brightmap;
nextPolyFlags = polygonArray[nextIndex].polyFlags; nextPolyFlags = polygonArray[nextIndex].polyFlags;
nextSurfaceInfo = polygonArray[nextIndex].surf; nextSurfaceInfo = polygonArray[nextIndex].surf;
if (nextPolyFlags & PF_NoTexture) if (nextPolyFlags & PF_NoTexture)
nextTexture = 0; nextTexture = nextBrightmap = 0;
if (currentShader != nextShader && cv_glshaders.value && gl_shadersavailable) if (currentShader != nextShader && cv_glshaders.value && gl_shadersavailable)
{ {
changeState = true; changeState = true;
changeShader = true; changeShader = true;
} }
if (currentTexture != nextTexture) if (currentTexture != nextTexture || currentBrightmap != nextBrightmap)
{ {
changeState = true; changeState = true;
changeTexture = true; changeTexture = true;
@ -406,7 +443,7 @@ void HWR_RenderBatches(void)
if (changeState || stopFlag) if (changeState || stopFlag)
{ {
// execute draw call // execute draw call
HWD.pfnDrawIndexedTriangles(&currentSurfaceInfo, finalVertexArray, finalIndexWritePos, currentPolyFlags, finalVertexIndexArray); HWD.pfnDrawIndexedTriangles(&currentSurfaceInfo, finalVertexArray, finalIndexWritePos, currentPolyFlags, finalVertexIndexArray);
// update stats // update stats
ps_hw_numcalls++; ps_hw_numcalls++;
ps_hw_numverts += finalIndexWritePos; ps_hw_numverts += finalIndexWritePos;
@ -431,10 +468,14 @@ void HWR_RenderBatches(void)
if (changeTexture) if (changeTexture)
{ {
// texture should be already ready for use from calls to SetTexture during batch collection // texture should be already ready for use from calls to SetTexture during batch collection
HWD.pfnSetTexture(nextTexture); HWD.pfnSetTexture(nextTexture);
currentTexture = nextTexture; currentTexture = nextTexture;
changeTexture = false;
if (nextBrightmap)
HWD.pfnSetTexture(nextBrightmap);
currentBrightmap = nextBrightmap;
changeTexture = false;
ps_hw_numtextures++; ps_hw_numtextures++;
} }
if (changePolyFlags) if (changePolyFlags)

View file

@ -27,6 +27,7 @@ typedef struct
FUINT numVerts; FUINT numVerts;
FBITFIELD polyFlags; FBITFIELD polyFlags;
GLMipmap_t *texture; GLMipmap_t *texture;
GLMipmap_t *brightmap;
int shader; int shader;
// this tells batching that the plane belongs to a horizon line and must be drawn in correct order with the skywalls // this tells batching that the plane belongs to a horizon line and must be drawn in correct order with the skywalls
boolean horizonSpecial; boolean horizonSpecial;