Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/CAE/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ namespace cae
void stop();

private:
void setupEngine(const std::string &rendererName, const std::string &windowName, const std::string &shaderName);
void setupEngine(const std::string &rendererName, const std::string &windowName,
const std::string &shaderFrontendName, const std::string &shaderIRname);

static EngineConfig parseEngineConf(const std::string &path);

Expand Down
3 changes: 1 addition & 2 deletions modules/Engine/include/Engine/Common.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///
/// @file Common.hpp
/// @brief This file contains
/// @brief This file contains common constants used across the engine
/// @namespace cae
///

Expand All @@ -22,7 +22,6 @@ namespace cae
inline constexpr auto MUTED = false;
} // namespace Audio


namespace Network
{
inline constexpr auto HOST = "127.0.0.1";
Expand Down
11 changes: 8 additions & 3 deletions modules/Engine/include/Engine/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
#pragma once

#include "Engine/Common.hpp"
#include "Engine/ShaderManager.hpp"

#include "Utils/Clock.hpp"
#include "Interfaces/IAudio.hpp"
#include "Interfaces/INetwork.hpp"
#include "Interfaces/Input/IInput.hpp"
#include "Interfaces/Renderer/IRenderer.hpp"
#include "Utils/Clock.hpp"

#include <functional>

Expand Down Expand Up @@ -49,7 +50,8 @@ namespace cae
const std::function<std::shared_ptr<IInput>()> &inputFactory,
const std::function<std::shared_ptr<INetwork>()> &networkFactory,
const std::function<std::shared_ptr<IRenderer>()> &rendererFactory,
const std::function<std::shared_ptr<IShader>()> &shaderFactory,
const std::function<std::shared_ptr<IShaderIR>()> &shaderIRFactory,
const std::vector<std::function<std::shared_ptr<IShaderFrontend>()>> &shaderFrontendFactories,
const std::function<std::shared_ptr<IWindow>()> &windowFactory);
~Engine() = default;

Expand All @@ -74,11 +76,14 @@ namespace cae
std::shared_ptr<IInput> m_inputPlugin = nullptr;
std::shared_ptr<INetwork> m_networkPlugin = nullptr;
std::shared_ptr<IRenderer> m_rendererPlugin = nullptr;
std::shared_ptr<IShader> m_shaderPlugin = nullptr;
std::unique_ptr<ShaderManager> m_shaderManager = nullptr;
std::shared_ptr<IWindow> m_windowPlugin = nullptr;

std::unique_ptr<utl::Clock> m_clock = nullptr;

void initShaders(
const std::function<std::shared_ptr<IShaderIR>()> &shaderIRFactory,
const std::vector<std::function<std::shared_ptr<IShaderFrontend>()>> &shaderFrontendFactories) const;
}; // class Engine

} // namespace cae
80 changes: 80 additions & 0 deletions modules/Engine/include/Engine/ShaderManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
///
/// @file ShaderManager.hpp
/// @brief This file contains the ShaderManager class declaration
/// @namespace cae
///

#pragma once

#include "Interfaces/Shader/IShaderIR.hpp"

#include <memory>
#include <ranges>
#include <unordered_map>

namespace cae
{
///
/// @class ShaderManager
/// @brief Class for managing shaders
/// @namespace cae
///
class ShaderManager
{
public:
ShaderManager() = default;
~ShaderManager() = default;

ShaderManager(const ShaderManager &) = delete;
ShaderManager &operator=(const ShaderManager &) = delete;
ShaderManager(ShaderManager &&) = delete;
ShaderManager &operator=(ShaderManager &&) = delete;

void registerFrontend(const std::shared_ptr<IShaderFrontend> &f) { m_frontends[f->sourceType()] = f; }
void registerIR(const std::shared_ptr<IShaderIR> &ir) { m_irs[ir->irType()] = ir; }

std::unordered_map<ShaderID, ShaderIRModule> build(const std::vector<ShaderSourceDesc> &sources,
const ShaderSourceType targetIR) const
{
std::unordered_map<ShaderID, ShaderIRModule> out;

const auto irProcessor = m_irs.at(targetIR);
for (const auto &src : sources)
{
const auto f = m_frontends.at(src.type);
ShaderIRModule ir = f->compile(src);
const ShaderIRModule final = irProcessor->process(ir);
out[src.id] = final;
}

return out;
}

template <std::ranges::input_range R> void optimizeAll(ShaderSourceType irType, R &&modules) const
{
if (auto it = m_irs.find(irType); it != m_irs.end())
{
std::vector<ShaderIRModule *> ptrs;
for (auto &m : modules)
ptrs.push_back(&m);

std::vector<ShaderIRModule> tmp;
tmp.reserve(ptrs.size());
for (auto *p : ptrs)
tmp.push_back(*p);

it->second->optimize(tmp);

// recopier vers les originaux si besoin
for (size_t i = 0; i < ptrs.size(); ++i)
*ptrs[i] = std::move(tmp[i]);
}
}

private:
std::unordered_map<ShaderSourceType, std::shared_ptr<IShaderFrontend>> m_frontends;
std::unordered_map<ShaderSourceType, std::shared_ptr<IShaderIR>> m_irs;

}; // class ShaderManager

} // namespace cae
96 changes: 59 additions & 37 deletions modules/Engine/src/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
#include "Engine/Engine.hpp"

#include "Utils/Logger.hpp"
#include "Utils/Utils.hpp"

#include <numeric>
#include <sstream>

void printFps(std::array<float, 10> &fpsBuffer, int &fpsIndex, const float deltaTime)
{
fpsBuffer[fpsIndex % 10] = 1.0F / deltaTime;
fpsIndex++;

float avgFps = std::accumulate(fpsBuffer.begin(), fpsBuffer.end(), 0.0f) / 10.0f;
utl::Logger::log(std::format("FPS: {}", avgFps), utl::LogLevel::INFO);
}

cae::Engine::Engine(const EngineConfig &config, const std::function<std::shared_ptr<IAudio>()> &audioFactory,
const std::function<std::shared_ptr<IInput>()> &inputFactory,
const std::function<std::shared_ptr<INetwork>()> &networkFactory,
const std::function<std::shared_ptr<IRenderer>()> &rendererFactory,
const std::function<std::shared_ptr<IShader>()> &shaderFactory,
const std::function<std::shared_ptr<IShaderIR>()> &shaderIRFactory,
const std::vector<std::function<std::shared_ptr<IShaderFrontend>()>> &shaderFrontendFactories,
const std::function<std::shared_ptr<IWindow>()> &windowFactory)
: m_audioPlugin(audioFactory()), m_inputPlugin(inputFactory()), m_networkPlugin(networkFactory()),
m_rendererPlugin(rendererFactory()), m_shaderPlugin(shaderFactory()), m_windowPlugin(windowFactory()), m_clock(std::make_unique<utl::Clock>())
m_rendererPlugin(rendererFactory()), m_shaderManager(std::make_unique<ShaderManager>()),
m_windowPlugin(windowFactory()), m_clock(std::make_unique<utl::Clock>())
{
utl::Logger::log("Loading engine with configuration:", utl::LogLevel::INFO);
utl::Logger::log("\tAudio master volume: " + std::to_string(config.audio_master_volume), utl::LogLevel::INFO);
utl::Logger::log("\tAudio muted: " + std::string(config.audio_muted ? "true" : "false"), utl::LogLevel::INFO);
utl::Logger::log("\tNetwork host: " + config.network_host, utl::LogLevel::INFO);
utl::Logger::log("\tNetwork port: " + std::to_string(config.network_port), utl::LogLevel::INFO);
utl::Logger::log("\tRenderer vsync: " + std::string(config.renderer_vsync ? "true" : "false"), utl::LogLevel::INFO);
utl::Logger::log("\tRenderer frame rate limit: " + std::to_string(config.renderer_frame_rate_limit),
utl::LogLevel::INFO);
utl::Logger::log("\tWindow width: " + std::to_string(config.window_width), utl::LogLevel::INFO);
utl::Logger::log("\tWindow height: " + std::to_string(config.window_height), utl::LogLevel::INFO);
utl::Logger::log("\tWindow fullscreen: " + std::string(config.window_fullscreen ? "true" : "false"),
utl::LogLevel::INFO);
utl::Logger::log("\tWindow name: " + config.window_name, utl::LogLevel::INFO);
m_windowPlugin->create(config.window_name, {.width = config.window_width, .height = config.window_height});
const std::vector<ShaderModuleDesc> shadersPipelineDesc = {
{.id = "basic_vertex", .source = utl::fileToString("assets/shaders/uniform_color.vert"),
.stage = ShaderStage::VERTEX},
{.id = "basic_fragment", .source = utl::fileToString("assets/shaders/uniform_color.frag"),
.stage = ShaderStage::FRAGMENT}};
m_shaderPlugin->addShader(shadersPipelineDesc.at(0));
m_shaderPlugin->addShader(shadersPipelineDesc.at(1));
if (!m_shaderPlugin->compileAll())
{
throw std::runtime_error("Failed to compile shaders");
}
m_rendererPlugin->initialize(m_windowPlugin->getNativeHandle(), m_shaderPlugin);
m_rendererPlugin->createPipeline({.id = "basic", .vertex = shadersPipelineDesc.at(0).id, .fragment = shadersPipelineDesc.at(1).id});
}
constexpr auto boolToStr = [](bool b) { return b ? "true" : "false"; };
std::ostringstream msg;
msg << "Loading engine with configuration:\n"
<< "\tAudio master volume: " << config.audio_master_volume << "\n"
<< "\tAudio muted: " << boolToStr(config.audio_muted) << "\n"
<< "\tNetwork host: " << config.network_host << "\n"
<< "\tNetwork port: " << config.network_port << "\n"
<< "\tRenderer vsync: " << boolToStr(config.renderer_vsync) << "\n"
<< "\tRenderer frame rate limit: " << config.renderer_frame_rate_limit << "\n"
<< "\tWindow width: " << config.window_width << "\n"
<< "\tWindow height: " << config.window_height << "\n"
<< "\tWindow fullscreen: " << boolToStr(config.window_fullscreen) << "\n"
<< "\tWindow name: " << config.window_name;
utl::Logger::log(msg.str(), utl::LogLevel::INFO);

void printFps(std::array<float, 10>& fpsBuffer, int& fpsIndex, float deltaTime)
{
fpsBuffer[fpsIndex % 10] = 1.0f / deltaTime;
fpsIndex++;

float avgFps = std::accumulate(fpsBuffer.begin(), fpsBuffer.end(), 0.0f) / 10.0f;
utl::Logger::log(std::format("FPS: {}", avgFps), utl::LogLevel::INFO);
m_windowPlugin->create(config.window_name, {.width = config.window_width, .height = config.window_height});
m_rendererPlugin->initialize(m_windowPlugin->getNativeHandle());
initShaders(shaderIRFactory, shaderFrontendFactories);
}

void cae::Engine::run() const
Expand All @@ -75,3 +70,30 @@ void cae::Engine::stop()
m_rendererPlugin = nullptr;
m_windowPlugin = nullptr;
}

void cae::Engine::initShaders(
const std::function<std::shared_ptr<IShaderIR>()> &shaderIRFactory,
const std::vector<std::function<std::shared_ptr<IShaderFrontend>()>> &shaderFrontendFactories) const
{
static const std::vector<ShaderSourceDesc> shaderSources = {
{.id = "basic_vertex",
.type = ShaderSourceType::GLSL,
.source = utl::fileToString("assets/shaders/uniform_color.vert"),
.stage = ShaderStage::VERTEX},

{.id = "basic_fragment",
.type = ShaderSourceType::GLSL,
.source = utl::fileToString("assets/shaders/uniform_color.frag"),
.stage = ShaderStage::FRAGMENT},
};

for (const auto &factory : shaderFrontendFactories)
{
auto frontend = factory();
m_shaderManager->registerFrontend(frontend);
}
m_shaderManager->registerIR(shaderIRFactory());
auto shaders = m_shaderManager->build(shaderSources, ShaderSourceType::SPIRV);
m_shaderManager->optimizeAll(ShaderSourceType::SPIRV, shaders | std::views::values);
m_rendererPlugin->createPipeline("basic", shaders["basic_vertex"], shaders["basic_fragment"]);
}
1 change: 0 additions & 1 deletion modules/Interfaces/include/Interfaces/IWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ namespace cae
// virtual void setFullScreen(bool fullScreen) const = 0;

private:

// std::unique_ptr<IInput> m_inputManager;

}; // interface IWindow
Expand Down
5 changes: 4 additions & 1 deletion modules/Interfaces/include/Interfaces/Input/AInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ namespace cae
const std::unique_ptr<IMouse> &getMouse() const override { return m_mouse; }
const std::vector<std::unique_ptr<IGamepad>> &getGamepads() const override { return m_gamepads; }

void setGamepads(std::vector<std::unique_ptr<IGamepad>> &gamepads) override { m_gamepads = std::move(gamepads); }
void setGamepads(std::vector<std::unique_ptr<IGamepad>> &gamepads) override
{
m_gamepads = std::move(gamepads);
}
void setKeyboard(std::unique_ptr<IKeyboard> &keyboard) override { m_keyboard = std::move(keyboard); }
void setMouse(std::unique_ptr<IMouse> &mouse) override { m_mouse = std::move(mouse); }

Expand Down
3 changes: 0 additions & 3 deletions modules/Interfaces/include/Interfaces/Renderer/ARenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@ namespace cae
public:
~ARenderer() override = default;

std::shared_ptr<IShader> getShader() const override { return m_shader; }
std::shared_ptr<IModel> getModel() const override { return m_model; }

void setShader(const std::shared_ptr<IShader> shader) override { m_shader = shader; }
void setModel(const std::shared_ptr<IModel> model) override { m_model = model; }

protected:
std::shared_ptr<IShader> m_shader;
std::shared_ptr<IModel> m_model;

}; // interface ARenderer
Expand Down
11 changes: 5 additions & 6 deletions modules/Interfaces/include/Interfaces/Renderer/IRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#pragma once

#include "Interfaces/Renderer/IModel.hpp"
#include "Interfaces/Renderer/IShader.hpp"
#include "Interfaces/IWindow.hpp"
#include "Interfaces/Renderer/IModel.hpp"
#include "Interfaces/Shader/IShaderFrontend.hpp"

#include <memory>

Expand All @@ -26,14 +26,13 @@ namespace cae
public:
~IRenderer() override = default;

virtual std::shared_ptr<IShader> getShader() const = 0;
virtual std::shared_ptr<IModel> getModel() const = 0;

virtual void setShader(std::shared_ptr<IShader> shader) = 0;
virtual void setModel(std::shared_ptr<IModel> model) = 0;

virtual void initialize(const NativeWindowHandle &nativeWindowHandle, std::shared_ptr<IShader> shader) = 0;
virtual void createPipeline(const ShaderPipelineDesc& pipeline) = 0;
virtual void initialize(const NativeWindowHandle &nativeWindowHandle) = 0;
virtual void createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
const ShaderIRModule &fragment) = 0;

virtual void draw(const WindowSize &windowSize) = 0;

Expand Down
Loading
Loading