Skip to content

Commit

Permalink
Optimization: Only a position vector gets send for every cell instanc…
Browse files Browse the repository at this point in the history
…e (vec3 instead of mat4)
  • Loading branch information
Zang3th committed Oct 21, 2023
1 parent 123eb1a commit 1143c72
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Apps/CellSim/CellSimApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace CS

//Create cell manager and add cell renderer
_cellManager = Engine::MakeScope<Engine::CellManager>();
_cellManager->AddCellRenderer(1.0f, "CellShader", glm::vec3(482.0f, 2.0f, 482.0f));
_cellManager->AddCellRenderer("CellShader", glm::vec3(482.0f, 2.0f, 482.0f));

//Create UI
_interface = Engine::MakeScope<CellSimInterface>();
Expand Down
4 changes: 2 additions & 2 deletions Engine/Rendering/Renderer/CellManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace Engine
_cellSpawnerStorage.reserve(5);
}

void CellManager::AddCellRenderer(float cellSize, const std::string& shader, const glm::vec3& worldSpawnPos)
void CellManager::AddCellRenderer(const std::string& shader, const glm::vec3& worldSpawnPos)
{
if(RenderManager::GetInitStatus())
{
_cellRenderer = Engine::RenderManager::AddCellRenderer(cellSize, shader, worldSpawnPos);
_cellRenderer = Engine::RenderManager::AddCellRenderer(shader, worldSpawnPos);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderer/CellManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Engine

public:
CellManager();
void AddCellRenderer(float cellSize, const std::string& shader, const glm::vec3& worldSpawnPos);
void AddCellRenderer(const std::string& shader, const glm::vec3& worldSpawnPos);
void AddCell(const CellParams& cellParams);
void AddCellSpawner(const CellParams& cellParams);
void DeleteCells();
Expand Down
32 changes: 12 additions & 20 deletions Engine/Rendering/Renderer/CellRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Engine
{
// ----- Private -----

CellRenderer::CellRenderer(float cellSize, Shader* shader, const glm::vec3& worldSpawnPos)
: _cellSize(cellSize), _verticeCount(36), _shader(shader), _worldSpawnPos(worldSpawnPos), _modelViewStorage{glm::mat4(0.0f)}
CellRenderer::CellRenderer(Shader* shader, const glm::vec3& worldSpawnPos)
: _verticeCount(36), _shader(shader), _worldSpawnPos(worldSpawnPos), _positionStorage{glm::vec3(0.0f)}
{
Logger::Info("Created", "Renderer",__func__);
InitGpuStorage();
Expand All @@ -21,29 +21,23 @@ namespace Engine
_vboVert = MakeScope<VertexBuffer>(CUBE_VERTICES, sizeof(CUBE_VERTICES), GL_STATIC_DRAW);
_vao->DefineAttributes(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);

_vboModel = MakeScope<VertexBuffer>(&_modelViewStorage[0], _modelViewStorage.size() * 16 * sizeof(float), GL_DYNAMIC_DRAW);
_vao->DefineAttributes(1, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(float), nullptr);
_vao->DefineAttributes(2, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(float), (void*)(4 * sizeof(float)));
_vao->DefineAttributes(3, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(float), (void*)(8 * sizeof(float)));
_vao->DefineAttributes(4, 4, GL_FLOAT, GL_FALSE, 16 * sizeof(float), (void*)(12 * sizeof(float)));
_vboPos = MakeScope<VertexBuffer>(&_positionStorage[0], _positionStorage.size() * 3 * sizeof(float), GL_DYNAMIC_DRAW);
_vao->DefineAttributes(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);

//Set attribute divisors for per instance data
_vao->AttributeDivisor(1, 1);
_vao->AttributeDivisor(2, 1);
_vao->AttributeDivisor(3, 1);
_vao->AttributeDivisor(4, 1);

//Unbind everything
_vao->Unbind();
_vboVert->Unbind();
_vboModel->Unbind();
_vboPos->Unbind();
}

void CellRenderer::UpdateGpuStorage()
{
_vboModel->Bind();
_vboModel->Update(&_modelViewStorage[0], _modelViewStorage.size() * 16 * sizeof(float));
_vboModel->Unbind();
_vboPos->Bind();
_vboPos->Update(&_positionStorage[0], _positionStorage.size() * 3 * sizeof(float));
_vboPos->Unbind();
}

// ----- Public -----
Expand All @@ -60,9 +54,10 @@ namespace Engine
//Bind vao and vbo's
_vao->Bind();
_vboVert->Bind();
_vboModel->Bind();
_vboPos->Bind();

//Set uniforms
_shader->SetUniformMat4f("model", glm::mat4(1.0f));
_shader->SetUniformMat4f("view", Camera3D::GetViewMatrix());
_shader->SetUniformMat4f("projection", ((SceneRenderer*)sceneRenderer)->GetProjMatrix());

Expand All @@ -73,7 +68,7 @@ namespace Engine
GLCall(glDrawArraysInstanced(GL_TRIANGLES, 0, _verticeCount, cellCount))

//Unbind vao and vbo's
_vboModel->Unbind();
_vboPos->Unbind();
_vboVert->Unbind();
_vao->Unbind();

Expand All @@ -89,9 +84,6 @@ namespace Engine

void CellRenderer::UpdateModelViewStorage(uint32 index, const glm::u32vec3& cellPos)
{
glm::mat4 model(1.0f);
model = glm::translate(model, _worldSpawnPos + glm::vec3(cellPos));
model = glm::scale(model, glm::vec3(_cellSize));
_modelViewStorage.at(index) = model;
_positionStorage.at(index) = _worldSpawnPos + glm::vec3(cellPos);
}
}
7 changes: 3 additions & 4 deletions Engine/Rendering/Renderer/CellRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ namespace Engine

private:
Scope<VertexArray> _vao;
Scope<VertexBuffer> _vboVert, _vboModel;
Scope<VertexBuffer> _vboVert, _vboPos;

float _cellSize;
uint32 _verticeCount;
Shader* _shader;
glm::vec3 _worldSpawnPos;

std::array<glm::mat4, CellSimParams::MAX_CELL_AMOUNT> _modelViewStorage;
std::array<glm::vec3, CellSimParams::MAX_CELL_AMOUNT> _positionStorage;

CellRenderer(float cellSize, Shader* shader, const glm::vec3& worldSpawnPos);
CellRenderer(Shader* shader, const glm::vec3& worldSpawnPos);
~CellRenderer() final = default;

void InitGpuStorage();
Expand Down
4 changes: 2 additions & 2 deletions Engine/Rendering/Renderer/RenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ namespace Engine
return _particleRenderer;
}

CellRenderer* RenderManager::AddCellRenderer(float cellSize, const std::string& shader, const glm::vec3& worldSpawnPos)
CellRenderer* RenderManager::AddCellRenderer(const std::string& shader, const glm::vec3& worldSpawnPos)
{
_cellRenderer = new CellRenderer(cellSize, ResourceManager::GetShader(shader), worldSpawnPos);
_cellRenderer = new CellRenderer(ResourceManager::GetShader(shader), worldSpawnPos);
_rendererStorage.push_back(_cellRenderer);

return _cellRenderer;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Rendering/Renderer/RenderManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Engine
uint32 count, float size, float speed, float gravityCompliance, float lifeLength,
float respawnThreshold, const std::string& textureAtlas, const std::string& shader, const glm::vec3& position
);
static CellRenderer* AddCellRenderer(float cellSize, const std::string& shader, const glm::vec3& worldSpawnPos);
static CellRenderer* AddCellRenderer(const std::string& shader, const glm::vec3& worldSpawnPos);

static void RenderScene();
static void RenderShadows();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Nature scene with water rendering, normal mapped objects, and a particle system

Cellular automata system with 3D cells/cubes. The plan is to simulate and render >1M cells/cubes at a good framerate. The system should include physics (multithreaded) and pretty graphics (lighting, shadows).

![CellSim](Res/Screenshots/CellSim/Screenshot_CS_008.png)
![CellSim](Res/Screenshots/CellSim/Screenshot_CS_009.png)

## Building and compiling

Expand Down
Binary file added Res/Screenshots/CellSim/Screenshot_CS_009.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions Res/Shader/CellSim/Cell_VS.glsl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#version 450 core

layout(location = 0) in vec3 vertexIn;
layout(location = 1) in mat4 modelIn;
layout(location = 1) in vec3 posIn;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
gl_Position = projection * view * modelIn * vec4(vertexIn, 1.0f);
gl_Position = projection * view * model * vec4(vertexIn + posIn, 1.0f);
}

0 comments on commit 1143c72

Please sign in to comment.