Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some dynamic buffer-related clean-up and fixes #1528

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ void R_AddMD5Surfaces( trRefEntity_t *ent )
// see if we are in a fog volume
fogNum = R_FogWorldBox( ent->worldBounds );

if ( !r_vboModels->integer || !model->numVBOSurfaces ||
if ( !r_vboModels.Get() || !model->numVBOSurfaces ||
( !glConfig2.vboVertexSkinningAvailable && ent->e.skeleton.type == refSkeletonType_t::SK_ABSOLUTE ) )
{
shader_t *shader;
Expand Down Expand Up @@ -983,7 +983,7 @@ void R_AddMD5Interactions( trRefEntity_t *ent, trRefLight_t *light, interactionT

cubeSideBits = R_CalcLightCubeSideBits( light, ent->worldBounds );

if ( !r_vboModels->integer || !model->numVBOSurfaces ||
if ( !r_vboModels.Get() || !model->numVBOSurfaces ||
( !glConfig2.vboVertexSkinningAvailable && ent->e.skeleton.type == refSkeletonType_t::SK_ABSOLUTE ) )
{
shader_t *shader = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cvar_t *r_vboFaces;
cvar_t *r_vboCurves;
cvar_t *r_vboTriangles;
cvar_t *r_vboModels;
Cvar::Cvar<bool> r_vboModels( "r_vboModels", "Use static GPU VBOs/IBOs for models", Cvar::NONE, true );
cvar_t *r_vboVertexSkinning;

cvar_t *r_mergeLeafSurfaces;
Expand Down Expand Up @@ -1212,7 +1212,7 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
r_vboFaces = Cvar_Get( "r_vboFaces", "1", CVAR_CHEAT );
r_vboCurves = Cvar_Get( "r_vboCurves", "1", CVAR_CHEAT );
r_vboTriangles = Cvar_Get( "r_vboTriangles", "1", CVAR_CHEAT );
r_vboModels = Cvar_Get( "r_vboModels", "1", CVAR_LATCH );
Cvar::Latch( r_vboModels );
r_vboVertexSkinning = Cvar_Get( "r_vboVertexSkinning", "1", CVAR_LATCH );

r_mergeLeafSurfaces = Cvar_Get( "r_mergeLeafSurfaces", "1", CVAR_LATCH );
Expand Down
6 changes: 1 addition & 5 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -3113,7 +3113,7 @@ enum class shaderProfilerRenderSubGroupsMode {
extern cvar_t *r_vboFaces;
extern cvar_t *r_vboCurves;
extern cvar_t *r_vboTriangles;
extern cvar_t *r_vboModels;
extern Cvar::Cvar<bool> r_vboModels;
extern cvar_t *r_vboVertexSkinning;

extern cvar_t *r_mergeLeafSurfaces;
Expand Down Expand Up @@ -3414,7 +3414,6 @@ inline bool checkGLErrors()
vec4_t texCoords;
};

#ifdef GL_ARB_sync
struct glRingbuffer_t {
// the BO is logically split into DYN_BUFFER_SEGMENTS
// segments, the active segment is the one the CPU may write
Expand All @@ -3428,7 +3427,6 @@ inline bool checkGLErrors()
// sync is always undefined
GLsync syncs[ DYN_BUFFER_SEGMENTS ];
};
#endif

struct shaderCommands_t
{
Expand Down Expand Up @@ -3498,10 +3496,8 @@ inline bool checkGLErrors()
VBO_t *vbo;
IBO_t *ibo;

#ifdef GL_ARB_sync
glRingbuffer_t vertexRB;
glRingbuffer_t indexRB;
#endif
};

alignas(16) extern shaderCommands_t tess;
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void R_AddMDVSurfaces( trRefEntity_t *ent )
fogNum = R_FogWorldBox( ent->worldBounds );

// draw all surfaces
if ( r_vboModels->integer && model->numVBOSurfaces )
if ( r_vboModels.Get() && model->numVBOSurfaces )
{
srfVBOMDVMesh_t *vboSurface;

Expand Down Expand Up @@ -416,7 +416,7 @@ void R_AddMDVInteractions( trRefEntity_t *ent, trRefLight_t *light, interactionT
cubeSideBits = R_CalcLightCubeSideBits( light, ent->worldBounds );

// generate interactions with all surfaces
if ( r_vboModels->integer && model->numVBOSurfaces )
if ( r_vboModels.Get() && model->numVBOSurfaces )
{
// new brute force method: just render everthing with static VBOs
srfVBOMDVMesh_t *vboSurface;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/tr_model_iqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ bool R_LoadIQModel( model_t *mod, const void *buffer, int filesize,
}

// convert data where necessary and create VBO
if( r_vboModels->integer && glConfig2.vboVertexSkinningAvailable
if( r_vboModels.Get() && glConfig2.vboVertexSkinningAvailable
&& IQModel->num_joints <= glConfig2.maxVertexSkinningBones ) {

uint16_t *boneFactorBuf = (uint16_t*)ri.Hunk_AllocateTempMemory( IQModel->num_vertexes * ( 4 * sizeof(uint16_t) ) );
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_model_md3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ bool R_LoadMD3( model_t *mod, int lod, const void *buffer, const char *modName )
LL( md3Surf->ofsXyzNormals );
LL( md3Surf->ofsEnd );

if ( md3Surf->numVerts > SHADER_MAX_VERTEXES )
if ( !r_vboModels.Get() && md3Surf->numVerts > SHADER_MAX_VERTEXES )
{
Sys::Drop( "R_LoadMD3: %s has more than %i verts on a surface (%i)",
modName, SHADER_MAX_VERTEXES, md3Surf->numVerts );
}

if ( md3Surf->numTriangles * 3 > SHADER_MAX_INDEXES )
if ( !r_vboModels.Get() && md3Surf->numTriangles * 3 > SHADER_MAX_INDEXES )
{
Sys::Drop( "R_LoadMD3: %s has more than %i triangles on a surface (%i)",
modName, SHADER_MAX_INDEXES / 3, md3Surf->numTriangles );
Expand Down
4 changes: 2 additions & 2 deletions src/engine/renderer/tr_model_md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ bool R_LoadMD5( model_t *mod, const char *buffer, const char *modName )
token = COM_ParseExt2( &buf_p, false );
surf->numVerts = atoi( token );

if ( surf->numVerts > SHADER_MAX_VERTEXES )
if ( !r_vboModels.Get() && surf->numVerts > SHADER_MAX_VERTEXES )
{
Sys::Drop( "R_LoadMD5: '%s' has more than %i verts on a surface (%i)",
modName, SHADER_MAX_VERTEXES, surf->numVerts );
Expand Down Expand Up @@ -417,7 +417,7 @@ bool R_LoadMD5( model_t *mod, const char *buffer, const char *modName )
token = COM_ParseExt2( &buf_p, false );
surf->numTriangles = atoi( token );

if ( surf->numTriangles > SHADER_MAX_TRIANGLES )
if ( !r_vboModels.Get() && surf->numTriangles > SHADER_MAX_TRIANGLES )
{
Sys::Drop( "R_LoadMD5: '%s' has more than %i triangles on a surface (%i)",
modName, SHADER_MAX_TRIANGLES, surf->numTriangles );
Expand Down
96 changes: 32 additions & 64 deletions src/engine/renderer/tr_vbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,23 @@ static uint32_t ComponentSize( GLenum type )
Sys::Error( "VBO ComponentSize: unknown type %d", type );
}

#if defined( GL_ARB_buffer_storage ) && defined( GL_ARB_sync )
/*
============
R_InitRingbuffer
============
*/
static void R_InitRingbuffer( GLenum target, GLsizei elementSize,
GLsizei segmentElements, glRingbuffer_t *rb ) {
GLsizei segmentElements, glRingbuffer_t *rb ) {
GLsizei totalSize = elementSize * segmentElements * DYN_BUFFER_SEGMENTS;
int i;

glBufferStorage( target, totalSize, nullptr,
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT );
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT );
rb->baseAddr = glMapBufferRange( target, 0, totalSize,
GL_MAP_WRITE_BIT |
GL_MAP_PERSISTENT_BIT |
GL_MAP_FLUSH_EXPLICIT_BIT );
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_FLUSH_EXPLICIT_BIT );
rb->elementSize = elementSize;
rb->segmentElements = segmentElements;
rb->activeSegment = 0;
for( i = 1; i < DYN_BUFFER_SEGMENTS; i++ ) {
for( int i = 1; i < DYN_BUFFER_SEGMENTS; i++ ) {
rb->syncs[ i ] = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
}
}
Expand All @@ -127,13 +123,15 @@ static GLsizei R_RotateRingbuffer( glRingbuffer_t *rb ) {
rb->syncs[ rb->activeSegment ] = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );

rb->activeSegment++;
if( rb->activeSegment >= DYN_BUFFER_SEGMENTS )
if ( rb->activeSegment >= DYN_BUFFER_SEGMENTS ) {
rb->activeSegment = 0;
}

// wait until next segment is ready in 1 sec intervals
while( glClientWaitSync( rb->syncs[ rb->activeSegment ], GL_SYNC_FLUSH_COMMANDS_BIT,
10000000 ) == GL_TIMEOUT_EXPIRED ) {
Log::Warn("long wait for GL buffer" );
// wait until next segment is ready in 10ms intervals
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, I've seen that warning occasionally. Maybe the timeout should be longer. (non-blocking comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't think I've ever actually seen that one. I've got some further in-progress changes to these dynamic VBOs/IBOs to fix navedit performance, it might fix the warn too.

const GLuint64 TIMEOUT = 10000000;
while( glClientWaitSync( rb->syncs[ rb->activeSegment ], GL_SYNC_FLUSH_COMMANDS_BIT, TIMEOUT )
== GL_TIMEOUT_EXPIRED ) {
Log::Warn( "Long wait for dynamic GL buffer: active segment: %i, timeout: %uns", rb->activeSegment, TIMEOUT );
};
glDeleteSync( rb->syncs[ rb->activeSegment ] );

Expand All @@ -146,24 +144,19 @@ R_ShutdownRingbuffer
============
*/
static void R_ShutdownRingbuffer( GLenum target, glRingbuffer_t *rb ) {
int i;

glUnmapBuffer( target );
rb->baseAddr = nullptr;

for( i = 0; i < DYN_BUFFER_SEGMENTS; i++ ) {
for( int i = 0; i < DYN_BUFFER_SEGMENTS; i++ ) {
if( i == rb->activeSegment )
continue;

glDeleteSync( rb->syncs[ i ] );
}
}
#endif

VBO_t *R_CreateDynamicVBO( const char *name, int numVertexes, uint32_t stateBits, vboLayout_t layout )
{
VBO_t *vbo;

if ( !numVertexes )
{
return nullptr;
Expand All @@ -172,7 +165,7 @@ VBO_t *R_CreateDynamicVBO( const char *name, int numVertexes, uint32_t stateBits
// make sure the render thread is stopped
R_SyncRenderThread();

vbo = (VBO_t*) ri.Hunk_Alloc( sizeof( *vbo ), ha_pref::h_low );
VBO_t* vbo = (VBO_t*) ri.Hunk_Alloc( sizeof( *vbo ), ha_pref::h_low );
*vbo = {};

tr.vbos.push_back( vbo );
Expand All @@ -191,16 +184,13 @@ VBO_t *R_CreateDynamicVBO( const char *name, int numVertexes, uint32_t stateBits

R_BindVBO( vbo );

#if defined( GL_ARB_buffer_storage ) && defined( GL_ARB_sync )
if( glConfig2.mapBufferRangeAvailable && glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
R_InitRingbuffer( GL_ARRAY_BUFFER, sizeof( shaderVertex_t ),
numVertexes, &tess.vertexRB );
} else
#endif
{
R_InitRingbuffer( GL_ARRAY_BUFFER, sizeof( shaderVertex_t ), numVertexes, &tess.vertexRB );
} else {
glBufferData( GL_ARRAY_BUFFER, vbo->vertexesSize, nullptr, vbo->usage );
}

R_BindNullVBO();

GL_CheckErrors();
Expand Down Expand Up @@ -361,12 +351,9 @@ VBO_t *R_CreateStaticVBO(
}
}

#ifdef GL_ARB_buffer_storage
if( glConfig2.bufferStorageAvailable ) {
glBufferStorage( GL_ARRAY_BUFFER, vbo->vertexesSize, interleavedData, 0 );
} else
#endif
{
} else {
glBufferData( GL_ARRAY_BUFFER, vbo->vertexesSize, interleavedData, vbo->usage );
}

Expand All @@ -385,12 +372,10 @@ R_CreateIBO
*/
IBO_t *R_CreateDynamicIBO( const char *name, int numIndexes )
{
IBO_t *ibo;

// make sure the render thread is stopped
R_SyncRenderThread();

ibo = (IBO_t*) ri.Hunk_Alloc( sizeof( *ibo ), ha_pref::h_low );
IBO_t* ibo = (IBO_t*) ri.Hunk_Alloc( sizeof( *ibo ), ha_pref::h_low );
tr.ibos.push_back( ibo );

Q_strncpyz( ibo->name, name, sizeof( ibo->name ) );
Expand All @@ -401,16 +386,13 @@ IBO_t *R_CreateDynamicIBO( const char *name, int numIndexes )
glGenBuffers( 1, &ibo->indexesVBO );

R_BindIBO( ibo );
#if defined( GL_ARB_buffer_storage ) && defined( GL_ARB_sync )
if( glConfig2.mapBufferRangeAvailable && glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
R_InitRingbuffer( GL_ELEMENT_ARRAY_BUFFER, sizeof( glIndex_t ),
numIndexes, &tess.indexRB );
} else
#endif
{
R_InitRingbuffer( GL_ELEMENT_ARRAY_BUFFER, sizeof( glIndex_t ), numIndexes, &tess.indexRB );
} else {
glBufferData( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize, nullptr, GL_DYNAMIC_DRAW );
}

R_BindNullIBO();

GL_CheckErrors();
Expand Down Expand Up @@ -447,14 +429,12 @@ IBO_t *R_CreateStaticIBO( const char *name, glIndex_t *indexes, int numIndexes )

R_BindIBO( ibo );

#ifdef GL_ARB_buffer_storage
if( glConfig2.bufferStorageAvailable ) {
glBufferStorage( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize, indexes, 0 );
} else
#endif
{
} else {
glBufferData( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize, indexes, GL_STATIC_DRAW );
}

R_BindNullIBO();

GL_CheckErrors();
Expand Down Expand Up @@ -484,16 +464,14 @@ IBO_t *R_CreateStaticIBO2( const char *name, int numTriangles, glIndex_t *indexe
glGenBuffers( 1, &ibo->indexesVBO );
R_BindIBO( ibo );

#ifdef GL_ARB_buffer_storage
if( glConfig2.bufferStorageAvailable ) {
glBufferStorage( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize,
indexes, 0 );
} else
#endif
{
indexes, 0 );
} else {
glBufferData( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize,
indexes, GL_STATIC_DRAW );
indexes, GL_STATIC_DRAW );
}

R_BindNullIBO();

return ibo;
Expand Down Expand Up @@ -787,17 +765,13 @@ void R_ShutdownVBOs()

if( !glConfig2.mapBufferRangeAvailable ) {
// nothing
}
#if defined( GL_ARB_buffer_storage ) && defined( GL_ARB_sync )
else if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
} else if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
R_BindVBO( tess.vbo );
R_ShutdownRingbuffer( GL_ARRAY_BUFFER, &tess.vertexRB );
R_BindIBO( tess.ibo );
R_ShutdownRingbuffer( GL_ELEMENT_ARRAY_BUFFER, &tess.indexRB );
}
#endif
else {
} else {
if( tess.verts != nullptr && tess.verts != tess.vertsBuffer ) {
R_BindVBO( tess.vbo );
glUnmapBuffer( GL_ARRAY_BUFFER );
Expand Down Expand Up @@ -869,17 +843,14 @@ void Tess_MapVBOs( bool forceCPU ) {
if( tess.verts == nullptr ) {
R_BindVBO( tess.vbo );

#if defined( GL_ARB_buffer_storage ) && defined( GL_ARB_sync )
if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
GLsizei segmentEnd = (tess.vertexRB.activeSegment + 1) * tess.vertexRB.segmentElements;
if( tess.vertsWritten + SHADER_MAX_VERTEXES > (unsigned) segmentEnd ) {
tess.vertsWritten = R_RotateRingbuffer( &tess.vertexRB );
}
tess.verts = ( shaderVertex_t * )tess.vertexRB.baseAddr + tess.vertsWritten;
} else
#endif
{
} else {
if( vertexCapacity - tess.vertsWritten < SHADER_MAX_VERTEXES ) {
// buffer is full, allocate a new one
glBufferData( GL_ARRAY_BUFFER, vertexCapacity * sizeof( shaderVertex_t ), nullptr, GL_DYNAMIC_DRAW );
Expand All @@ -896,17 +867,14 @@ void Tess_MapVBOs( bool forceCPU ) {
if( tess.indexes == nullptr ) {
R_BindIBO( tess.ibo );

#if defined( GL_ARB_buffer_storage ) && defined( GL_ARB_sync )
if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
GLsizei segmentEnd = (tess.indexRB.activeSegment + 1) * tess.indexRB.segmentElements;
if( tess.indexesWritten + SHADER_MAX_INDEXES > (unsigned) segmentEnd ) {
tess.indexesWritten = R_RotateRingbuffer( &tess.indexRB );
}
tess.indexes = ( glIndex_t * )tess.indexRB.baseAddr + tess.indexesWritten;
} else
#endif
{
} else {
if( indexCapacity - tess.indexesWritten < SHADER_MAX_INDEXES ) {
// buffer is full, allocate a new one
glBufferData( GL_ELEMENT_ARRAY_BUFFER, indexCapacity * sizeof( glIndex_t ), nullptr, GL_DYNAMIC_DRAW );
Expand Down
1 change: 0 additions & 1 deletion src/engine/sys/sdl_glimp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2388,7 +2388,6 @@ static void GLimp_InitExtensions()
glConfig2.getProgramBinaryAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, ARB_get_program_binary, formats > 0 );
}

glConfig2.bufferStorageAvailable = false;
glConfig2.bufferStorageAvailable = LOAD_EXTENSION_WITH_TEST( ExtFlag_NONE, ARB_buffer_storage, r_arb_buffer_storage.Get() );

// made required since OpenGL 3.1
Expand Down