diff --git a/src/render/wgpu_context.cpp b/src/render/wgpu_context.cpp index 453beb7..b3fa79f 100644 --- a/src/render/wgpu_context.cpp +++ b/src/render/wgpu_context.cpp @@ -6,7 +6,7 @@ #include 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() { diff --git a/src/render/wgpu_context.h b/src/render/wgpu_context.h index 41b8ae8..cfe38a6 100644 --- a/src/render/wgpu_context.h +++ b/src/render/wgpu_context.h @@ -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_; }; } \ No newline at end of file diff --git a/src/window/window.cpp b/src/window/window.cpp index e13d6c8..e0c38b1 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -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);