move renderer ownership to engine (WIP)

This commit is contained in:
Attila Uygun 2023-05-25 07:43:09 +02:00
parent d5ee1c7313
commit a7ea09b149
12 changed files with 123 additions and 114 deletions

View File

@ -13,36 +13,51 @@
#include "engine/mesh.h" #include "engine/mesh.h"
#include "engine/platform/platform.h" #include "engine/platform/platform.h"
#include "engine/renderer/geometry.h" #include "engine/renderer/geometry.h"
#include "engine/renderer/opengl/renderer_opengl.h"
#include "engine/renderer/renderer.h" #include "engine/renderer/renderer.h"
#include "engine/renderer/shader.h" #include "engine/renderer/shader.h"
#include "engine/renderer/texture.h" #include "engine/renderer/texture.h"
#include "engine/renderer/vulkan/renderer_vulkan.h"
#include "engine/shader_source.h" #include "engine/shader_source.h"
#include "third_party/texture_compressor/texture_compressor.h" #include "third_party/texture_compressor/texture_compressor.h"
#define USE_VULKAN_RENDERER 0
using namespace base; using namespace base;
namespace eng { namespace eng {
Engine* Engine::singleton = nullptr; Engine* Engine::singleton = nullptr;
Engine::Engine(Platform* platform, Renderer* renderer) Engine::Engine(Platform* platform)
: platform_(platform), : platform_(platform),
renderer_(renderer), #if (USE_VULKAN_RENDERER == 1)
audio_mixer_{std::make_unique<AudioMixer>()} { renderer_{std::make_unique<RendererVulkan>()},
#else
renderer_{std::make_unique<RendererOpenGL>()},
#endif
audio_mixer_{std::make_unique<AudioMixer>()},
quad_{CreateRenderResource<Geometry>()},
pass_through_shader_{CreateRenderResource<Shader>()},
solid_shader_{CreateRenderResource<Shader>()} {
DCHECK(!singleton); DCHECK(!singleton);
singleton = this; singleton = this;
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
quad_ = CreateRenderResource<Geometry>();
pass_through_shader_ = CreateRenderResource<Shader>();
solid_shader_ = CreateRenderResource<Shader>();
stats_ = std::make_unique<ImageQuad>(); stats_ = std::make_unique<ImageQuad>();
} }
Engine::~Engine() { Engine::~Engine() {
LOG << "Shutting down engine.";
game_.reset();
stats_.reset(); stats_.reset();
textures_.clear();
shaders_.clear();
quad_.reset();
pass_through_shader_.reset();
solid_shader_.reset();
renderer_.reset();
singleton = nullptr; singleton = nullptr;
} }
@ -51,6 +66,15 @@ Engine& Engine::Get() {
} }
bool Engine::Initialize() { bool Engine::Initialize() {
bool res;
#if defined(__ANDROID__)
res = renderer_->Initialize(platform_->GetWindow());
#elif defined(__linux__)
res = renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow());
#endif
CHECK(res) << "Failed to initialize " << renderer_->GetDebugName()
<< " renderer.";
// Normalize viewport. // Normalize viewport.
if (GetScreenWidth() > GetScreenHeight()) { if (GetScreenWidth() > GetScreenHeight()) {
float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight(); float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight();
@ -91,14 +115,6 @@ bool Engine::Initialize() {
return true; return true;
} }
void Engine::Shutdown() {
LOG << "Shutting down engine.";
game_.reset();
stats_->Destory();
textures_.clear();
shaders_.clear();
}
void Engine::Update(float delta_time) { void Engine::Update(float delta_time) {
seconds_accumulated_ += delta_time; seconds_accumulated_ += delta_time;
++tick_; ++tick_;
@ -180,25 +196,25 @@ void Engine::RemoveAnimator(Animator* animator) {
} }
void Engine::SwitchRenderer(bool vulkan) { void Engine::SwitchRenderer(bool vulkan) {
Renderer* new_renderer = platform_->SwitchRenderer(vulkan); // Renderer* new_renderer = platform_->SwitchRenderer(vulkan);
if (new_renderer == renderer_) // if (new_renderer == renderer_)
return; // return;
renderer_ = new_renderer; // renderer_ = new_renderer;
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); // renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this));
CreateTextureCompressors(); // CreateTextureCompressors();
for (auto& t : textures_) // for (auto& t : textures_)
t.second.texture->SetRenderer(renderer_); // t.second.texture->SetRenderer(renderer_);
for (auto& s : shaders_) // for (auto& s : shaders_)
s.second.shader->SetRenderer(renderer_); // s.second.shader->SetRenderer(renderer_);
quad_->SetRenderer(renderer_); // quad_->SetRenderer(renderer_);
pass_through_shader_->SetRenderer(renderer_); // pass_through_shader_->SetRenderer(renderer_);
solid_shader_->SetRenderer(renderer_); // solid_shader_->SetRenderer(renderer_);
ContextLost(); // ContextLost();
} }
void Engine::Exit() { void Engine::Exit() {

View File

@ -33,15 +33,13 @@ class Engine {
public: public:
using CreateImageCB = std::function<std::unique_ptr<Image>()>; using CreateImageCB = std::function<std::unique_ptr<Image>()>;
Engine(Platform* platform, Renderer* renderer); Engine(Platform* platform);
~Engine(); ~Engine();
static Engine& Get(); static Engine& Get();
bool Initialize(); bool Initialize();
void Shutdown();
void Update(float delta_time); void Update(float delta_time);
void Draw(float frame_frac); void Draw(float frame_frac);
@ -66,7 +64,7 @@ class Engine {
template <typename T> template <typename T>
std::unique_ptr<T> CreateRenderResource() { std::unique_ptr<T> CreateRenderResource() {
return std::unique_ptr<T>(static_cast<T*>(new T(renderer_))); return std::unique_ptr<T>(static_cast<T*>(new T(renderer_.get())));
} }
void SetImageSource(const std::string& asset_name, void SetImageSource(const std::string& asset_name,
@ -174,10 +172,9 @@ class Engine {
static Engine* singleton; static Engine* singleton;
Platform* platform_ = nullptr; Platform* platform_ = nullptr;
Renderer* renderer_ = nullptr;
std::unique_ptr<Renderer> renderer_;
std::unique_ptr<AudioMixer> audio_mixer_; std::unique_ptr<AudioMixer> audio_mixer_;
std::unique_ptr<Game> game_; std::unique_ptr<Game> game_;
std::unique_ptr<Geometry> quad_; std::unique_ptr<Geometry> quad_;

View File

@ -32,6 +32,7 @@ void ImageQuad::Create(const std::string& asset_name,
asset_name_ = asset_name; asset_name_ = asset_name;
} }
// TODO: typo
void ImageQuad::Destory() { void ImageQuad::Destory() {
if (texture_) { if (texture_) {
Engine::Get().ReleaseTexture(asset_name_); Engine::Get().ReleaseTexture(asset_name_);

View File

@ -3,10 +3,6 @@
#include "base/log.h" #include "base/log.h"
#include "base/task_runner.h" #include "base/task_runner.h"
#include "engine/engine.h" #include "engine/engine.h"
#include "engine/renderer/opengl/renderer_opengl.h"
#include "engine/renderer/vulkan/renderer_vulkan.h"
#define USE_VULKAN_RENDERER 1
using namespace base; using namespace base;
@ -16,47 +12,35 @@ Platform::Platform() = default;
Platform::~Platform() = default; Platform::~Platform() = default;
Renderer* Platform::SwitchRenderer(bool vulkan) { // Renderer* Platform::SwitchRenderer(bool vulkan) {
DCHECK(renderer_); // DCHECK(renderer_);
if ((dynamic_cast<RendererVulkan*>(renderer_.get()) && vulkan) || // if ((dynamic_cast<RendererVulkan*>(renderer_.get()) && vulkan) ||
(dynamic_cast<RendererOpenGL*>(renderer_.get()) && !vulkan)) // (dynamic_cast<RendererOpenGL*>(renderer_.get()) && !vulkan))
return renderer_.get(); // return renderer_.get();
if (vulkan) // if (vulkan)
renderer_ = std::make_unique<RendererVulkan>(); // renderer_ = std::make_unique<RendererVulkan>();
else // else
renderer_ = std::make_unique<RendererOpenGL>(); // renderer_ = std::make_unique<RendererOpenGL>();
bool result = InitializeRenderer(); // bool result = InitializeRenderer();
CHECK(result) << "Failed to initialize " << renderer_->GetDebugName() // CHECK(result) << "Failed to initialize " << renderer_->GetDebugName()
<< " renderer."; // << " renderer.";
LOG << "Switched to " << renderer_->GetDebugName() << " renderer."; // LOG << "Switched to " << renderer_->GetDebugName() << " renderer.";
return renderer_.get(); // return renderer_.get();
} // }
void Platform::InitializeCommon() { void Platform::InitializeCommon() {
LOG << "Initializing platform."; LOG << "Initializing platform.";
thread_pool_.Initialize(); thread_pool_.Initialize();
TaskRunner::CreateThreadLocalTaskRunner(); TaskRunner::CreateThreadLocalTaskRunner();
#if (USE_VULKAN_RENDERER == 1)
renderer_ = std::make_unique<RendererVulkan>();
#else
renderer_ = std::make_unique<RendererOpenGL>();
#endif
}
void Platform::ShutdownCommon() {
LOG << "Shutting down platform.";
renderer_->Shutdown();
} }
void Platform::RunMainLoop() { void Platform::RunMainLoop() {
engine_ = engine_ =
std::make_unique<Engine>(this, renderer_.get()); std::make_unique<Engine>(this);
bool res = engine_->Initialize(); bool res = engine_->Initialize();
CHECK(res) << "Failed to initialize the engine."; CHECK(res) << "Failed to initialize the engine.";
@ -83,8 +67,7 @@ void Platform::RunMainLoop() {
if (should_exit_) { if (should_exit_) {
thread_pool_.Shutdown(); thread_pool_.Shutdown();
engine_->Shutdown(); // engine_.reset();
engine_.reset();
return; return;
} }
accumulator -= time_step; accumulator -= time_step;

View File

@ -24,7 +24,6 @@ struct ANativeWindow;
namespace eng { namespace eng {
class Renderer;
class Engine; class Engine;
class Platform { class Platform {
@ -44,7 +43,7 @@ class Platform {
void Exit(); void Exit();
Renderer* SwitchRenderer(bool vulkan); // Renderer* SwitchRenderer(bool vulkan);
void Vibrate(int duration); void Vibrate(int duration);
@ -66,6 +65,13 @@ class Platform {
bool mobile_device() const { return mobile_device_; } bool mobile_device() const { return mobile_device_; }
#if defined(__ANDROID__)
ANativeWindow* GetWindow();
#elif defined(__linux__)
Display* GetDisplay();
Window GetWindow();
#endif
protected: protected:
base::Timer timer_; base::Timer timer_;
@ -78,7 +84,6 @@ class Platform {
bool has_focus_ = false; bool has_focus_ = false;
bool should_exit_ = false; bool should_exit_ = false;
std::unique_ptr<Renderer> renderer_;
std::unique_ptr<Engine> engine_; std::unique_ptr<Engine> engine_;
base::ThreadPool thread_pool_; base::ThreadPool thread_pool_;
@ -116,12 +121,11 @@ class Platform {
bool CreateWindow(int width, int height); bool CreateWindow(int width, int height);
void DestroyWindow(); void DestroyWindow();
XVisualInfo* GetXVisualInfo(Display* display);
#endif #endif
void InitializeCommon(); void InitializeCommon();
void ShutdownCommon();
bool InitializeRenderer();
Platform(const Platform&) = delete; Platform(const Platform&) = delete;
Platform& operator=(const Platform&) = delete; Platform& operator=(const Platform&) = delete;

View File

@ -9,7 +9,6 @@
#include "base/task_runner.h" #include "base/task_runner.h"
#include "engine/engine.h" #include "engine/engine.h"
#include "engine/input_event.h" #include "engine/input_event.h"
#include "engine/renderer/renderer.h"
using namespace base; using namespace base;
@ -386,7 +385,7 @@ void Platform::Initialize(android_app* app) {
} }
void Platform::Shutdown() { void Platform::Shutdown() {
Platform::ShutdownCommon(); LOG << "Shutting down platform.";
} }
void Platform::Update() { void Platform::Update() {
@ -439,8 +438,8 @@ void Platform::SetFrameRate(float frame_rate) {
} }
} }
bool Platform::InitializeRenderer() { ANativeWindow* Platform::GetWindow() {
return renderer_->Initialize(app_->window); return app_->window;
} }
} // namespace eng } // namespace eng

View File

@ -5,7 +5,6 @@
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/engine.h" #include "engine/engine.h"
#include "engine/input_event.h" #include "engine/input_event.h"
#include "engine/renderer/renderer.h"
using namespace base; using namespace base;
@ -26,10 +25,6 @@ void Platform::Initialize() {
bool res = CreateWindow(800, 1205); bool res = CreateWindow(800, 1205);
CHECK(res) << "Failed to create window."; CHECK(res) << "Failed to create window.";
res = InitializeRenderer();
CHECK(res) << "Failed to initialize " << renderer_->GetDebugName()
<< " renderer.";
XSelectInput(display_, window_, XSelectInput(display_, window_,
KeyPressMask | Button1MotionMask | ButtonPressMask | KeyPressMask | Button1MotionMask | ButtonPressMask |
ButtonReleaseMask | FocusChangeMask); ButtonReleaseMask | FocusChangeMask);
@ -38,8 +33,7 @@ void Platform::Initialize() {
} }
void Platform::Shutdown() { void Platform::Shutdown() {
Platform::ShutdownCommon(); LOG << "Shutting down platform.";
DestroyWindow(); DestroyWindow();
} }
@ -127,7 +121,7 @@ bool Platform::CreateWindow(int width, int height) {
Window root_window = DefaultRootWindow(display_); Window root_window = DefaultRootWindow(display_);
XVisualInfo* visual_info = renderer_->GetXVisualInfo(display_); XVisualInfo* visual_info = GetXVisualInfo(display_);
if (!visual_info) { if (!visual_info) {
LOG << "No appropriate visual found."; LOG << "No appropriate visual found.";
return false; return false;
@ -157,8 +151,21 @@ void Platform::DestroyWindow() {
} }
} }
bool Platform::InitializeRenderer() { Display* Platform::GetDisplay() {
return renderer_->Initialize(display_, window_); return display_;
}
Window Platform::GetWindow() {
return window_;
}
XVisualInfo* Platform::GetXVisualInfo(Display* display) {
long visual_mask = VisualScreenMask;
int num_visuals;
XVisualInfo visual_info_template = {};
visual_info_template.screen = DefaultScreen(display);
return XGetVisualInfo(display, visual_mask, &visual_info_template,
&num_visuals);
} }
} // namespace eng } // namespace eng

View File

@ -89,9 +89,9 @@ class RendererOpenGL final : public Renderer {
const char* GetDebugName() final { return "OpenGL"; } const char* GetDebugName() final { return "OpenGL"; }
#if defined(__linux__) && !defined(__ANDROID__) // #if defined(__linux__) && !defined(__ANDROID__)
XVisualInfo* GetXVisualInfo(Display* display) final; // XVisualInfo* GetXVisualInfo(Display* display) final;
#endif // #endif
private: private:
struct GeometryOpenGL { struct GeometryOpenGL {

View File

@ -21,9 +21,11 @@ bool RendererOpenGL::Initialize(Display* display, Window window) {
void RendererOpenGL::OnDestroy() {} void RendererOpenGL::OnDestroy() {}
bool RendererOpenGL::InitInternal() { bool RendererOpenGL::InitInternal() {
// Create the OpenGL context. GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER,
None};
XVisualInfo* visual_info = glXChooseVisual(display_, 0, glx_attributes);
glx_context_ = glx_context_ =
glXCreateContext(display_, GetXVisualInfo(display_), NULL, GL_TRUE); glXCreateContext(display_, visual_info, NULL, GL_TRUE);
if (!glx_context_) { if (!glx_context_) {
LOG << "Couldn't create the glx context."; LOG << "Couldn't create the glx context.";
return false; return false;
@ -59,11 +61,11 @@ void RendererOpenGL::HandleCmdPresent(RenderCommand* cmd) {
} }
} }
XVisualInfo* RendererOpenGL::GetXVisualInfo(Display* display) { // XVisualInfo* RendererOpenGL::GetXVisualInfo(Display* display) {
// Look for the right visual to set up the OpenGL context. // // Look for the right visual to set up the OpenGL context.
GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, // GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER,
None}; // None};
return glXChooseVisual(display, 0, glx_attributes); // return glXChooseVisual(display, 0, glx_attributes);
} // }
} // namespace eng } // namespace eng

View File

@ -94,9 +94,9 @@ class Renderer {
virtual const char* GetDebugName() = 0; virtual const char* GetDebugName() = 0;
#if defined(__linux__) && !defined(__ANDROID__) // #if defined(__linux__) && !defined(__ANDROID__)
virtual XVisualInfo* GetXVisualInfo(Display* display) = 0; // virtual XVisualInfo* GetXVisualInfo(Display* display) = 0;
#endif // #endif
protected: protected:
struct TextureCompression { struct TextureCompression {

View File

@ -74,9 +74,9 @@ class RendererVulkan final : public Renderer {
const char* GetDebugName() final { return "Vulkan"; } const char* GetDebugName() final { return "Vulkan"; }
#if defined(__linux__) && !defined(__ANDROID__) // #if defined(__linux__) && !defined(__ANDROID__)
XVisualInfo* GetXVisualInfo(Display* display) final; // XVisualInfo* GetXVisualInfo(Display* display) final;
#endif // #endif
private: private:
// VkBuffer or VkImage with allocator. // VkBuffer or VkImage with allocator.

View File

@ -24,13 +24,13 @@ bool RendererVulkan::Initialize(Display* display, Window window) {
return InitializeInternal(); return InitializeInternal();
} }
XVisualInfo* RendererVulkan::GetXVisualInfo(Display* display) { // XVisualInfo* RendererVulkan::GetXVisualInfo(Display* display) {
long visual_mask = VisualScreenMask; // long visual_mask = VisualScreenMask;
int num_visuals; // int num_visuals;
XVisualInfo visual_info_template = {}; // XVisualInfo visual_info_template = {};
visual_info_template.screen = DefaultScreen(display); // visual_info_template.screen = DefaultScreen(display);
return XGetVisualInfo(display, visual_mask, &visual_info_template, // return XGetVisualInfo(display, visual_mask, &visual_info_template,
&num_visuals); // &num_visuals);
} // }
} // namespace eng } // namespace eng