@@ -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
3739mod 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
0 commit comments