tirbofish/dropbear
main / crates / dropbear-engine / src / billboarding.rs · 10222 bytes
crates/dropbear-engine/src/billboarding.rs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::asset::ASSET_REGISTRY;
use crate::buffer::{DynamicBuffer, UniformBuffer, WritableBuffer};
use crate::graphics::SharedGraphicsContext;
use crate::shader::Shader;
use glam::Mat4;
use std::sync::Arc;
use wgpu::MultisampleState;
pub struct BillboardPipeline {
pipeline: wgpu::RenderPipeline,
transform_buffer: UniformBuffer<Mat4>,
projection_buffer: UniformBuffer<Mat4>,
position_buffer: DynamicBuffer<[f32; 3]>,
tex_coord_buffer: DynamicBuffer<[f32; 2]>,
uniform_bind_group_layout: wgpu::BindGroupLayout,
sampler: wgpu::Sampler,
}
impl BillboardPipeline {
pub fn new(graphics: Arc<SharedGraphicsContext>) -> Self {
puffin::profile_function!();
log::debug!("Initialising billboard pipeline");
let shader = Shader::new(
graphics.clone(),
include_str!("shaders/billboard.wgsl"),
Some("billboard shader"),
);
let positions: [[f32; 3]; 4] = [
[10.0, -10.0, 0.0],
[-10.0, -10.0, 0.0],
[10.0, 10.0, 0.0],
[-10.0, 10.0, 0.0],
];
let mut position_buffer = DynamicBuffer::new(
&graphics.device,
positions.len(),
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
"billboard position buffer"
);
position_buffer.write(&graphics.device, &graphics.queue, &positions);
let tex_coords: [[f32; 2]; 4] = [[1.0, 1.0], [1.0, 0.0], [0.0, 1.0], [0.0, 0.0]];
let mut tex_coord_buffer = DynamicBuffer::new(
&graphics.device,
tex_coords.len(),
wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
"billboard tex coords buffer"
);
tex_coord_buffer.write(
&graphics.device,
&graphics.queue,
bytemuck::cast_slice(&tex_coords)
);
let sampler = graphics.device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("billboard sampler"),
address_mode_u: wgpu::AddressMode::Repeat,
address_mode_v: wgpu::AddressMode::Repeat,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::MipmapFilterMode::Linear,
..Default::default()
});
let uniform_bind_group_layout =
graphics
.device
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("billboard group layout"),
entries: &[
// transform
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// projection
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
// t_diffuse
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
// s_diffuse
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
count: None,
},
],
});
let transform_buffer: UniformBuffer<Mat4> =
UniformBuffer::new(&graphics.device, "billboard transform buffer");
let projection_buffer: UniformBuffer<Mat4> =
UniformBuffer::new(&graphics.device, "billboard projection buffer");
{
let mut registry = ASSET_REGISTRY.write();
let handle = registry.solid_texture_rgba8(graphics.clone(), [0, 0, 0, 0], None); // make transparent for now
let _ = registry.get_texture(handle);
}
let pipeline_layout =
graphics
.device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("billboard pipeline layout"),
bind_group_layouts: &[Some(&uniform_bind_group_layout)],
immediate_size: 0,
});
let format = { graphics.hdr.read().format().clone() };
let pipeline = graphics
.device
.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("billboard render pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
compilation_options: Default::default(),
buffers: &[
// positions
wgpu::VertexBufferLayout {
array_stride: (std::mem::size_of::<f32>() * 3) as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x3],
},
// tex coords
wgpu::VertexBufferLayout {
array_stride: (std::mem::size_of::<f32>() * 2) as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![1 => Float32x2],
},
],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
front_face: wgpu::FrontFace::Cw,
cull_mode: None,
..Default::default()
},
depth_stencil: Some(wgpu::DepthStencilState {
format: graphics.depth_texture.texture.format(),
depth_write_enabled: Some(false),
depth_compare: Some(wgpu::CompareFunction::Greater),
stencil: Default::default(),
bias: Default::default(),
}),
multisample: MultisampleState {
count: (*graphics.antialiasing.read()).into(),
mask: !0,
alpha_to_coverage_enabled: false,
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
})],
}),
cache: None,
multiview_mask: None,
});
log::debug!("Created billboard pipeline");
Self {
pipeline,
transform_buffer,
projection_buffer,
position_buffer,
tex_coord_buffer,
uniform_bind_group_layout,
sampler,
}
}
pub fn draw(
&self,
graphics: Arc<SharedGraphicsContext>,
render_pass: &mut wgpu::RenderPass<'_>,
transform: Mat4,
projection: Mat4,
texture_view: &wgpu::TextureView,
) {
self.transform_buffer.write(&graphics.queue, &transform);
self.projection_buffer.write(&graphics.queue, &projection);
let uniform_bind_group = graphics
.device
.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("billboard bind group"),
layout: &self.uniform_bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: self.transform_buffer.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 1,
resource: self.projection_buffer.buffer().as_entire_binding(),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(texture_view),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&self.sampler),
},
],
});
render_pass.set_pipeline(&self.pipeline);
render_pass.set_vertex_buffer(0, self.position_buffer.buffer().slice(..));
render_pass.set_vertex_buffer(1, self.tex_coord_buffer.buffer().slice(..));
render_pass.set_bind_group(0, &uniform_bind_group, &[]);
render_pass.draw(0..4, 0..1);
}
}