1use wgpu::{BlendState, BufferBindingType, Device, SurfaceConfiguration};
2use wgpu::ShaderModule;
3use crate::assets::vertex_layout::BufferLayout;
4use crate::assets_manager::asset_manager::AssetManager;
5use crate::assets_manager::handle::Handle;
6
7pub struct RenderPipeline {
8 pub pipeline: wgpu::RenderPipeline,
9 pub material_layout: wgpu::BindGroupLayout,
10}
11
12pub struct RenderPipelineBuilder<'a> {
13 shader: Handle<ShaderModule>,
14 vertex_layouts: Vec<wgpu::VertexBufferLayout<'a>>,
15 pub(crate) depth_format: Option<wgpu::TextureFormat>,
16 depth_writes_enabled: bool,
17 material_entries: Vec<wgpu::BindGroupLayoutEntry>,
18 blend: BlendState,
19 vs_entry: &'a str,
20 fs_entry: &'a str,
21}
22
23impl<'a> RenderPipelineBuilder<'a> {
24 pub fn new(
25 shader: Handle<ShaderModule>,
26 ) -> Self {
27 Self {
28 shader,
29 vertex_layouts: Vec::new(),
30 depth_format: None,
31 depth_writes_enabled: true,
32 material_entries: vec![],
33 blend: BlendState::ALPHA_BLENDING,
34 vs_entry: "vs_main",
35 fs_entry: "fs_main",
36 }
37 }
38 pub fn material_layout(
39 mut self,
40 entries: &[wgpu::BindGroupLayoutEntry],
41 ) -> Self {
42 self.material_entries = entries.to_vec();
43 self
44 }
45
46 pub fn vertex_layout(mut self, layout: BufferLayout) -> Self {
47 self.vertex_layouts.push(layout.to_wgpu_layout());
48 self
49 }
50
51 pub fn depth_format(mut self, format: wgpu::TextureFormat) -> Self {
52 self.depth_format = Some(format);
53 self
54 }
55
56 pub fn depth_writes_enabled(mut self, enabled: bool) -> Self {
57 self.depth_writes_enabled = enabled;
58 self
59 }
60
61 pub fn vs_entry_point(mut self, entry: &'a str) -> Self{
62 self.vs_entry = entry;
63 self
64 }
65
66 pub fn fs_entry_point(mut self, entry: &'a str) -> Self{
67 self.fs_entry = entry;
68 self
69 }
70
71 pub fn additive_alpha_blending(mut self) -> Self{
72 self.blend = wgpu::BlendState {
73 color:wgpu::BlendComponent {
74 src_factor: wgpu::BlendFactor::SrcAlpha,
75 dst_factor: wgpu::BlendFactor::One,
76 operation: wgpu::BlendOperation::Add,
77 },
78 alpha: wgpu::BlendComponent {
79 src_factor: wgpu::BlendFactor::One,
80 dst_factor: wgpu::BlendFactor::One,
81 operation: wgpu::BlendOperation::Add,
82 }};
83 self
84 }
85
86 pub fn blend_mode(mut self,blend_state: BlendState) -> Self{
87 self.blend = blend_state;
88 self
89 }
90
91 pub fn build(self,device: &Device,asset_manager: &AssetManager,surface_config: &SurfaceConfiguration) -> RenderPipeline {
92 let shader = asset_manager.shaders.get(self.shader).unwrap();
93
94 let material_layout = device.create_bind_group_layout(
95 &wgpu::BindGroupLayoutDescriptor {
96 label: Some("material layout"),
97 entries: &self.material_entries,
98 }
99 );
100
101 let pipeline_layout = device.create_pipeline_layout(
102 &wgpu::PipelineLayoutDescriptor {
103 label: Some("pipeline layout"),
104 bind_group_layouts: &[
105 Some(&material_layout),
106 ],
107 immediate_size: 0,
108 }
109 );
110
111 let pipeline = device.create_render_pipeline(
112 &wgpu::RenderPipelineDescriptor {
113 label: Some("pipeline"),
114 layout: Some(&pipeline_layout),
115 vertex: wgpu::VertexState {
116 module: shader,
117 entry_point: Option::from(self.vs_entry),
118 compilation_options: Default::default(),
119 buffers: &self.vertex_layouts,
120 },
121 fragment: Some(wgpu::FragmentState {
122 module: shader,
123 entry_point: Option::from(self.vs_entry),
124 compilation_options: Default::default(),
125 targets: &[Some(wgpu::ColorTargetState {
126 format: surface_config.format,
127 blend: Some(self.blend),
128 write_mask: wgpu::ColorWrites::ALL,
129 })],
130 }),
131 multiview_mask: None,
132 primitive: wgpu::PrimitiveState::default(),
133 depth_stencil: self.depth_format.map(|format| {
134 wgpu::DepthStencilState {
135 format,
136 depth_write_enabled: Option::from(self.depth_writes_enabled),
137 depth_compare: Option::from(wgpu::CompareFunction::LessEqual),
138 stencil: Default::default(),
139 bias: Default::default(),
140 }
141 }),
142 multisample: wgpu::MultisampleState::default(),
143 cache: None,
144 }
145 );
146
147 RenderPipeline { pipeline, material_layout }
148 }
149}
150
151pub fn texture(binding: u32) -> wgpu::BindGroupLayoutEntry {
152 wgpu::BindGroupLayoutEntry {
153 binding,
154 visibility: wgpu::ShaderStages::FRAGMENT,
155 ty: wgpu::BindingType::Texture {
156 multisampled: false,
157 view_dimension: wgpu::TextureViewDimension::D2,
158 sample_type: wgpu::TextureSampleType::Float { filterable: true },
159 },
160 count: None,
161 }
162}
163
164pub fn sampler(binding: u32) -> wgpu::BindGroupLayoutEntry {
165 wgpu::BindGroupLayoutEntry {
166 binding,
167 visibility: wgpu::ShaderStages::FRAGMENT,
168 ty: wgpu::BindingType::Sampler(
169 wgpu::SamplerBindingType::Filtering
170 ),
171 count: None,
172 }
173}
174
175pub fn uniform(binding: u32) -> wgpu::BindGroupLayoutEntry {
176 wgpu::BindGroupLayoutEntry {
177 binding,
178 visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
179 ty: wgpu::BindingType::Buffer {
180 ty: BufferBindingType::Uniform,
181 has_dynamic_offset: false,
182 min_binding_size: None,
183 },
184 count: None,
185 }
186}