Skip to content

Commit

Permalink
Replace RendererDefaults with Resources
Browse files Browse the repository at this point in the history
  • Loading branch information
amasson42 committed Nov 12, 2024
1 parent fa05dac commit dd22faa
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 196 deletions.
7 changes: 3 additions & 4 deletions Engine/Render/include/Render/OpenGL/OpenGLRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WorldNode;

namespace Stone::Render::OpenGL {

class RendererDefaults;
class OpenGLResources;

class OpenGLRenderer : public Renderer {
public:
Expand All @@ -24,17 +24,16 @@ class OpenGLRenderer : public Renderer {
/** Renderer */

void updateDataForWorld(const std::shared_ptr<Scene::WorldNode> &world) override;
const Scene::RendererDefaults &getRendererDefaults() const override;
void renderWorld(const std::shared_ptr<Scene::WorldNode> &world) override;

void updateFrameSize(std::pair<uint32_t, uint32_t> size) override;

void initialize();
const RendererDefaults &getOpenGLRendererDefaults() const;
const OpenGLResources &getOpenGLResources() const;

private:
std::pair<uint32_t, uint32_t> _frameSize;
std::unique_ptr<RendererDefaults> _defaults;
std::unique_ptr<OpenGLResources> _resources;
};

} // namespace Stone::Render::OpenGL
2 changes: 0 additions & 2 deletions Engine/Render/include/Render/Vulkan/VulkanRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class VulkanRenderer : public Renderer {
/** Renderer */

void updateDataForWorld(const std::shared_ptr<Scene::WorldNode> &world) override;
const Scene::RendererDefaults &getRendererDefaults() const override;
void renderWorld(const std::shared_ptr<Scene::WorldNode> &world) override;

void updateFrameSize(std::pair<uint32_t, uint32_t> size) override;
Expand All @@ -50,7 +49,6 @@ class VulkanRenderer : public Renderer {
std::shared_ptr<RenderPass> _renderPass;
std::shared_ptr<FramesRenderer> _framesRenderer;
std::shared_ptr<SwapChain> _swapChain;
std::unique_ptr<Scene::RendererDefaults> _rendererDefaults;
};

} // namespace Stone::Render::Vulkan
15 changes: 5 additions & 10 deletions Engine/Render/src/Render/OpenGL/OpenGLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

#include "Render/OpenGL/OpenGLRenderer.hpp"

#include "OpenGLResources.hpp"
#include "RenderContext.hpp"
#include "RendererDefaults.hpp"
#include "RendererObjectManager.hpp"
#include "Scene/Node/WorldNode.hpp"
#include "Scene/Renderer/RendererDefaults.hpp"

#include <GL/glew.h>

Expand All @@ -26,7 +25,7 @@ static void initializeOpenGL() {
}

OpenGLRenderer::OpenGLRenderer(RendererSettings &settings)
: Renderer(), _frameSize(settings.frame_size), _defaults(nullptr) {
: Renderer(), _frameSize(settings.frame_size), _resources(nullptr) {
}

OpenGLRenderer::~OpenGLRenderer() {
Expand All @@ -43,10 +42,6 @@ void OpenGLRenderer::updateDataForWorld(const std::shared_ptr<Scene::WorldNode>
});
}

const Scene::RendererDefaults &OpenGLRenderer::getRendererDefaults() const {
return *_defaults;
}

void OpenGLRenderer::renderWorld(const std::shared_ptr<Scene::WorldNode> &world) {
glViewport(0, 0, static_cast<GLsizei>(_frameSize.first), static_cast<GLsizei>(_frameSize.second));
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Expand All @@ -65,11 +60,11 @@ void OpenGLRenderer::initialize() {
initializeOpenGL();
std::cout << "OpenGLRenderer created" << std::endl;
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
_defaults = std::make_unique<RendererDefaults>(std::static_pointer_cast<OpenGLRenderer>(shared_from_this()));
_resources = std::make_unique<OpenGLResources>(std::static_pointer_cast<OpenGLRenderer>(shared_from_this()));
}

const RendererDefaults &OpenGLRenderer::getOpenGLRendererDefaults() const {
return *_defaults;
const OpenGLResources &OpenGLRenderer::getOpenGLResources() const {
return *_resources;
}


Expand Down
12 changes: 12 additions & 0 deletions Engine/Render/src/Render/OpenGL/OpenGLResources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2024 Stone-Engine

#include "Render/OpenGL/OpenGLRenderer.hpp"
#include "Render/OpenGL/OpenGLResources.hpp"

namespace Stone::Render::OpenGL {

OpenGLResources::OpenGLResources(const std::shared_ptr<OpenGLRenderer> &renderer) : _renderer(renderer) {
}


} // namespace Stone::Render::OpenGL
23 changes: 23 additions & 0 deletions Engine/Render/src/Render/OpenGL/OpenGLResources.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 Stone-Engine

#pragma once

#include <memory>

namespace Stone::Render::OpenGL {

class OpenGLRenderer;

class OpenGLResources {
public:
OpenGLResources() = default;

OpenGLResources(const std::shared_ptr<OpenGLRenderer> &renderer);

virtual ~OpenGLResources() = default;

private:
std::weak_ptr<OpenGLRenderer> _renderer;
};

} // namespace Stone::Render::OpenGL
74 changes: 1 addition & 73 deletions Engine/Render/src/Render/OpenGL/Renderable/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#pragma once

#include "../RendererDefaults.hpp"
#include "Render/OpenGL/OpenGLRenderer.hpp"
#include "Scene/Renderable/Material.hpp"

Expand All @@ -28,78 +27,7 @@ class Material : public Scene::IRendererObject {
}

void render(Scene::RenderContext &context) override {
auto getLoc = [this](const char *name) -> GLint {
GLint loc = glGetUniformLocation(_lastUsedProgram, name);
if (loc == -1) {
throw std::runtime_error("Failed to get location of uniform: " + std::string(name));
}
return loc;
};

glUniformMatrix4fv(getLoc("u_mat_projection"), 1, GL_FALSE, glm::value_ptr(context.mvp.projMatrix));
glUniformMatrix4fv(getLoc("u_mat_view"), 1, GL_FALSE, glm::value_ptr(context.mvp.viewMatrix));
glUniformMatrix4fv(getLoc("u_mat_model"), 1, GL_FALSE, glm::value_ptr(context.mvp.modelMatrix));
}

void makeMeshProgram() {
if (_gl_meshProgram != 0)
return;

if (_renderer.expired())
return;

auto renderer = _renderer.lock();

auto vertexShader = renderer->getOpenGLRendererDefaults().getMeshVertexShader();

assert(vertexShader != nullptr);

// std::shared_ptr<Scene::FragmentShader> fragmentShader =
// _material.getFragmentShader() ? _material.getFragmentShader() :
// renderer->getOpenGLRendererDefaults().getFragmentShader();

// assert(fragmentShader != nullptr);

auto glFragmentShader = renderer->getOpenGLRendererDefaults().getFragmentShader();
// fragmentShader->getRendererObject<FragmentShader>();

_gl_meshProgram = glCreateProgram();
glAttachShader(_gl_meshProgram, vertexShader->getGLShader());
glAttachShader(_gl_meshProgram, glFragmentShader->getGLShader());
glLinkProgram(_gl_meshProgram);

GLint success;
glGetProgramiv(_gl_meshProgram, GL_LINK_STATUS, &success);
if (!success) {
GLchar infoLog[1024];
glGetProgramInfoLog(_gl_meshProgram, 1024, nullptr, infoLog);
throw std::runtime_error("Failed to link program: " + std::string(infoLog));
}
}

void makeSkinMeshProgram() {
if (_gl_skinMeshProgram != 0)
return;
}

void makeInstancedMeshProgram() {
if (_gl_instancedMeshProgram != 0)
return;
}

void useMeshProgram() {
glUseProgram(_gl_meshProgram);
_lastUsedProgram = _gl_meshProgram;
}

void useSkinMeshProgram() {
glUseProgram(_gl_skinMeshProgram);
_lastUsedProgram = _gl_skinMeshProgram;
}

void useInstancedMeshProgram() {
glUseProgram(_gl_instancedMeshProgram);
_lastUsedProgram = _gl_instancedMeshProgram;
(void)context;
}

private:
Expand Down
5 changes: 1 addition & 4 deletions Engine/Render/src/Render/OpenGL/Renderable/MeshNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#pragma once

#include "../OpenGLResources.hpp"
#include "Material.hpp"
#include "Mesh.hpp"
#include "Render/OpenGL/OpenGLRenderer.hpp"
#include "Scene/Node/MeshNode.hpp"
#include "Scene/Renderable/Mesh.hpp"
#include "Scene/Renderer/RendererDefaults.hpp"

#include <GL/glew.h>

Expand All @@ -23,8 +23,6 @@ class MeshNode : public Scene::IRendererObject {
: meshNode.getMesh() != nullptr && meshNode.getMesh()->getDefaultMaterial() != nullptr
? meshNode.getMesh()->getDefaultMaterial()
: nullptr;

usedMaterial->getRendererObject<Material>()->makeMeshProgram();
}

~MeshNode() override = default;
Expand All @@ -49,7 +47,6 @@ class MeshNode : public Scene::IRendererObject {

std::shared_ptr<Material> material = sceneMaterial->getRendererObject<Material>();

material->useMeshProgram();
material->render(context);

glEnable(GL_DEPTH_TEST);
Expand Down
16 changes: 0 additions & 16 deletions Engine/Render/src/Render/OpenGL/RendererDefaults.cpp

This file was deleted.

41 changes: 0 additions & 41 deletions Engine/Render/src/Render/OpenGL/RendererDefaults.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions Engine/Render/src/Render/Vulkan/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Device.hpp"
#include "FramesRenderer.hpp"
#include "RenderPass.hpp"
#include "Scene/Renderer/RendererDefaults.hpp"
#include "SwapChain.hpp"

namespace Stone::Render::Vulkan {
Expand All @@ -20,7 +19,6 @@ VulkanRenderer::VulkanRenderer(RendererSettings &settings) : Renderer() {
_renderPass = std::make_shared<RenderPass>(_device, swapChainProperties.surfaceFormat.format);
_swapChain = std::make_shared<SwapChain>(_device, _renderPass->getRenderPass(), swapChainProperties);
_framesRenderer = std::make_shared<FramesRenderer>(_device, _swapChain->getImageCount());
_rendererDefaults = std::make_unique<Scene::RendererDefaults>();
assert(_framesRenderer->getImageCount() == _swapChain->getImageCount());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ void VulkanRenderer::updateDataForWorld(const std::shared_ptr<Scene::WorldNode>
});
}

const Scene::RendererDefaults &VulkanRenderer::getRendererDefaults() const {
return *_rendererDefaults;
}

void VulkanRenderer::renderWorld(const std::shared_ptr<Scene::WorldNode> &world) {

if (!_framesRenderer) {
Expand Down
1 change: 0 additions & 1 deletion Engine/Scene/include/Scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "Scene/Renderable/Texture.hpp"
#include "Scene/Renderer/ISceneRenderer.hpp"
#include "Scene/Renderer/RenderContext.hpp"
#include "Scene/Renderer/RendererDefaults.hpp"
#include "Scene/Renderer/RendererObjectManager.hpp"
#include "Scene/Transform.hpp"
#include "Scene/Vertex.hpp"
7 changes: 0 additions & 7 deletions Engine/Scene/include/Scene/Renderer/ISceneRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace Stone::Scene {

class RendererDefaults;

/**
* @brief Interface for a scene renderer.
*/
Expand All @@ -20,11 +18,6 @@ class ISceneRenderer {
*/
virtual void updateDataForWorld(const std::shared_ptr<WorldNode> &world) = 0;

/**
* @brief Get the renderer defaults.
*/
[[nodiscard]] virtual const RendererDefaults &getRendererDefaults() const = 0;

/**
* @brief Request the renderer to render the world from the given world root node.
*/
Expand Down
20 changes: 0 additions & 20 deletions Engine/Scene/include/Scene/Renderer/RendererDefaults.hpp

This file was deleted.

12 changes: 0 additions & 12 deletions Engine/Scene/src/Scene/Renderer/RendererDefaults.cpp

This file was deleted.

0 comments on commit dd22faa

Please sign in to comment.