-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathhello_world_more.rs
311 lines (281 loc) · 9.6 KB
/
hello_world_more.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! This program shows how to render two simple triangles with different configurations.
//!
//! The direct / indexed methods just show you how you’re supposed to use them (don’t try and find
//! any differences in the rendered images, because there’s none!).
//!
//! Press the <main action> to switch between methods to operate on vertex entities.
//!
//! <https://docs.rs/luminance>
use crate::{Example, InputAction, LoopFeedback, PlatformServices};
use luminance::{
backend::Backend,
context::Context,
dim::{Dim2, Size2},
framebuffer::{Back, Framebuffer},
namespace,
pipeline::PipelineState,
pixel::RGB32F,
primitive::Triangle,
render_state::RenderState,
shader::{Program, ProgramBuilder},
vertex_entity::{VertexEntity, VertexEntityBuilder, View},
vertex_storage::{Deinterleaved, Deinterleaving, Interleaved, Interleaving},
RenderSlots, Vertex,
};
// We get the shader at compile time from local files
const VS: &'static str = include_str!("simple-vs.glsl");
const FS: &'static str = include_str!("simple-fs.glsl");
// Vertex namespace.
//
// A namespace is tag-like type that is used to spawn named indices, allowing to uniquely identify various piece of
// protocol information, such as positions, normals, colors, etc. Theoretically, namespaces and named indices can be
// used for anything and everything.
namespace! {
VertexNamespace = { "pos", "rgb" }
}
// Our vertex type.
//
// We derive the Vertex trait automatically and map the type to the namespace, so that a mapping can be done between the
// namespace names and the vertex fields.
//
// Also, currently, we need to use #[repr(C))] to ensure Rust is not going to move struct’s fields around.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Vertex)]
#[vertex(namespace = "VertexNamespace")]
struct Vertex {
pos: mint::Vector2<f32>,
// Here, we can use the special normalized = <bool> construct to state whether we want integral
// vertex attributes to be available as normalized floats in the shaders, when fetching them from
// the vertex buffers. If you set it to "false" or ignore it, you will get non-normalized integer
// values (i.e. value ranging from 0 to 255 for u8, for instance).
#[vertex(normalized = "true")]
rgb: mint::Vector3<u8>,
}
impl Vertex {
const fn new(pos: mint::Vector2<f32>, rgb: mint::Vector3<u8>) -> Self {
Self { pos, rgb }
}
}
// The vertices. We define two triangles.
const TRI_VERTICES: [Vertex; 6] = [
// First triangle – an RGB one.
Vertex::new(
mint::Vector2 { x: 0.5, y: -0.5 },
mint::Vector3 { x: 0, y: 255, z: 0 },
),
Vertex::new(
mint::Vector2 { x: 0.0, y: 0.5 },
mint::Vector3 { x: 0, y: 0, z: 255 },
),
Vertex::new(
mint::Vector2 { x: -0.5, y: -0.5 },
mint::Vector3 { x: 255, y: 0, z: 0 },
),
// Second triangle, a purple one, positioned differently.
Vertex::new(
mint::Vector2 { x: -0.5, y: 0.5 },
mint::Vector3 {
x: 255,
y: 51,
z: 255,
},
),
Vertex::new(
mint::Vector2 { x: 0.0, y: -0.5 },
mint::Vector3 {
x: 51,
y: 255,
z: 255,
},
),
Vertex::new(
mint::Vector2 { x: 0.5, y: 0.5 },
mint::Vector3 {
x: 51,
y: 51,
z: 255,
},
),
];
// The vertices, deinterleaved versions. We still define two triangles.
const TRI_DEINT_POS_VERTICES: &[mint::Vector2<f32>] = &[
mint::Vector2 { x: 0.5, y: -0.5 },
mint::Vector2 { x: 0.0, y: 0.5 },
mint::Vector2 { x: -0.5, y: -0.5 },
mint::Vector2 { x: -0.5, y: 0.5 },
mint::Vector2 { x: 0.0, y: -0.5 },
mint::Vector2 { x: 0.5, y: 0.5 },
];
const TRI_DEINT_COLOR_VERTICES: &[mint::Vector3<u8>] = &[
mint::Vector3 { x: 0, y: 255, z: 0 },
mint::Vector3 { x: 0, y: 0, z: 255 },
mint::Vector3 { x: 255, y: 0, z: 0 },
mint::Vector3 {
x: 255,
y: 51,
z: 255,
},
mint::Vector3 {
x: 51,
y: 255,
z: 255,
},
mint::Vector3 {
x: 51,
y: 51,
z: 255,
},
];
// Indices into TRI_VERTICES to use to build up the triangles.
const TRI_INDICES: [u32; 6] = [
0, 1, 2, // First triangle.
3, 4, 5, // Second triangle.
];
// Another namespace for render slots (see below).
namespace! {
RenderSlotNamespace = { "frag" }
}
// Render slots.
//
// A render slot represents the channels the end stage of a shader program is going to end up writing to. In our case,
// since we are only interested in rendering the color of each pixel, we will just have one single channel for the
// color.
#[derive(Clone, Copy, Debug, PartialEq, RenderSlots)]
#[slot(namespace = "RenderSlotNamespace")]
pub struct Slots {
frag: RGB32F,
}
// Convenience type to demonstrate the difference between direct, indirect (indexed), interleaved and deinterleaved
// vertex entities.
#[derive(Copy, Clone, Debug)]
enum Method {
Direct,
Indexed,
DirectDeinterleaved,
IndexedDeinterleaved,
}
impl Method {
fn toggle(self) -> Self {
match self {
Method::Direct => Method::Indexed,
Method::Indexed => Method::DirectDeinterleaved,
Method::DirectDeinterleaved => Method::IndexedDeinterleaved,
Method::IndexedDeinterleaved => Method::Direct,
}
}
}
/// Local example; this will be picked by the example runner.
pub struct LocalExample {
back_buffer: Framebuffer<Dim2, Back<Slots>, Back<()>>,
// the program will render by mapping our Vertex type as triangles to the color slot, containing a single color
program: Program<Vertex, (), Triangle, Slots, ()>,
direct_triangles: VertexEntity<Vertex, Triangle, Interleaving>,
indexed_triangles: VertexEntity<Vertex, Triangle, Interleaving>,
direct_deinterleaved_triangles: VertexEntity<Vertex, Triangle, Deinterleaving>,
indexed_deinterleaved_triangles: VertexEntity<Vertex, Triangle, Deinterleaving>,
method: Method,
}
impl Example for LocalExample {
type Err = luminance::backend::Error;
const TITLE: &'static str = "Hello, world! (more)";
fn bootstrap(
[width, height]: [u32; 2],
_platform: &mut impl PlatformServices,
context: &mut Context<impl Backend>,
) -> Result<Self, Self::Err> {
// We need a program to “shade” our triangles
let program = context.new_program(
ProgramBuilder::new()
.add_vertex_stage(VS)
.no_primitive_stage()
.add_shading_stage(FS),
)?;
// Create a vertex entity for direct geometry; that is, a vertex entity that will render vertices by
// taking one after another in the provided slice.
let direct_triangles = context.new_vertex_entity(
VertexEntityBuilder::new().add_vertices(Interleaved::new().set_vertices(TRI_VERTICES)),
)?;
// Indexed vertex entity; that is, the vertices will be picked by using the indexes provided
// by the second slice and this indexes will reference the first slice (useful not to duplicate
// vertices on more complex objects than just two triangles).
let indexed_triangles = context.new_vertex_entity(
VertexEntityBuilder::new()
.add_vertices(Interleaved::new().set_vertices(TRI_VERTICES))
.add_indices(TRI_INDICES),
)?;
// Create a direct, deinterleaved vertex entity; such vertex entity allows to separate vertex
// attributes in several contiguous regions of memory.
let direct_deinterleaved_triangles = context.new_vertex_entity(
VertexEntityBuilder::new().add_vertices(
Deinterleaved::new()
.set_components::<"pos">(TRI_DEINT_POS_VERTICES)
.set_components::<"rgb">(TRI_DEINT_COLOR_VERTICES),
),
)?;
// Create an indexed, deinterleaved vertex entity.
let indexed_deinterleaved_triangles = context.new_vertex_entity(
VertexEntityBuilder::new()
.add_vertices(
Deinterleaved::new()
.set_components::<"pos">(TRI_DEINT_POS_VERTICES)
.set_components::<"rgb">(TRI_DEINT_COLOR_VERTICES),
)
.add_indices(TRI_INDICES),
)?;
let method = Method::Direct;
let back_buffer = context.back_buffer(Size2::new(width, height))?;
Ok(Self {
back_buffer,
program,
direct_triangles,
indexed_triangles,
direct_deinterleaved_triangles,
indexed_deinterleaved_triangles,
method,
})
}
fn render_frame(
mut self,
_time_ms: f32,
actions: impl Iterator<Item = InputAction>,
context: &mut Context<impl Backend>,
) -> Result<LoopFeedback<Self>, Self::Err> {
for action in actions {
match action {
InputAction::Quit => return Ok(LoopFeedback::Exit),
InputAction::MainToggle => {
self.method = self.method.toggle();
log::info!("now rendering {:?}", self.method);
}
_ => (),
}
}
context.with_framebuffer(
&self.back_buffer,
&PipelineState::default(),
|mut with_framebuffer| {
with_framebuffer.with_program(&self.program, |mut with_program| {
with_program.with_render_state(
&RenderState::default(),
|mut with_render_state| match self.method {
Method::Direct => {
with_render_state.render_vertex_entity(self.direct_triangles.view(..))
}
Method::Indexed => {
with_render_state.render_vertex_entity(self.indexed_triangles.view(..))
}
Method::DirectDeinterleaved => {
with_render_state.render_vertex_entity(self.direct_deinterleaved_triangles.view(..))
}
Method::IndexedDeinterleaved => with_render_state
.render_vertex_entity(self.indexed_deinterleaved_triangles.view(..)),
},
)
})
},
)?;
// Finally, swap the backbuffer with the frontbuffer in order to render our triangles onto your
// screen.
Ok(LoopFeedback::Continue(self))
}
}