Revert "Try Vulkan renderer first, fallback to OpenGL if it fails."

This reverts commit daa3a8c06f.
This commit is contained in:
Attila Uygun 2023-05-16 11:26:57 +02:00
parent be36d121b0
commit 94242fca0b
5 changed files with 39 additions and 32 deletions

View File

@ -12,6 +12,8 @@
#include "engine/audio/audio_driver_alsa.h" #include "engine/audio/audio_driver_alsa.h"
#endif #endif
#define USE_VULKAN_RENDERER 1
using namespace base; using namespace base;
namespace eng { namespace eng {
@ -34,13 +36,11 @@ void Platform::InitializeCommon() {
bool res = audio_driver_->Initialize(); bool res = audio_driver_->Initialize();
CHECK(res) << "Failed to initialize audio driver."; CHECK(res) << "Failed to initialize audio driver.";
auto context = std::make_unique<VulkanContext>(); #if (USE_VULKAN_RENDERER == 1)
if (context->Initialize()) { renderer_ = std::make_unique<RendererVulkan>();
renderer_ = std::make_unique<RendererVulkan>(std::move(context)); #else
} else {
LOG << "Failed to initialize Vulkan context. Fallback to OpenGL.";
renderer_ = std::make_unique<RendererOpenGL>(); renderer_ = std::make_unique<RendererOpenGL>();
} #endif
} }
void Platform::ShutdownCommon() { void Platform::ShutdownCommon() {

View File

@ -370,8 +370,7 @@ std::pair<int, int> GetNumBlocksForImageFormat(VkFormat format,
namespace eng { namespace eng {
RendererVulkan::RendererVulkan(std::unique_ptr<VulkanContext> context) RendererVulkan::RendererVulkan() = default;
: context_{std::move(context)} {}
RendererVulkan::~RendererVulkan() = default; RendererVulkan::~RendererVulkan() = default;
@ -707,7 +706,7 @@ uint64_t RendererVulkan::CreateShader(
pipeline_info.pDepthStencilState = &depth_stencil; pipeline_info.pDepthStencilState = &depth_stencil;
pipeline_info.pDynamicState = &dynamic_state_create_info; pipeline_info.pDynamicState = &dynamic_state_create_info;
pipeline_info.layout = shader.pipeline_layout; pipeline_info.layout = shader.pipeline_layout;
pipeline_info.renderPass = context_->GetRenderPass(); pipeline_info.renderPass = context_.GetRenderPass();
pipeline_info.subpass = 0; pipeline_info.subpass = 0;
pipeline_info.basePipelineHandle = VK_NULL_HANDLE; pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
@ -828,7 +827,7 @@ void RendererVulkan::UploadUniforms(uint64_t resource_id) {
} }
void RendererVulkan::PrepareForDrawing() { void RendererVulkan::PrepareForDrawing() {
context_->PrepareBuffers(); context_.PrepareBuffers();
DrawListBegin(); DrawListBegin();
} }
@ -840,20 +839,20 @@ void RendererVulkan::Present() {
bool RendererVulkan::InitializeInternal() { bool RendererVulkan::InitializeInternal() {
glslang::InitializeProcess(); glslang::InitializeProcess();
device_ = context_->GetDevice(); device_ = context_.GetDevice();
// Allocate one extra frame to ensure it's unused at any time without having // Allocate one extra frame to ensure it's unused at any time without having
// to use a fence. // to use a fence.
int frame_count = context_->GetSwapchainImageCount() + 1; int frame_count = context_.GetSwapchainImageCount() + 1;
frames_.resize(frame_count); frames_.resize(frame_count);
frames_drawn_ = frame_count; frames_drawn_ = frame_count;
// Initialize allocator // Initialize allocator
VmaAllocatorCreateInfo allocator_info; VmaAllocatorCreateInfo allocator_info;
memset(&allocator_info, 0, sizeof(VmaAllocatorCreateInfo)); memset(&allocator_info, 0, sizeof(VmaAllocatorCreateInfo));
allocator_info.physicalDevice = context_->GetPhysicalDevice(); allocator_info.physicalDevice = context_.GetPhysicalDevice();
allocator_info.device = device_; allocator_info.device = device_;
allocator_info.instance = context_->GetInstance(); allocator_info.instance = context_.GetInstance();
vmaCreateAllocator(&allocator_info, &allocator_); vmaCreateAllocator(&allocator_info, &allocator_);
for (size_t i = 0; i < frames_.size(); i++) { for (size_t i = 0; i < frames_.size(); i++) {
@ -861,7 +860,7 @@ bool RendererVulkan::InitializeInternal() {
VkCommandPoolCreateInfo cmd_pool_info; VkCommandPoolCreateInfo cmd_pool_info;
cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
cmd_pool_info.pNext = nullptr; cmd_pool_info.pNext = nullptr;
cmd_pool_info.queueFamilyIndex = context_->GetGraphicsQueue(); cmd_pool_info.queueFamilyIndex = context_.GetGraphicsQueue();
cmd_pool_info.flags = 0; cmd_pool_info.flags = 0;
VkResult err = vkCreateCommandPool(device_, &cmd_pool_info, nullptr, VkResult err = vkCreateCommandPool(device_, &cmd_pool_info, nullptr,
@ -1014,8 +1013,8 @@ void RendererVulkan::Shutdown() {
current_staging_buffer_ = 0; current_staging_buffer_ = 0;
staging_buffer_used_ = false; staging_buffer_used_ = false;
context_->DestroyWindow(); context_.DestroyWindow();
context_->Shutdown(); context_.Shutdown();
glslang::FinalizeProcess(); glslang::FinalizeProcess();
} }
@ -1023,8 +1022,8 @@ void RendererVulkan::Shutdown() {
void RendererVulkan::BeginFrame() { void RendererVulkan::BeginFrame() {
FreePendingResources(current_frame_); FreePendingResources(current_frame_);
context_->AppendCommandBuffer(frames_[current_frame_].setup_command_buffer); context_.AppendCommandBuffer(frames_[current_frame_].setup_command_buffer);
context_->AppendCommandBuffer(frames_[current_frame_].draw_command_buffer); context_.AppendCommandBuffer(frames_[current_frame_].draw_command_buffer);
vkResetCommandPool(device_, frames_[current_frame_].setup_command_pool, 0); vkResetCommandPool(device_, frames_[current_frame_].setup_command_pool, 0);
vkResetCommandPool(device_, frames_[current_frame_].draw_command_pool, 0); vkResetCommandPool(device_, frames_[current_frame_].draw_command_pool, 0);
@ -1063,7 +1062,7 @@ void RendererVulkan::BeginFrame() {
void RendererVulkan::FlushSetupBuffer() { void RendererVulkan::FlushSetupBuffer() {
vkEndCommandBuffer(frames_[current_frame_].setup_command_buffer); vkEndCommandBuffer(frames_[current_frame_].setup_command_buffer);
context_->Flush(false); context_.Flush(false);
vkResetCommandPool(device_, frames_[current_frame_].setup_command_pool, 0); vkResetCommandPool(device_, frames_[current_frame_].setup_command_pool, 0);
@ -1079,7 +1078,7 @@ void RendererVulkan::FlushSetupBuffer() {
DLOG << "vkBeginCommandBuffer failed with error " << string_VkResult(err); DLOG << "vkBeginCommandBuffer failed with error " << string_VkResult(err);
return; return;
} }
context_->AppendCommandBuffer(frames_[current_frame_].setup_command_buffer, context_.AppendCommandBuffer(frames_[current_frame_].setup_command_buffer,
true); true);
} }
@ -1907,8 +1906,8 @@ void RendererVulkan::DrawListBegin() {
VkRenderPassBeginInfo render_pass_begin; VkRenderPassBeginInfo render_pass_begin;
render_pass_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; render_pass_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
render_pass_begin.pNext = nullptr; render_pass_begin.pNext = nullptr;
render_pass_begin.renderPass = context_->GetRenderPass(); render_pass_begin.renderPass = context_.GetRenderPass();
render_pass_begin.framebuffer = context_->GetFramebuffer(); render_pass_begin.framebuffer = context_.GetFramebuffer();
render_pass_begin.renderArea.extent.width = screen_width_; render_pass_begin.renderArea.extent.width = screen_width_;
render_pass_begin.renderArea.extent.height = screen_height_; render_pass_begin.renderArea.extent.height = screen_height_;
@ -1974,7 +1973,7 @@ void RendererVulkan::SwapBuffers() {
vkEndCommandBuffer(frames_[current_frame_].setup_command_buffer); vkEndCommandBuffer(frames_[current_frame_].setup_command_buffer);
vkEndCommandBuffer(frames_[current_frame_].draw_command_buffer); vkEndCommandBuffer(frames_[current_frame_].draw_command_buffer);
context_->SwapBuffers(); context_.SwapBuffers();
current_frame_ = (current_frame_ + 1) % frames_.size(); current_frame_ = (current_frame_ + 1) % frames_.size();
active_pipeline_ = VK_NULL_HANDLE; active_pipeline_ = VK_NULL_HANDLE;
@ -2033,13 +2032,13 @@ bool RendererVulkan::SetUniformInternal(ShaderVulkan& shader,
bool RendererVulkan::IsFormatSupported(VkFormat format) { bool RendererVulkan::IsFormatSupported(VkFormat format) {
VkFormatProperties properties; VkFormatProperties properties;
vkGetPhysicalDeviceFormatProperties(context_->GetPhysicalDevice(), format, vkGetPhysicalDeviceFormatProperties(context_.GetPhysicalDevice(), format,
&properties); &properties);
return properties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT; return properties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
} }
size_t RendererVulkan::GetAndResetFPS() { size_t RendererVulkan::GetAndResetFPS() {
return context_->GetAndResetFPS(); return context_.GetAndResetFPS();
} }
void RendererVulkan::DestroyAllResources() { void RendererVulkan::DestroyAllResources() {

View File

@ -22,7 +22,7 @@ class Image;
class RendererVulkan final : public Renderer { class RendererVulkan final : public Renderer {
public: public:
RendererVulkan(std::unique_ptr<VulkanContext> context); RendererVulkan();
~RendererVulkan() final; ~RendererVulkan() final;
#if defined(__ANDROID__) #if defined(__ANDROID__)
@ -156,7 +156,7 @@ class RendererVulkan final : public Renderer {
bool context_lost_ = false; bool context_lost_ = false;
std::unique_ptr<VulkanContext> context_; VulkanContext context_;
VmaAllocator allocator_ = nullptr; VmaAllocator allocator_ = nullptr;

View File

@ -12,7 +12,11 @@ bool RendererVulkan::Initialize(ANativeWindow* window) {
screen_width_ = ANativeWindow_getWidth(window); screen_width_ = ANativeWindow_getWidth(window);
screen_height_ = ANativeWindow_getHeight(window); screen_height_ = ANativeWindow_getHeight(window);
if (!context_->CreateWindow(window, screen_width_, screen_height_)) { if (!context_.Initialize()) {
LOG << "Failed to initialize Vulkan context.";
return false;
}
if (!context_.CreateWindow(window, screen_width_, screen_height_)) {
LOG << "Vulkan context failed to create window."; LOG << "Vulkan context failed to create window.";
return false; return false;
} }

View File

@ -12,7 +12,11 @@ bool RendererVulkan::Initialize(Display* display, Window window) {
screen_width_ = xwa.width; screen_width_ = xwa.width;
screen_height_ = xwa.height; screen_height_ = xwa.height;
if (!context_->CreateWindow(display, window, screen_width_, screen_height_)) { if (!context_.Initialize()) {
LOG << "Failed to initialize Vulkan context.";
return false;
}
if (!context_.CreateWindow(display, window, screen_width_, screen_height_)) {
LOG << "Vulkan context failed to create window."; LOG << "Vulkan context failed to create window.";
return false; return false;
} }