Removing big memory hog from renderer

This commit is contained in:
Nigel Barink 2022-12-21 19:11:27 +01:00
parent a1ec94e983
commit e7f1bd7d52
5 changed files with 142 additions and 82 deletions

View File

@ -125,25 +125,11 @@ public:
class Viewport : EditorWindow { class Viewport : EditorWindow {
public: public:
Viewport (Scene& scene) : EditorWindow("SceneView") { Viewport (Framebuffer& fb) : EditorWindow("SceneView") {
Framebuffer framebuffer = Framebuffer((int)ImGui::GetWindowWidth(),(int)ImGui::GetWindowHeight());
Renderer renderer = Renderer();
renderer.Prepare(scene);
renderer.Render(&framebuffer, scene);
ImGui::Image( ImGui::Image(
(void*)(intptr_t)framebuffer.GetColourAttachment(), (void*)(intptr_t)fb.GetColourAttachment(),
ImVec2{ (float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight() } ImVec2{ (float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight() }
); );
//ImGuizmo::SetDrawlist(); //ImGuizmo::SetDrawlist();
//ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight()); //ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());

View File

@ -14,17 +14,28 @@
#include "AssetManagement/AssetManager.h" #include "AssetManagement/AssetManager.h"
#include "UI/MainMenuBar.h" #include "UI/MainMenuBar.h"
const unsigned int MS_PER_UPDATE = 2; const unsigned int MS_PER_UPDATE = 2;
void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene); void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene);
class Editor : public Application { class Editor : public Application {
public: public:
Editor() : Application("Editor") {} Editor() : Application("Editor"){
}
void Run() override void Run() override
{ {
auto NativeEditorWindow = NativeWindow(1200, 700); auto NativeEditorWindow = NativeWindow(1200, 700);
//auto renderer = Renderer();
framebuffer = new Framebuffer(800, 600);
auto renderer = Renderer(RendererConfig{
1200, // Screen Width
700, // Screen Height
glm::vec3{0,0,0}, // Clear Color
true // Depth testing
});
auto GuiRenderer = GUIRenderer(NativeEditorWindow); auto GuiRenderer = GUIRenderer(NativeEditorWindow);
Selected = (entt::entity)-1; Selected = (entt::entity)-1;
@ -33,10 +44,11 @@ public:
ActiveScene.Start(); ActiveScene.Start();
renderer.setCurrentFrameBuffer(*framebuffer);
double previous = glfwGetTime(); double previous = glfwGetTime();
double lag = 0.0; double lag = 0.0;
while (!NativeEditorWindow.WindowShouldClose()) while (!NativeEditorWindow.WindowShouldClose())
{ {
@ -53,8 +65,13 @@ public:
lag -= MS_PER_UPDATE; lag -= MS_PER_UPDATE;
} }
// renderer.Render(framebuffer, ActiveScene); // submit DrawCommands for all render3DComponents
auto group = ActiveScene.getReg().view<TransformComponent, Render3DComponent>();
group.each([&renderer](auto enity, TransformComponent& t, Render3DComponent& renderComponent) {
renderer.Submit(renderComponent, t);
});
renderer.Render();
GuiRenderer.Begin(); GuiRenderer.Begin();
RenderGUI(); RenderGUI();
@ -66,6 +83,8 @@ public:
} }
delete framebuffer;
ActiveScene.Stop(); ActiveScene.Stop();
} }
@ -86,7 +105,7 @@ public:
} }
{ {
Viewport sceneview = Viewport(ActiveScene); Viewport sceneview = Viewport(*framebuffer);
} }
{ {
@ -108,7 +127,7 @@ public:
} }
{ {
AssetFinder assetsView = AssetFinder(); // AssetFinder assetsView = AssetFinder();
} }
@ -127,6 +146,8 @@ private:
std::unique_ptr<Project> CurrentProject; std::unique_ptr<Project> CurrentProject;
Scene ActiveScene; Scene ActiveScene;
entt::entity Selected; entt::entity Selected;
Framebuffer* framebuffer ;
}; };

View File

@ -0,0 +1,12 @@
#pragma once
#include "Scene/Components.h"
namespace YoggieEngine {
struct DrawCommand {
unsigned int VAO_identifier;
unsigned int num_elements;
unsigned int IBO_identifier;
TransformComponent transform;
Shader shader;
// Material
};
};

View File

@ -1,24 +1,34 @@
#include <YoggieEngine.h> #include <YoggieEngine.h>
#include "Renderer.h" #include "Renderer.h"
#include "../Scene/Components.h" #include "../Scene/Components.h"
#include "../Graphics/Memory/VertexArray.h"
#include "../Graphics/Memory/Buffer.h" #include "../Graphics/Memory/Buffer.h"
#include "../Graphics/Memory/VertexArray.h"
#include "../Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine { namespace YoggieEngine {
float Angle = 0.0; float Angle = 0.0;
Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f); Camera cam = Camera(glm::vec3(12.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
Renderer::Renderer(){ Renderer::Renderer(RendererConfig& config)
glEnable(GL_DEPTH_TEST); : m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight))
{
} }
Renderer::~Renderer(){} Renderer::~Renderer(){}
void Renderer::Prepare(Scene& scene ) {
auto group = scene.getReg().view<Render3DComponent>(); void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) {
group.each([](auto enity, Render3DComponent& renderComponent) {
if (renderComponent.VAO == 0 || renderComponent.IBO == 0)
{
if (renderComponent.VAO != 0)
glDeleteVertexArrays(1, &(renderComponent.VAO));
if (renderComponent.IBO != 0)
glDeleteBuffers(1, &(renderComponent.IBO));
VertexArray va = VertexArray(); VertexArray va = VertexArray();
Buffer vertexBuffer = Buffer(); Buffer vertexBuffer = Buffer();
Buffer elementBuffer = Buffer(); Buffer elementBuffer = Buffer();
@ -46,64 +56,80 @@ void Renderer::Prepare(Scene& scene ) {
renderComponent.VAO = va.getID(); renderComponent.VAO = va.getID();
renderComponent.IBO = elementBuffer.getBufferID(); renderComponent.IBO = elementBuffer.getBufferID();
}); }
DrawCommand dc = { renderComponent.VAO, renderComponent.mesh.elements.size(), renderComponent.IBO, transform, renderComponent.shader };
commands.push_back(dc);
} }
void Renderer::Render()
void Renderer::Render(Scene& scene)
{ {
auto group = scene.getReg().view<TransformComponent, Render3DComponent>(); glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId());
group.each([&](auto entity , TransformComponent& trans, Render3DComponent& renderComponent)
{
glBindVertexArray(renderComponent.VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderComponent.IBO);
renderComponent.shader.Use(); if (m_depthTest) {
glEnable(GL_DEPTH_TEST);
}
auto lights = scene.getReg().view<LightComponent>(); glClearColor(m_clearColor.r, m_clearColor.g, m_clearColor.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
std::cout << "Draw commands this frame:" << commands.size() << std::endl;
for (const DrawCommand& command : commands)
{
glBindVertexArray(command.VAO_identifier);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.IBO_identifier);
command.shader.Use();
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), command.transform.Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), command.transform.Rotation.y, glm::vec3(0.0f, 1.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), command.transform.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), command.transform.Position) * glm::scale(glm::mat4(1.0f), command.transform.Scale) * rotation;
command.shader.setUniformVec3("Color", glm::vec3(1.0f, 0.0f, 0.0f));
command.shader.setUniformMat4("M", modelMatrix);
command.shader.setUniformMat4("V", cam.GetViewMatrix());
command.shader.setUniformMat4("P", projection);
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(command.num_elements),
GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
}
/*
* Lighting pass
* auto lights = scene.getReg().view<LightComponent>();
lights.each([&](auto entity, LightComponent& light) { lights.each([&](auto entity, LightComponent& light) {
renderComponent.shader.setUniformVec3("lighting.color", light.Color); renderComponent.shader.setUniformVec3("lighting.color", light.Color);
renderComponent.shader.setUniformFloat("lighting.strength", light.Strength); renderComponent.shader.setUniformFloat("lighting.strength", light.Strength);
}); });
*
*/
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), trans.Rotation.x, glm::vec3(1.0f, 0.0f, 0.0f));
rotation *= glm::rotate(glm::mat4(1.0f), trans.Rotation.y, glm::vec3(0.0f, 1.0f, 0.0f)); commands.clear();
rotation *= glm::rotate(glm::mat4(1.0f), trans.Rotation.z, glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), trans.Position) * glm::scale(glm::mat4(1.0f), trans.Scale) *rotation;
renderComponent.shader.setUniformVec3("Color", renderComponent.color);
renderComponent.shader.setUniformMat4("M", modelMatrix);
renderComponent.shader.setUniformMat4("V", cam.GetViewMatrix());
renderComponent.shader.setUniformMat4("P", projection);
//std::cout << "Draw " << renderComponent.mesh.elements.size() << " elements" << std::endl;
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(renderComponent.mesh.elements.size()) ,
GL_UNSIGNED_INT, NULL);
glBindVertexArray(0);
});
}
void Renderer::Render(Framebuffer* framebuffer, Scene& scene)
{
if (framebuffer == nullptr)
return;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->GetId());
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Render(scene);
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void Renderer::setCurrentFrameBuffer(const Framebuffer& fb)
{
m_framebuffer = fb;
}
void Renderer::setClearColor(const glm::vec3& ClearColor)
{
m_clearColor = ClearColor;
}
} }

View File

@ -1,23 +1,38 @@
#pragma once #pragma once
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include <vector>
#include "../PerfCounter.h" #include "../PerfCounter.h"
#include "Renderable.h" #include "Renderable.h"
#include "Memory/Framebuffer.h" #include "Memory/Framebuffer.h"
#include "../Scene/Components.h" #include "../Scene/Components.h"
#include"../Scene/Scene.h" #include"../Scene/Scene.h"
#include "Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine { namespace YoggieEngine {
struct RendererConfig {
int ScreenWidth, ScreenHeight;
glm::vec3 ClearColor;
bool DepthTest;
};
class Renderer { class Renderer {
public: public:
Renderer(); Renderer(RendererConfig& config);
~Renderer(); ~Renderer();
void Prepare(Scene& scene);
void Render(Scene& scene );
void Render(Framebuffer* framebuffer, Scene& scene); void Submit(Render3DComponent& renderComponent, TransformComponent& transform); // Collects DrawCommands
void Render(); // Draw to screen (usingthe drawCalls
void setCurrentFrameBuffer(const Framebuffer& fb);
void setClearColor(const glm::vec3& ClearColor);
private:
Framebuffer m_framebuffer;
glm::vec3 m_clearColor;
bool m_depthTest;
std::vector<DrawCommand> commands;
}; };
} }