Skip to content

Commit 705e71d

Browse files
committed
Added code to handle the new dynamic VertAttribs in OpenGL.
Not so sure about getting active program and searching for the bound pipeline in the pipeline store, but not set in any particular solution either for the moment.
1 parent 344b97c commit 705e71d

9 files changed

Lines changed: 91 additions & 61 deletions

File tree

cmake/c_cpp_properties.json.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
"@CMAKE_SOURCE_DIR@/tools/worldeditor",
1313
"@CMAKE_BINARY_DIR@/tools/worldeditor",
1414
"@CMAKE_BINARY_DIR@/engine/renderers/opengl",
15-
"@CMAKE_BINARY_DIR@/engine/renderers/vulkan",
1615
"@CMAKE_BINARY_DIR@/engine",
1716
"@CMAKE_BINARY_DIR@/proto/"
1817
],

engine/renderers/opengl/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2016-2019,2021 Rodrigo Jose Hernandez Cordoba
1+
# Copyright (C) 2016-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
44
# use this file except in compliance with the License. You may obtain a copy of
@@ -17,7 +17,7 @@ if(UNIX)
1717
find_package(X11)
1818
endif()
1919

20-
include_directories(${CMAKE_CURRENT_BINARY_DIR})
20+
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include)
2121

2222
set(CMAKE_REQUIRED_QUIET OFF)
2323

@@ -151,11 +151,7 @@ set(OPENGL_RENDERER_HEADERS
151151
${CMAKE_CURRENT_BINARY_DIR}/glAssignments.h
152152
${CMAKE_CURRENT_BINARY_DIR}/glProxyFunctions.h
153153
OpenGLRenderer.h
154-
#OpenGLWinAPIRenderer.h
155-
#OpenGLX11Renderer.h
156154
OpenGLWindow.h
157-
#OpenGLWinAPIWindow.h
158-
#OpenGLX11Window.h
159155
OpenGLFunctions.h
160156
OpenGLBuffer.h
161157
OpenGLMesh.h
@@ -168,11 +164,7 @@ set(OPENGL_RENDERER_HEADERS
168164
set(OPENGL_RENDERER_SOURCES
169165
${CMAKE_CURRENT_BINARY_DIR}/glProxyFunctions.cpp
170166
OpenGLRenderer.cpp
171-
#OpenGLWinAPIRenderer.cpp
172-
#OpenGLX11Renderer.cpp
173167
OpenGLWindow.cpp
174-
#OpenGLWinAPIWindow.cpp
175-
#OpenGLX11Window.cpp
176168
OpenGLBuffer.cpp
177169
OpenGLMesh.cpp
178170
OpenGLPipeline.cpp

engine/renderers/opengl/OpenGLMesh.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2016-2019,2021 Rodrigo Jose Hernandez Cordoba
2+
Copyright (C) 2016-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
#include <cassert>
1818
#include <unordered_map>
19+
#include <algorithm>
1920
#include "OpenGLFunctions.h"
2021
#include "aeongames/Mesh.h"
2122
#include "OpenGLMesh.h"
@@ -62,20 +63,37 @@ namespace AeonGames
6263
{
6364
glBindBuffer ( GL_ARRAY_BUFFER, mVertexBuffer.GetBufferId() );
6465
OPENGL_CHECK_ERROR_THROW;
65-
/** @todo Find out what is best disable all or only unused */
66-
for ( GLuint i = 0; i < Mesh::SEMANTIC_COUNT; ++i )
66+
}
67+
68+
void OpenGLMesh::EnableAttributes ( const std::vector<OpenGLVertexAttribute>& aAttributes ) const
69+
{
70+
for ( GLuint i = 0; i < 8; ++i )
6771
{
6872
glDisableVertexAttribArray ( i );
6973
}
74+
7075
size_t offset{0};
7176
for ( auto& attribute : mMesh->GetAttributes() )
7277
{
73-
glEnableVertexAttribArray ( std::get<0> ( attribute ) );
78+
auto it = std::lower_bound ( aAttributes.begin(), aAttributes.end(), std::get<0> ( attribute ),
79+
[] ( const OpenGLVertexAttribute & a, uint32_t b )
80+
{
81+
return a.name < b;
82+
} );
83+
if ( it == aAttributes.end() || it->name != std::get<0> ( attribute ) )
84+
{
85+
/* If we get here, it means the attribute is not present in aAttributes,
86+
but it is present in the mesh, so skip it and continue. */
87+
offset += GetAttributeTotalSize ( attribute );
88+
continue;
89+
}
90+
91+
glEnableVertexAttribArray ( it->location );
7492
OPENGL_CHECK_ERROR_THROW;
7593
if ( ! ( std::get<3> ( attribute ) & Mesh::AttributeFlag::INTEGER ) )
7694
{
7795
glVertexAttribPointer (
78-
std::get<0> ( attribute ),
96+
it->location,
7997
std::get<1> ( attribute ),
8098
MeshTypeToOGL[std::get<2> ( attribute )],
8199
std::get<3> ( attribute ) & Mesh::AttributeFlag::NORMALIZED,
@@ -86,7 +104,7 @@ namespace AeonGames
86104
else
87105
{
88106
glVertexAttribIPointer (
89-
std::get<0> ( attribute ),
107+
it->location,
90108
std::get<1> ( attribute ),
91109
MeshTypeToOGL[std::get<2> ( attribute )],
92110
static_cast<GLsizei> ( mMesh->GetStride() ),
@@ -105,4 +123,12 @@ namespace AeonGames
105123
}
106124
OPENGL_CHECK_ERROR_THROW;
107125
}
126+
127+
void OpenGLMesh::DisableAttributes ( const std::vector<OpenGLVertexAttribute>& aAttributes ) const
128+
{
129+
for ( const auto& attribute : aAttributes )
130+
{
131+
glDisableVertexAttribArray ( attribute.location );
132+
}
133+
}
108134
}

engine/renderers/opengl/OpenGLMesh.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2016-2019,2021 Rodrigo Jose Hernandez Cordoba
2+
Copyright (C) 2016-2019,2021,2025 Rodrigo Jose Hernandez Cordoba
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,8 +16,9 @@ limitations under the License.
1616
#ifndef AEONGAMES_OPENGLMESH_H
1717
#define AEONGAMES_OPENGLMESH_H
1818

19+
#include <vector>
1920
#include "OpenGLBuffer.h"
20-
21+
#include "OpenGLVertexAttribute.h"
2122
namespace AeonGames
2223
{
2324
class Mesh;
@@ -32,6 +33,8 @@ namespace AeonGames
3233
OpenGLMesh& operator= ( const OpenGLMesh& aOpenGLMesh ) = delete;
3334
OpenGLMesh& operator= ( OpenGLMesh&& aOpenGLMesh ) = delete;
3435
void Bind() const;
36+
void EnableAttributes ( const std::vector<OpenGLVertexAttribute>& aAttributes ) const;
37+
void DisableAttributes ( const std::vector<OpenGLVertexAttribute>& aAttributes ) const;
3538
private:
3639
const OpenGLRenderer& mOpenGLRenderer;
3740
const Mesh* mMesh{nullptr};

engine/renderers/opengl/OpenGLPipeline.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
#include "aeongames/Pipeline.h"
17-
#include "aeongames/CRC.h"
1816
#include "OpenGLPipeline.h"
1917
#include "OpenGLFunctions.h"
18+
#include "aeongames/CRC.h"
2019
#include <vector>
20+
#include <algorithm>
2121

2222
namespace AeonGames
2323
{
@@ -27,6 +27,7 @@ namespace AeonGames
2727
{
2828
std::swap ( mPipeline, aOpenGLPipeline.mPipeline );
2929
std::swap ( mProgramId, aOpenGLPipeline.mProgramId );
30+
mAttributes.swap ( aOpenGLPipeline.mAttributes );
3031
}
3132
#if 0
3233
static std::string GetVertexShaderCode ( const Pipeline& aPipeline )
@@ -238,7 +239,7 @@ namespace AeonGames
238239
GLint num_active_attributes;
239240
glGetProgramiv ( mProgramId, GL_ACTIVE_ATTRIBUTES, &num_active_attributes );
240241
OPENGL_CHECK_ERROR_THROW;
241-
242+
mAttributes.reserve ( num_active_attributes );
242243
for ( GLint i = 0; i < num_active_attributes; ++i )
243244
{
244245
GLchar name[256];
@@ -249,13 +250,13 @@ namespace AeonGames
249250
OPENGL_CHECK_ERROR_THROW;
250251
GLint location = glGetAttribLocation ( mProgramId, name );
251252
OPENGL_CHECK_ERROR_THROW;
252-
if ( location >= 0 )
253-
{
254-
mAttributes.push_back ( Attribute{ crc32i ( name, length ), static_cast<uint32_t> ( location ), static_cast<uint32_t> ( size ), static_cast<uint32_t> ( type ) } );
255-
}
256-
std::cout << "Attribute " << i << ": " << name << " (location: " << location << ", size: " << size << ", type: " << type << ")" << std::endl;
253+
mAttributes.push_back ( { crc32i ( name, length ), location, size, type } );
254+
std::cout << "Attribute " << i << ": " << name << " (crc: " << std::hex << mAttributes.back().name << std::dec << " location: " << location << ", size: " << size << ", type: " << type << ")" << std::endl;
257255
}
258-
256+
std::sort ( mAttributes.begin(), mAttributes.end(), [] ( const OpenGLVertexAttribute & a, const OpenGLVertexAttribute & b )
257+
{
258+
return a.name < b.name;
259+
} );
259260

260261
GLint num_active_uniforms;
261262
glGetProgramiv ( mProgramId, GL_ACTIVE_UNIFORMS, &num_active_uniforms );
@@ -346,8 +347,13 @@ namespace AeonGames
346347
OPENGL_CHECK_ERROR_NO_THROW;
347348
}
348349

349-
uint32_t OpenGLPipeline::GetProgramId() const
350+
GLint OpenGLPipeline::GetProgramId() const
350351
{
351352
return mProgramId;
352353
}
354+
355+
const std::vector<OpenGLVertexAttribute>& OpenGLPipeline::GetVertexAttributes() const
356+
{
357+
return mAttributes;
358+
}
353359
}

engine/renderers/opengl/OpenGLPipeline.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
#include <cstdint>
1919
#include <vector>
2020
#include <string_view>
21+
#include "OpenGLVertexAttribute.h"
2122
#include "aeongames/Pipeline.h"
2223

2324
namespace AeonGames
@@ -33,27 +34,19 @@ namespace AeonGames
3334
class OpenGLPipeline
3435
{
3536
public:
36-
struct Attribute
37-
{
38-
uint32_t name;
39-
uint32_t location;
40-
uint32_t size;
41-
uint32_t type;
42-
};
4337
OpenGLPipeline ( const OpenGLRenderer& aOpenGLRenderer, const Pipeline& aPipeline );
4438
OpenGLPipeline ( OpenGLPipeline&& aOpenGLPipeline );
4539
OpenGLPipeline ( const OpenGLPipeline& ) = delete;
4640
OpenGLPipeline& operator= ( const OpenGLPipeline& ) = delete;
4741
OpenGLPipeline& operator= ( OpenGLPipeline&& ) = delete;
4842
~OpenGLPipeline();
49-
uint32_t GetProgramId() const;
50-
const Attribute& GetAttribute ( uint32_t name ) const;
51-
const Attribute& GetAttribute ( std::string_view name ) const;
43+
GLint GetProgramId() const;
44+
const std::vector<OpenGLVertexAttribute>& GetVertexAttributes () const;
5245
private:
5346
const OpenGLRenderer& mOpenGLRenderer;
5447
const Pipeline* mPipeline{};
55-
uint32_t mProgramId{};
56-
std::vector<Attribute> mAttributes{};
48+
GLint mProgramId{};
49+
std::vector<OpenGLVertexAttribute> mAttributes{};
5750
};
5851
}
5952
#endif

engine/renderers/opengl/OpenGLRenderer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,16 @@ void main()
512512
it = mMeshStore.find(aMesh.GetConsecutiveId());
513513
}
514514
it->second.Bind();
515+
GLint currentProgram{0};
516+
glGetIntegerv(GL_CURRENT_PROGRAM, &currentProgram);
517+
auto pipeline = std::find_if(mPipelineStore.begin(), mPipelineStore.end(),
518+
[currentProgram](const auto& pair) {
519+
return pair.second.GetProgramId() == currentProgram;
520+
});
521+
if (pipeline != mPipelineStore.end())
522+
{
523+
it->second.EnableAttributes(pipeline->second.GetVertexAttributes());
524+
}
515525
}
516526

517527
void OpenGLRenderer::LoadPipeline(const Pipeline& aPipeline)

include/aeongames/Mesh.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2016-2021 Rodrigo Jose Hernandez Cordoba
2+
Copyright (C) 2016-2021,2025 Rodrigo Jose Hernandez Cordoba
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <memory>
2121
#include <vector>
2222
#include "aeongames/AABB.h"
23+
#include "aeongames/CRC.h"
2324
#include "aeongames/Resource.h"
2425

2526
namespace AeonGames
@@ -28,17 +29,16 @@ namespace AeonGames
2829
class Mesh final : public Resource
2930
{
3031
public:
31-
enum AttributeSemantic : uint8_t
32+
enum AttributeSemantic : uint32_t
3233
{
33-
POSITION = 0,
34-
NORMAL = 1,
35-
TANGENT = 2,
36-
BITANGENT = 3,
37-
TEXCOORD = 4,
38-
WEIGHT_INDEX = 5,
39-
WEIGHT_VALUE = 6,
40-
COLOR = 7,
41-
SEMANTIC_COUNT
34+
POSITION = "VertexPosition"_crc32,
35+
NORMAL = "VertexNormal"_crc32,
36+
TANGENT = "VertexTangent"_crc32,
37+
BITANGENT = "VertexBitangent"_crc32,
38+
TEXCOORD = "VertexUV"_crc32,
39+
WEIGHT_INDEX = "VertexWeightIndices"_crc32,
40+
WEIGHT_VALUE = "VertexWeights"_crc32,
41+
COLOR = "VertexColor"_crc32,
4242
};
4343

4444
enum AttributeType : uint8_t

proto/mesh.proto

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2016,2018,2019,2021 Rodrigo Jose Hernandez Cordoba
1+
// Copyright (C) 2016,2018,2019,2021,2025 Rodrigo Jose Hernandez Cordoba
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -21,14 +21,15 @@ message AttributeMsg
2121
{
2222
enum AttributeSemantic
2323
{
24-
POSITION = 0;
25-
NORMAL = 1;
26-
TANGENT = 2;
27-
BITANGENT = 3;
28-
TEXCOORD = 4;
29-
WEIGHT_INDEX = 5;
30-
WEIGHT_VALUE = 6;
31-
COLOR = 7;
24+
UNUSED = 0x0;
25+
POSITION = 0x3DE95988;
26+
NORMAL = 0x06843867;
27+
TANGENT = 0x508CD822;
28+
BITANGENT = -863954250; //0xCC811AB6;
29+
TEXCOORD = 0x7683CFA6;
30+
WEIGHT_INDEX = 0x48129712;
31+
WEIGHT_VALUE = 0x6E44A092;
32+
COLOR = 0x65476198;
3233
}
3334
enum AttributeType
3435
{

0 commit comments

Comments
 (0)