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
251
252
253
254
255
use {
crate::{
camera::Camera,
color::{IntoLinear, Linear},
frame::Frame,
instance::Instance,
mesh::Mesh,
r#loop::Error,
render::{InstanceHandle, MeshHandle, TextureHandle, ViewHandle},
shader,
storage::Storage,
texture::Texture,
vertex::{ColorVertex, FlatVertex, TextureVertex, UsesDepth},
},
std::marker::PhantomData,
wgpu::{Queue, RenderPass},
};
/// The layer builder. It creates a configured [`Layer`].
#[must_use]
pub struct Builder<'l, 'd, V> {
frame: &'l mut Frame<'d>,
clear_color: Option<Linear<f64>>,
clear_depth: bool,
vertex_type: PhantomData<V>,
}
impl<'l, 'd, V> Builder<'l, 'd, V> {
pub(crate) fn new(frame: &'l mut Frame<'d>) -> Self {
Self {
frame,
clear_color: None,
clear_depth: false,
vertex_type: PhantomData,
}
}
/// Sets clear color for the layer.
///
/// It takes a color parameter, which must implement the [`IntoLinear`] trait.
///
/// Don't set this setting if you don't want to fill
/// the previous layer (or frame) with some color.
/// Or set to clear the current buffer if a layer is already drawn
/// into the frame by calling [`commit_in_frame`](crate::Frame::commit_in_frame).
///
/// # Example
/// ```
/// # use dunge::color::Srgba;
/// # struct Frame;
/// # impl Frame {
/// # fn texture_layer(self) -> Self { self }
/// # fn with_clear_color(self, _: Srgba<u8>) -> Self { self }
/// # fn start(self) {}
/// # }
/// # let frame = Frame;
/// let color = Srgba([20, 30, 40, 255]);
/// let mut layer = frame
/// .texture_layer()
/// .with_clear_color(color)
/// .start();
/// ```
///
/// To clear a layer with a transparent color, it is enough to pass `()` as a parameter.
///
/// # Example
/// ```
/// # struct Frame;
/// # impl Frame {
/// # fn texture_layer(self) -> Self { self }
/// # fn with_clear_color(self, _: ()) -> Self { self }
/// # fn start(self) {}
/// # }
/// # let frame = Frame;
/// let mut layer = frame
/// .texture_layer()
/// .with_clear_color(())
/// .start();
/// ```
pub fn with_clear_color<C>(self, color: C) -> Self
where
C: IntoLinear,
{
Self {
clear_color: Some(color.into_linear()),
..self
}
}
/// Sets the flag to clear the depth buffer or not for the layer.
pub fn with_clear_depth(self) -> Self
where
V: UsesDepth,
{
Self {
clear_depth: true,
..self
}
}
}
impl<'l> Builder<'l, '_, TextureVertex> {
pub fn start(self) -> Layer<'l, TextureVertex> {
self.frame.start_layer(self.clear_color, self.clear_depth)
}
}
impl<'l> Builder<'l, '_, ColorVertex> {
pub fn start(self) -> Layer<'l, ColorVertex> {
self.frame.start_layer(self.clear_color, self.clear_depth)
}
}
impl<'l> Builder<'l, '_, FlatVertex> {
pub fn start(self) -> Layer<'l, FlatVertex> {
self.frame.start_layer(self.clear_color, self.clear_depth)
}
}
/// The frame layer. Can be created from a [`Frame`] instance.
#[must_use]
pub struct Layer<'l, V> {
pass: RenderPass<'l>,
size: (u32, u32),
queue: &'l Queue,
resources: &'l Resources,
instance: Option<&'l Instance>,
vertex_type: PhantomData<V>,
drawn_in_frame: &'l mut bool,
}
impl<'l, V> Layer<'l, V> {
pub(crate) fn new(
pass: RenderPass<'l>,
size: (u32, u32),
queue: &'l Queue,
resources: &'l Resources,
drawn_in_frame: &'l mut bool,
) -> Self {
Self {
pass,
size,
queue,
resources,
instance: None,
vertex_type: PhantomData,
drawn_in_frame,
}
}
/// Binds a [instance](crate::InstanceHandle).
///
/// # Errors
/// Returns [`Error::ResourceNotFound`] if given instance handler was deleted.
pub fn bind_instance(&mut self, handle: InstanceHandle) -> Result<(), Error> {
let instance = self.resources.instances.get(handle.0)?;
self.instance = Some(instance);
Ok(())
}
/// Draws nothing.
pub fn draw_empty(&mut self) {
*self.drawn_in_frame = true;
}
/// Draws a [mesh](crate::MeshHandle).
///
/// # Errors
/// Returns [`Error::ResourceNotFound`] if given mesh handler was deleted.
/// Returns [`Error::InstanceNotSet`] if no any [instance](InstanceHandle) is set.
/// Call [`bind_instance`](crate::Layer::bind_instance) to set an instance.
pub fn draw(&mut self, handle: MeshHandle<V>) -> Result<(), Error> {
use wgpu::IndexFormat;
let mesh = self.resources.meshes.get(handle.id())?;
let instance = self.instance.ok_or(Error::InstanceNotSet)?;
self.pass
.set_vertex_buffer(shader::INSTANCE_BUFFER_SLOT, instance.buffer().slice(..));
self.pass
.set_vertex_buffer(shader::VERTEX_BUFFER_SLOT, mesh.vertex_buffer().slice(..));
self.pass
.set_index_buffer(mesh.index_buffer().slice(..), IndexFormat::Uint16);
self.pass
.draw_indexed(0..mesh.n_indices(), 0, 0..instance.n_instances());
self.draw_empty();
Ok(())
}
fn bind_view_handle(&mut self, handle: ViewHandle, group: u32) -> Result<(), Error> {
let camera = self.resources.views.get(handle.0)?;
camera.resize(self.size, self.queue);
self.pass.set_bind_group(group, camera.bind_group(), &[]);
Ok(())
}
fn bind_texture_handle(&mut self, handle: TextureHandle, group: u32) -> Result<(), Error> {
let texture = self.resources.textures.get(handle.0)?;
self.pass.set_bind_group(group, texture.bind_group(), &[]);
Ok(())
}
}
impl Layer<'_, TextureVertex> {
/// Binds a [view](crate::ViewHandle).
///
/// # Errors
/// Returns [`Error::ResourceNotFound`] if given view handler was deleted.
pub fn bind_view(&mut self, handle: ViewHandle) -> Result<(), Error> {
self.bind_view_handle(handle, shader::TEXTURED_CAMERA_GROUP)
}
}
impl Layer<'_, ColorVertex> {
/// Binds a [view](crate::ViewHandle).
///
/// # Errors
/// Returns [`Error::ResourceNotFound`] if given view handler was deleted.
pub fn bind_view(&mut self, handle: ViewHandle) -> Result<(), Error> {
self.bind_view_handle(handle, shader::COLOR_CAMERA_GROUP)
}
}
impl Layer<'_, TextureVertex> {
/// Binds a [texture](crate::TextureHandle).
///
/// # Errors
/// Returns [`Error::ResourceNotFound`] if given texture handler was deleted.
pub fn bind_texture(&mut self, handle: TextureHandle) -> Result<(), Error> {
self.bind_texture_handle(handle, shader::TEXTURED_SDIFF_GROUP)
}
}
impl Layer<'_, FlatVertex> {
/// Binds a [texture](crate::TextureHandle).
///
/// # Errors
/// Returns [`Error::ResourceNotFound`] if given texture handler was deleted.
pub fn bind_texture(&mut self, handle: TextureHandle) -> Result<(), Error> {
self.bind_texture_handle(handle, shader::FLAT_SDIFF_GROUP)
}
}
/// A container of drawable resources.
#[derive(Default)]
pub(crate) struct Resources {
pub(crate) textures: Storage<Texture>,
pub(crate) instances: Storage<Instance>,
pub(crate) meshes: Storage<Mesh>,
pub(crate) views: Storage<Camera>,
}