Skip to content

Use VAOs for most vertex specification #1690

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 5 commits into from
Aug 1, 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
3 changes: 3 additions & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ set(RENDERERLIST
${ENGINE_DIR}/renderer/GeometryOptimiser.h
${ENGINE_DIR}/renderer/GLMemory.cpp
${ENGINE_DIR}/renderer/GLMemory.h
${ENGINE_DIR}/renderer/GLUtils.h
${ENGINE_DIR}/renderer/InternalImage.cpp
${ENGINE_DIR}/renderer/InternalImage.h
${ENGINE_DIR}/renderer/Material.cpp
Expand Down Expand Up @@ -137,6 +138,8 @@ set(RENDERERLIST
${ENGINE_DIR}/renderer/tr_surface.cpp
${ENGINE_DIR}/renderer/tr_types.h
${ENGINE_DIR}/renderer/tr_vbo.cpp
${ENGINE_DIR}/renderer/VBO.h
${ENGINE_DIR}/renderer/VertexSpecification.h
${ENGINE_DIR}/renderer/tr_video.cpp
${ENGINE_DIR}/renderer/tr_world.cpp
${ENGINE_DIR}/sys/sdl_glimp.cpp
Expand Down
14 changes: 10 additions & 4 deletions src/engine/renderer/GLMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endif
#include <GL/glew.h>

#include "tr_local.h"
#include "BufferBind.h"
#include "GLUtils.h"
#include "VertexSpecification.h"

class GLBuffer {
public:
Expand Down Expand Up @@ -222,14 +223,14 @@ class GLVAO {
vboAttributeLayout_t attrs[ATTR_INDEX_MAX];
uint32_t enabledAttrs;

GLVAO( const GLuint newVBOBindingPoint ) :
GLVAO( const GLuint newVBOBindingPoint = 0 ) :
VBOBindingPoint( newVBOBindingPoint ) {
}

~GLVAO() = default;

void Bind() {
glBindVertexArray( id );
GL_BindVAO( id );
}

void SetAttrs( const vertexAttributeSpec_t* attrBegin, const vertexAttributeSpec_t* attrEnd ) {
Expand Down Expand Up @@ -274,6 +275,11 @@ class GLVAO {
glVertexArrayElementBuffer( id, buffer.id );
}

// For compatibility with IBO_t
void SetIndexBuffer( const GLuint bufferID ) {
glVertexArrayElementBuffer( id, bufferID );
}

void GenVAO() {
glGenVertexArrays( 1, &id );
}
Expand All @@ -284,7 +290,7 @@ class GLVAO {

private:
GLuint id;
const GLuint VBOBindingPoint;
GLuint VBOBindingPoint;
GLuint stride;
};

Expand Down
43 changes: 43 additions & 0 deletions src/engine/renderer/GLUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>

This file is part of Daemon source code.

Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// GLUtils.h

#ifndef GLUTILS_H
#define GLUTILS_H

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

#include "tr_public.h"
#include "tr_types.h"

extern glconfig_t glConfig; // outside of TR since it shouldn't be cleared during ref re-init
extern glconfig2_t glConfig2;

void GL_CheckErrors_( const char *filename, int line );

void GL_BindVAO( const GLuint id );

#define GL_CheckErrors() do { if ( !glConfig.smpActive ) GL_CheckErrors_( __FILE__, __LINE__ ); } while ( false )

#endif // GLUTILS_H
2 changes: 1 addition & 1 deletion src/engine/renderer/GeometryCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ void GeometryCache::AddMapGeometry( const uint32_t verticesNumber, const uint32_

stagingBuffer.FlushAll();

glBindVertexArray( backEnd.currentVAO );
GL_BindVAO( backEnd.defaultVAO );
}
1 change: 1 addition & 0 deletions src/engine/renderer/InternalImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// InternalImage.cpp
#include "tr_local.h"
#include "GLUtils.h"

// Comments may include short quotes from other authors.

Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ void MaterialSystem::RenderMaterials( const shaderSort_t fromSort, const shaderS
}
}

glBindVertexArray( backEnd.currentVAO );
GL_BindVAO( backEnd.defaultVAO );

// Draw the skybox here because we skipped R_AddWorldSurfaces()
const bool environmentFogDraw = ( fromSort <= shaderSort_t::SS_ENVIRONMENT_FOG ) && ( toSort >= shaderSort_t::SS_ENVIRONMENT_FOG );
Expand Down
1 change: 1 addition & 0 deletions src/engine/renderer/TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// TextureManager.cpp

#include "tr_local.h"
#include "GLUtils.h"

Texture::Texture() {
}
Expand Down
86 changes: 86 additions & 0 deletions src/engine/renderer/VBO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>

This file is part of Daemon source code.

Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// VBO.h

#ifndef VBO_H
#define VBO_H

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

#include "GLMemory.h"
#include "VertexSpecification.h"
#include "tr_types.h"

struct VBO_t
{
char name[96]; // only for debugging with /listVBOs

uint32_t vertexesVBO;

uint32_t vertexesSize; // total amount of memory data allocated for this vbo

uint32_t vertexesNum;
uint32_t framesNum; // number of frames for vertex animation

std::array<vboAttributeLayout_t, ATTR_INDEX_MAX> attribs; // info for buffer manipulation

vboLayout_t layout;
uint32_t attribBits; // Which attributes it has. Mostly for detecting errors
GLenum usage;

GLVAO VAO;
bool dynamicVAO = false;
};

struct IBO_t
{
char name[96]; // only for debugging with /listVBOs

uint32_t indexesVBO;
uint32_t indexesSize; // amount of memory data allocated for all triangles in bytes
uint32_t indexesNum;
};

void R_CopyVertexAttribute( const vboAttributeLayout_t& attrib, const vertexAttributeSpec_t& spec,
uint32_t count, byte* interleavedData );

VBO_t* R_CreateStaticVBO(
Str::StringRef name, const vertexAttributeSpec_t* attrBegin, const vertexAttributeSpec_t* attrEnd,
uint32_t numVerts, uint32_t numFrames = 0 );

IBO_t* R_CreateStaticIBO( const char* name, glIndex_t* indexes, int numIndexes );

void SetupVAOBuffers( VBO_t* VBO, const IBO_t* IBO, const uint32_t stateBits,
GLVAO* VAO );

void R_BindVBO( VBO_t* vbo );
void R_BindNullVBO();

void R_BindIBO( IBO_t* ibo );
void R_BindNullIBO();

void R_InitVBOs();
void R_ShutdownVBOs();

#endif // GLMEMORY_H
110 changes: 110 additions & 0 deletions src/engine/renderer/VertexSpecification.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>

This file is part of Daemon source code.

Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// VertexSpecification.h

#ifndef VERTEX_SPECIFICATION_H
#define VERTEX_SPECIFICATION_H

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

enum
{
ATTR_INDEX_POSITION = 0,
ATTR_INDEX_TEXCOORD, // TODO split into 2-element texcoords and 4-element tex + lm coords
ATTR_INDEX_QTANGENT,
ATTR_INDEX_COLOR,

// GPU vertex skinning
ATTR_INDEX_BONE_FACTORS,

// GPU vertex animations
ATTR_INDEX_POSITION2,
ATTR_INDEX_QTANGENT2,
ATTR_INDEX_MAX
};

// must match order of ATTR_INDEX enums
static const char* const attributeNames[] =
{
"attr_Position",
"attr_TexCoord0",
"attr_QTangent",
"attr_Color",
"attr_BoneFactors",
"attr_Position2",
"attr_QTangent2"
};

enum
{
ATTR_POSITION = BIT( ATTR_INDEX_POSITION ),
ATTR_TEXCOORD = BIT( ATTR_INDEX_TEXCOORD ),
ATTR_QTANGENT = BIT( ATTR_INDEX_QTANGENT ),
ATTR_COLOR = BIT( ATTR_INDEX_COLOR ),

ATTR_BONE_FACTORS = BIT( ATTR_INDEX_BONE_FACTORS ),

// for .md3 interpolation
ATTR_POSITION2 = BIT( ATTR_INDEX_POSITION2 ),
ATTR_QTANGENT2 = BIT( ATTR_INDEX_QTANGENT2 ),

ATTR_INTERP_BITS = ATTR_POSITION2 | ATTR_QTANGENT2,
};

struct vboAttributeLayout_t
{
GLint numComponents; // how many components in a single attribute for a single vertex
GLenum componentType; // the input type for a single component
GLboolean normalize; // convert signed integers to the floating point range [-1, 1], and unsigned integers to the range [0, 1]
GLsizei stride;
GLsizei ofs;
GLsizei frameOffset; // for vertex animation, real offset computed as ofs + frame * frameOffset
};

enum class vboLayout_t
{
VBO_LAYOUT_CUSTOM,
VBO_LAYOUT_STATIC,
};

enum
{
ATTR_OPTION_NORMALIZE = BIT( 0 ),
ATTR_OPTION_HAS_FRAMES = BIT( 1 ),
};

struct vertexAttributeSpec_t
{
int attrIndex;
GLenum componentInputType;
GLenum componentStorageType;
const void* begin;
uint32_t numComponents;
uint32_t stride;
int attrOptions;
};

uint32_t R_ComponentSize( GLenum type );

#endif // VERTEX_SPECIFICATION_H
1 change: 1 addition & 0 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "tr_local.h"
#include "BufferBind.h"
#include "GLUtils.h"
#include <stdexcept>

#define USE_UNIFORM_FIREWALL 1
Expand Down
Loading