实现测试着色器功能并创建渲染管线

This commit is contained in:
2026-01-19 13:27:37 +08:00
parent b710062afd
commit 25a4df2d1d
3 changed files with 63 additions and 2 deletions

View File

@@ -6,7 +6,7 @@
#include <fstream>
namespace mirai {
void test_shader() {
void wgpu_context::test_shader() {
std::filesystem::path shader_path = R"(F:\Projects\mirai\src\render\test_shader_code.wgsl)";
std::ifstream shader_file(shader_path, std::ios::in);
if (!shader_file.is_open()) {
@@ -25,12 +25,63 @@ namespace mirai {
shader_module_descriptor.label = "Test shader module";
shader_module_descriptor.nextInChain = &wgpu_shader_module_descriptor.chain;
wgpu::ShaderModule shader_module = wgpu_context::instance().get_device().createShaderModule(shader_module_descriptor);
wgpu::ShaderModule shader_module = get_device().createShaderModule(shader_module_descriptor);
if (!shader_module) {
MIRAI_LOG_ERROR("无法创建测试着色器模块");
return;
}
MIRAI_LOG_INFO("测试着色器模块创建成功");
wgpu::ColorTargetState color_target_state{};
color_target_state.setDefault();
color_target_state.format = wgpu::TextureFormat::BGRA8Unorm;
color_target_state.writeMask = wgpu::ColorWriteMask::All;
wgpu::PrimitiveState primitive_state{};
primitive_state.setDefault();
primitive_state.topology = wgpu::PrimitiveTopology::TriangleList;
primitive_state.frontFace = wgpu::FrontFace::CCW;
primitive_state.cullMode = wgpu::CullMode::None;
wgpu::MultisampleState multisample_state{};
multisample_state.setDefault();
multisample_state.count = 1;
multisample_state.mask = 0xFFFFFFFF;
multisample_state.alphaToCoverageEnabled = false;
wgpu::VertexState vertex_state{};
vertex_state.setDefault();
vertex_state.module = shader_module;
vertex_state.entryPoint = "vs_main";
wgpu::FragmentState fragment_state{};
fragment_state.setDefault();
fragment_state.module = shader_module;
fragment_state.entryPoint = "fs_main";
fragment_state.targetCount = 1;
fragment_state.targets = &color_target_state;
wgpu::PipelineLayoutDescriptor pipeline_layout_descriptor{};
pipeline_layout_descriptor.setDefault();
pipeline_layout_descriptor.label = "Test pipeline layout";
wgpu::PipelineLayout pipeline_layout = get_device().createPipelineLayout(pipeline_layout_descriptor);
if (!pipeline_layout) {
MIRAI_LOG_ERROR("无法创建测试管线布局");
return;
}
wgpu::RenderPipelineDescriptor render_pipeline_descriptor{};
render_pipeline_descriptor.setDefault();
render_pipeline_descriptor.label = "Test render pipeline";
render_pipeline_descriptor.layout = pipeline_layout;
render_pipeline_descriptor.vertex = vertex_state;
render_pipeline_descriptor.fragment = &fragment_state;
render_pipeline_descriptor.primitive = primitive_state;
render_pipeline_descriptor.multisample = multisample_state;
render_pipeline_ = get_device().createRenderPipeline(render_pipeline_descriptor);
if (!render_pipeline_) {
MIRAI_LOG_ERROR("无法创建测试渲染管线");
return;
}
}
bool wgpu_context::setup() {

View File

@@ -22,9 +22,14 @@ namespace mirai {
auto get_device() {
return wgpu_device_;
}
auto get_test_pipeline() {
return render_pipeline_;
}
private:
void test_shader();
wgpu_context() = default;
wgpu::Instance wgpu_instance_;
wgpu::Device wgpu_device_;
wgpu::RenderPipeline render_pipeline_;
};
}

View File

@@ -48,6 +48,8 @@ namespace mirai {
}
wgpu::raii::CommandEncoder encoder = wgpu_context::instance().get_command_encoder();
wgpu::raii::Queue queue = wgpu_context::instance().get_queue();
auto size = get_framebuffer_size();
auto pipeline = wgpu_context::instance().get_test_pipeline();
auto [texture, texture_view] = acquire_next_swapchain_texture();
wgpu::RenderPassColorAttachment color_attachment{};
@@ -63,7 +65,10 @@ namespace mirai {
render_pass_descriptor.colorAttachments = &color_attachment;
wgpu::raii::RenderPassEncoder render_pass = encoder->beginRenderPass(render_pass_descriptor);
render_pass->setPipeline(pipeline);
render_pass->draw(3, 1, 0, 0);
render_pass->end();
wgpu::raii::CommandBuffer command_buffer = encoder->finish();
queue->submit(*command_buffer);