-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.rs
More file actions
193 lines (181 loc) · 6.44 KB
/
pipeline.rs
File metadata and controls
193 lines (181 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use super::resources::InstanceData;
use super::shaders::{PONG_SHADER, TRAIL_SHADER};
use crate::mesh::Vertex;
use wgpu::*;
pub struct PipelineState {
pub main_pipeline: RenderPipeline,
pub trail_pipeline: RenderPipeline,
pub camera_layout: BindGroupLayout,
pub trail_layout: BindGroupLayout,
}
pub fn create_pipelines(device: &Device, format: TextureFormat) -> PipelineState {
// 1. Camera Bind Group Layout
let camera_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("Camera Bind Group Layout"),
entries: &[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
// 2. Trail Bind Group Layout
let trail_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("Trail Bind Group Layout"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Texture {
multisampled: false,
view_dimension: TextureViewDimension::D2,
sample_type: TextureSampleType::Float { filterable: true },
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler(SamplerBindingType::Filtering),
count: None,
},
],
});
// 3. Main Pipeline
let shader = device.create_shader_module(ShaderModuleDescriptor {
label: Some("Pong Shader"),
source: ShaderSource::Wgsl(PONG_SHADER.into()),
});
let main_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Render Pipeline Layout"),
bind_group_layouts: &[&camera_layout],
push_constant_ranges: &[],
});
let vertex_buffer_layout = VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as u64,
step_mode: VertexStepMode::Vertex,
attributes: &[VertexAttribute {
offset: 0,
shader_location: 0,
format: VertexFormat::Float32x3,
}],
};
let instance_buffer_layout = VertexBufferLayout {
array_stride: std::mem::size_of::<InstanceData>() as u64,
step_mode: VertexStepMode::Instance,
attributes: &[
VertexAttribute {
offset: 0,
shader_location: 1,
format: VertexFormat::Float32x4, // transform
},
VertexAttribute {
offset: std::mem::size_of::<[f32; 4]>() as u64,
shader_location: 2,
format: VertexFormat::Float32x4, // tint
},
],
};
let main_pipeline = device.create_render_pipeline(&RenderPipelineDescriptor {
label: Some("Render Pipeline"),
layout: Some(&main_layout),
vertex: VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[vertex_buffer_layout, instance_buffer_layout],
compilation_options: Default::default(),
},
fragment: Some(FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(ColorTargetState {
format,
blend: Some(BlendState::REPLACE),
write_mask: ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: PrimitiveState {
topology: PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: MultisampleState::default(),
multiview: None,
cache: None,
});
// 4. Trail Pipeline
let trail_shader = device.create_shader_module(ShaderModuleDescriptor {
label: Some("Trail Shader"),
source: ShaderSource::Wgsl(TRAIL_SHADER.into()),
});
let trail_pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor {
label: Some("Trail Pipeline Layout"),
bind_group_layouts: &[&trail_layout],
push_constant_ranges: &[],
});
let trail_pipeline = device.create_render_pipeline(&RenderPipelineDescriptor {
label: Some("Trail Render Pipeline"),
layout: Some(&trail_pipeline_layout),
vertex: VertexState {
module: &trail_shader,
entry_point: Some("vs_main"),
buffers: &[VertexBufferLayout {
array_stride: 16,
step_mode: VertexStepMode::Vertex,
attributes: &[
VertexAttribute {
offset: 0,
shader_location: 0,
format: VertexFormat::Float32x2,
},
VertexAttribute {
offset: 8,
shader_location: 1,
format: VertexFormat::Float32x2,
},
],
}],
compilation_options: Default::default(),
},
fragment: Some(FragmentState {
module: &trail_shader,
entry_point: Some("fs_main"),
targets: &[Some(ColorTargetState {
format,
blend: Some(BlendState::ALPHA_BLENDING),
write_mask: ColorWrites::ALL,
})],
compilation_options: Default::default(),
}),
primitive: PrimitiveState {
topology: PrimitiveTopology::TriangleStrip,
// ...
strip_index_format: None,
front_face: FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: MultisampleState::default(),
multiview: None,
cache: None,
});
PipelineState {
main_pipeline,
trail_pipeline,
camera_layout,
trail_layout,
}
}