Skip to content

Commit 724703a

Browse files
committed
Move indices for material system surfaces when merging
This allows merging surfaces that originally don't have consecutive indexes.
1 parent 8b4ccb9 commit 724703a

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

src/engine/renderer/GeometryOptimiser.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828
#include "GeometryOptimiser.h"
2929

3030
#include "ShadeCommon.h"
31+
#include "GeometryCache.h"
3132

3233
static int LeafSurfaceCompare( const void* a, const void* b ) {
3334
bspSurface_t* aa, * bb;
@@ -421,7 +422,8 @@ void MergeDuplicateVertices( bspSurface_t** rendererSurfaces, int numSurfaces, s
421422
Log::Notice( "Merged %i vertices into %i in %i ms", numVerticesIn, numVerticesOut, Sys::Milliseconds() - start );
422423
}
423424

424-
std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** rendererSurfaces, int numSurfaces ) {
425+
std::vector<MaterialSurface> OptimiseMapGeometryMaterial( bspSurface_t** rendererSurfaces, int numSurfaces,
426+
const srfVert_t* vertices, const int numVerticesIn, const glIndex_t* indices, const int numIndicesIn ) {
425427
std::vector<MaterialSurface> materialSurfaces;
426428
materialSurfaces.reserve( numSurfaces );
427429

@@ -469,14 +471,22 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
469471

470472
std::vector<MaterialSurface> processedMaterialSurfaces;
471473
processedMaterialSurfaces.reserve( numSurfaces );
474+
475+
glIndex_t* idxs = ( glIndex_t* ) ri.Hunk_AllocateTempMemory( numIndicesIn * sizeof( glIndex_t ) );
476+
uint32_t numIndices = 0;
472477
for ( uint32_t i = 0; i < materialSurfaces.size(); i++ ) {
473478
MaterialSurface* surface = &materialSurfaces[i];
474479

475480
if ( surface->merged ) {
476481
continue;
477482
}
478483

479-
uint32_t lastIndex = surface->firstIndex + surface->count;
484+
memcpy( idxs + numIndices, indices + surface->firstIndex, surface->count * sizeof( glIndex_t ) );
485+
486+
surface->firstIndex = numIndices;
487+
488+
numIndices += surface->count;
489+
480490
for ( uint32_t j = i + 1; j < materialSurfaces.size(); j++ ) {
481491
MaterialSurface* surface2 = &materialSurfaces[j];
482492

@@ -558,15 +568,15 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
558568
continue;
559569
}
560570

561-
if ( surface2->firstIndex == lastIndex ) {
562-
surface->count += surface2->count;
563-
lastIndex += surface2->count;
571+
memcpy( idxs + numIndices, indices + surface2->firstIndex, surface2->count * sizeof( glIndex_t ) );
572+
numIndices += surface2->count;
564573

565-
VectorCopy( origin, surface->origin );
566-
surface->radius = radius;
574+
surface->count += surface2->count;
567575

568-
surface2->merged = true;
569-
}
576+
VectorCopy( origin, surface->origin );
577+
surface->radius = radius;
578+
579+
surface2->merged = true;
570580

571581
if ( surface->count >= MAX_MATERIAL_SURFACE_INDEXES ) {
572582
break;
@@ -584,5 +594,16 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial(bspSurface_t** renderer
584594
materialSystem.SetWorldBounds( worldBounds );
585595
materialSystem.GenerateWorldCommandBuffer( processedMaterialSurfaces );
586596

597+
vertexAttributeSpec_t attrs[] {
598+
{ ATTR_INDEX_POSITION, GL_FLOAT, GL_FLOAT, &vertices[0].xyz, 3, sizeof( *vertices ), 0 },
599+
{ ATTR_INDEX_COLOR, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, &vertices[0].lightColor, 4, sizeof( *vertices ), ATTR_OPTION_NORMALIZE },
600+
{ ATTR_INDEX_QTANGENT, GL_SHORT, GL_SHORT, &vertices[0].qtangent, 4, sizeof( *vertices ), ATTR_OPTION_NORMALIZE },
601+
{ ATTR_INDEX_TEXCOORD, GL_FLOAT, GL_HALF_FLOAT, &vertices[0].st, 4, sizeof( *vertices ), 0 },
602+
};
603+
604+
geometryCache.AddMapGeometry( numVerticesIn, numIndices, std::begin( attrs ), std::end( attrs ), idxs );
605+
606+
ri.Hunk_FreeTempMemory( idxs );
607+
587608
return materialSurfaces;
588609
}

src/engine/renderer/GeometryOptimiser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ void OptimiseMapGeometryCore( world_t* world, bspSurface_t** rendererSurfaces, i
9999
void MergeLeafSurfacesCore( world_t* world, bspSurface_t** rendererSurfaces, int numSurfaces );
100100
void MergeDuplicateVertices( bspSurface_t** rendererSurfaces, int numSurfaces, srfVert_t* vertices, int numVerticesIn,
101101
glIndex_t* indices, int numIndicesIn, int& numVerticesOut, int& numIndicesOut );
102-
std::vector<MaterialSurface> OptimiseMapGeometryMaterial( bspSurface_t** rendererSurfaces, int numSurfaces );
102+
std::vector<MaterialSurface> OptimiseMapGeometryMaterial( bspSurface_t** rendererSurfaces, int numSurfaces,
103+
const srfVert_t* vertices, const int numVerticesIn, const glIndex_t* indices, const int numIndicesIn );
103104

104105
#endif // GEOMETRY_OPTIMISER_H

src/engine/renderer/tr_bsp.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,7 +2721,7 @@ static void R_CreateWorldVBO() {
27212721
MergeDuplicateVertices( rendererSurfaces, numSurfaces, vboVerts, numVertsInitial, vboIdxs, 3 * numTriangles, numVerts, numIndices );
27222722

27232723
if ( glConfig2.usingMaterialSystem ) {
2724-
OptimiseMapGeometryMaterial( rendererSurfaces, numSurfaces );
2724+
OptimiseMapGeometryMaterial( rendererSurfaces, numSurfaces, vboVerts, numVerts, vboIdxs, numIndices );
27252725
}
27262726

27272727
vertexAttributeSpec_t attrs[]{
@@ -2731,10 +2731,6 @@ static void R_CreateWorldVBO() {
27312731
{ ATTR_INDEX_TEXCOORD, GL_FLOAT, GL_HALF_FLOAT, &vboVerts[0].st, 4, sizeof( *vboVerts ), 0 },
27322732
};
27332733

2734-
if ( glConfig2.usingGeometryCache ) {
2735-
geometryCache.AddMapGeometry( numVerts, numIndices, std::begin( attrs ), std::end( attrs ), vboIdxs );
2736-
}
2737-
27382734
s_worldData.vbo = R_CreateStaticVBO(
27392735
"staticWorld_VBO", std::begin( attrs ), std::end( attrs ), numVerts );
27402736
s_worldData.ibo = R_CreateStaticIBO2( "staticWorld_IBO", numTriangles, vboIdxs );

0 commit comments

Comments
 (0)