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,26 +125,12 @@ public:
class Viewport : EditorWindow {
public:
Viewport (Scene& scene) : EditorWindow("SceneView") {
Framebuffer framebuffer = Framebuffer((int)ImGui::GetWindowWidth(),(int)ImGui::GetWindowHeight());
Renderer renderer = Renderer();
renderer.Prepare(scene);
renderer.Render(&framebuffer, scene);
Viewport (Framebuffer& fb) : EditorWindow("SceneView") {
ImGui::Image(
(void*)(intptr_t)framebuffer.GetColourAttachment(),
(void*)(intptr_t)fb.GetColourAttachment(),
ImVec2{ (float)ImGui::GetWindowWidth(),(float)ImGui::GetWindowHeight() }
);
//ImGuizmo::SetDrawlist();
//ImGuizmo::SetRect(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y, ImGui::GetWindowWidth(), ImGui::GetWindowHeight());
//ImGuizmo::Enable(true);

View File

@ -14,17 +14,28 @@
#include "AssetManagement/AssetManager.h"
#include "UI/MainMenuBar.h"
const unsigned int MS_PER_UPDATE = 2;
void CreateTestProject(std::unique_ptr<Project>& project, Scene& scene);
class Editor : public Application {
public:
Editor() : Application("Editor") {}
Editor() : Application("Editor"){
}
void Run() override
{
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);
Selected = (entt::entity)-1;
@ -33,10 +44,11 @@ public:
ActiveScene.Start();
renderer.setCurrentFrameBuffer(*framebuffer);
double previous = glfwGetTime();
double lag = 0.0;
while (!NativeEditorWindow.WindowShouldClose())
{
@ -53,8 +65,13 @@ public:
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();
RenderGUI();
@ -66,6 +83,8 @@ public:
}
delete framebuffer;
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;
Scene ActiveScene;
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 "Renderer.h"
#include "../Scene/Components.h"
#include "../Graphics/Memory/VertexArray.h"
#include "../Graphics/Memory/Buffer.h"
#include "../Graphics/Memory/VertexArray.h"
#include "../Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine {
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);
glm::mat4 projection = glm::perspective(glm::radians(cam.Zoom), (800.0f / 600.0f), 0.001f, 100.0f);
Renderer::Renderer(){
glEnable(GL_DEPTH_TEST);
Renderer::Renderer(RendererConfig& config)
: m_framebuffer(Framebuffer(config.ScreenWidth, config.ScreenHeight))
{
}
Renderer::~Renderer(){}
void Renderer::Prepare(Scene& scene ) {
auto group = scene.getReg().view<Render3DComponent>();
group.each([](auto enity, Render3DComponent& renderComponent) {
void Renderer::Submit( Render3DComponent& renderComponent, TransformComponent& transform) {
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();
Buffer vertexBuffer = Buffer();
Buffer elementBuffer = Buffer();
@ -46,64 +56,80 @@ void Renderer::Prepare(Scene& scene ) {
renderComponent.VAO = va.getID();
renderComponent.IBO = elementBuffer.getBufferID();
});
}
DrawCommand dc = { renderComponent.VAO, renderComponent.mesh.elements.size(), renderComponent.IBO, transform, renderComponent.shader };
commands.push_back(dc);
}
void Renderer::Render(Scene& scene)
void Renderer::Render()
{
auto group = scene.getReg().view<TransformComponent, Render3DComponent>();
group.each([&](auto entity , TransformComponent& trans, Render3DComponent& renderComponent)
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer.GetId());
if (m_depthTest) {
glEnable(GL_DEPTH_TEST);
}
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(renderComponent.VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderComponent.IBO);
glBindVertexArray(command.VAO_identifier);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.IBO_identifier);
renderComponent.shader.Use();
command.shader.Use();
auto lights = scene.getReg().view<LightComponent>();
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) {
renderComponent.shader.setUniformVec3("lighting.color", light.Color);
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));
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);
commands.clear();
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
#include "GLFW/glfw3.h"
#include <vector>
#include "../PerfCounter.h"
#include "Renderable.h"
#include "Memory/Framebuffer.h"
#include "../Scene/Components.h"
#include"../Scene/Scene.h"
#include "Graphics/Primitives/DrawCommand.h"
namespace YoggieEngine {
struct RendererConfig {
int ScreenWidth, ScreenHeight;
glm::vec3 ClearColor;
bool DepthTest;
};
class Renderer {
public:
Renderer();
Renderer(RendererConfig& config);
~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;
};
}