Skip to content

Material and u_Lights buffer clean-up #1671

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 6 commits into from
May 30, 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
1 change: 1 addition & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ set(COMMONTESTLIST
)

set(RENDERERLIST
${ENGINE_DIR}/renderer/BufferBind.h
${ENGINE_DIR}/renderer/DetectGLVendors.cpp
${ENGINE_DIR}/renderer/DetectGLVendors.h
${ENGINE_DIR}/renderer/gl_shader.cpp
Expand Down
69 changes: 69 additions & 0 deletions src/engine/renderer/BufferBind.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
===========================================================================

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.

===========================================================================
*/
// BufferBind.h

#ifndef BUFFERBIND_H
#define BUFFERBIND_H

namespace BufferBind {
enum : uint32_t {
// UBO
MATERIALS = 0,
TEX_DATA = 1,
LIGHTMAP_DATA = 2,
LIGHTS = 3,

SURFACE_BATCHES = 4,

// SSBO
SURFACE_DESCRIPTORS = 0,
SURFACE_COMMANDS = 1,
CULLED_COMMANDS = 2,
PORTAL_SURFACES = 4,

GEOMETRY_CACHE_INPUT_VBO = 5,
GEOMETRY_CACHE_VBO = 6,
GEOMETRY_CACHE_IBO = 7,

COMMAND_COUNTERS_STORAGE = 9,
TEX_DATA_STORAGE = 11,

DEBUG = 10,

// Atomic
COMMAND_COUNTERS_ATOMIC = 0
};
};

#endif // BUFFERBIND_H
6 changes: 3 additions & 3 deletions src/engine/renderer/GeometryCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class GeometryCache {

GLVAO VAO = GLVAO( 0 );

GLBuffer inputVBO = GLBuffer( "geometryCacheInputVBO", Util::ordinal( BufferBind::GEOMETRY_CACHE_INPUT_VBO ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer VBO = GLBuffer( "geometryCacheVBO", Util::ordinal( BufferBind::GEOMETRY_CACHE_VBO ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLBuffer IBO = GLBuffer( "geometryCacheIBO", Util::ordinal( BufferBind::UNUSED ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
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 );
};

extern GeometryCache geometryCache;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/GeometryOptimiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,8 @@ std::vector<MaterialSurface> OptimiseMapGeometryMaterial( world_t* world, bspSur
materialSystem.GeneratePortalBoundingSpheres();
materialSystem.GenerateWorldCommandBuffer( processedMaterialSurfaces );

materialSystem.BindBuffers();

vertexAttributeSpec_t attrs[] {
{ ATTR_INDEX_POSITION, GL_FLOAT, GL_FLOAT, &vertices[0].xyz, 3, sizeof( *vertices ), 0 },
{ ATTR_INDEX_COLOR, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, &vertices[0].lightColor, 4, sizeof( *vertices ), ATTR_OPTION_NORMALIZE },
Expand Down
109 changes: 45 additions & 64 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "tr_local.h"
#include "Material.h"
#include "BufferBind.h"
#include "ShadeCommon.h"
#include "GeometryCache.h"

GLUBO materialsUBO( "materials", Util::ordinal( BufferBind::MATERIALS ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer texDataBuffer( "texData", Util::ordinal( BufferBind::TEX_DATA ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO lightMapDataUBO( "lightMapData", Util::ordinal( BufferBind::LIGHTMAP_DATA ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO materialsUBO( "materials", BufferBind::MATERIALS, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer texDataBuffer( "texData", BufferBind::TEX_DATA, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO lightMapDataUBO( "lightMapData", BufferBind::LIGHTMAP_DATA, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );

GLSSBO surfaceDescriptorsSSBO( "surfaceDescriptors", Util::ordinal( BufferBind::SURFACE_DESCRIPTORS ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLSSBO surfaceCommandsSSBO( "surfaceCommands", Util::ordinal( BufferBind::SURFACE_COMMANDS ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLBuffer culledCommandsBuffer( "culledCommands", Util::ordinal( BufferBind::CULLED_COMMANDS ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO surfaceBatchesUBO( "surfaceBatches", Util::ordinal( BufferBind::SURFACE_BATCHES ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer atomicCommandCountersBuffer( "atomicCommandCounters", Util::ordinal( BufferBind::COMMAND_COUNTERS_ATOMIC ), GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLSSBO portalSurfacesSSBO( "portalSurfaces", Util::ordinal( BufferBind::PORTAL_SURFACES ), GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT, 0 );
GLSSBO surfaceDescriptorsSSBO( "surfaceDescriptors", BufferBind::SURFACE_DESCRIPTORS, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLSSBO surfaceCommandsSSBO( "surfaceCommands", BufferBind::SURFACE_COMMANDS, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLBuffer culledCommandsBuffer( "culledCommands", BufferBind::CULLED_COMMANDS, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLUBO surfaceBatchesUBO( "surfaceBatches", BufferBind::SURFACE_BATCHES, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLBuffer atomicCommandCountersBuffer( "atomicCommandCounters", BufferBind::COMMAND_COUNTERS_ATOMIC, GL_MAP_WRITE_BIT, GL_MAP_FLUSH_EXPLICIT_BIT );
GLSSBO portalSurfacesSSBO( "portalSurfaces", BufferBind::PORTAL_SURFACES, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT, 0 );

GLSSBO debugSSBO( "debug", Util::ordinal( BufferBind::DEBUG ), GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );
GLSSBO debugSSBO( "debug", BufferBind::DEBUG, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_RANGE_BIT );

PortalView portalStack[MAX_VIEWS];

Expand Down Expand Up @@ -541,7 +542,13 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
surfaceDescriptorsSSBO.BufferData( surfaceDescriptorsCount * descriptorSize, nullptr, GL_STATIC_DRAW );
uint32_t* surfaceDescriptors = surfaceDescriptorsSSBO.MapBufferRange( surfaceDescriptorsCount * descriptorSize );

texDataBufferType = glConfig2.maxUniformBlockSize >= MIN_MATERIAL_UBO_SIZE ? GL_UNIFORM_BUFFER : GL_SHADER_STORAGE_BUFFER;
if ( glConfig2.maxUniformBlockSize >= MIN_MATERIAL_UBO_SIZE ) {
texDataBufferType = GL_UNIFORM_BUFFER;
texDataBindingPoint = BufferBind::TEX_DATA;
} else {
texDataBufferType = GL_SHADER_STORAGE_BUFFER;
texDataBindingPoint = BufferBind::TEX_DATA_STORAGE;
}

texDataBuffer.BufferStorage( ( texData.size() + dynamicTexData.size() ) * TEX_BUNDLE_SIZE, 1, nullptr );
texDataBuffer.MapAll();
Expand Down Expand Up @@ -718,6 +725,31 @@ void MaterialSystem::GenerateWorldCommandBuffer( std::vector<MaterialSurface>& s
GL_CheckErrors();
}

void MaterialSystem::BindBuffers() {
materialsUBO.BindBufferBase();
texDataBuffer.BindBufferBase( texDataBufferType, texDataBindingPoint );
lightMapDataUBO.BindBufferBase();

culledCommandsBuffer.BindBuffer( GL_DRAW_INDIRECT_BUFFER );
atomicCommandCountersBuffer.BindBuffer( GL_PARAMETER_BUFFER_ARB );

atomicCommandCountersBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER, BufferBind::COMMAND_COUNTERS_STORAGE );

surfaceDescriptorsSSBO.BindBufferBase();
surfaceCommandsSSBO.BindBufferBase();
culledCommandsBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER );
surfaceBatchesUBO.BindBufferBase();
atomicCommandCountersBuffer.BindBufferBase( GL_ATOMIC_COUNTER_BUFFER );

if ( totalPortals > 0 ) {
portalSurfacesSSBO.BindBufferBase();
}

if ( r_materialDebug.Get() ) {
debugSSBO.BindBufferBase();
}
}

void MaterialSystem::GenerateDepthImages( const int width, const int height, imageParams_t imageParms ) {
imageParms.bits ^= ( IF_NOPICMIP | IF_PACKED_DEPTH24_STENCIL8 );
imageParms.bits |= IF_ONECOMP32F;
Expand Down Expand Up @@ -805,8 +837,6 @@ void BindShaderLightMapping( Material* material ) {
}

if ( glConfig2.realtimeLighting ) {
gl_lightMappingShaderMaterial->SetUniformBlock_Lights( tr.dlightUBO );

// bind u_LightTiles
gl_lightMappingShaderMaterial->SetUniform_LightTilesBindless(
GL_BindToTMU( BIND_LIGHTTILES, tr.lighttileRenderImage )
Expand Down Expand Up @@ -1464,14 +1494,10 @@ void MaterialSystem::UpdateDynamicSurfaces() {
}

void MaterialSystem::UpdateFrameData() {
atomicCommandCountersBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER, Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );

gl_clearSurfacesShader->BindProgram( 0 );
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );
gl_clearSurfacesShader->DispatchCompute( MAX_VIEWS, 1, 1 );

atomicCommandCountersBuffer.UnBindBufferBase( GL_SHADER_STORAGE_BUFFER, Util::ordinal( BufferBind::COMMAND_COUNTERS_STORAGE ) );

GL_CheckErrors();
}

Expand Down Expand Up @@ -1523,29 +1549,15 @@ void MaterialSystem::DepthReduction() {

glMemoryBarrier( GL_SHADER_IMAGE_ACCESS_BARRIER_BIT );
}

GL_CheckErrors();
}

void MaterialSystem::CullSurfaces() {
if ( r_gpuOcclusionCulling.Get() ) {
DepthReduction();
}

surfaceDescriptorsSSBO.BindBufferBase();
surfaceCommandsSSBO.BindBufferBase();
culledCommandsBuffer.BindBufferBase( GL_SHADER_STORAGE_BUFFER );
surfaceBatchesUBO.BindBufferBase();
atomicCommandCountersBuffer.BindBufferBase( GL_ATOMIC_COUNTER_BUFFER );

if ( totalPortals > 0 ) {
portalSurfacesSSBO.BindBufferBase();
}

if ( r_materialDebug.Get() ) {
debugSSBO.BindBufferBase();
}

GL_CheckErrors();

for ( uint32_t view = 0; view < frames[nextFrame].viewCount; view++ ) {
vec3_t origin;
frustum_t* frustum = &frames[nextFrame].viewFrames[view].frustum;
Expand Down Expand Up @@ -1616,20 +1628,6 @@ void MaterialSystem::CullSurfaces() {
gl_processSurfacesShader->DispatchCompute( totalBatchCount, 1, 1 );
}

surfaceDescriptorsSSBO.UnBindBufferBase();
surfaceCommandsSSBO.UnBindBufferBase();
culledCommandsBuffer.UnBindBufferBase( GL_SHADER_STORAGE_BUFFER );
surfaceBatchesUBO.UnBindBufferBase();
atomicCommandCountersBuffer.UnBindBufferBase( GL_ATOMIC_COUNTER_BUFFER );

if ( totalPortals > 0 ) {
portalSurfacesSSBO.UnBindBufferBase();
}

if ( r_materialDebug.Get() ) {
debugSSBO.UnBindBufferBase();
}

GL_CheckErrors();
}

Expand Down Expand Up @@ -1848,7 +1846,6 @@ void MaterialSystem::AddPortalSurfaces() {
return;
}

portalSurfacesSSBO.BindBufferBase();
PortalSurface* portalSurfs = ( PortalSurface* ) portalSurfacesSSBO.GetCurrentAreaData();
viewCount = 0;
// This will recursively find potentially visible portals in each view based on the data read back from the GPU
Expand Down Expand Up @@ -1887,8 +1884,6 @@ void MaterialSystem::RenderMaterials( const shaderSort_t fromSort, const shaderS
frameStart = false;
}

materialsUBO.BindBufferBase();

geometryCache.Bind();

for ( MaterialPack& materialPack : materialPacks ) {
Expand Down Expand Up @@ -2019,13 +2014,6 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
}
material.texturesResident = true;

culledCommandsBuffer.BindBuffer( GL_DRAW_INDIRECT_BUFFER );

atomicCommandCountersBuffer.BindBuffer( GL_PARAMETER_BUFFER_ARB );

texDataBuffer.BindBufferBase( texDataBufferType );
lightMapDataUBO.BindBufferBase();

if ( r_showGlobalMaterials.Get() && material.sort != 0
&& ( material.shaderBinder == BindShaderLightMapping || material.shaderBinder == BindShaderGeneric3D ) ) {
vec3_t color;
Expand Down Expand Up @@ -2136,13 +2124,6 @@ void MaterialSystem::RenderMaterial( Material& material, const uint32_t viewID )
}
}

culledCommandsBuffer.UnBindBuffer( GL_DRAW_INDIRECT_BUFFER );

atomicCommandCountersBuffer.UnBindBuffer( GL_PARAMETER_BUFFER_ARB );

texDataBuffer.UnBindBufferBase( texDataBufferType );
lightMapDataUBO.UnBindBufferBase();

if ( material.usePolygonOffset ) {
glDisable( GL_POLYGON_OFFSET_FILL );
}
Expand Down
20 changes: 3 additions & 17 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,6 @@ struct SurfaceCommandBatch {
uint32_t materialIDs[2] { 0, 0 };
};

enum class BufferBind {
MATERIALS = 1, // LightTile UBO uses binding point 0, so avoid it here
TEX_DATA = 6,
LIGHTMAP_DATA = 2,
SURFACE_DESCRIPTORS = 0,
SURFACE_COMMANDS = 1,
CULLED_COMMANDS = 2,
SURFACE_BATCHES = 3,
COMMAND_COUNTERS_ATOMIC = 0,
COMMAND_COUNTERS_STORAGE = 4, // Avoid needlessly rebinding buffers
PORTAL_SURFACES = 5,
GEOMETRY_CACHE_INPUT_VBO = 6,
GEOMETRY_CACHE_VBO = 7,
DEBUG = 10,
UNUSED = INT32_MAX
};

class MaterialSystem {
public:
vec3_t worldViewBounds[2];
Expand Down Expand Up @@ -381,6 +364,8 @@ class MaterialSystem {
void InitGLBuffers();
void FreeGLBuffers();

void BindBuffers();

uint32_t GetTexDataSize() const {
return texData.size();
}
Expand Down Expand Up @@ -428,6 +413,7 @@ class MaterialSystem {
std::vector<shaderStage_t*> dynamicStages;

GLenum texDataBufferType;
GLuint texDataBindingPoint;
std::vector<TextureData> texData;
std::vector<TextureData> dynamicTexData;

Expand Down
Loading