Skip to content

Implement staging buffer and use it for the material system #1675

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

Merged
merged 4 commits into from
Jun 14, 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
2 changes: 2 additions & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ set(RENDERERLIST
${ENGINE_DIR}/renderer/GeometryCache.h
${ENGINE_DIR}/renderer/GeometryOptimiser.cpp
${ENGINE_DIR}/renderer/GeometryOptimiser.h
${ENGINE_DIR}/renderer/GLMemory.cpp
${ENGINE_DIR}/renderer/GLMemory.h
${ENGINE_DIR}/renderer/InternalImage.cpp
${ENGINE_DIR}/renderer/InternalImage.h
${ENGINE_DIR}/renderer/Material.cpp
Expand Down
1 change: 1 addition & 0 deletions src/engine/renderer/BufferBind.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace BufferBind {

COMMAND_COUNTERS_STORAGE = 9,
TEX_DATA_STORAGE = 11,
STAGING = 12,

DEBUG = 10,

Expand Down
128 changes: 128 additions & 0 deletions src/engine/renderer/GLMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
===========================================================================

Daemon BSD Source Code
Copyright (c) 2025 Daemon Developers
All rights reserved.

This file is part of the Daemon BSD Source Code (Daemon Source Code).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

===========================================================================
*/
// GLMemory.cpp

#include "common/Common.h"

#include "GLMemory.h"

// 128 MB, should be enough to fit anything in BAR without going overboard
const GLsizeiptr GLStagingBuffer::SIZE = 128 * 1024 * 1024 / sizeof( uint32_t );

GLStagingBuffer stagingBuffer;

void GLBufferCopy( GLBuffer* src, GLBuffer* dst, GLintptr srcOffset, GLintptr dstOffset, GLsizeiptr size ) {
glCopyNamedBufferSubData( src->id, dst->id,
srcOffset * sizeof( uint32_t ), dstOffset * sizeof( uint32_t ), size * sizeof( uint32_t ) );
}

uint32_t* GLStagingBuffer::MapBuffer( const GLsizeiptr size ) {
if ( size > SIZE ) {
Sys::Drop( "Couldn't map GL staging buffer: size too large (%u/%u)", size, SIZE );
}

if ( pointer + size > SIZE ) {
FlushAll();

GLsync sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );

constexpr GLuint64 SYNC_TIMEOUT = 10000000000; // 10 seconds
if ( glClientWaitSync( sync, GL_SYNC_FLUSH_COMMANDS_BIT, SYNC_TIMEOUT ) == GL_TIMEOUT_EXPIRED ) {
Sys::Drop( "Failed GL staging buffer copy sync" );
}
glDeleteSync( sync );

pointer = 0;
current = 0;
last = 0;
}

uint32_t* ret = buffer.GetData() + pointer;
last = pointer;
pointer += size;

return ret;
}

void GLStagingBuffer::FlushBuffer() {
buffer.FlushRange( current, pointer - current );

GL_CheckErrors();

current = pointer;
}

void GLStagingBuffer::QueueStagingCopy( GLBuffer* dst, const GLsizeiptr dstOffset ) {
copyQueue[currentCopy].dst = dst;
copyQueue[currentCopy].dstOffset = dstOffset;
copyQueue[currentCopy].stagingOffset = last;
copyQueue[currentCopy].size = pointer - last;

currentCopy++;

if ( currentCopy == MAX_COPIES ) {
FlushStagingCopyQueue();
}
}

void GLStagingBuffer::FlushStagingCopyQueue() {
for ( GLStagingCopy& copy : copyQueue ) {
if ( copy.dst ) {
GLBufferCopy( &buffer, copy.dst, copy.stagingOffset, copy.dstOffset, copy.size );
copy.dst = nullptr;
}
}

currentCopy = 0;

GL_CheckErrors();
}

void GLStagingBuffer::FlushAll() {
FlushBuffer();
FlushStagingCopyQueue();
}

void GLStagingBuffer::InitGLBuffer() {
buffer.GenBuffer();

buffer.BufferStorage( SIZE, 1, nullptr );
buffer.MapAll();

GL_CheckErrors();
}

void GLStagingBuffer::FreeGLBuffer() {
buffer.DelBuffer();
}
78 changes: 78 additions & 0 deletions src/engine/renderer/GLMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
===========================================================================

Daemon BSD Source Code
Copyright (c) 2025 Daemon Developers
All rights reserved.

This file is part of the Daemon BSD Source Code (Daemon Source Code).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

===========================================================================
*/
// GLMemory.h

#ifndef GLMEMORY_H
#define GLMEMORY_H

#include "gl_shader.h"

void GLBufferCopy( GLBuffer* src, GLBuffer* dst, GLintptr srcOffset, GLintptr dstOffset, GLsizeiptr size );

struct GLStagingCopy {
GLBuffer* dst;
GLsizeiptr stagingOffset;
GLsizeiptr dstOffset;
GLsizeiptr size;
};

class GLStagingBuffer {
public:
uint32_t* MapBuffer( const GLsizeiptr size );
void FlushBuffer();
void QueueStagingCopy( GLBuffer* dst, const GLsizeiptr dstOffset );
void FlushStagingCopyQueue();
void FlushAll();

void InitGLBuffer();
void FreeGLBuffer();

private:
static const GLsizeiptr SIZE;

GLsizeiptr pointer = 0;
GLsizeiptr current = 0;
GLsizeiptr last = 0;

static const uint32_t MAX_COPIES = 16;
GLStagingCopy copyQueue[MAX_COPIES];
uint32_t currentCopy = 0;

GLBuffer buffer = GLBuffer( "staging", BufferBind::STAGING, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT,
GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_INVALIDATE_BUFFER_BIT );
};

extern GLStagingBuffer stagingBuffer;

#endif // GLMEMORY_H
24 changes: 9 additions & 15 deletions src/engine/renderer/GeometryCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "common/Common.h"

#include "GeometryCache.h"

#include "GLMemory.h"
#include "tr_local.h"

GeometryCache geometryCache;
Expand All @@ -61,12 +61,6 @@ void GeometryCache::FreeGLBuffers() {
VAO.DelVAO();
}

void GeometryCache::AllocBuffers() {
VBO.BufferData( mapVerticesNumber * 8, nullptr, GL_STATIC_DRAW );

IBO.BufferData( mapIndicesNumber, nullptr, GL_STATIC_DRAW );
}

void GeometryCache::AddMapGeometry( const uint32_t verticesNumber, const uint32_t indicesNumber,
const vertexAttributeSpec_t* attrBegin, const vertexAttributeSpec_t* attrEnd,
const glIndex_t* indices ) {
Expand All @@ -75,28 +69,28 @@ void GeometryCache::AddMapGeometry( const uint32_t verticesNumber, const uint32_

VAO.Bind();

AllocBuffers();

VAO.SetAttrs( attrBegin, attrEnd );

VAO.SetVertexBuffer( VBO, 0 );
VAO.SetIndexBuffer( IBO );

VBO.BufferStorage( mapVerticesNumber * 8, 1, nullptr );
VBO.MapAll();
uint32_t* VBOVerts = VBO.GetData();
uint32_t* VBOVerts = stagingBuffer.MapBuffer( mapVerticesNumber * 8 );
for ( const vertexAttributeSpec_t* spec = attrBegin; spec < attrEnd; spec++ ) {
vboAttributeLayout_t& attr = VAO.attrs[spec->attrIndex];

R_CopyVertexAttribute( attr, *spec, mapVerticesNumber, ( byte* ) VBOVerts );
}
VBO.UnmapBuffer();

stagingBuffer.QueueStagingCopy( &VBO, 0 );

IBO.BufferStorage( mapIndicesNumber, 1, nullptr );
IBO.MapAll();
uint32_t* IBOIndices = IBO.GetData();
uint32_t* IBOIndices = stagingBuffer.MapBuffer( mapIndicesNumber );
memcpy( IBOIndices, indices, mapIndicesNumber * sizeof( uint32_t ) );
IBO.UnmapBuffer();

stagingBuffer.QueueStagingCopy( &IBO, 0 );

stagingBuffer.FlushAll();

glBindVertexArray( backEnd.currentVAO );
}
5 changes: 2 additions & 3 deletions src/engine/renderer/GeometryCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class GeometryCache {
void InitGLBuffers();
void FreeGLBuffers();

void AllocBuffers();
void AddMapGeometry( const uint32_t verticesNumber, const uint32_t indicesNumber,
const vertexAttributeSpec_t* attrBegin,
const vertexAttributeSpec_t* attrEnd,
Expand All @@ -59,8 +58,8 @@ class GeometryCache {
GLVAO VAO = GLVAO( 0 );

GLBuffer inputVBO = GLBuffer( "geometryCacheInputVBO", BufferBind::GEOMETRY_CACHE_INPUT_VBO, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer VBO = GLBuffer( "geometryCacheVBO", BufferBind::GEOMETRY_CACHE_VBO, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLBuffer IBO = GLBuffer( "geometryCacheIBO", BufferBind::GEOMETRY_CACHE_IBO, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer VBO = GLBuffer( "geometryCacheVBO", BufferBind::GEOMETRY_CACHE_VBO, 0, 0 );
GLBuffer IBO = GLBuffer( "geometryCacheIBO", BufferBind::GEOMETRY_CACHE_IBO, 0, 0 );
};

extern GeometryCache geometryCache;
Expand Down
Loading