Skip to content

Commit 38aff4b

Browse files
authored
Improved error handling (#546)
* render_with_material return error * render_with_effect return error
1 parent 9f628dd commit 38aff4b

File tree

14 files changed

+122
-78
lines changed

14 files changed

+122
-78
lines changed

examples/lighting/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ pub async fn run() {
253253

254254
// Draw
255255
if shadows_enabled {
256-
directional0.generate_shadow_map(1024, &model);
257-
directional1.generate_shadow_map(1024, &model);
258-
spot0.generate_shadow_map(1024, &model);
256+
directional0.generate_shadow_map(1024, &model).unwrap();
257+
directional1.generate_shadow_map(1024, &model).unwrap();
258+
spot0.generate_shadow_map(1024, &model).unwrap();
259259
}
260260

261261
let lights = [

examples/picking/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ pub async fn run() {
114114
position,
115115
monkey.into_iter().chain(&cone).chain(&instanced_mesh),
116116
Cull::Back,
117-
) {
117+
)
118+
.unwrap()
119+
{
118120
pick_mesh.set_transformation(
119121
Mat4::from_translation(pick.position) * Mat4::from_scale(0.3),
120122
);

examples/statues/src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ pub async fn run() {
9797
Srgba::new_opaque(204, 178, 127),
9898
vec3(0.0, -1.0, -1.0),
9999
);
100-
directional.generate_shadow_map(
101-
1024,
102-
models.iter().flat_map(|m| m.into_iter()).chain(&fountain),
103-
);
100+
directional
101+
.generate_shadow_map(
102+
1024,
103+
models.iter().flat_map(|m| m.into_iter()).chain(&fountain),
104+
)
105+
.unwrap();
104106
// Bounding boxes
105107
let mut aabb = AxisAlignedBoundingBox::EMPTY;
106108
let mut bounding_boxes = Vec::new();

src/renderer.rs

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub enum RendererError {
3232
#[cfg(feature = "text")]
3333
#[error("Failed to find font with index {0} in the given font collection")]
3434
MissingFont(u32),
35+
#[error("CoreError: {0}")]
36+
CoreError(#[from] CoreError),
3537
}
3638

3739
mod shader_ids;
@@ -194,16 +196,17 @@ macro_rules! impl_render_target_extensions_body {
194196
lights: &[&dyn Light],
195197
) -> &Self {
196198
let frustum = Frustum::new(viewer.projection() * viewer.view());
197-
self.write_partially::<RendererError>(scissor_box, || {
199+
if let Err(e) = self.write_partially::<RendererError>(scissor_box, || {
198200
for geometry in geometries
199201
.into_iter()
200202
.filter(|o| frustum.contains(o.aabb()))
201203
{
202-
render_with_material(&self.context, &viewer, geometry, material, lights);
204+
render_with_material(&self.context, &viewer, geometry, material, lights)?;
203205
}
204206
Ok(())
205-
})
206-
.unwrap();
207+
}) {
208+
panic!("{}", e.to_string());
209+
}
207210
self
208211
}
209212

@@ -246,7 +249,7 @@ macro_rules! impl_render_target_extensions_body {
246249
depth_texture: Option<DepthTexture>,
247250
) -> &Self {
248251
let frustum = Frustum::new(viewer.projection() * viewer.view());
249-
self.write_partially::<RendererError>(scissor_box, || {
252+
if let Err(e) = self.write_partially::<RendererError>(scissor_box, || {
250253
for geometry in geometries
251254
.into_iter()
252255
.filter(|o| frustum.contains(o.aabb()))
@@ -259,11 +262,12 @@ macro_rules! impl_render_target_extensions_body {
259262
lights,
260263
color_texture,
261264
depth_texture,
262-
);
265+
)?;
263266
}
264267
Ok(())
265-
})
266-
.unwrap();
268+
}) {
269+
panic!("{}", e.to_string());
270+
}
267271
self
268272
}
269273

@@ -412,22 +416,25 @@ pub fn render_with_material(
412416
geometry: impl Geometry,
413417
material: impl Material,
414418
lights: &[&dyn Light],
415-
) {
419+
) -> Result<(), RendererError> {
416420
let id = combine_ids(geometry.id(), material.id(), lights.iter().map(|l| l.id()));
417421

418422
let mut programs = context.programs.write().unwrap();
419-
let program = programs.entry(id).or_insert_with(|| {
420-
match Program::from_source(
421-
context,
422-
&geometry.vertex_shader_source(),
423-
&material.fragment_shader_source(lights),
424-
) {
425-
Ok(program) => program,
426-
Err(err) => panic!("{}", err.to_string()),
427-
}
428-
});
423+
if !programs.contains_key(&id) {
424+
programs.insert(
425+
id.clone(),
426+
Program::from_source(
427+
context,
428+
&geometry.vertex_shader_source(),
429+
&material.fragment_shader_source(lights),
430+
)?,
431+
);
432+
}
433+
let program = programs.get(&id).unwrap();
434+
429435
material.use_uniforms(program, &viewer, lights);
430436
geometry.draw(&viewer, program, material.render_states());
437+
Ok(())
431438
}
432439

433440
///
@@ -443,26 +450,28 @@ pub fn render_with_effect(
443450
lights: &[&dyn Light],
444451
color_texture: Option<ColorTexture>,
445452
depth_texture: Option<DepthTexture>,
446-
) {
453+
) -> Result<(), RendererError> {
447454
let id = combine_ids(
448455
geometry.id(),
449456
effect.id(color_texture, depth_texture),
450457
lights.iter().map(|l| l.id()),
451458
);
452459

453460
let mut programs = context.programs.write().unwrap();
454-
let program = programs.entry(id).or_insert_with(|| {
455-
match Program::from_source(
456-
context,
457-
&geometry.vertex_shader_source(),
458-
&effect.fragment_shader_source(lights, color_texture, depth_texture),
459-
) {
460-
Ok(program) => program,
461-
Err(err) => panic!("{}", err.to_string()),
462-
}
463-
});
461+
if !programs.contains_key(&id) {
462+
programs.insert(
463+
id.clone(),
464+
Program::from_source(
465+
context,
466+
&geometry.vertex_shader_source(),
467+
&effect.fragment_shader_source(lights, color_texture, depth_texture),
468+
)?,
469+
);
470+
}
471+
let program = programs.get(&id).unwrap();
464472
effect.use_uniforms(program, &viewer, lights, color_texture, depth_texture);
465473
geometry.draw(&viewer, program, effect.render_states());
474+
Ok(())
466475
}
467476

468477
///
@@ -579,7 +588,7 @@ pub fn pick(
579588
pixel: impl Into<PhysicalPoint> + Copy,
580589
geometries: impl IntoIterator<Item = impl Geometry>,
581590
culling: Cull,
582-
) -> Option<IntersectionResult> {
591+
) -> Result<Option<IntersectionResult>, RendererError> {
583592
let pos = camera.position_at_pixel(pixel);
584593
let dir = camera.view_direction_at_pixel(pixel);
585594
ray_intersect(
@@ -615,7 +624,7 @@ pub fn ray_intersect(
615624
max_depth: f32,
616625
geometries: impl IntoIterator<Item = impl Geometry>,
617626
culling: Cull,
618-
) -> Option<IntersectionResult> {
627+
) -> Result<Option<IntersectionResult>, RendererError> {
619628
use crate::core::*;
620629
let viewport = Viewport::new_at_origo(1, 1);
621630
let up = if direction.dot(vec3(1.0, 0.0, 0.0)).abs() > 0.99 {
@@ -661,21 +670,20 @@ pub fn ray_intersect(
661670
.write::<RendererError>(|| {
662671
for (id, geometry) in geometries.into_iter().enumerate() {
663672
material.geometry_id = id as u32;
664-
render_with_material(context, &camera, &geometry, &material, &[]);
673+
render_with_material(context, &camera, &geometry, &material, &[])?;
665674
}
666675
Ok(())
667-
})
668-
.unwrap()
676+
})?
669677
.read_color::<[f32; 4]>()[0];
670678
let depth = result[0];
671679
if depth < 1.0 {
672-
Some(IntersectionResult {
680+
Ok(Some(IntersectionResult {
673681
position: position + direction * depth * max_depth,
674682
geometry_id: result[1].to_bits(),
675683
instance_id: result[2].to_bits(),
676-
})
684+
}))
677685
} else {
678-
None
686+
Ok(None)
679687
}
680688
}
681689

src/renderer/geometry/instanced_mesh.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ impl Geometry for InstancedMesh {
276276
viewer: &dyn Viewer,
277277
lights: &[&dyn Light],
278278
) {
279-
render_with_material(&self.context, viewer, self, material, lights)
279+
if let Err(e) = render_with_material(&self.context, viewer, self, material, lights) {
280+
panic!("{}", e.to_string());
281+
}
280282
}
281283

282284
fn render_with_effect(
@@ -287,15 +289,17 @@ impl Geometry for InstancedMesh {
287289
color_texture: Option<ColorTexture>,
288290
depth_texture: Option<DepthTexture>,
289291
) {
290-
render_with_effect(
292+
if let Err(e) = render_with_effect(
291293
&self.context,
292294
viewer,
293295
self,
294296
material,
295297
lights,
296298
color_texture,
297299
depth_texture,
298-
)
300+
) {
301+
panic!("{}", e.to_string());
302+
}
299303
}
300304
}
301305

src/renderer/geometry/mesh.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ impl Geometry for Mesh {
189189
viewer: &dyn Viewer,
190190
lights: &[&dyn Light],
191191
) {
192-
render_with_material(&self.context, viewer, &self, material, lights);
192+
if let Err(e) = render_with_material(&self.context, viewer, &self, material, lights) {
193+
panic!("{}", e.to_string());
194+
}
193195
}
194196

195197
fn render_with_effect(
@@ -200,14 +202,16 @@ impl Geometry for Mesh {
200202
color_texture: Option<ColorTexture>,
201203
depth_texture: Option<DepthTexture>,
202204
) {
203-
render_with_effect(
205+
if let Err(e) = render_with_effect(
204206
&self.context,
205207
viewer,
206208
self,
207209
material,
208210
lights,
209211
color_texture,
210212
depth_texture,
211-
)
213+
) {
214+
panic!("{}", e.to_string());
215+
}
212216
}
213217
}

src/renderer/geometry/particles.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ impl Geometry for ParticleSystem {
252252
viewer: &dyn Viewer,
253253
lights: &[&dyn Light],
254254
) {
255-
render_with_material(&self.context, viewer, &self, material, lights)
255+
if let Err(e) = render_with_material(&self.context, viewer, &self, material, lights) {
256+
panic!("{}", e.to_string());
257+
}
256258
}
257259

258260
fn render_with_effect(
@@ -263,15 +265,17 @@ impl Geometry for ParticleSystem {
263265
color_texture: Option<ColorTexture>,
264266
depth_texture: Option<DepthTexture>,
265267
) {
266-
render_with_effect(
268+
if let Err(e) = render_with_effect(
267269
&self.context,
268270
viewer,
269271
self,
270272
material,
271273
lights,
272274
color_texture,
273275
depth_texture,
274-
)
276+
) {
277+
panic!("{}", e.to_string());
278+
}
275279
}
276280

277281
fn animate(&mut self, time: f32) {

src/renderer/geometry/sprites.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ impl Geometry for Sprites {
130130
viewer: &dyn Viewer,
131131
lights: &[&dyn Light],
132132
) {
133-
render_with_material(&self.context, viewer, &self, material, lights);
133+
if let Err(e) = render_with_material(&self.context, viewer, &self, material, lights) {
134+
panic!("{}", e.to_string());
135+
}
134136
}
135137

136138
fn render_with_effect(
@@ -141,15 +143,17 @@ impl Geometry for Sprites {
141143
color_texture: Option<ColorTexture>,
142144
depth_texture: Option<DepthTexture>,
143145
) {
144-
render_with_effect(
146+
if let Err(e) = render_with_effect(
145147
&self.context,
146148
viewer,
147149
self,
148150
material,
149151
lights,
150152
color_texture,
151153
depth_texture,
152-
)
154+
) {
155+
panic!("{}", e.to_string());
156+
}
153157
}
154158

155159
fn aabb(&self) -> AxisAlignedBoundingBox {

src/renderer/light/directional_light.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl DirectionalLight {
5555
&mut self,
5656
texture_size: u32,
5757
geometries: impl IntoIterator<Item = impl Geometry> + Clone,
58-
) {
58+
) -> Result<(), RendererError> {
5959
let up = compute_up_direction(self.direction);
6060

6161
let viewport = Viewport::new_at_origo(texture_size, texture_size);
@@ -64,7 +64,7 @@ impl DirectionalLight {
6464
aabb.expand_with_aabb(geometry.aabb());
6565
}
6666
if aabb.is_empty() {
67-
return;
67+
return Ok(());
6868
}
6969
let position = aabb.center();
7070
let target = position + self.direction.normalize();
@@ -109,13 +109,13 @@ impl DirectionalLight {
109109
&geometry,
110110
&depth_material,
111111
&[],
112-
);
112+
)?;
113113
}
114114
Ok(())
115-
})
116-
.unwrap();
115+
})?;
117116
self.shadow_texture = Some(shadow_texture);
118117
self.shadow_matrix = shadow_matrix(&shadow_camera);
118+
Ok(())
119119
}
120120

121121
///

0 commit comments

Comments
 (0)