From aa9398bad9f16b8c4ee85cf985b43f8d7a8aa238 Mon Sep 17 00:00:00 2001 From: Nanako <469449812@qq.com> Date: Thu, 6 Feb 2025 09:30:53 +0800 Subject: [PATCH] t --- example/src/main.cpp | 14 ++-- src/core/CMakeLists.txt | 35 ---------- src/core/aorii.cpp | 59 +++++++++++++++++ src/core/aorii.h | 16 +++++ src/core/renderer/device_selector.h | 2 + src/core/renderer/renderer.cpp | 92 +++++++------------------- src/core/renderer/renderer.h | 7 +- src/core/renderer/renderer_buffer.cpp | 5 ++ src/core/renderer/renderer_context.cpp | 4 +- src/core/renderer/renderer_text.cpp | 50 ++++++++++---- src/core/renderer/renderer_text.h | 24 +++---- src/core/renderer/renderer_types.h | 2 + src/core/window/window_manager.cpp | 14 +++- src/core/window/window_manager.h | 2 +- 14 files changed, 179 insertions(+), 147 deletions(-) create mode 100644 src/core/aorii.cpp create mode 100644 src/core/aorii.h diff --git a/example/src/main.cpp b/example/src/main.cpp index b366361..823825b 100644 --- a/example/src/main.cpp +++ b/example/src/main.cpp @@ -1,11 +1,4 @@ - - -#include - -#include "async/thread_pool.h" -#include "renderer/renderer.h" -#include "window/window_manager.h" -#include "window/renderer_window.h" +#include "aorii.h" int main(int argc, char* argv[]) { window_desc desc{}; @@ -13,5 +6,8 @@ int main(int argc, char* argv[]) { desc.resolution.width = 1280; desc.resolution.height = 720; desc.resizable = true; - return aorii::run(desc); + + aorii::init_info init{}; + init.main_window_desc = desc; + return run(init); } diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index fa0a8b7..bce8a3e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -26,41 +26,6 @@ if (APPLE) target_link_libraries(${PROJECT_NAME} PUBLIC ${COCOA_LIBRARY}) endif () - - -if (LLGL_BUILD_RENDERER_NULL) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_Null) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_NULL) -endif () -if (LLGL_BUILD_RENDERER_OPENGL) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_OpenGL) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_OPENGL) -endif () -if (LLGL_BUILD_RENDERER_OPENGL_ES3) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_OpenGLES3) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_OPENGL_ES3) -endif () -if (LLGL_BUILD_RENDERER_WEBGL) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_WebGL) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_WEBGL) -endif () -if (LLGL_BUILD_RENDERER_DIRECT3D11) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_Direct3D11) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_DIRECT3D11) -endif () -if (LLGL_BUILD_RENDERER_DIRECT3D12) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_Direct3D12) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_DIRECT3D12) -endif () -if (LLGL_BUILD_RENDERER_VULKAN) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_Vulkan) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_VULKAN) -endif () -if (LLGL_BUILD_RENDERER_METAL) - target_link_libraries(${PROJECT_NAME} PUBLIC LLGL_Metal) - target_compile_definitions(${PROJECT_NAME} PRIVATE LLGL_BUILD_RENDERER_METAL) -endif () - # 添加编译shader的自定义命令 if (NOT DEFINED SHADER_OUTPUT_DIR) set(SHADER_OUTPUT_DIR ${CMAKE_BINARY_DIR}/shaders CACHE PATH "Output directory for compiled shaders") diff --git a/src/core/aorii.cpp b/src/core/aorii.cpp new file mode 100644 index 0000000..a47e2ae --- /dev/null +++ b/src/core/aorii.cpp @@ -0,0 +1,59 @@ +#include "aorii.h" + +#include "async/thread_pool.h" +#include "renderer/renderer_text.h" +#include "window/window_manager.h" + +using time_type = decltype(std::chrono::high_resolution_clock::now()); +std::chrono::duration delta_time = {}; +time_type begin_time = {}; +time_type last_time = {}; + +namespace aorii { + bool init(const window_desc& in_main_window_desc) { + begin_time = std::chrono::high_resolution_clock::now(); + if (!create_font_manager()) + return false; + if (!create_window_manager()) + return false; + const auto main_window = get_window_manager()->create_window(in_main_window_desc); + if (!create_renderer()) + return false; + last_time = std::chrono::high_resolution_clock::now(); + return true; + } + + void destroy() { + destroy_window_manager(); + destroy_renderer(); + destroy_font_manager(); + } + + void update() { + thread_pool::global().process_main_thread_callbacks(); + + const auto& current_time = std::chrono::high_resolution_clock::now(); + delta_time = current_time - last_time; + last_time = current_time; + + get_window_manager()->update(); + + std::this_thread::yield(); + } + + int run(const init_info& in_init_info) { + if (!init(in_init_info.main_window_desc)) + return -1; + while (!should_exit()) { + update(); + } + destroy(); + return 0; + } + + const std::chrono::duration& get_delta_time() { return delta_time; } + + std::chrono::duration get_total_time() { + return std::chrono::high_resolution_clock::now() - begin_time; + } +} diff --git a/src/core/aorii.h b/src/core/aorii.h new file mode 100644 index 0000000..d2f7772 --- /dev/null +++ b/src/core/aorii.h @@ -0,0 +1,16 @@ +#pragma once +#include "window/renderer_window.h" + +#define AORII_VERSION_MAJOR 0 +#define AORII_VERSION_MINOR 1 +#define AORII_VERSION_PATCH 0 + +namespace aorii { + struct init_info { + window_desc main_window_desc{}; + }; + int run(const init_info& in_init_info); + + const std::chrono::duration& get_delta_time(); + std::chrono::duration get_total_time(); +} diff --git a/src/core/renderer/device_selector.h b/src/core/renderer/device_selector.h index 5b1c9f6..dae9e8e 100644 --- a/src/core/renderer/device_selector.h +++ b/src/core/renderer/device_selector.h @@ -111,6 +111,8 @@ private: } score += static_cast(total_memory / (1024 * 1024 * 1024)); // 每GB加1分 + std::string device_name = properties.deviceName; + spdlog::info("{}: {}分", device_name, score); return score; } diff --git a/src/core/renderer/renderer.cpp b/src/core/renderer/renderer.cpp index 72ce85d..026fc84 100644 --- a/src/core/renderer/renderer.cpp +++ b/src/core/renderer/renderer.cpp @@ -5,15 +5,12 @@ #include "window/renderer_window.h" #define STB_IMAGE_IMPLEMENTATION #define STBI_FAILURE_USERMSG +#include "aorii.h" #include "renderer_text.h" #include "async/thread_pool.h" #include "window/window_manager.h" #include "misc/stb_image.h" -using time_type = decltype(std::chrono::high_resolution_clock::now()); -std::chrono::duration delta_time = {}; -time_type begin_time = {}; -time_type last_time = {}; aorii_renderer* s_renderer{}; // renderer_texture* renderer::load_image(const std::string& file_path, texture_format in_format) { @@ -69,52 +66,11 @@ aorii_renderer* s_renderer{}; // return texture; // } -namespace aorii { - bool init(const window_desc& in_main_window_desc) { - if (!create_window_manager()) - return false; - const auto main_window = get_window_manager()->create_window(in_main_window_desc); - if (!create_renderer()) - return false; - return true; - } - - void destroy() { - destroy_renderer(); - destroy_window_manager(); - } - - void update() { - thread_pool::global().process_main_thread_callbacks(); - - const auto& current_time = std::chrono::high_resolution_clock::now(); - delta_time = current_time - last_time; - last_time = current_time; - - get_window_manager()->update(); - - std::this_thread::yield(); - } -} - -int aorii::run(const window_desc& in_main_window_desc) { - if (!init(in_main_window_desc)) - return -1; - while (!should_exit()) { - update(); - } - destroy(); - return 0; -} - aorii_renderer* aorii::get_renderer() { return s_renderer; } bool aorii::create_renderer() { - if (!aorii_text::init_freetype()) - return false; - begin_time = std::chrono::high_resolution_clock::now(); if (s_renderer) return true; s_renderer = new aorii_renderer(); @@ -124,7 +80,6 @@ bool aorii::create_renderer() { spdlog::error("创建渲染器失败"); return false; } - last_time = std::chrono::high_resolution_clock::now(); return s_renderer != nullptr; } @@ -133,42 +88,45 @@ void aorii::destroy_renderer() { s_renderer->destroy(); delete s_renderer; - aorii_text::destroy_freetype(); -} - -const std::chrono::duration& aorii::get_delta_time() { - return delta_time; -} - -std::chrono::duration aorii::get_total_time() { - return std::chrono::high_resolution_clock::now() - begin_time; } bool aorii_renderer::init() { spdlog::info("初始化渲染器"); + uint32_t vulkan_api_version = 0; + + if (vk::enumerateInstanceVersion(&vulkan_api_version) != vk::Result::eSuccess) { + spdlog::warn("无法获取 Vulkan 版本号, 将使用默认版本 1.0.0"); + vulkan_api_version = VK_API_VERSION_1_0; + } + vk::ApplicationInfo app_info{}; app_info.setPEngineName("Aorii"); - app_info.setEngineVersion(VK_MAKE_VERSION(1, 0, 0)); - app_info.setApiVersion(VK_API_VERSION_1_0); + app_info.setEngineVersion(VK_MAKE_VERSION(AORII_VERSION_MAJOR, AORII_VERSION_MINOR, AORII_VERSION_PATCH)); + app_info.setApiVersion(vulkan_api_version); app_info.setPApplicationName("AoriiHelloWorld"); app_info.setApplicationVersion(VK_MAKE_VERSION(1, 0, 0)); + spdlog::info("Vulkan 版本: {}.{}.{}", VK_VERSION_MAJOR(app_info.apiVersion), VK_VERSION_MINOR(app_info.apiVersion), VK_VERSION_PATCH(app_info.apiVersion)); vk::InstanceCreateInfo create_info{}; create_info.setPApplicationInfo(&app_info); std::vector extensions; + { // #if DEBUG -// extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); +// extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); // #endif + uint32_t glfw_extension_count = 0; + auto glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count); + for (int i = 0; i < glfw_extension_count; ++i) { + extensions.push_back(glfw_extensions[i]); + } + for (auto extension: extensions) { + spdlog::info("启用扩展: {}", extension); + } - uint32_t glfw_extension_count = 0; - auto glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count); - for (int i = 0; i < glfw_extension_count; ++i) { - extensions.push_back(glfw_extensions[i]); + create_info.setPEnabledExtensionNames(extensions); } - create_info.setPEnabledExtensionNames(extensions); - instance = createInstance(create_info); if (!instance) { spdlog::critical("创建 Vulkan 实例失败"); @@ -185,6 +143,7 @@ bool aorii_renderer::init() { vk::DeviceCreateInfo device_create_info{}; device_create_info.setPEnabledExtensionNames(device_extensions); main_device.create_device(selector.get_main_device(), device_create_info); + spdlog::info("Vulkan 主设备: {}", main_device.get_physical_device_name()); vk::SwapchainCreateInfoKHR swap_chain_create_info{}; swap_chain_create_info.setMinImageCount(2); @@ -206,16 +165,15 @@ bool aorii_renderer::init() { void aorii_renderer::destroy() { instance.destroy(); - glfwTerminate(); } -vk::SurfaceKHR aorii_renderer::create_surface(GLFWwindow* in_window) { +vk::SurfaceKHR aorii_renderer::create_surface(GLFWwindow* in_window) const { VkSurfaceKHR surface; if (instance && glfwCreateWindowSurface(instance, in_window, nullptr, &surface) != VK_SUCCESS) { spdlog::critical("创建窗口表面失败"); return nullptr; } - return vk::SurfaceKHR(surface); + return { surface }; } vk::SwapchainKHR aorii_renderer::create_swap_chain(const vk::SwapchainCreateInfoKHR& in_create_info) { diff --git a/src/core/renderer/renderer.h b/src/core/renderer/renderer.h index 2155a60..31a270f 100644 --- a/src/core/renderer/renderer.h +++ b/src/core/renderer/renderer.h @@ -14,7 +14,7 @@ public: bool init(); void destroy(); - vk::SurfaceKHR create_surface(GLFWwindow* in_window); + vk::SurfaceKHR create_surface(GLFWwindow* in_window) const; vk::SwapchainKHR create_swap_chain(const vk::SwapchainCreateInfoKHR& in_create_info); private: vk::Instance instance; @@ -23,16 +23,11 @@ private: }; namespace aorii { - int run(const window_desc& in_main_window_desc); - aorii_renderer* get_renderer(); bool create_renderer(); void destroy_renderer(); - const std::chrono::duration& get_delta_time(); - std::chrono::duration get_total_time(); - inline std::filesystem::path s_shader_relative_path = "resource/shaders"; inline void set_shader_relative_path(const std::filesystem::path& path) { s_shader_relative_path = path; } inline std::filesystem::path get_shader_path(const std::string& shader_name) { return std::filesystem::current_path() / s_shader_relative_path / shader_name; } diff --git a/src/core/renderer/renderer_buffer.cpp b/src/core/renderer/renderer_buffer.cpp index af9fb40..c456e51 100644 --- a/src/core/renderer/renderer_buffer.cpp +++ b/src/core/renderer/renderer_buffer.cpp @@ -7,6 +7,11 @@ uint32_t device_set::get_physical_device_id() const { return props.deviceID; } +std::string device_set::get_physical_device_name() const { + const auto& props = physical_device.getProperties(); + return props.deviceName; +} + renderer_buffer_desc::operator vk::BufferCreateInfo() const { vk::BufferCreateInfo buffer_info; buffer_info.size = size; diff --git a/src/core/renderer/renderer_context.cpp b/src/core/renderer/renderer_context.cpp index 78b0977..0ea2c1f 100644 --- a/src/core/renderer/renderer_context.cpp +++ b/src/core/renderer/renderer_context.cpp @@ -6,9 +6,9 @@ #include "pipeline/segment_pipeline.h" #include "pipeline/texture_pipeline.h" -aorii_text* text = nullptr; +aorii_font_manager* text = nullptr; void renderer_context::init() { - text = new aorii_text(); + text = new aorii_font_manager(); // D:\Projects\aorii\JetBrainsMono-Regular.ttf // text->initialize(LR"(C:\Windows\Fonts\Deng.ttf)"); // text->add_font(LR"(C:\Windows\Fonts\seguiemj.ttf)"); diff --git a/src/core/renderer/renderer_text.cpp b/src/core/renderer/renderer_text.cpp index 15fcfd1..26bed62 100644 --- a/src/core/renderer/renderer_text.cpp +++ b/src/core/renderer/renderer_text.cpp @@ -1,30 +1,54 @@ #include "renderer_text.h" -#include -#include #include -#include "renderer.h" - -#include "misc/scope_exit.h" - // 在SDFFontCache.cpp中添加实现 -const std::u32string aorii_text::COMMON_ASCII = +const std::u32string aorii_font_manager::COMMON_ASCII = U"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; -const std::u32string aorii_text::COMMON_CHINESE = +const std::u32string aorii_font_manager::COMMON_CHINESE = U"的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进着等部度家电力里如水化高自二理起小物现实加量都两体制机当使点从业本去把性好应开它合还因由其些然前外天政四日那社义事平形相全表间样与关各重新线内数正心反你明看原又么利比或但质气第向道命此变条只没结解问意建月公无系军很情者最立代想已通并提直题党程展五果料象员革位入常文总次品式活设及管特件长求老头基资边流路级少图山统接知较将组见计别她手角期根论运农指几九区强放决西被干做必战先回则任取据处队南给色光门即保治北造百规热领七海口东导器压志世金增争济阶油思术极交受联什认六共权收证改清己美再采转更单风切打白教速花带安场身车例真务具万每目至达走积示议声报斗完类八离华名确才科张信马节话米整空元况今集温传土许步群广石记需段研界拉林律叫且究观越织装影算低持音众书布复容儿须际商非验连断深难近矿千周委素技备半办青省列习响约支般史感劳便团往酸历市克何除消构府称太准精值号率族维划选标写存候毛亲快效斯院查江型眼王按格养易置派层片始却专状育厂京识适属圆包火住调满县局照参红细引听该铁价严"; -const std::u32string aorii_text::COMMON_PUNCTUATION = +const std::u32string aorii_font_manager::COMMON_PUNCTUATION = U",。!?:;''""《》【】()、"; -const std::u32string aorii_text::COMMON_NUMBERS = +const std::u32string aorii_font_manager::COMMON_NUMBERS = U"0123456789"; -bool aorii_text::init_freetype() { +aorii_font_manager* s_font_manager = nullptr; + +aorii_font_manager::aorii_font_manager() { +} + +aorii_font_manager::~aorii_font_manager() { + FT_Done_FreeType(library); +} + +bool aorii_font_manager::init() { + const FT_Error error = FT_Init_FreeType(&library); + if (error) { + spdlog::error("初始化FreeType失败: {}", FT_Error_String(error)); + return false; + } return true; } -void aorii_text::destroy_freetype() { - +bool aorii::create_font_manager() { + s_font_manager = new aorii_font_manager(); + if (!s_font_manager->init()) { + delete s_font_manager; + s_font_manager = nullptr; + spdlog::error("初始化文本系统失败"); + return false; + } + return true; +} + +void aorii::destroy_font_manager() { + delete s_font_manager; + s_font_manager = nullptr; +} + +aorii_font_manager* aorii::get_font_manager() { + return s_font_manager; } diff --git a/src/core/renderer/renderer_text.h b/src/core/renderer/renderer_text.h index abc4396..a0ad315 100644 --- a/src/core/renderer/renderer_text.h +++ b/src/core/renderer/renderer_text.h @@ -1,19 +1,13 @@ #pragma once -#include "../misc/pixel.h" #include "misc/mapped_file.h" -#include +#include -class renderer_texture_array; - -class aorii_text { +class aorii_font_manager { public: - aorii_text(); + aorii_font_manager(); + ~aorii_font_manager(); - ~aorii_text(); - - static bool init_freetype(); - - static void destroy_freetype(); + bool init(); // 预缓存一组字符 bool precache_characters(const std::u32string &characters) { @@ -28,7 +22,7 @@ public: precache_characters(COMMON_CHINESE); } private: - renderer_texture_array *texture_array; + FT_Library library; // 常用字符集定义 static const std::u32string COMMON_ASCII; // ASCII字符 @@ -36,3 +30,9 @@ private: static const std::u32string COMMON_NUMBERS; // 数字 static const std::u32string COMMON_CHINESE; // 常用汉字 }; + +namespace aorii { + bool create_font_manager(); + void destroy_font_manager(); + aorii_font_manager* get_font_manager(); +} diff --git a/src/core/renderer/renderer_types.h b/src/core/renderer/renderer_types.h index b7fc485..317475a 100644 --- a/src/core/renderer/renderer_types.h +++ b/src/core/renderer/renderer_types.h @@ -20,6 +20,8 @@ struct device_set { } // 获取物理设备GUID [[nodiscard]] uint32_t get_physical_device_id() const; + // 获取物理设备名称 + [[nodiscard]] std::string get_physical_device_name() const; }; struct renderer_buffer_desc { diff --git a/src/core/window/window_manager.cpp b/src/core/window/window_manager.cpp index a7b6c65..33619cf 100644 --- a/src/core/window/window_manager.cpp +++ b/src/core/window/window_manager.cpp @@ -8,21 +8,31 @@ window_manager* s_window_manager; +void glfw_error_callback(int error, const char* description) { + spdlog::error("GLFW错误: {}, {}", error, description); +} + window_manager::~window_manager() { windows.clear(); } bool window_manager::init() { + glfwSetErrorCallback(glfw_error_callback); + auto glfw_version = glfwGetVersionString(); + spdlog::info("GLFW 版本: {}", glfw_version); if (glfwInit() != GLFW_TRUE) { - spdlog::error("初始化GLFW失败"); + spdlog::error("初始化 GLFW 失败"); return false; } glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + spdlog::info("初始化 GLFW 成功"); return true; } void window_manager::destroy() { - + windows.clear(); + glfwTerminate(); + spdlog::info("销毁 GLFW"); } std::weak_ptr window_manager::create_window(const window_desc& in_desc) { diff --git a/src/core/window/window_manager.h b/src/core/window/window_manager.h index 2235cc5..561f9eb 100644 --- a/src/core/window/window_manager.h +++ b/src/core/window/window_manager.h @@ -17,7 +17,7 @@ public: std::weak_ptr get_main_window() { return windows.empty() ? std::weak_ptr() : windows.front(); } - const auto& get_windows() const { return windows; } + [[nodiscard]] const auto& get_windows() const { return windows; } void update(); [[nodiscard]] bool should_exit() const {