Skip to content

Implement PushBuffer #1684

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
5 changes: 3 additions & 2 deletions src/engine/renderer/BufferBind.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ namespace BufferBind {
MATERIALS = 0,
TEX_DATA = 1,
LIGHTMAP_DATA = 2,
LIGHTS = 3,
GLOBAL_DATA = 3,
LIGHTS = 4,

SURFACE_BATCHES = 4,
SURFACE_BATCHES = 5,

// SSBO
SURFACE_DESCRIPTORS = 0,
Expand Down
40 changes: 40 additions & 0 deletions src/engine/renderer/GLMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "GLMemory.h"

#include "gl_shader.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;
PushBuffer pushBuffer;

void GLBufferCopy( GLBuffer* src, GLBuffer* dst, GLintptr srcOffset, GLintptr dstOffset, GLsizeiptr size ) {
glCopyNamedBufferSubData( src->id, dst->id,
Expand Down Expand Up @@ -114,6 +117,10 @@ void GLStagingBuffer::FlushAll() {
FlushStagingCopyQueue();
}

bool GLStagingBuffer::Active() const {
return buffer.id;
}

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

Expand All @@ -130,3 +137,36 @@ void GLStagingBuffer::FreeGLBuffer() {
current = 0;
last = 0;
}

void PushBuffer::InitGLBuffers() {
globalUBO.GenBuffer();

globalUBO.BufferStorage( pushBuffer.constUniformsSize + pushBuffer.frameUniformsSize, 1, nullptr );

globalUBO.BindBufferBase();
}

void PushBuffer::FreeGLBuffers() {
globalUBO.DelBuffer();
}

uint32_t* PushBuffer::MapGlobalUniformData( const int updateType ) {
switch ( updateType ) {
case GLUniform::CONST:
globalUBOData = stagingBuffer.MapBuffer( constUniformsSize );
stagingBuffer.QueueStagingCopy( &globalUBO, 0 );
break;
case GLUniform::FRAME:
globalUBOData = stagingBuffer.MapBuffer( frameUniformsSize );
stagingBuffer.QueueStagingCopy( &globalUBO, constUniformsSize );
break;
default:
ASSERT_UNREACHABLE();
}

return globalUBOData;
}

void PushBuffer::PushGlobalUniforms() {
stagingBuffer.FlushAll();
}
28 changes: 27 additions & 1 deletion src/engine/renderer/GLMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "common/Common.h"
#include "GL/glew.h"

#include "tr_local.h"
#include "BufferBind.h"

Expand Down Expand Up @@ -172,6 +173,7 @@ class GLBuffer {

void DelBuffer() {
glDeleteBuffers( 1, &id );
id = 0;
mapped = false;
}

Expand Down Expand Up @@ -300,6 +302,8 @@ class GLStagingBuffer {
void FlushStagingCopyQueue();
void FlushAll();

bool Active() const;

void InitGLBuffer();
void FreeGLBuffer();

Expand All @@ -318,6 +322,28 @@ class GLStagingBuffer {
GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_INVALIDATE_BUFFER_BIT );
};

struct PushBuffer {
uint32_t constUniformsSize;
uint32_t frameUniformsSize;
uint32_t globalUBOSize;
uint32_t* globalUBOData;

uint32_t pushStart = UINT32_MAX;
uint32_t pushEnd = 0;

uint32_t sector = 0;
const uint32_t MAX_SECTORS = 1024;

GLUBO globalUBO = GLUBO( "globalUniforms", BufferBind::GLOBAL_DATA, 0, 0 );

void InitGLBuffers();
void FreeGLBuffers();

uint32_t* MapGlobalUniformData( const int updateType );
void PushGlobalUniforms();
};

extern GLStagingBuffer stagingBuffer;
extern PushBuffer pushBuffer;

#endif // GLMEMORY_H
#endif // GLMEMORY_H
48 changes: 27 additions & 21 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void UpdateSurfaceDataGeneric3D( uint32_t* materials, shaderStage_t* pStage, boo
gl_genericShaderMaterial->SetUniform_DepthScale( pStage->depthFadeValue );
}

gl_genericShaderMaterial->WriteUniformsToBuffer( materials );
gl_genericShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataLightMapping( uint32_t* materials, shaderStage_t* pStage, bool, bool vertexLit, bool fullbright ) {
Expand Down Expand Up @@ -212,7 +212,7 @@ void UpdateSurfaceDataLightMapping( uint32_t* materials, shaderStage_t* pStage,
gl_lightMappingShaderMaterial->SetUniform_SpecularExponent( specExpMin, specExpMax );
}

gl_lightMappingShaderMaterial->WriteUniformsToBuffer( materials );
gl_lightMappingShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataReflection( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
Expand Down Expand Up @@ -253,7 +253,7 @@ void UpdateSurfaceDataReflection( uint32_t* materials, shaderStage_t* pStage, bo
gl_reflectionShaderMaterial->SetUniform_ReliefOffsetBias( shader->reliefOffsetBias );
}

gl_reflectionShaderMaterial->WriteUniformsToBuffer( materials );
gl_reflectionShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataSkybox( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
Expand All @@ -264,7 +264,7 @@ void UpdateSurfaceDataSkybox( uint32_t* materials, shaderStage_t* pStage, bool,
// u_AlphaThreshold
gl_skyboxShaderMaterial->SetUniform_AlphaTest( GLS_ATEST_NONE );

gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials );
gl_skyboxShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataScreen( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
Expand All @@ -277,7 +277,7 @@ void UpdateSurfaceDataScreen( uint32_t* materials, shaderStage_t* pStage, bool,
this seems to be the only material system shader that might need it to not be global */
gl_screenShaderMaterial->SetUniform_CurrentMapBindless( BindAnimatedImage( 0, &pStage->bundle[TB_COLORMAP] ) );

gl_screenShaderMaterial->WriteUniformsToBuffer( materials );
gl_screenShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataHeatHaze( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
Expand All @@ -296,7 +296,7 @@ void UpdateSurfaceDataHeatHaze( uint32_t* materials, shaderStage_t* pStage, bool
gl_heatHazeShaderMaterial->SetUniform_NormalScale( normalScale );
}

gl_heatHazeShaderMaterial->WriteUniformsToBuffer( materials );
gl_heatHazeShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataLiquid( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
Expand Down Expand Up @@ -348,15 +348,15 @@ void UpdateSurfaceDataLiquid( uint32_t* materials, shaderStage_t* pStage, bool,
gl_liquidShaderMaterial->SetUniform_NormalScale( normalScale );
}

gl_liquidShaderMaterial->WriteUniformsToBuffer( materials );
gl_liquidShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

void UpdateSurfaceDataFog( uint32_t* materials, shaderStage_t* pStage, bool, bool, bool ) {
// shader_t* shader = pStage->shader;

materials += pStage->bufferOffset;

gl_fogQuake3ShaderMaterial->WriteUniformsToBuffer( materials );
gl_fogQuake3ShaderMaterial->WriteUniformsToBuffer( materials, GLShader::MATERIAL );
}

/*
Expand Down Expand Up @@ -828,12 +828,12 @@ void BindShaderLightMapping( Material* material ) {

// bind u_LightGrid1
if ( material->enableGridLighting ) {
gl_lightMappingShaderMaterial->SetUniform_LightGrid1Bindless( GL_BindToTMU( BIND_LIGHTMAP, tr.lightGrid1Image ) );
gl_lightMappingShaderMaterial->SetUniform_LightGrid1Bindless( GL_BindToTMU( BIND_LIGHTGRID1, tr.lightGrid1Image ) );
}

// bind u_LightGrid2
if ( material->enableGridDeluxeMapping ) {
gl_lightMappingShaderMaterial->SetUniform_LightGrid2Bindless( GL_BindToTMU( BIND_DELUXEMAP, tr.lightGrid2Image ) );
gl_lightMappingShaderMaterial->SetUniform_LightGrid2Bindless( GL_BindToTMU( BIND_LIGHTGRID2, tr.lightGrid2Image ) );
}

if ( glConfig2.realtimeLighting ) {
Expand Down Expand Up @@ -1313,7 +1313,7 @@ void MaterialSystem::ProcessStage( MaterialSurface* surface, shaderStage_t* pSta

material.bspSurface = surface->bspSurface;
pStage->materialProcessor( &material, pStage, surface );
pStage->paddedSize = material.shader->GetSTD430Size();
pStage->paddedSize = material.shader->GetSTD430PaddedSize();

// HACK: Copy the shaderStage_t and MaterialSurface that we need into the material, so we can use it with glsl_restart
material.refStage = pStage;
Expand Down Expand Up @@ -1495,6 +1495,22 @@ void MaterialSystem::UpdateDynamicSurfaces() {
GL_CheckErrors();
}

void MaterialSystem::SetConstUniforms() {
globalUBOProxy->SetUniform_SurfaceDescriptorsCount( surfaceDescriptorsCount );
uint32_t globalWorkGroupX = surfaceDescriptorsCount % MAX_COMMAND_COUNTERS == 0 ?
surfaceDescriptorsCount / MAX_COMMAND_COUNTERS : surfaceDescriptorsCount / MAX_COMMAND_COUNTERS + 1;

globalUBOProxy->SetUniform_FirstPortalGroup( globalWorkGroupX );
globalUBOProxy->SetUniform_TotalPortals( totalPortals );
}

void MaterialSystem::SetFrameUniforms() {
globalUBOProxy->SetUniform_Frame( nextFrame );

globalUBOProxy->SetUniform_UseFrustumCulling( r_gpuFrustumCulling.Get() );
globalUBOProxy->SetUniform_UseOcclusionCulling( r_gpuOcclusionCulling.Get() );
}

void MaterialSystem::UpdateFrameData() {
gl_clearSurfacesShader->BindProgram( 0 );
gl_clearSurfacesShader->SetUniform_Frame( nextFrame );
Expand Down Expand Up @@ -1729,17 +1745,7 @@ void MaterialSystem::Free() {
texData.clear();
dynamicTexData.clear();

R_SyncRenderThread();

surfaceCommandsSSBO.UnmapBuffer();
culledCommandsBuffer.UnmapBuffer();
atomicCommandCountersBuffer.UnmapBuffer();
texDataBuffer.UnmapBuffer();
lightMapDataUBO.UnmapBuffer();

if ( totalPortals > 0 ) {
portalSurfacesSSBO.UnmapBuffer();

for ( PortalView& portalView : portalStack ) {
memset( portalView.views, 0, MAX_VIEWS * sizeof( uint32_t ) );
portalView.count = 0;
Expand Down
2 changes: 2 additions & 0 deletions src/engine/renderer/Material.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ class MaterialSystem {

void StartFrame();
void EndFrame();
void SetConstUniforms();
void SetFrameUniforms();

void GenerateDepthImages( const int width, const int height, imageParams_t imageParms );

Expand Down
Loading