AronaCore/core/rhi/renderer.h
2024-02-27 23:24:40 +08:00

89 lines
2.5 KiB
C++

#pragma once
#include "imgui_impl_vulkan.h"
#include "imgui.h"
#include "GLFW/glfw3.h"
#include <vulkan/vulkan.hpp>
// #ifdef _DEBUG
// #define APP_USE_VULKAN_DEBUG_REPORT
// #endif
#define APP_USE_UNLIMITED_FRAME_RATE
class render_target;
class texture;
constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
constexpr float clear_color_with_alpha[4] = {
clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w
};
static void check_vk_result(vk::Result err) {
if (err == vk::Result::eSuccess)
return;
spdlog::error("[vulkan] Error: VkResult = {}", vk::to_string(err));
abort();
}
static void check_vk_result(VkResult err) {
if (err == VK_SUCCESS)
return;
if (err < 0) {
spdlog::error("[vulkan] Error: VkResult = {}", (int)err);
abort();
}
}
class renderer {
public:
virtual ~renderer() = default;
void pre_init();
bool init(GLFWwindow* window_handle);
virtual void shutdown();
void new_frame(GLFWwindow* window_handle);
void end_frame(GLFWwindow* window_handle);
static CORE_API std::shared_ptr<texture> create_texture(const uint8_t* data, int width, int height, vk::Format format);
void set_vsync(const bool vsync) { vsync_ = vsync; }
vk::AllocationCallbacks* allocator = nullptr;
vk::Instance instance = VK_NULL_HANDLE;
vk::PhysicalDevice physical_device = VK_NULL_HANDLE;
vk::Device device = VK_NULL_HANDLE;
uint32_t queue_family = (uint32_t) -1;
vk::Queue queue = VK_NULL_HANDLE;
vk::DescriptorPool descriptor_pool = VK_NULL_HANDLE;
#ifdef APP_USE_VULKAN_DEBUG_REPORT
VkDebugReportCallbackEXT debug_report = VK_NULL_HANDLE;
#endif
[[nodiscard]] vk::CommandPool get_command_pool() const;
[[nodiscard]] vk::CommandBuffer create_command_buffer(vk::CommandBufferLevel level, bool begin) const;
void end_command_buffer(vk::CommandBuffer command_buffer, bool use_fence = false) const;
ImGui_ImplVulkanH_Window main_window_data;
int min_image_count = 2;
protected:
void init_vulkan(GLFWwindow* window_handle);
[[nodiscard]] vk::PhysicalDevice setup_vulkan_select_physical_device() const;
void setup_vulkan(std::vector<const char*> instance_extensions);
void setup_vulkan_window(VkSurfaceKHR surface, int width, int height);
void cleanup_vulkan() const;
void cleanup_vulkan_window();
void frame_render(ImDrawData* draw_data);
void frame_present();
bool has_initialized_ = false;
bool swap_chain_rebuild_ = false;
bool vsync_ = true;
};