From a7ea09b149d71cba418a92b28a211817b1002c99 Mon Sep 17 00:00:00 2001 From: Attila Uygun Date: Thu, 25 May 2023 07:43:09 +0200 Subject: [PATCH] move renderer ownership to engine (WIP) --- src/engine/engine.cc | 74 +++++++++++-------- src/engine/engine.h | 9 +-- src/engine/image_quad.cc | 1 + src/engine/platform/platform.cc | 51 +++++-------- src/engine/platform/platform.h | 16 ++-- src/engine/platform/platform_android.cc | 7 +- src/engine/platform/platform_linux.cc | 27 ++++--- src/engine/renderer/opengl/renderer_opengl.h | 6 +- .../renderer/opengl/renderer_opengl_linux.cc | 18 +++-- src/engine/renderer/renderer.h | 6 +- src/engine/renderer/vulkan/renderer_vulkan.h | 6 +- .../renderer/vulkan/renderer_vulkan_linux.cc | 16 ++-- 12 files changed, 123 insertions(+), 114 deletions(-) diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 7a6de43..9037d52 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -13,36 +13,51 @@ #include "engine/mesh.h" #include "engine/platform/platform.h" #include "engine/renderer/geometry.h" +#include "engine/renderer/opengl/renderer_opengl.h" #include "engine/renderer/renderer.h" #include "engine/renderer/shader.h" #include "engine/renderer/texture.h" +#include "engine/renderer/vulkan/renderer_vulkan.h" #include "engine/shader_source.h" #include "third_party/texture_compressor/texture_compressor.h" +#define USE_VULKAN_RENDERER 0 + using namespace base; namespace eng { Engine* Engine::singleton = nullptr; -Engine::Engine(Platform* platform, Renderer* renderer) +Engine::Engine(Platform* platform) : platform_(platform), - renderer_(renderer), - audio_mixer_{std::make_unique()} { +#if (USE_VULKAN_RENDERER == 1) + renderer_{std::make_unique()}, +#else + renderer_{std::make_unique()}, +#endif + audio_mixer_{std::make_unique()}, + quad_{CreateRenderResource()}, + pass_through_shader_{CreateRenderResource()}, + solid_shader_{CreateRenderResource()} { DCHECK(!singleton); singleton = this; renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); - quad_ = CreateRenderResource(); - pass_through_shader_ = CreateRenderResource(); - solid_shader_ = CreateRenderResource(); - stats_ = std::make_unique(); } Engine::~Engine() { + LOG << "Shutting down engine."; + game_.reset(); stats_.reset(); + textures_.clear(); + shaders_.clear(); + quad_.reset(); + pass_through_shader_.reset(); + solid_shader_.reset(); + renderer_.reset(); singleton = nullptr; } @@ -51,6 +66,15 @@ Engine& Engine::Get() { } 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. if (GetScreenWidth() > GetScreenHeight()) { float aspect_ratio = (float)GetScreenWidth() / (float)GetScreenHeight(); @@ -91,14 +115,6 @@ bool Engine::Initialize() { return true; } -void Engine::Shutdown() { - LOG << "Shutting down engine."; - game_.reset(); - stats_->Destory(); - textures_.clear(); - shaders_.clear(); -} - void Engine::Update(float delta_time) { seconds_accumulated_ += delta_time; ++tick_; @@ -180,25 +196,25 @@ void Engine::RemoveAnimator(Animator* animator) { } void Engine::SwitchRenderer(bool vulkan) { - Renderer* new_renderer = platform_->SwitchRenderer(vulkan); - if (new_renderer == renderer_) - return; + // Renderer* new_renderer = platform_->SwitchRenderer(vulkan); + // if (new_renderer == renderer_) + // return; - renderer_ = new_renderer; - renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); - CreateTextureCompressors(); + // renderer_ = new_renderer; + // renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); + // CreateTextureCompressors(); - for (auto& t : textures_) - t.second.texture->SetRenderer(renderer_); + // for (auto& t : textures_) + // t.second.texture->SetRenderer(renderer_); - for (auto& s : shaders_) - s.second.shader->SetRenderer(renderer_); + // for (auto& s : shaders_) + // s.second.shader->SetRenderer(renderer_); - quad_->SetRenderer(renderer_); - pass_through_shader_->SetRenderer(renderer_); - solid_shader_->SetRenderer(renderer_); + // quad_->SetRenderer(renderer_); + // pass_through_shader_->SetRenderer(renderer_); + // solid_shader_->SetRenderer(renderer_); - ContextLost(); + // ContextLost(); } void Engine::Exit() { diff --git a/src/engine/engine.h b/src/engine/engine.h index 5bf42a1..7ad79ad 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -33,15 +33,13 @@ class Engine { public: using CreateImageCB = std::function()>; - Engine(Platform* platform, Renderer* renderer); + Engine(Platform* platform); ~Engine(); static Engine& Get(); bool Initialize(); - void Shutdown(); - void Update(float delta_time); void Draw(float frame_frac); @@ -66,7 +64,7 @@ class Engine { template std::unique_ptr CreateRenderResource() { - return std::unique_ptr(static_cast(new T(renderer_))); + return std::unique_ptr(static_cast(new T(renderer_.get()))); } void SetImageSource(const std::string& asset_name, @@ -174,10 +172,9 @@ class Engine { static Engine* singleton; Platform* platform_ = nullptr; - Renderer* renderer_ = nullptr; + std::unique_ptr renderer_; std::unique_ptr audio_mixer_; - std::unique_ptr game_; std::unique_ptr quad_; diff --git a/src/engine/image_quad.cc b/src/engine/image_quad.cc index ab2e11a..d995a0a 100644 --- a/src/engine/image_quad.cc +++ b/src/engine/image_quad.cc @@ -32,6 +32,7 @@ void ImageQuad::Create(const std::string& asset_name, asset_name_ = asset_name; } +// TODO: typo void ImageQuad::Destory() { if (texture_) { Engine::Get().ReleaseTexture(asset_name_); diff --git a/src/engine/platform/platform.cc b/src/engine/platform/platform.cc index c65d6ea..c717f84 100644 --- a/src/engine/platform/platform.cc +++ b/src/engine/platform/platform.cc @@ -3,10 +3,6 @@ #include "base/log.h" #include "base/task_runner.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; @@ -16,47 +12,35 @@ Platform::Platform() = default; Platform::~Platform() = default; -Renderer* Platform::SwitchRenderer(bool vulkan) { - DCHECK(renderer_); +// Renderer* Platform::SwitchRenderer(bool vulkan) { +// DCHECK(renderer_); - if ((dynamic_cast(renderer_.get()) && vulkan) || - (dynamic_cast(renderer_.get()) && !vulkan)) - return renderer_.get(); +// if ((dynamic_cast(renderer_.get()) && vulkan) || +// (dynamic_cast(renderer_.get()) && !vulkan)) +// return renderer_.get(); - if (vulkan) - renderer_ = std::make_unique(); - else - renderer_ = std::make_unique(); +// if (vulkan) +// renderer_ = std::make_unique(); +// else +// renderer_ = std::make_unique(); - bool result = InitializeRenderer(); - CHECK(result) << "Failed to initialize " << renderer_->GetDebugName() - << " renderer."; - LOG << "Switched to " << renderer_->GetDebugName() << " renderer."; - return renderer_.get(); -} +// bool result = InitializeRenderer(); +// CHECK(result) << "Failed to initialize " << renderer_->GetDebugName() +// << " renderer."; +// LOG << "Switched to " << renderer_->GetDebugName() << " renderer."; +// return renderer_.get(); +// } void Platform::InitializeCommon() { LOG << "Initializing platform."; thread_pool_.Initialize(); TaskRunner::CreateThreadLocalTaskRunner(); - -#if (USE_VULKAN_RENDERER == 1) - renderer_ = std::make_unique(); -#else - renderer_ = std::make_unique(); -#endif -} - -void Platform::ShutdownCommon() { - LOG << "Shutting down platform."; - - renderer_->Shutdown(); } void Platform::RunMainLoop() { engine_ = - std::make_unique(this, renderer_.get()); + std::make_unique(this); bool res = engine_->Initialize(); CHECK(res) << "Failed to initialize the engine."; @@ -83,8 +67,7 @@ void Platform::RunMainLoop() { if (should_exit_) { thread_pool_.Shutdown(); - engine_->Shutdown(); - engine_.reset(); + // engine_.reset(); return; } accumulator -= time_step; diff --git a/src/engine/platform/platform.h b/src/engine/platform/platform.h index 977a868..a027a93 100644 --- a/src/engine/platform/platform.h +++ b/src/engine/platform/platform.h @@ -24,7 +24,6 @@ struct ANativeWindow; namespace eng { -class Renderer; class Engine; class Platform { @@ -44,7 +43,7 @@ class Platform { void Exit(); - Renderer* SwitchRenderer(bool vulkan); + // Renderer* SwitchRenderer(bool vulkan); void Vibrate(int duration); @@ -66,6 +65,13 @@ class Platform { bool mobile_device() const { return mobile_device_; } +#if defined(__ANDROID__) + ANativeWindow* GetWindow(); +#elif defined(__linux__) + Display* GetDisplay(); + Window GetWindow(); +#endif + protected: base::Timer timer_; @@ -78,7 +84,6 @@ class Platform { bool has_focus_ = false; bool should_exit_ = false; - std::unique_ptr renderer_; std::unique_ptr engine_; base::ThreadPool thread_pool_; @@ -116,12 +121,11 @@ class Platform { bool CreateWindow(int width, int height); void DestroyWindow(); + XVisualInfo* GetXVisualInfo(Display* display); + #endif void InitializeCommon(); - void ShutdownCommon(); - - bool InitializeRenderer(); Platform(const Platform&) = delete; Platform& operator=(const Platform&) = delete; diff --git a/src/engine/platform/platform_android.cc b/src/engine/platform/platform_android.cc index 1bf6c13..7d0fd2a 100644 --- a/src/engine/platform/platform_android.cc +++ b/src/engine/platform/platform_android.cc @@ -9,7 +9,6 @@ #include "base/task_runner.h" #include "engine/engine.h" #include "engine/input_event.h" -#include "engine/renderer/renderer.h" using namespace base; @@ -386,7 +385,7 @@ void Platform::Initialize(android_app* app) { } void Platform::Shutdown() { - Platform::ShutdownCommon(); + LOG << "Shutting down platform."; } void Platform::Update() { @@ -439,8 +438,8 @@ void Platform::SetFrameRate(float frame_rate) { } } -bool Platform::InitializeRenderer() { - return renderer_->Initialize(app_->window); +ANativeWindow* Platform::GetWindow() { + return app_->window; } } // namespace eng diff --git a/src/engine/platform/platform_linux.cc b/src/engine/platform/platform_linux.cc index 4d0982d..c2a451d 100644 --- a/src/engine/platform/platform_linux.cc +++ b/src/engine/platform/platform_linux.cc @@ -5,7 +5,6 @@ #include "base/vecmath.h" #include "engine/engine.h" #include "engine/input_event.h" -#include "engine/renderer/renderer.h" using namespace base; @@ -26,10 +25,6 @@ void Platform::Initialize() { bool res = CreateWindow(800, 1205); CHECK(res) << "Failed to create window."; - res = InitializeRenderer(); - CHECK(res) << "Failed to initialize " << renderer_->GetDebugName() - << " renderer."; - XSelectInput(display_, window_, KeyPressMask | Button1MotionMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask); @@ -38,8 +33,7 @@ void Platform::Initialize() { } void Platform::Shutdown() { - Platform::ShutdownCommon(); - + LOG << "Shutting down platform."; DestroyWindow(); } @@ -127,7 +121,7 @@ bool Platform::CreateWindow(int width, int height) { Window root_window = DefaultRootWindow(display_); - XVisualInfo* visual_info = renderer_->GetXVisualInfo(display_); + XVisualInfo* visual_info = GetXVisualInfo(display_); if (!visual_info) { LOG << "No appropriate visual found."; return false; @@ -157,8 +151,21 @@ void Platform::DestroyWindow() { } } -bool Platform::InitializeRenderer() { - return renderer_->Initialize(display_, window_); +Display* Platform::GetDisplay() { + 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 diff --git a/src/engine/renderer/opengl/renderer_opengl.h b/src/engine/renderer/opengl/renderer_opengl.h index 75f983a..c0922ce 100644 --- a/src/engine/renderer/opengl/renderer_opengl.h +++ b/src/engine/renderer/opengl/renderer_opengl.h @@ -89,9 +89,9 @@ class RendererOpenGL final : public Renderer { const char* GetDebugName() final { return "OpenGL"; } -#if defined(__linux__) && !defined(__ANDROID__) - XVisualInfo* GetXVisualInfo(Display* display) final; -#endif +// #if defined(__linux__) && !defined(__ANDROID__) +// XVisualInfo* GetXVisualInfo(Display* display) final; +// #endif private: struct GeometryOpenGL { diff --git a/src/engine/renderer/opengl/renderer_opengl_linux.cc b/src/engine/renderer/opengl/renderer_opengl_linux.cc index 5bb452a..f40c1f2 100644 --- a/src/engine/renderer/opengl/renderer_opengl_linux.cc +++ b/src/engine/renderer/opengl/renderer_opengl_linux.cc @@ -21,9 +21,11 @@ bool RendererOpenGL::Initialize(Display* display, Window window) { void RendererOpenGL::OnDestroy() {} 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_ = - glXCreateContext(display_, GetXVisualInfo(display_), NULL, GL_TRUE); + glXCreateContext(display_, visual_info, NULL, GL_TRUE); if (!glx_context_) { LOG << "Couldn't create the glx context."; return false; @@ -59,11 +61,11 @@ void RendererOpenGL::HandleCmdPresent(RenderCommand* cmd) { } } -XVisualInfo* RendererOpenGL::GetXVisualInfo(Display* display) { - // Look for the right visual to set up the OpenGL context. - GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, - None}; - return glXChooseVisual(display, 0, glx_attributes); -} +// XVisualInfo* RendererOpenGL::GetXVisualInfo(Display* display) { +// // Look for the right visual to set up the OpenGL context. +// GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, +// None}; +// return glXChooseVisual(display, 0, glx_attributes); +// } } // namespace eng diff --git a/src/engine/renderer/renderer.h b/src/engine/renderer/renderer.h index e6bed68..1c057f9 100644 --- a/src/engine/renderer/renderer.h +++ b/src/engine/renderer/renderer.h @@ -94,9 +94,9 @@ class Renderer { virtual const char* GetDebugName() = 0; -#if defined(__linux__) && !defined(__ANDROID__) - virtual XVisualInfo* GetXVisualInfo(Display* display) = 0; -#endif +// #if defined(__linux__) && !defined(__ANDROID__) +// virtual XVisualInfo* GetXVisualInfo(Display* display) = 0; +// #endif protected: struct TextureCompression { diff --git a/src/engine/renderer/vulkan/renderer_vulkan.h b/src/engine/renderer/vulkan/renderer_vulkan.h index 38b9bee..9b63347 100644 --- a/src/engine/renderer/vulkan/renderer_vulkan.h +++ b/src/engine/renderer/vulkan/renderer_vulkan.h @@ -74,9 +74,9 @@ class RendererVulkan final : public Renderer { const char* GetDebugName() final { return "Vulkan"; } -#if defined(__linux__) && !defined(__ANDROID__) - XVisualInfo* GetXVisualInfo(Display* display) final; -#endif +// #if defined(__linux__) && !defined(__ANDROID__) +// XVisualInfo* GetXVisualInfo(Display* display) final; +// #endif private: // VkBuffer or VkImage with allocator. diff --git a/src/engine/renderer/vulkan/renderer_vulkan_linux.cc b/src/engine/renderer/vulkan/renderer_vulkan_linux.cc index 4cb66bd..38b1ddd 100644 --- a/src/engine/renderer/vulkan/renderer_vulkan_linux.cc +++ b/src/engine/renderer/vulkan/renderer_vulkan_linux.cc @@ -24,13 +24,13 @@ bool RendererVulkan::Initialize(Display* display, Window window) { return InitializeInternal(); } -XVisualInfo* RendererVulkan::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); -} +// XVisualInfo* RendererVulkan::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