着色器加载

This commit is contained in:
Nanako 2025-02-06 21:57:07 +08:00
parent b02811f3a7
commit 5708ea706e
10 changed files with 107 additions and 14 deletions

View File

@ -28,6 +28,8 @@ endif ()
# shader
shader_compile_target(${CMAKE_CURRENT_SOURCE_DIR}/shaders)
# , aorii_core, aorii_compile_shaders
add_dependencies(${PROJECT_NAME} aorii_compile_shaders)
# example, shader
if (BUILD_EXAMPLE)

View File

@ -25,7 +25,7 @@ vk::PhysicalDevice device_selector::get_main_device() const {
}
// 如果没有显示设备,则使用计算设备
if (!compute_group.empty()) {
spdlog::warn("没有找到同时支持显示和计算的设备,将使用计算设备");
spdlog::warn("没有找到同时支持显示和计算的设备,将使用计算设备: {}", compute_group[0].getProperties().deviceName.data());
return compute_group[0];
}
return nullptr;
@ -42,7 +42,7 @@ vk::PhysicalDevice device_selector::get_main_compute_device() const {
}
// 如果没有计算设备,则使用显示设备
if (!display_group.empty()) {
spdlog::warn("没有找到计算设备,将使用显示设备");
spdlog::warn("没有找到计算设备,将使用显示设备: {}", display_group[0].getProperties().deviceName.data());
return display_group[0];
}
return nullptr;

View File

@ -6,6 +6,7 @@
#define STB_IMAGE_IMPLEMENTATION
#define STBI_FAILURE_USERMSG
#include "aorii.h"
#include "renderer_shader.h"
#include "renderer_text.h"
#include "async/thread_pool.h"
#include "window/window_manager.h"
@ -179,6 +180,17 @@ bool aorii_renderer::init() {
if (!main_window->create_swap_chain(swapchain_info))
return false;
renderer_shader frag_shader;
frag_shader.load("resource/shaders/test.frag.spv", main_device);
if (!frag_shader.is_valid()) {
return false;
}
renderer_shader vert_shader;
vert_shader.load("resource/shaders/test.vert.spv", main_device);
if (!vert_shader.is_valid()) {
return false;
}
return true;
}

View File

@ -0,0 +1,56 @@
#include "renderer_shader.h"
#include <fstream>
#include <spdlog/spdlog.h>
#include "renderer.h"
renderer_shader::~renderer_shader() {
destroy();
}
bool renderer_shader::load(const std::filesystem::path& in_file_path, vk::Device in_device) {
if (!in_device)
return false;
destroy();
device = in_device;
file_path = in_file_path;
// 读取着色器文件
std::ifstream file(in_file_path.c_str(), std::ios::binary | std::ios::ate);
if (!file.is_open()) {
spdlog::error("无法打开着色器文件: {}", in_file_path.generic_string());
return false;
}
const auto file_size = file.tellg();
std::vector<uint8_t> buffer(file_size);
file.seekg(0);
file.read(reinterpret_cast<char*>(buffer.data()), file_size);
file.close();
auto result = create_shader_module(buffer);
if (!result) {
spdlog::error("创建着色器模块失败");
return false;
}
spdlog::info("加载着色器: {}", in_file_path.generic_string());
return true;
}
void renderer_shader::destroy() {
if (shader_module) {
device.destroyShaderModule(shader_module);
shader_module = nullptr;
spdlog::info("销毁着色器: {}", file_path.generic_string());
}
}
bool renderer_shader::create_shader_module(const std::vector<uint8_t>& in_code) {
vk::ShaderModuleCreateInfo create_info{};
create_info.setCodeSize(in_code.size());
create_info.setPCode(reinterpret_cast<const uint32_t*>(in_code.data()));
shader_module = device.createShaderModule(create_info);
return shader_module;
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <vulkan/vulkan.hpp>
#include <filesystem>
class renderer_shader {
public:
~renderer_shader();
bool load(const std::filesystem::path& in_file_path, vk::Device in_device);
[[nodiscard]] const auto& get_shader_module() const {
return shader_module;
}
[[nodiscard]] const auto& get_device() const {
return device;
}
[[nodiscard]] bool is_valid() const {
return shader_module;
}
private:
void destroy();
bool create_shader_module(const std::vector<uint8_t>& in_code);
vk::ShaderModule shader_module;
vk::Device device;
std::filesystem::path file_path;
};

View File

@ -10,8 +10,8 @@ struct device_handle {
vk::Device* operator->() { return &device; }
const vk::Device* operator->() const { return &device; }
explicit operator vk::PhysicalDevice() const { return physical_device; }
explicit operator vk::Device() const { return device; }
operator vk::PhysicalDevice() const { return physical_device; }
operator vk::Device() const { return device; }
operator bool() const { return static_cast<bool>(device); }
bool operator!() const { return !device; }

View File

@ -9,8 +9,5 @@ struct VSInput {
// 将uv坐标系移动到[-1, -1] ~ [1, 1]
float2 uv_to_ndc(float2 uv) {
#ifdef UV_FLIP
uv.y = 1.0 - uv.y;
#endif
return uv * 2.0 - 1.0;
}

View File

@ -1,7 +1,7 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 fragColor;
layout(location = 0) out vec4 outColor;
void main() {

View File

@ -1,9 +1,5 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
out gl_PerVertex {
vec4 gl_Position;
};
layout(location = 0) out vec3 fragColor;
vec2 positions[3] = vec2[](
@ -11,6 +7,7 @@ vec2 positions[3] = vec2[](
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
@ -20,4 +17,4 @@ vec3 colors[3] = vec3[](
void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
}
}

View File

@ -19,7 +19,7 @@ renderer_window::renderer_window(const window_desc& in_desc) {
window = glfwCreateWindow(in_desc.resolution.width, in_desc.resolution.height, in_desc.title.c_str(), in_desc.monitor, nullptr);
glfwSetWindowUserPointer(window, this);
if (!window) {
spdlog::error("Failed to create window");
spdlog::error("创建 GLFW 窗口失败");
return;
}