45
45
//! is sRGB (if you don't know, it probably is) the `internal_format` is
46
46
//! one of the `SRGB*` values.
47
47
48
- use std:: { borrow:: Cow , error:: Error , fmt:: Display , mem:: size_of} ;
48
+ use std:: { borrow:: Cow , error:: Error , fmt:: Display , mem:: size_of, num :: NonZeroU32 } ;
49
49
50
50
use imgui:: { internal:: RawWrapper , DrawCmd , DrawData , DrawVert } ;
51
51
@@ -61,7 +61,7 @@ pub type GlBuffer = <Context as HasContext>::Buffer;
61
61
pub type GlTexture = <Context as HasContext >:: Texture ;
62
62
pub type GlVertexArray = <Context as HasContext >:: VertexArray ;
63
63
type GlProgram = <Context as HasContext >:: Program ;
64
- type GlUniformLocation = <Context as HasContext >:: Program ;
64
+ type GlUniformLocation = <Context as HasContext >:: UniformLocation ;
65
65
66
66
/// Renderer which owns the OpenGL context and handles textures itself. Also
67
67
/// converts all output colors to sRGB for display. Useful for simple applications,
@@ -135,11 +135,11 @@ impl Drop for AutoRenderer {
135
135
pub struct Renderer {
136
136
shaders : Shaders ,
137
137
state_backup : GlStateBackup ,
138
- pub vbo_handle : GlBuffer ,
139
- pub ebo_handle : GlBuffer ,
140
- pub font_atlas_texture : GlTexture ,
138
+ pub vbo_handle : Option < GlBuffer > ,
139
+ pub ebo_handle : Option < GlBuffer > ,
140
+ pub font_atlas_texture : Option < GlTexture > ,
141
141
#[ cfg( feature = "bind_vertex_array_support" ) ]
142
- pub vertex_array_object : GlVertexArray ,
142
+ pub vertex_array_object : Option < GlVertexArray > ,
143
143
pub gl_version : GlVersion ,
144
144
pub has_clip_origin_support : bool ,
145
145
pub is_destroyed : bool ,
@@ -205,11 +205,13 @@ impl Renderer {
205
205
let mut state_backup = GlStateBackup :: default ( ) ;
206
206
state_backup. pre_init ( gl) ;
207
207
208
- let font_atlas_texture = prepare_font_atlas ( gl, imgui_context. fonts ( ) , texture_map) ?;
208
+ let font_atlas_texture = Some ( prepare_font_atlas ( gl, imgui_context. fonts ( ) , texture_map) ?) ;
209
209
210
210
let shaders = Shaders :: new ( gl, gl_version, output_srgb) ?;
211
- let vbo_handle = unsafe { gl. create_buffer ( ) } . map_err ( InitError :: CreateBufferObject ) ?;
212
- let ebo_handle = unsafe { gl. create_buffer ( ) } . map_err ( InitError :: CreateBufferObject ) ?;
211
+ let vbo_handle =
212
+ Some ( unsafe { gl. create_buffer ( ) } . map_err ( InitError :: CreateBufferObject ) ?) ;
213
+ let ebo_handle =
214
+ Some ( unsafe { gl. create_buffer ( ) } . map_err ( InitError :: CreateBufferObject ) ?) ;
213
215
214
216
state_backup. post_init ( gl) ;
215
217
@@ -220,7 +222,7 @@ impl Renderer {
220
222
ebo_handle,
221
223
font_atlas_texture,
222
224
#[ cfg( feature = "bind_vertex_array_support" ) ]
223
- vertex_array_object : 0 ,
225
+ vertex_array_object : None ,
224
226
gl_version,
225
227
has_clip_origin_support,
226
228
is_destroyed : false ,
@@ -240,21 +242,17 @@ impl Renderer {
240
242
return ;
241
243
}
242
244
243
- if self . vbo_handle != 0 {
244
- unsafe { gl. delete_buffer ( self . vbo_handle ) } ;
245
- self . vbo_handle = 0 ;
245
+ if let Some ( vbo) = self . vbo_handle . take ( ) {
246
+ unsafe { gl. delete_buffer ( vbo) } ;
246
247
}
247
- if self . ebo_handle != 0 {
248
- unsafe { gl. delete_buffer ( self . ebo_handle ) } ;
249
- self . ebo_handle = 0 ;
248
+ if let Some ( ebo) = self . ebo_handle . take ( ) {
249
+ unsafe { gl. delete_buffer ( ebo) } ;
250
250
}
251
- let program = self . shaders . program ;
252
- if program != 0 {
251
+ if let Some ( program) = self . shaders . program . take ( ) {
253
252
unsafe { gl. delete_program ( program) } ;
254
253
}
255
- if self . font_atlas_texture != 0 {
256
- unsafe { gl. delete_texture ( self . font_atlas_texture ) } ;
257
- self . font_atlas_texture = 0 ;
254
+ if let Some ( atlas) = self . font_atlas_texture . take ( ) {
255
+ unsafe { gl. delete_texture ( atlas) } ;
258
256
}
259
257
260
258
self . is_destroyed = true ;
@@ -285,10 +283,11 @@ impl Renderer {
285
283
#[ cfg( feature = "bind_vertex_array_support" ) ]
286
284
if self . gl_version . bind_vertex_array_support ( ) {
287
285
unsafe {
288
- self . vertex_array_object = gl
286
+ let vao = gl
289
287
. create_vertex_array ( )
290
288
. map_err ( |err| format ! ( "Error creating vertex array object: {}" , err) ) ?;
291
- gl. bind_vertex_array ( Some ( self . vertex_array_object ) ) ;
289
+ self . vertex_array_object = Some ( vao) ;
290
+ gl. bind_vertex_array ( self . vertex_array_object ) ;
292
291
}
293
292
}
294
293
@@ -333,7 +332,9 @@ impl Renderer {
333
332
334
333
#[ cfg( feature = "bind_vertex_array_support" ) ]
335
334
if self . gl_version . bind_vertex_array_support ( ) {
336
- unsafe { gl. delete_vertex_array ( self . vertex_array_object ) } ;
335
+ if let Some ( vao) = self . vertex_array_object . take ( ) {
336
+ unsafe { gl. delete_vertex_array ( vao) } ;
337
+ }
337
338
}
338
339
339
340
self . state_backup . post_render ( gl, self . gl_version ) ;
@@ -398,7 +399,7 @@ impl Renderer {
398
399
let projection_matrix = calculate_matrix ( draw_data, clip_origin_is_lower_left) ;
399
400
400
401
unsafe {
401
- gl. use_program ( Some ( self . shaders . program ) ) ;
402
+ gl. use_program ( self . shaders . program ) ;
402
403
gl. uniform_1_i32 ( Some ( & self . shaders . texture_uniform_location ) , 0 ) ;
403
404
gl. uniform_matrix_4_f32_slice (
404
405
Some ( & self . shaders . matrix_uniform_location ) ,
@@ -418,8 +419,8 @@ impl Renderer {
418
419
let color_field_offset = memoffset:: offset_of!( DrawVert , col) as _ ;
419
420
420
421
unsafe {
421
- gl. bind_buffer ( glow:: ARRAY_BUFFER , Some ( self . vbo_handle ) ) ;
422
- gl. bind_buffer ( glow:: ELEMENT_ARRAY_BUFFER , Some ( self . ebo_handle ) ) ;
422
+ gl. bind_buffer ( glow:: ARRAY_BUFFER , self . vbo_handle ) ;
423
+ gl. bind_buffer ( glow:: ELEMENT_ARRAY_BUFFER , self . ebo_handle ) ;
423
424
gl. enable_vertex_attrib_array ( self . shaders . position_attribute_index ) ;
424
425
gl. vertex_attrib_pointer_f32 (
425
426
self . shaders . position_attribute_index ,
@@ -483,7 +484,13 @@ impl Renderer {
483
484
let clip_x2 = ( clip_rect[ 2 ] - clip_off[ 0 ] ) * scale[ 0 ] ;
484
485
let clip_y2 = ( clip_rect[ 3 ] - clip_off[ 1 ] ) * scale[ 1 ] ;
485
486
486
- if clip_x1 >= fb_width || clip_y1 >= fb_height || clip_x2 < 0.0 || clip_y2 < 0.0 {
487
+ if clip_x1 >= fb_width
488
+ || clip_y1 >= fb_height
489
+ || clip_x2 < 0.0
490
+ || clip_y2 < 0.0
491
+ || clip_x2 <= clip_x1
492
+ || clip_y2 <= clip_y1
493
+ {
487
494
return ;
488
495
}
489
496
@@ -565,13 +572,13 @@ pub struct SimpleTextureMap();
565
572
impl TextureMap for SimpleTextureMap {
566
573
#[ inline( always) ]
567
574
fn register ( & mut self , gl_texture : glow:: Texture ) -> Option < imgui:: TextureId > {
568
- Some ( imgui:: TextureId :: new ( gl_texture as _ ) )
575
+ Some ( imgui:: TextureId :: new ( gl_texture. 0 . get ( ) as usize ) )
569
576
}
570
577
571
578
#[ inline( always) ]
572
579
fn gl_texture ( & self , imgui_texture : imgui:: TextureId ) -> Option < glow:: Texture > {
573
580
#[ allow( clippy:: cast_possible_truncation) ]
574
- Some ( imgui_texture. id ( ) as _ )
581
+ NonZeroU32 :: new ( imgui_texture. id ( ) as u32 ) . map ( glow :: NativeTexture )
575
582
}
576
583
}
577
584
@@ -637,7 +644,10 @@ impl GlStateBackup {
637
644
fn post_init ( & mut self , gl : & Context ) {
638
645
#[ allow( clippy:: cast_sign_loss) ]
639
646
unsafe {
640
- gl. bind_texture ( glow:: TEXTURE_2D , Some ( self . texture as _ ) ) ;
647
+ gl. bind_texture (
648
+ glow:: TEXTURE_2D ,
649
+ NonZeroU32 :: new ( self . texture as u32 ) . map ( glow:: NativeTexture ) ,
650
+ ) ;
641
651
}
642
652
}
643
653
@@ -658,7 +668,8 @@ impl GlStateBackup {
658
668
#[ cfg( feature = "bind_vertex_array_support" ) ]
659
669
if gl_version. bind_vertex_array_support ( ) {
660
670
self . vertex_array_object =
661
- Some ( gl. get_parameter_i32 ( glow:: VERTEX_ARRAY_BINDING ) as _ ) ;
671
+ NonZeroU32 :: new ( gl. get_parameter_i32 ( glow:: VERTEX_ARRAY_BINDING ) as _ )
672
+ . map ( glow:: NativeVertexArray )
662
673
}
663
674
664
675
#[ cfg( feature = "polygon_mode_support" ) ]
@@ -695,18 +706,24 @@ impl GlStateBackup {
695
706
fn post_render ( & mut self , gl : & Context , _gl_version : GlVersion ) {
696
707
#![ allow( clippy:: cast_sign_loss) ]
697
708
unsafe {
698
- gl. use_program ( Some ( self . program as _ ) ) ;
699
- gl. bind_texture ( glow:: TEXTURE_2D , Some ( self . texture as _ ) ) ;
709
+ gl. use_program ( NonZeroU32 :: new ( self . program as _ ) . map ( glow:: NativeProgram ) ) ;
710
+ gl. bind_texture (
711
+ glow:: TEXTURE_2D ,
712
+ NonZeroU32 :: new ( self . texture as _ ) . map ( glow:: NativeTexture ) ,
713
+ ) ;
700
714
#[ cfg( feature = "bind_sampler_support" ) ]
701
715
if let Some ( sampler) = self . sampler {
702
- gl. bind_sampler ( 0 , Some ( sampler as _ ) ) ;
716
+ gl. bind_sampler ( 0 , NonZeroU32 :: new ( sampler as _ ) . map ( glow :: NativeSampler ) ) ;
703
717
}
704
718
gl. active_texture ( self . active_texture as _ ) ;
705
719
#[ cfg( feature = "bind_vertex_array_support" ) ]
706
720
if let Some ( vao) = self . vertex_array_object {
707
721
gl. bind_vertex_array ( Some ( vao) ) ;
708
722
}
709
- gl. bind_buffer ( glow:: ARRAY_BUFFER , Some ( self . array_buffer as _ ) ) ;
723
+ gl. bind_buffer (
724
+ glow:: ARRAY_BUFFER ,
725
+ NonZeroU32 :: new ( self . array_buffer as _ ) . map ( glow:: NativeBuffer ) ,
726
+ ) ;
710
727
gl. blend_equation_separate (
711
728
self . blend_equation_rgb as _ ,
712
729
self . blend_equation_alpha as _ ,
@@ -774,7 +791,7 @@ impl GlStateBackup {
774
791
/// generate shaders which should work on a wide variety of modern devices
775
792
/// (GL >= 3.3 and GLES >= 2.0 are expected to work).
776
793
struct Shaders {
777
- program : GlProgram ,
794
+ program : Option < GlProgram > ,
778
795
texture_uniform_location : GlUniformLocation ,
779
796
matrix_uniform_location : GlUniformLocation ,
780
797
position_attribute_index : u32 ,
@@ -829,7 +846,7 @@ impl Shaders {
829
846
830
847
Ok ( unsafe {
831
848
Self {
832
- program,
849
+ program : Some ( program ) ,
833
850
texture_uniform_location : gl
834
851
. get_uniform_location ( program, "tex" )
835
852
. ok_or_else ( || ShaderError :: UniformNotFound ( "tex" . into ( ) ) ) ?,
0 commit comments