-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindexed_multi_vertex_buffers.rs
More file actions
361 lines (319 loc) · 9.22 KB
/
indexed_multi_vertex_buffers.rs
File metadata and controls
361 lines (319 loc) · 9.22 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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#![allow(clippy::needless_return)]
//! Example: Indexed draw with multiple vertex buffers.
//!
//! This example renders a simple quad composed from two triangles using
//! separate vertex buffers for positions and colors plus a 16-bit index
//! buffer. It exercises `BindVertexBuffer` for multiple slots and
//! `BindIndexBuffer`/`DrawIndexed` in the render command stream.
use lambda::{
component::Component,
events::{
EventMask,
WindowEvent,
},
logging,
render::{
buffer::{
BufferBuilder,
BufferType,
Properties,
Usage,
},
command::{
IndexFormat,
RenderCommand,
},
pipeline::{
CullingMode,
RenderPipelineBuilder,
},
render_pass::RenderPassBuilder,
shader::{
Shader,
ShaderBuilder,
ShaderKind,
VirtualShader,
},
vertex::{
ColorFormat,
VertexAttribute,
VertexElement,
},
viewport,
RenderContext,
ResourceId,
},
runtime::start_runtime,
runtimes::{
application::ComponentResult,
ApplicationRuntimeBuilder,
},
};
// ------------------------------ SHADER SOURCE --------------------------------
const VERTEX_SHADER_SOURCE: &str = r#"
#version 450
layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec3 vertex_color;
layout (location = 0) out vec3 frag_color;
void main() {
gl_Position = vec4(vertex_position, 1.0);
frag_color = vertex_color;
}
"#;
const FRAGMENT_SHADER_SOURCE: &str = r#"
#version 450
layout (location = 0) in vec3 frag_color;
layout (location = 0) out vec4 fragment_color;
void main() {
fragment_color = vec4(frag_color, 1.0);
}
"#;
// ------------------------------- VERTEX TYPES --------------------------------
#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct PositionVertex {
position: [f32; 3],
}
unsafe impl lambda::pod::PlainOldData for PositionVertex {}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
struct ColorVertex {
color: [f32; 3],
}
unsafe impl lambda::pod::PlainOldData for ColorVertex {}
// --------------------------------- COMPONENT ---------------------------------
pub struct IndexedMultiBufferExample {
vertex_shader: Shader,
fragment_shader: Shader,
render_pass_id: Option<ResourceId>,
render_pipeline_id: Option<ResourceId>,
index_buffer_id: Option<ResourceId>,
index_count: u32,
width: u32,
height: u32,
}
impl Component<ComponentResult, String> for IndexedMultiBufferExample {
fn on_attach(
&mut self,
render_context: &mut RenderContext,
) -> Result<ComponentResult, String> {
let render_pass = RenderPassBuilder::new().build(
render_context.gpu(),
render_context.surface_format(),
render_context.depth_format(),
);
// Quad composed from two triangles in clip space.
let positions: Vec<PositionVertex> = vec![
PositionVertex {
position: [-0.5, -0.5, 0.0],
},
PositionVertex {
position: [0.5, -0.5, 0.0],
},
PositionVertex {
position: [0.5, 0.5, 0.0],
},
PositionVertex {
position: [-0.5, 0.5, 0.0],
},
];
let colors: Vec<ColorVertex> = vec![
ColorVertex {
color: [1.0, 0.0, 0.0],
},
ColorVertex {
color: [0.0, 1.0, 0.0],
},
ColorVertex {
color: [0.0, 0.0, 1.0],
},
ColorVertex {
color: [1.0, 1.0, 1.0],
},
];
let indices: Vec<u16> = vec![0, 1, 2, 2, 3, 0];
let index_count = indices.len() as u32;
// Build vertex buffers for positions and colors in separate slots.
let position_buffer = BufferBuilder::new()
.with_usage(Usage::VERTEX)
.with_properties(Properties::DEVICE_LOCAL)
.with_buffer_type(BufferType::Vertex)
.with_label("indexed-positions")
.build(render_context.gpu(), positions)
.map_err(|e| e.to_string())?;
let color_buffer = BufferBuilder::new()
.with_usage(Usage::VERTEX)
.with_properties(Properties::DEVICE_LOCAL)
.with_buffer_type(BufferType::Vertex)
.with_label("indexed-colors")
.build(render_context.gpu(), colors)
.map_err(|e| e.to_string())?;
// Build a 16-bit index buffer.
let index_buffer = BufferBuilder::new()
.with_usage(Usage::INDEX)
.with_properties(Properties::DEVICE_LOCAL)
.with_buffer_type(BufferType::Index)
.with_label("indexed-indices")
.build(render_context.gpu(), indices)
.map_err(|e| e.to_string())?;
let pipeline = RenderPipelineBuilder::new()
.with_culling(CullingMode::Back)
.with_buffer(
position_buffer,
vec![VertexAttribute {
location: 0,
offset: 0,
element: VertexElement {
format: ColorFormat::Rgb32Sfloat,
offset: 0,
},
}],
)
.with_buffer(
color_buffer,
vec![VertexAttribute {
location: 1,
offset: 0,
element: VertexElement {
format: ColorFormat::Rgb32Sfloat,
offset: 0,
},
}],
)
.build(
render_context.gpu(),
render_context.surface_format(),
render_context.depth_format(),
&render_pass,
&self.vertex_shader,
Some(&self.fragment_shader),
);
self.render_pass_id = Some(render_context.attach_render_pass(render_pass));
self.render_pipeline_id = Some(render_context.attach_pipeline(pipeline));
self.index_buffer_id = Some(render_context.attach_buffer(index_buffer));
self.index_count = index_count;
logging::info!("Indexed multi-vertex-buffer example attached");
return Ok(ComponentResult::Success);
}
fn on_detach(
&mut self,
_render_context: &mut RenderContext,
) -> Result<ComponentResult, String> {
logging::info!("Indexed multi-vertex-buffer example detached");
return Ok(ComponentResult::Success);
}
fn event_mask(&self) -> EventMask {
return EventMask::WINDOW;
}
fn on_window_event(&mut self, event: &WindowEvent) -> Result<(), String> {
if let WindowEvent::Resize { width, height } = event {
self.width = *width;
self.height = *height;
logging::info!("Window resized to {}x{}", width, height);
}
return Ok(());
}
fn on_update(
&mut self,
_last_frame: &std::time::Duration,
) -> Result<ComponentResult, String> {
return Ok(ComponentResult::Success);
}
fn on_render(
&mut self,
_render_context: &mut RenderContext,
) -> Vec<RenderCommand> {
let viewport =
viewport::ViewportBuilder::new().build(self.width, self.height);
let render_pass_id = self
.render_pass_id
.expect("Render pass must be attached before rendering");
let pipeline_id = self
.render_pipeline_id
.expect("Pipeline must be attached before rendering");
let index_buffer_id = self
.index_buffer_id
.expect("Index buffer must be attached before rendering");
return vec![
RenderCommand::BeginRenderPass {
render_pass: render_pass_id,
viewport: viewport.clone(),
},
RenderCommand::SetPipeline {
pipeline: pipeline_id,
},
RenderCommand::SetViewports {
start_at: 0,
viewports: vec![viewport.clone()],
},
RenderCommand::SetScissors {
start_at: 0,
viewports: vec![viewport.clone()],
},
RenderCommand::BindVertexBuffer {
pipeline: pipeline_id,
buffer: 0,
},
RenderCommand::BindVertexBuffer {
pipeline: pipeline_id,
buffer: 1,
},
RenderCommand::BindIndexBuffer {
buffer: index_buffer_id,
format: IndexFormat::Uint16,
},
RenderCommand::DrawIndexed {
indices: 0..self.index_count,
base_vertex: 0,
instances: 0..1,
},
RenderCommand::EndRenderPass,
];
}
}
impl Default for IndexedMultiBufferExample {
fn default() -> Self {
let vertex_virtual_shader = VirtualShader::Source {
source: VERTEX_SHADER_SOURCE.to_string(),
kind: ShaderKind::Vertex,
entry_point: "main".to_string(),
name: "indexed_multi_vertex_buffers".to_string(),
};
let fragment_virtual_shader = VirtualShader::Source {
source: FRAGMENT_SHADER_SOURCE.to_string(),
kind: ShaderKind::Fragment,
entry_point: "main".to_string(),
name: "indexed_multi_vertex_buffers".to_string(),
};
let mut builder = ShaderBuilder::new();
let vertex_shader = builder.build(vertex_virtual_shader);
let fragment_shader = builder.build(fragment_virtual_shader);
return Self {
vertex_shader,
fragment_shader,
render_pass_id: None,
render_pipeline_id: None,
index_buffer_id: None,
index_count: 0,
width: 800,
height: 600,
};
}
}
fn main() {
let runtime =
ApplicationRuntimeBuilder::new("Indexed Multi-Vertex-Buffer Example")
.with_window_configured_as(move |window_builder| {
return window_builder
.with_dimensions(800, 600)
.with_name("Indexed Multi-Vertex-Buffer Example");
})
.with_renderer_configured_as(|renderer_builder| {
return renderer_builder.with_render_timeout(1_000_000_000);
})
.with_component(move |runtime, example: IndexedMultiBufferExample| {
return (runtime, example);
})
.build();
start_runtime(runtime);
}