Skip to content
Snippets Groups Projects
Commit 00001729 authored by Abdulhadi Al-Sayed's avatar Abdulhadi Al-Sayed
Browse files

Added tools for memory testing, fixed current memory leak issue, and increased...

Added tools for memory testing, fixed current memory leak issue, and increased performance to about 100fps in release build
parent 6a7266d4
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
extern engine::u_Ptr<engine::AppFrame> engine::createApp(); extern engine::u_Ptr<engine::AppFrame> engine::createApp();
int main(int argc, char** argv) { int main(int argc, char** argv) {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); //_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
//Intro //Intro
std::cout << "Engine is running ...\n"; std::cout << "Engine is running ...\n";
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include "texture.h" #include "texture.h"
#include <tiny_obj_loader.h> #include <tiny_obj_loader.h>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
namespace engine { namespace engine {
...@@ -39,6 +41,11 @@ namespace engine { ...@@ -39,6 +41,11 @@ namespace engine {
glm::vec4 color; glm::vec4 color;
glm::vec2 texCoord; glm::vec2 texCoord;
float texID; float texID;
// Override == operator with custom for vertex optimization
bool operator==(const PolyVertex& other) const {
return position == other.position && normal == other.normal && color == other.color && texCoord == other.texCoord && texID == other.texID;
}
}; };
class Renderer { class Renderer {
...@@ -70,6 +77,7 @@ namespace engine { ...@@ -70,6 +77,7 @@ namespace engine {
int texID); int texID);
static GLuint compileModel(std::vector<PolyVertex>& vertices); static GLuint compileModel(std::vector<PolyVertex>& vertices);
static void cleanVAO(GLuint& vao);
/* /*
Following param descriptions: Following param descriptions:
...@@ -112,3 +120,15 @@ namespace engine { ...@@ -112,3 +120,15 @@ namespace engine {
}; };
} }
// In std namespace define a template specialization for PolyVertex comparison to exist
namespace std {
template<> struct hash<engine::PolyVertex> {
size_t operator()(engine::PolyVertex const& vertex) const {
return ((hash<glm::vec3>()(vertex.position) ^ (hash<glm::vec3>()(vertex.normal) << 1)) >> 1) ^
(hash<glm::vec4>()(vertex.color) << 1) ^
(hash<glm::vec2>()(vertex.texCoord) << 1) ^
(hash<float>()(vertex.texID) << 1);
}
};
}
\ No newline at end of file
#include "engine/include/graphics/renderer.h" #include "engine/include/graphics/renderer.h"
#include <set>
namespace engine { namespace engine {
...@@ -8,6 +9,9 @@ namespace engine { ...@@ -8,6 +9,9 @@ namespace engine {
const uint32_t MAXPOLYVERTICES = MAXPOLYGONS * 3; // Maximum polygon count vertices const uint32_t MAXPOLYVERTICES = MAXPOLYGONS * 3; // Maximum polygon count vertices
const uint32_t MAXPOLYINDICES = MAXPOLYGONS * 3; // Maximum polygon count indices const uint32_t MAXPOLYINDICES = MAXPOLYGONS * 3; // Maximum polygon count indices
GLuint VAO;
GLuint VBO;
std::map<std::string, std::vector<PolyVertex>> verticesMap; std::map<std::string, std::vector<PolyVertex>> verticesMap;
std::map<std::string, std::vector<unsigned int>> indicesMap; std::map<std::string, std::vector<unsigned int>> indicesMap;
std::vector<PolyVertex> vertices; // Stored vertices for batch rendering std::vector<PolyVertex> vertices; // Stored vertices for batch rendering
...@@ -38,8 +42,8 @@ namespace engine { ...@@ -38,8 +42,8 @@ namespace engine {
s_Ptr<Texture> whiteTexture; // Generating textures/ flat colors s_Ptr<Texture> whiteTexture; // Generating textures/ flat colors
uint32_t quadIndexCount = 0; // Current index count uint32_t quadIndexCount = 0; // Current index count
QuadVertex* quadVertexBufferStore = nullptr; s_Ptr<std::vector<QuadVertex>> quadVertexBufferStore;
QuadVertex* quadVertexBufferPtr = nullptr; s_Ptr<std::vector<QuadVertex>> quadVertexBufferPtr;
glm::vec4 quadVertexPositions[4]; // For applying vertex positions on a loop glm::vec4 quadVertexPositions[4]; // For applying vertex positions on a loop
// TEXTURE SLOTS // TEXTURE SLOTS
...@@ -73,8 +77,8 @@ namespace engine { ...@@ -73,8 +77,8 @@ namespace engine {
DATA DEFINITION FOR QUAD DRAWING DATA DEFINITION FOR QUAD DRAWING
*/ */
s_Data.quadVertexArray = m_SPtr<VertexArray>(); s_Data.quadVertexArray = m_SPtr<VertexArray>();
s_Data.quadVertexBufferStore = NEW QuadVertex[s_Data.MAXVERTICES]; //s_Data.quadVertexBufferStore = NEW QuadVertex[s_Data.MAXVERTICES];
s_Data.quadVertexBufferStore = m_SPtr<std::vector<QuadVertex>>(s_Data.MAXVERTICES);
// Vertex default positioning // Vertex default positioning
s_Data.quadVertexPositions[0] = { -0.5f, -0.5f, 0.0f, 1.0f }; s_Data.quadVertexPositions[0] = { -0.5f, -0.5f, 0.0f, 1.0f };
s_Data.quadVertexPositions[1] = { 0.5f, -0.5f, 0.0f, 1.0f }; s_Data.quadVertexPositions[1] = { 0.5f, -0.5f, 0.0f, 1.0f };
...@@ -268,6 +272,9 @@ namespace engine { ...@@ -268,6 +272,9 @@ namespace engine {
s_3DData.polyIndexBuffer.reset(); s_3DData.polyIndexBuffer.reset();
s_3DData.polyVertexArray.reset(); s_3DData.polyVertexArray.reset();
// clean VAO
cleanVAO(s_3DData.VAO);
// RESET quad ptrs // RESET quad ptrs
s_Data.quadIndexCount = 0; s_Data.quadIndexCount = 0;
s_Data.quadVertexBufferPtr = s_Data.quadVertexBufferStore; s_Data.quadVertexBufferPtr = s_Data.quadVertexBufferStore;
...@@ -343,6 +350,7 @@ namespace engine { ...@@ -343,6 +350,7 @@ namespace engine {
glm::translate(glm::mat4(1.0f), position) * glm::translate(glm::mat4(1.0f), position) *
glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
/*
// Iterate and set attributes of quad in vertex buffer // Iterate and set attributes of quad in vertex buffer
for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++) for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++)
{ {
...@@ -353,6 +361,19 @@ namespace engine { ...@@ -353,6 +361,19 @@ namespace engine {
s_Data.quadVertexBufferPtr->tileCount = tileCount; s_Data.quadVertexBufferPtr->tileCount = tileCount;
s_Data.quadVertexBufferPtr++; s_Data.quadVertexBufferPtr++;
} }
*/
int idx = 0;
// Iterate and set attributes of quad in vertex buffer
for (auto it = s_Data.quadVertexBufferPtr->begin(); idx < s_Data.QUADVERTEXCOUNT; it++) {
it->position = transform * s_Data.quadVertexPositions[idx];
it->color = color;
it->texCoord = s_Data.textureCoordMapping[idx];
it->texID = texID;
it->tileCount = tileCount;
idx++;
}
s_Data.quadIndexCount += 6; // Increment index count for potential next buffer s_Data.quadIndexCount += 6; // Increment index count for potential next buffer
} }
...@@ -398,6 +419,7 @@ namespace engine { ...@@ -398,6 +419,7 @@ namespace engine {
glm::translate(glm::mat4(1.0f), position) * glm::translate(glm::mat4(1.0f), position) *
glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
/*
// Iterate and set attributes of quad in vertex buffer // Iterate and set attributes of quad in vertex buffer
for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++) for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++)
{ {
...@@ -408,7 +430,19 @@ namespace engine { ...@@ -408,7 +430,19 @@ namespace engine {
s_Data.quadVertexBufferPtr->tileCount = tileCount; s_Data.quadVertexBufferPtr->tileCount = tileCount;
s_Data.quadVertexBufferPtr++; s_Data.quadVertexBufferPtr++;
} }
*/
int idx = 0;
// Iterate and set attributes of quad in vertex buffer
for (auto it = s_Data.quadVertexBufferPtr->begin(); idx < s_Data.QUADVERTEXCOUNT; it++) {
it->position = transform * s_Data.quadVertexPositions[idx];
it->color = s_Data.DEFAULTCOLOR;
it->texCoord = s_Data.textureCoordMapping[idx];
it->texID = texID;
it->tileCount = tileCount;
idx++;
}
s_Data.quadIndexCount += 6; // Increment index count for potential next buffer s_Data.quadIndexCount += 6; // Increment index count for potential next buffer
} }
...@@ -440,6 +474,7 @@ namespace engine { ...@@ -440,6 +474,7 @@ namespace engine {
glm::rotate(glm::mat4(1.0f), glm::radians(rotation), { 0.0f, 0.0f, 1.0f }) * glm::rotate(glm::mat4(1.0f), glm::radians(rotation), { 0.0f, 0.0f, 1.0f }) *
glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
/*
// Iterate and set attributes of quad in vertex buffer // Iterate and set attributes of quad in vertex buffer
for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++) for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++)
{ {
...@@ -450,6 +485,19 @@ namespace engine { ...@@ -450,6 +485,19 @@ namespace engine {
s_Data.quadVertexBufferPtr->tileCount = tileCount; s_Data.quadVertexBufferPtr->tileCount = tileCount;
s_Data.quadVertexBufferPtr++; s_Data.quadVertexBufferPtr++;
} }
*/
int idx = 0;
// Iterate and set attributes of quad in vertex buffer
for (auto it = s_Data.quadVertexBufferPtr->begin(); idx < s_Data.QUADVERTEXCOUNT; it++) {
it->position = transform * s_Data.quadVertexPositions[idx];
it->color = color;
it->texCoord = s_Data.textureCoordMapping[idx];
it->texID = texID;
it->tileCount = tileCount;
idx++;
}
s_Data.quadIndexCount += 6; // Increment index count for potential next buffer s_Data.quadIndexCount += 6; // Increment index count for potential next buffer
} }
...@@ -497,6 +545,7 @@ namespace engine { ...@@ -497,6 +545,7 @@ namespace engine {
glm::rotate(glm::mat4(1.0f), glm::radians(rotation), { 0.0f, 0.0f, 1.0f }) * glm::rotate(glm::mat4(1.0f), glm::radians(rotation), { 0.0f, 0.0f, 1.0f }) *
glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f });
/*
// Iterate and set attributes of quad in vertex buffer // Iterate and set attributes of quad in vertex buffer
for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++) for (uint32_t i = 0; i < s_Data.QUADVERTEXCOUNT; i++)
{ {
...@@ -507,6 +556,19 @@ namespace engine { ...@@ -507,6 +556,19 @@ namespace engine {
s_Data.quadVertexBufferPtr->tileCount = tileCount; s_Data.quadVertexBufferPtr->tileCount = tileCount;
s_Data.quadVertexBufferPtr++; s_Data.quadVertexBufferPtr++;
} }
*/
int idx = 0;
// Iterate and set attributes of quad in vertex buffer
for (auto it = s_Data.quadVertexBufferPtr->begin(); idx < s_Data.QUADVERTEXCOUNT; it++) {
it->position = transform * s_Data.quadVertexPositions[idx];
it->color = s_Data.DEFAULTCOLOR;
it->texCoord = s_Data.textureCoordMapping[idx];
it->texID = texID;
it->tileCount = tileCount;
idx++;
}
s_Data.quadIndexCount += 6; // Increment index count for potential next buffer s_Data.quadIndexCount += 6; // Increment index count for potential next buffer
} }
...@@ -664,7 +726,7 @@ namespace engine { ...@@ -664,7 +726,7 @@ namespace engine {
glm::vec4 color, glm::vec4 color,
int texID) { int texID) {
RawShape s = s_ObjectLibrary->get(name); RawShape s (s_ObjectLibrary->get(name)); // Copy loaded object from library
//For each shape defined in the obj file //For each shape defined in the obj file
for (auto shape : s.shapes) { for (auto shape : s.shapes) {
...@@ -672,18 +734,18 @@ namespace engine { ...@@ -672,18 +734,18 @@ namespace engine {
for (auto meshIndex : shape.mesh.indices) { for (auto meshIndex : shape.mesh.indices) {
//And store the data for each vertice, including normals //And store the data for each vertice, including normals
glm::vec3 vertice = { glm::vec3 vertice = {
s.attrib.vertices[meshIndex.vertex_index * 3] + position.x, s.attrib.vertices[(double)meshIndex.vertex_index * 3] + position.x,
s.attrib.vertices[(meshIndex.vertex_index * 3) + 1] + position.y, s.attrib.vertices[((double)meshIndex.vertex_index * 3) + 1] + position.y,
s.attrib.vertices[(meshIndex.vertex_index * 3) + 2] + position.z s.attrib.vertices[((double)meshIndex.vertex_index * 3) + 2] + position.z
}; };
glm::vec3 normal = { glm::vec3 normal = {
s.attrib.normals[meshIndex.normal_index * 3], s.attrib.normals[(double)meshIndex.normal_index * 3],
s.attrib.normals[(meshIndex.normal_index * 3) + 1], s.attrib.normals[((double)meshIndex.normal_index * 3) + 1],
s.attrib.normals[(meshIndex.normal_index * 3) + 2] s.attrib.normals[((double)meshIndex.normal_index * 3) + 2]
}; };
glm::vec2 textureCoordinate = { //These go unnused, but if you want textures, you will need them. glm::vec2 textureCoordinate = { //These go unnused, but if you want textures, you will need them.
s.attrib.texcoords[meshIndex.texcoord_index * 2], s.attrib.texcoords[(double)meshIndex.texcoord_index * 2],
s.attrib.texcoords[(meshIndex.texcoord_index * 2) + 1] s.attrib.texcoords[((double)meshIndex.texcoord_index * 2) + 1]
}; };
PolyVertex vertex = PolyVertex(); PolyVertex vertex = PolyVertex();
...@@ -696,19 +758,18 @@ namespace engine { ...@@ -696,19 +758,18 @@ namespace engine {
vertices.push_back(vertex); //We add our new vertice struct to our vector vertices.push_back(vertex); //We add our new vertice struct to our vector
} }
} }
} }
GLuint Renderer::compileModel(std::vector<PolyVertex>& vertices) { GLuint Renderer::compileModel(std::vector<PolyVertex>& vertices) {
GLuint VAO; //GLuint VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &s_3DData.VAO);
glBindVertexArray(VAO); glBindVertexArray(s_3DData.VAO);
//GLuint VBO;
glGenBuffers(1, &s_3DData.VBO);
glBindBuffer(GL_ARRAY_BUFFER, s_3DData.VBO);
GLuint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//As you can see, OpenGL will accept a vector of structs as a valid input here
glBufferData(GL_ARRAY_BUFFER, sizeof(PolyVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(PolyVertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
...@@ -729,7 +790,38 @@ namespace engine { ...@@ -729,7 +790,38 @@ namespace engine {
//This will be needed later to specify how much we need to draw. Look at the main loop to find this variable again. //This will be needed later to specify how much we need to draw. Look at the main loop to find this variable again.
s_3DData.vertexCount = vertices.size(); s_3DData.vertexCount = vertices.size();
return VAO; return s_3DData.VAO;
} }
void Renderer::cleanVAO(GLuint& vao)
{
GLint nAttr = 0;
std::set<GLuint> vbos;
GLint eboId;
glGetVertexArrayiv(vao, GL_ELEMENT_ARRAY_BUFFER_BINDING, &eboId);
glDeleteBuffers(1, (GLuint*)&eboId);
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nAttr);
glBindVertexArray(vao);
for (int iAttr = 0; iAttr < nAttr; ++iAttr)
{
GLint vboId = 0;
glGetVertexAttribiv(iAttr, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &vboId);
if (vboId > 0)
{
vbos.insert(vboId);
}
glDisableVertexAttribArray(iAttr);
}
for (auto vbo : vbos)
{
glDeleteBuffers(1, &vbo);
}
glDeleteVertexArrays(1, &vao);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment