From b889c56e925b73837b5d0efb0e92622218370bf6 Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Fri, 30 Aug 2024 16:41:32 +0200 Subject: [PATCH] Raster triangles faster at the expense of memory --- base/devtools.cfg | 3 +- neo/idlib/sys/sys_alloc_tags.h | 1 + neo/renderer/Model.h | 5 ++ neo/renderer/RenderCommon.h | 1 + .../tr_frontend_masked_occlusion_culling.cpp | 10 +++ neo/renderer/tr_trisurf.cpp | 75 +++++++++++++++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/base/devtools.cfg b/base/devtools.cfg index d7a160e1..a9766436 100644 --- a/base/devtools.cfg +++ b/base/devtools.cfg @@ -1,7 +1,8 @@ bind "I" "toggle r_showSurfaceInfo" bind "N" "noclip" -bind "M" "spawn moveable_macbethchart" +//bind "M" "spawn moveable_macbethchart" +bind "M" "maskShot" bind "O" "toggle r_useMaskedOcclusionCulling" bind "P" "toggle r_showPortals" // bind "P" "toggle r_showViewEnvprobes 1 2 0" diff --git a/neo/idlib/sys/sys_alloc_tags.h b/neo/idlib/sys/sys_alloc_tags.h index 51426715..664bcc06 100644 --- a/neo/idlib/sys/sys_alloc_tags.h +++ b/neo/idlib/sys/sys_alloc_tags.h @@ -40,6 +40,7 @@ MEM_TAG( TRI_SIL_EDGE ) MEM_TAG( TRI_DOMINANT_TRIS ) MEM_TAG( TRI_MIR_VERT ) MEM_TAG( TRI_DUP_VERT ) +MEM_TAG( TRI_MOC_VERT ) MEM_TAG( SRFTRIS ) MEM_TAG( TEMP ) // Temp data which should be automatically freed at the end of the function MEM_TAG( PAGE ) diff --git a/neo/renderer/Model.h b/neo/renderer/Model.h index 76c81431..edeb6bab 100644 --- a/neo/renderer/Model.h +++ b/neo/renderer/Model.h @@ -84,6 +84,11 @@ struct srfTriangles_t triIndex_t* silIndexes; // indexes changed to be the first vertex with same XYZ, ignoring normal and texcoords + // RB begin + idVec4* mocVerts; // idDrawVert has no w position + unsigned int* mocIndexes; // uint32 instead of uint16 for the Masked Software Occlusion Culling SIMD loading code + // RB end + int numMirroredVerts; // this many verts at the end of the vert list are tangent mirrors int* mirroredVerts; // tri->mirroredVerts[0] is the mirror of tri->numVerts - tri->numMirroredVerts + 0 diff --git a/neo/renderer/RenderCommon.h b/neo/renderer/RenderCommon.h index ab33b8fe..21db6571 100644 --- a/neo/renderer/RenderCommon.h +++ b/neo/renderer/RenderCommon.h @@ -1632,6 +1632,7 @@ int R_TriSurfMemory( const srfTriangles_t* tri ); void R_BoundTriSurf( srfTriangles_t* tri ); void R_RemoveDuplicatedTriangles( srfTriangles_t* tri ); void R_CreateSilIndexes( srfTriangles_t* tri ); +void R_CreateMaskedOcclusionCullingTris( srfTriangles_t* tri ); // RB void R_RemoveDegenerateTriangles( srfTriangles_t* tri ); void R_RemoveUnusedVerts( srfTriangles_t* tri ); void R_RangeCheckIndexes( const srfTriangles_t* tri ); diff --git a/neo/renderer/tr_frontend_masked_occlusion_culling.cpp b/neo/renderer/tr_frontend_masked_occlusion_culling.cpp index 0f9111d9..aa33d9f7 100644 --- a/neo/renderer/tr_frontend_masked_occlusion_culling.cpp +++ b/neo/renderer/tr_frontend_masked_occlusion_culling.cpp @@ -334,6 +334,8 @@ void R_RenderSingleModel( viewEntity_t* vEntity ) if( !gpuSkinned ) //if( model->IsStaticWorldModel() ) { + +#if 0 // super simple bruteforce idVec4 triVerts[3]; unsigned int triIndices[] = { 0, 1, 2 }; @@ -359,6 +361,14 @@ void R_RenderSingleModel( viewEntity_t* vEntity ) tr.maskedOcclusionCulling->RenderTriangles( ( float* )triVerts, triIndices, 1, NULL, MaskedOcclusionCulling::BACKFACE_CCW ); } +#else + R_CreateMaskedOcclusionCullingTris( tri ); + + idRenderMatrix mvp; + idRenderMatrix::Transpose( vEntity->unjitteredMVP, mvp ); + + tr.maskedOcclusionCulling->RenderTriangles( tri->mocVerts->ToFloatPtr(), tri->mocIndexes, tri->numIndexes / 3, ( float* )&mvp[0][0], MaskedOcclusionCulling::BACKFACE_CCW, MaskedOcclusionCulling::CLIP_PLANE_ALL, MaskedOcclusionCulling::VertexLayout( 16, 4, 8 ) ); +#endif } #if 0 else diff --git a/neo/renderer/tr_trisurf.cpp b/neo/renderer/tr_trisurf.cpp index eb64ae60..0ed80310 100644 --- a/neo/renderer/tr_trisurf.cpp +++ b/neo/renderer/tr_trisurf.cpp @@ -4,6 +4,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. Copyright (C) 2020 Stephen Pridham (Mikkelsen tangent space support) +Copyright (C) 2021-2024 Robert Beckebans This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -251,6 +252,13 @@ int R_TriSurfMemory( const srfTriangles_t* tri ) total += tri->numDupVerts * sizeof( tri->dupVerts[0] ); } + // RB: added MOC + if( tri->mocIndexes != NULL ) + { + total += tri->numIndexes * sizeof( tri->mocIndexes[0] ); + } + // RB end + total += sizeof( *tri ); return total; @@ -293,6 +301,13 @@ void R_FreeStaticTriSurf( srfTriangles_t* tri ) Mem_Free( tri->verts ); } } + + // RB begin + if( tri->mocVerts != NULL ) + { + Mem_Free( tri->mocVerts ); + } + // RB end } if( !tri->referencedIndexes ) @@ -309,6 +324,12 @@ void R_FreeStaticTriSurf( srfTriangles_t* tri ) { Mem_Free( tri->silIndexes ); } + // RB begin + if( tri->mocIndexes != NULL ) + { + Mem_Free( tri->mocIndexes ); + } + // RB end if( tri->dominantTris != NULL ) { Mem_Free( tri->dominantTris ); @@ -449,6 +470,28 @@ void R_AllocStaticTriSurfDupVerts( srfTriangles_t* tri, int numDupVerts ) tri->dupVerts = ( int* )Mem_Alloc16( numDupVerts * 2 * sizeof( *tri->dupVerts ), TAG_TRI_DUP_VERT ); } +/* +================= +R_AllocStaticTriSurfMocIndexes +================= +*/ +void R_AllocStaticTriSurfMocIndexes( srfTriangles_t* tri, int numIndexes ) +{ + assert( tri->mocIndexes == NULL ); + tri->mocIndexes = ( unsigned int* )Mem_Alloc16( numIndexes * sizeof( unsigned int ), TAG_TRI_MOC_VERT ); +} + +/* +================= +R_AllocStaticTriSurfMocVerts +================= +*/ +void R_AllocStaticTriSurfMocVerts( srfTriangles_t* tri, int numVerts ) +{ + assert( tri->mocVerts == NULL ); + tri->mocVerts = ( idVec4* )Mem_Alloc16( numVerts * sizeof( idVec4 ), TAG_TRI_MOC_VERT ); +} + /* ================= R_ResizeStaticTriSurfVerts @@ -693,6 +736,9 @@ void R_CreateDupVerts( srfTriangles_t* tri ) memcpy( tri->dupVerts, tempDupVerts.Ptr(), tri->numDupVerts * 2 * sizeof( tri->dupVerts[0] ) ); } + + + /* =============== R_FaceNegativePolarity @@ -1795,6 +1841,9 @@ void R_CleanupTriangles( srfTriangles_t* tri, bool createNormals, bool identifyS { R_DeriveTangents( tri ); } + + // RB: duplicate data appropiate for MOC SIMD fetches + R_CreateMaskedOcclusionCullingTris( tri ); } /* @@ -2192,3 +2241,29 @@ idVec3 R_ClosestPointPointTriangle( const idVec3& point, const idVec3& vertex1, return result; } + +void R_CreateMaskedOcclusionCullingTris( srfTriangles_t* tri ) +{ + //assert( tri->mocVerts == NULL ); + if( tri->mocVerts == NULL ) + { + R_AllocStaticTriSurfMocVerts( tri, tri->numVerts ); + + for( int i = 0; i < tri->numVerts; i++ ) + { + tri->mocVerts[i].ToVec3() = tri->verts[i].xyz; + tri->mocVerts[i].w = 1.0f; + } + } + + if( tri->mocIndexes == NULL ) + { + R_AllocStaticTriSurfMocIndexes( tri, tri->numIndexes ); + + for( int i = 0; i < tri->numIndexes; i++ ) + { + tri->mocIndexes[i] = tri->indexes[i]; + } + } +} +// RB end