Cleanup renderer

This commit is contained in:
Attila Uygun 2023-05-26 13:50:29 +02:00
parent 43afdf855f
commit f6f67d7e53
13 changed files with 51 additions and 77 deletions

View File

@ -84,14 +84,12 @@ void Engine::Run() {
// Subdivide the frame time using fixed time steps. // Subdivide the frame time using fixed time steps.
while (accumulator >= time_step_) { while (accumulator >= time_step_) {
TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun(); TaskRunner::GetThreadLocalTaskRunner()->SingleConsumerRun();
platform_->Update(); platform_->Update();
Update(time_step_); Update(time_step_);
if (platform_->should_exit()) {
return;
}
accumulator -= time_step_; accumulator -= time_step_;
if (platform_->should_exit())
return;
}; };
// Calculate frame fraction from remainder of the frame time. // Calculate frame fraction from remainder of the frame time.
@ -100,6 +98,8 @@ void Engine::Run() {
} }
void Engine::Initialize() { void Engine::Initialize() {
LOG << "Initializing engine.";
thread_pool_.Initialize(); thread_pool_.Initialize();
CreateRenderer(true); CreateRenderer(true);
@ -203,19 +203,18 @@ void Engine::CreateRenderer(bool vulkan) {
return; return;
if (vulkan) if (vulkan)
renderer_ = std::make_unique<RendererVulkan>(); renderer_ =
std::make_unique<RendererVulkan>(std::bind(&Engine::ContextLost, this));
else else
renderer_ = std::make_unique<RendererOpenGL>(); renderer_ =
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); std::make_unique<RendererOpenGL>(std::bind(&Engine::ContextLost, this));
bool result = InitializeRenderer(); bool result = renderer_->Initialize(platform_);
LOG_IF(!result) << "Failed to initialize " << renderer_->GetDebugName()
<< " renderer.";
if (!result && vulkan) { if (!result && vulkan) {
LOG << "Failed to initialize " << renderer_->GetDebugName() << " renderer.";
LOG << "Fallback to OpenGL renderer."; LOG << "Fallback to OpenGL renderer.";
renderer_ = std::make_unique<RendererOpenGL>(); CreateRenderer(false);
renderer_->SetContextLostCB(std::bind(&Engine::ContextLost, this)); return;
result = InitializeRenderer();
} }
CHECK(result) << "Failed to initialize " << renderer_->GetDebugName() CHECK(result) << "Failed to initialize " << renderer_->GetDebugName()
<< " renderer."; << " renderer.";
@ -480,7 +479,7 @@ bool Engine::IsMobile() const {
} }
void Engine::OnWindowCreated() { void Engine::OnWindowCreated() {
InitializeRenderer(); renderer_->Initialize(platform_);
} }
void Engine::OnWindowDestroyed() { void Engine::OnWindowDestroyed() {
@ -491,7 +490,7 @@ void Engine::OnWindowResized(int width, int height) {
if (width != renderer_->screen_width() || if (width != renderer_->screen_width() ||
height != renderer_->screen_height()) { height != renderer_->screen_height()) {
renderer_->Shutdown(); renderer_->Shutdown();
InitializeRenderer(); renderer_->Initialize(platform_);
} }
} }
@ -546,14 +545,6 @@ void Engine::AddInputEvent(std::unique_ptr<InputEvent> event) {
input_queue_.push_back(std::move(event)); input_queue_.push_back(std::move(event));
} }
bool Engine::InitializeRenderer() {
#if defined(__ANDROID__)
return renderer_->Initialize(platform_->GetWindow());
#elif defined(__linux__)
return renderer_->Initialize(platform_->GetDisplay(), platform_->GetWindow());
#endif
}
void Engine::CreateTextureCompressors() { void Engine::CreateTextureCompressors() {
tex_comp_alpha_.reset(); tex_comp_alpha_.reset();
tex_comp_opaque_.reset(); tex_comp_opaque_.reset();

View File

@ -231,8 +231,6 @@ class Engine : public PlatformObserver {
void GainedFocus(bool from_interstitial_ad) final; void GainedFocus(bool from_interstitial_ad) final;
void AddInputEvent(std::unique_ptr<InputEvent> event) final; void AddInputEvent(std::unique_ptr<InputEvent> event) final;
bool InitializeRenderer();
void CreateTextureCompressors(); void CreateTextureCompressors();
void ContextLost(); void ContextLost();

View File

@ -338,6 +338,8 @@ void Platform::HandleCmd(android_app* app, int32_t cmd) {
} }
Platform::Platform(android_app* app) { Platform::Platform(android_app* app) {
LOG << "Initializing platform.";
app_ = app; app_ = app;
mobile_device_ = true; mobile_device_ = true;

View File

@ -14,6 +14,8 @@ namespace eng {
void KaliberMain(Platform* platform); void KaliberMain(Platform* platform);
Platform::Platform() { Platform::Platform() {
LOG << "Initializing platform.";
root_path_ = "../../"; root_path_ = "../../";
LOG << "Root path: " << root_path_.c_str(); LOG << "Root path: " << root_path_.c_str();

View File

@ -40,8 +40,9 @@ const std::string kAttributeNames[eng::kAttribType_Max] = {
namespace eng { namespace eng {
#ifdef THREADED_RENDERING #ifdef THREADED_RENDERING
RendererOpenGL::RendererOpenGL() RendererOpenGL::RendererOpenGL(base::Closure context_lost_cb)
: main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()) {} : Renderer(context_lost_cb),
main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()) {}
#else #else
RendererOpenGL::RendererOpenGL() = default; RendererOpenGL::RendererOpenGL() = default;
#endif // THREADED_RENDERING #endif // THREADED_RENDERING

View File

@ -37,15 +37,10 @@ struct RenderCommand;
class RendererOpenGL final : public Renderer { class RendererOpenGL final : public Renderer {
public: public:
RendererOpenGL(); RendererOpenGL(base::Closure context_lost_cb);
~RendererOpenGL() final; ~RendererOpenGL() final;
#if defined(__ANDROID__) virtual bool Initialize(Platform* platform) final;
bool Initialize(ANativeWindow* window) final;
#elif defined(__linux__)
bool Initialize(Display* display, Window window) final;
#endif
void Shutdown() final; void Shutdown() final;
uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final; uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final;

View File

@ -3,14 +3,15 @@
#include <android/native_window.h> #include <android/native_window.h>
#include "base/log.h" #include "base/log.h"
#include "engine/platform/platform.h"
#include "third_party/android/GLContext.h" #include "third_party/android/GLContext.h"
namespace eng { namespace eng {
bool RendererOpenGL::Initialize(ANativeWindow* window) { bool RendererOpenGL::Initialize(Platform* platform) {
LOG << "Initializing renderer."; LOG << "Initializing renderer.";
window_ = window; window_ = platform->GetWindow();
return StartRenderThread(); return StartRenderThread();
} }

View File

@ -1,14 +1,15 @@
#include "engine/renderer/opengl/renderer_opengl.h" #include "engine/renderer/opengl/renderer_opengl.h"
#include "base/log.h" #include "base/log.h"
#include "engine/platform/platform.h"
namespace eng { namespace eng {
bool RendererOpenGL::Initialize(Display* display, Window window) { bool RendererOpenGL::Initialize(Platform* platform) {
LOG << "Initializing renderer."; LOG << "Initializing renderer.";
display_ = display; display_ = platform->GetDisplay();
window_ = window; window_ = platform->GetWindow();
XWindowAttributes xwa; XWindowAttributes xwa;
XGetWindowAttributes(display_, window_, &xwa); XGetWindowAttributes(display_, window_, &xwa);
@ -24,8 +25,7 @@ bool RendererOpenGL::InitInternal() {
GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, GLint glx_attributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER,
None}; None};
XVisualInfo* visual_info = glXChooseVisual(display_, 0, glx_attributes); XVisualInfo* visual_info = glXChooseVisual(display_, 0, glx_attributes);
glx_context_ = glx_context_ = glXCreateContext(display_, visual_info, 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;

View File

@ -4,40 +4,26 @@
#include <memory> #include <memory>
#include <string> #include <string>
#if defined(__linux__) && !defined(__ANDROID__)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif
#include "base/closure.h" #include "base/closure.h"
#include "base/vecmath.h" #include "base/vecmath.h"
#include "engine/renderer/renderer_types.h" #include "engine/renderer/renderer_types.h"
#if defined(__ANDROID__)
struct ANativeWindow;
#endif
namespace eng { namespace eng {
class Image; class Image;
class ShaderSource; class ShaderSource;
class Mesh; class Mesh;
class Platform;
class Renderer { class Renderer {
public: public:
const unsigned kInvalidId = 0; const unsigned kInvalidId = 0;
Renderer() = default; Renderer(base::Closure context_lost_cb)
: context_lost_cb_{std::move(context_lost_cb)} {}
virtual ~Renderer() = default; virtual ~Renderer() = default;
void SetContextLostCB(base::Closure cb) { context_lost_cb_ = std::move(cb); } virtual bool Initialize(Platform* platform) = 0;
#if defined(__ANDROID__)
virtual bool Initialize(ANativeWindow* window) = 0;
#elif defined(__linux__)
virtual bool Initialize(Display* display, Window window) = 0;
#endif
virtual void Shutdown() = 0; virtual void Shutdown() = 0;
virtual uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) = 0; virtual uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) = 0;

View File

@ -370,7 +370,8 @@ std::pair<int, int> GetNumBlocksForImageFormat(VkFormat format,
namespace eng { namespace eng {
RendererVulkan::RendererVulkan() = default; RendererVulkan::RendererVulkan(base::Closure context_lost_cb)
: Renderer(context_lost_cb) {}
RendererVulkan::~RendererVulkan() { RendererVulkan::~RendererVulkan() {
Shutdown(); Shutdown();

View File

@ -18,19 +18,12 @@
namespace eng { namespace eng {
class Image;
class RendererVulkan final : public Renderer { class RendererVulkan final : public Renderer {
public: public:
RendererVulkan(); RendererVulkan(base::Closure context_lost_cb);
~RendererVulkan() final; ~RendererVulkan() final;
#if defined(__ANDROID__) virtual bool Initialize(Platform* platform) final;
bool Initialize(ANativeWindow* window) final;
#elif defined(__linux__)
bool Initialize(Display* display, Window window) final;
#endif
void Shutdown() final; void Shutdown() final;
uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final; uint64_t CreateGeometry(std::unique_ptr<Mesh> mesh) final;

View File

@ -3,20 +3,22 @@
#include <android/native_window.h> #include <android/native_window.h>
#include "base/log.h" #include "base/log.h"
#include "engine/platform/platform.h"
namespace eng { namespace eng {
bool RendererVulkan::Initialize(ANativeWindow* window) { bool RendererVulkan::Initialize(Platform* platform) {
LOG << "Initializing renderer."; LOG << "Initializing renderer.";
screen_width_ = ANativeWindow_getWidth(window); screen_width_ = ANativeWindow_getWidth(platform->GetWindow());
screen_height_ = ANativeWindow_getHeight(window); screen_height_ = ANativeWindow_getHeight(platform->GetWindow());
if (!context_.Initialize()) { if (!context_.Initialize()) {
LOG << "Failed to initialize Vulkan context."; LOG << "Failed to initialize Vulkan context.";
return false; return false;
} }
if (!context_.CreateWindow(window, screen_width_, screen_height_)) { if (!context_.CreateWindow(platform->GetWindow(), screen_width_,
screen_height_)) {
LOG << "Vulkan context failed to create window."; LOG << "Vulkan context failed to create window.";
return false; return false;
} }

View File

@ -1,14 +1,15 @@
#include "engine/renderer/vulkan/renderer_vulkan.h" #include "engine/renderer/vulkan/renderer_vulkan.h"
#include "base/log.h" #include "base/log.h"
#include "engine/platform/platform.h"
namespace eng { namespace eng {
bool RendererVulkan::Initialize(Display* display, Window window) { bool RendererVulkan::Initialize(Platform* platform) {
LOG << "Initializing renderer."; LOG << "Initializing renderer.";
XWindowAttributes xwa; XWindowAttributes xwa;
XGetWindowAttributes(display, window, &xwa); XGetWindowAttributes(platform->GetDisplay(), platform->GetWindow(), &xwa);
screen_width_ = xwa.width; screen_width_ = xwa.width;
screen_height_ = xwa.height; screen_height_ = xwa.height;
@ -16,7 +17,8 @@ bool RendererVulkan::Initialize(Display* display, Window window) {
LOG << "Failed to initialize Vulkan context."; LOG << "Failed to initialize Vulkan context.";
return false; return false;
} }
if (!context_.CreateWindow(display, window, screen_width_, screen_height_)) { if (!context_.CreateWindow(platform->GetDisplay(), platform->GetWindow(),
screen_width_, screen_height_)) {
LOG << "Vulkan context failed to create window."; LOG << "Vulkan context failed to create window.";
return false; return false;
} }