mraphics_core/render/
pipeline_manager.rs1use std::collections::HashMap;
2
3use crate::MaterialView;
4
5pub struct PipelineManager {
6 pub pipeline_pool: HashMap<String, wgpu::RenderPipeline>,
7}
8
9impl PipelineManager {
10 pub fn new() -> Self {
11 Self {
12 pipeline_pool: HashMap::new(),
13 }
14 }
15
16 pub fn acquire_pipeline(
17 &mut self,
18 device: &wgpu::Device,
19 texture_format: wgpu::TextureFormat,
20 material: &MaterialView,
21 bind_groups: &[&wgpu::BindGroupLayout],
22 force_update: bool,
23 ) -> &wgpu::RenderPipeline {
24 let pipeline_identifier = &material.identifier;
25
26 if !self.pipeline_pool.contains_key(pipeline_identifier) || force_update {
27 let shader_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
28 label: Some("Mraphics Shader"),
29 source: wgpu::ShaderSource::Wgsl((&material.shader_code).into()),
30 });
31
32 let render_pipeline_layout =
33 device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
34 label: Some("Mraphics Render Pipeline Layout"),
35 bind_group_layouts: bind_groups,
36 push_constant_ranges: &[],
37 });
38
39 let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
40 label: Some("Mraphics Render Pipeline"),
41 layout: Some(&render_pipeline_layout),
42 vertex: wgpu::VertexState {
43 module: &shader_module,
44 entry_point: Some("vs"),
45 buffers: &[],
46 compilation_options: wgpu::PipelineCompilationOptions::default(),
47 },
48 fragment: Some(wgpu::FragmentState {
49 module: &shader_module,
50 entry_point: Some("fs"),
51 targets: &[Some(wgpu::ColorTargetState {
52 format: texture_format,
53 blend: Some(wgpu::BlendState::REPLACE),
54 write_mask: wgpu::ColorWrites::ALL,
55 })],
56 compilation_options: wgpu::PipelineCompilationOptions::default(),
57 }),
58 primitive: wgpu::PrimitiveState {
59 topology: wgpu::PrimitiveTopology::TriangleList,
60 strip_index_format: None,
61 front_face: wgpu::FrontFace::Ccw,
62 cull_mode: None,
63 polygon_mode: wgpu::PolygonMode::Fill,
64 unclipped_depth: false,
65 conservative: false,
66 },
67 depth_stencil: None,
68 multisample: wgpu::MultisampleState::default(),
69 multiview: None,
70 cache: None,
71 });
72
73 self.pipeline_pool
74 .insert(String::from(&material.identifier), render_pipeline);
75 }
76
77 self.pipeline_pool.get(pipeline_identifier).unwrap()
79 }
80}