-
Notifications
You must be signed in to change notification settings - Fork 63
Implement geometry cache #1515
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
Implement geometry cache #1515
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
=========================================================================== | ||
|
||
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. | ||
|
||
=========================================================================== | ||
*/ | ||
// GeometryCache.cpp | ||
|
||
#include "common/Common.h" | ||
|
||
#include "GeometryCache.h" | ||
|
||
#include "tr_local.h" | ||
|
||
GeometryCache geometryCache; | ||
|
||
void GeometryCache::Bind() { | ||
VAO.Bind(); | ||
} | ||
|
||
void GeometryCache::InitGLBuffers() { | ||
inputVBO.GenBuffer(); | ||
VBO.GenBuffer(); | ||
IBO.GenBuffer(); | ||
|
||
VAO.GenVAO(); | ||
} | ||
|
||
void GeometryCache::FreeGLBuffers() { | ||
inputVBO.DelBuffer(); | ||
VBO.DelBuffer(); | ||
IBO.DelBuffer(); | ||
|
||
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 ) { | ||
mapVerticesNumber = verticesNumber; | ||
mapIndicesNumber = indicesNumber; | ||
|
||
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(); | ||
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(); | ||
|
||
IBO.BufferStorage( mapIndicesNumber, 1, nullptr ); | ||
IBO.MapAll(); | ||
uint32_t* IBOIndices = IBO.GetData(); | ||
memcpy( IBOIndices, indices, mapIndicesNumber * sizeof( uint32_t ) ); | ||
IBO.UnmapBuffer(); | ||
|
||
glBindVertexArray( backEnd.currentVAO ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
=========================================================================== | ||
|
||
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. | ||
|
||
=========================================================================== | ||
*/ | ||
// GeometryCache.h | ||
|
||
#ifndef GEOMETRY_CACHE_H | ||
#define GEOMETRY_CACHE_H | ||
|
||
#include "gl_shader.h" | ||
#include "Material.h" | ||
|
||
class GeometryCache { | ||
public: | ||
void Bind(); | ||
|
||
void InitGLBuffers(); | ||
void FreeGLBuffers(); | ||
|
||
void AllocBuffers(); | ||
void AddMapGeometry( const uint32_t verticesNumber, const uint32_t indicesNumber, | ||
const vertexAttributeSpec_t* attrBegin, | ||
const vertexAttributeSpec_t* attrEnd, | ||
const glIndex_t* indices ); | ||
|
||
private: | ||
uint32_t mapVerticesNumber; | ||
uint32_t mapIndicesNumber; | ||
|
||
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 ); | ||
}; | ||
|
||
extern GeometryCache geometryCache; | ||
|
||
#endif // GEOMETRY_CACHE_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,9 +114,6 @@ struct Material { | |
|
||
bool usePolygonOffset = false; | ||
|
||
VBO_t* vbo; | ||
IBO_t* ibo; | ||
|
||
fog_t* fog = nullptr; | ||
|
||
std::vector<drawSurf_t*> drawSurfs; | ||
|
@@ -125,7 +122,7 @@ struct Material { | |
std::vector<Texture*> textures; | ||
|
||
bool operator==( const Material& other ) { | ||
return program == other.program && stateBits == other.stateBits && vbo == other.vbo && ibo == other.ibo | ||
return program == other.program && stateBits == other.stateBits | ||
&& fog == other.fog && cullType == other.cullType && usePolygonOffset == other.usePolygonOffset; | ||
} | ||
|
||
|
@@ -288,7 +285,10 @@ enum class BufferBind { | |
COMMAND_COUNTERS_ATOMIC = 0, | ||
COMMAND_COUNTERS_STORAGE = 4, // Avoid needlessly rebinding buffers | ||
PORTAL_SURFACES = 5, | ||
DEBUG = 10 | ||
GEOMETRY_CACHE_INPUT_VBO = 6, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if they are supposed to be unique but there are two 6's. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now it will work fine since buffers are still always being bound before the draws/dispatches, and later I'll probably add something to avoid rebinding both old and new-style buffers. |
||
GEOMETRY_CACHE_VBO = 7, | ||
DEBUG = 10, | ||
UNUSED = INT32_MAX | ||
}; | ||
|
||
class MaterialSystem { | ||
|
Uh oh!
There was an error while loading. Please reload this page.