Merge pull request #43 from antopilo/feature/NK-137-optimize-1

Optimized cache locality and rendering state change
This commit is contained in:
Antoine Pilote
2023-07-22 02:47:55 -04:00
committed by GitHub
6 changed files with 46 additions and 17 deletions

View File

@@ -36,15 +36,19 @@ namespace Nuake
void Flush(Shader* shader, bool depthOnly = false)
{
shader->Bind();
const uint32_t entityIdUniformLocation = shader->FindUniformLocation("u_EntityID");
const uint32_t modelMatrixUniformLocation = shader->FindUniformLocation("u_Model");
for (auto& i : m_RenderList)
{
if(!depthOnly)
if (!depthOnly)
{
i.first->Bind(shader);
}
for (auto& m : i.second)
{
shader->SetUniformMat4f("u_Model", m.transform);
shader->SetUniform1i("u_EntityID", m.entityId + 1);
shader->SetUniformMat4f(modelMatrixUniformLocation, m.transform);
shader->SetUniform1i(entityIdUniformLocation, m.entityId + 1);
m.Mesh->Draw(shader, false);
}
}

View File

@@ -49,6 +49,9 @@ namespace Nuake
mGBuffer->QueueResize(framebuffer.GetSize());
GBufferPass(scene);
mShadingBuffer->QueueResize(framebuffer.GetSize());
ShadingPass(scene);
const auto& sceneEnv = scene.GetEnvironment();
Ref<Texture> finalOutput = mShadingBuffer->GetTexture();
if (scene.GetEnvironment()->BloomEnabled)
@@ -141,9 +144,6 @@ namespace Nuake
mShadingBuffer->QueueResize(framebuffer.GetSize());
ShadingPass(scene);
RenderCommand::Enable(RendererEnum::DEPTH_TEST);
Renderer::EndDraw();
}
@@ -199,8 +199,6 @@ namespace Nuake
}
Renderer::Flush(shader, true);
}
light.m_Framebuffers[i]->Unbind();
}
}
}
@@ -249,7 +247,6 @@ namespace Nuake
}
Renderer::Flush(gBufferShader, false);
}
mGBuffer->Unbind();
}
void SceneRenderer::ShadingPass(Scene& scene)
@@ -308,7 +305,6 @@ namespace Nuake
Renderer::DrawQuad(Matrix4());
}
mShadingBuffer->Unbind();
}
void SceneRenderer::PostProcessPass(const Scene& scene)

View File

@@ -190,7 +190,7 @@ namespace Nuake
int addr = glGetUniformLocation(ProgramId, uniform.c_str());
if (addr == -1)
return addr;//std::cout << "Warning: uniform '" << uniform << "' doesn't exists!" << std::endl;
return addr;
else
UniformCache[uniform] = addr;
@@ -250,7 +250,12 @@ namespace Nuake
//ASSERT(addr != -1);
if (addr != -1)
glUniform1i(addr, v0);
SetUniform1i(addr, v0);
}
void Shader::SetUniform1i(uint32_t location, int v0)
{
glUniform1i(location, v0);
}
void Shader::SetUniform1iv(const std::string& name, int size, int* value)
@@ -277,12 +282,19 @@ namespace Nuake
glUniformMatrix3fv(addr, 1, GL_FALSE, &mat[0][0]);
}
void Shader::SetUniformMat4f(const std::string& name, Matrix4 mat)
void Shader::SetUniformMat4f(uint32_t location, const Matrix4& mat)
{
glUniformMatrix4fv(location, 1, GL_FALSE, &mat[0][0]);
}
void Shader::SetUniformMat4f(const std::string& name, const Matrix4& mat)
{
int addr = FindUniformLocation(name);
if (addr != -1)
glUniformMatrix4fv(addr, 1, GL_FALSE, &mat[0][0]);
{
SetUniformMat4f(addr, std::move(mat));
}
}
void Shader::SetUniform1f(const std::string& name, float v0)

View File

@@ -42,16 +42,20 @@ namespace Nuake
void SetUniform1b(const std::string& name, bool v0);
void SetUniformTex(const std::string& name, Texture* texture, unsigned int slot = 0);
void SetUniform1i(const std::string& name, int v0);
void SetUniform1i(uint32_t location, int v0);
void SetUniform1iv(const std::string& name, int size, int* value);
void SetUniform1fv(const std::string& name, int size, float* value);
void SetUniformMat3f(const std::string& name, Matrix3 mat);
void SetUniformMat4f(const std::string& name, Matrix4 mat);
void SetUniformMat4f(uint32_t name, const Matrix4& mat);
void SetUniformMat4f(const std::string& name, const Matrix4& mat);
int FindUniformLocation(std::string uniform);
private:
ShaderSource ParseShader(const std::string& filePath);
unsigned int CreateProgram(ShaderSource source);
unsigned int Compile(unsigned int type, ShaderSource source);
int FindUniformLocation(std::string uniform);
};
}

View File

@@ -75,16 +75,25 @@ namespace Nuake {
Entity Scene::GetEntityByID(int id)
{
if (_EntitiesIDMap.find(id) != _EntitiesIDMap.end())
{
return _EntitiesIDMap[id];
}
auto idView = m_Registry.view<NameComponent>();
for (auto e : idView)
{
NameComponent& nameC = idView.get<NameComponent>(e);
if (nameC.ID == id)
{
return Entity{ e, this };
auto newEntity = Entity{ e, this };
_EntitiesIDMap[id] = newEntity;
return newEntity;
}
}
Logger::Log("Entity not found with id: " + std::to_string(id), "scene", CRITICAL);
assert("Entity not found");
}
@@ -242,6 +251,8 @@ namespace Nuake {
nameComponent.Name = entityName;
nameComponent.ID = id;
_EntitiesIDMap[id] = entity;
Logger::Log("Entity created with name: " + nameComponent.Name, "scene", LOG_TYPE::VERBOSE);
return entity;
}

View File

@@ -37,9 +37,11 @@ namespace Nuake
public:
Ref<EditorCamera> m_EditorCamera;
entt::registry m_Registry;
std::map<uint32_t, Entity> _EntitiesIDMap;
std::string Path = "";
SceneRenderer* mSceneRenderer;
static Ref<Scene> New();
Scene();
~Scene();