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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
mod shader;

use vulkano::{buffer::{BufferAccess, BufferUsage, CpuBufferPool}, command_buffer::SubpassContents};
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState, sys::*};
use vulkano::descriptor::descriptor_set::{DescriptorSet, PersistentDescriptorSet};
use vulkano::descriptor::PipelineLayoutAbstract;
use vulkano::descriptor::descriptor::ShaderStages;
use vulkano::device::{Device, Queue};
use vulkano::pipeline::{GraphicsPipeline, GraphicsPipelineCreationError, GraphicsPipelineAbstract, input_assembly::IndexType};
use vulkano::sync::GpuFuture;

use vulkano::image::{ImmutableImage, Dimensions, AttachmentImage};
use vulkano::sampler::{Sampler};
// use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};
use vulkano::format::{Format, ClearValue};
use vulkano::framebuffer::{Subpass, RenderPassAbstract};
use vulkano::pipeline::viewport::Scissor;
use vulkano::framebuffer::{Framebuffer, FramebufferAbstract};
use vulkano::pipeline::viewport::Viewport;

use vulkano::image::ImageViewAccess;

use std::sync::Arc;
use std::fmt;

use imgui::{DrawVert, Textures, DrawCmd, DrawCmdParams, internal::RawWrapper, TextureId, ImString};

#[derive(Default, Debug, Clone)]
#[repr(C)]
struct Vertex {
    pub pos: [f32; 2],
    pub uv : [f32; 2],
    pub col: u32,
    // pub col: [u8; 4],
}

vulkano::impl_vertex!(Vertex, pos, uv, col);

impl From<DrawVert> for Vertex {
    fn from(v : DrawVert) -> Vertex {
        unsafe{std::mem::transmute(v)}
    }
}

#[derive(Debug)]
pub enum RendererError {
    GraphicsPipelineCreationError(GraphicsPipelineCreationError),
    DeviceMemoryAllocError(vulkano::memory::DeviceMemoryAllocError),
    OomError(vulkano::OomError),
    DrawIndexedError(vulkano::command_buffer::DrawIndexedError),
    PersistentDescriptorSetError(vulkano::descriptor::descriptor_set::PersistentDescriptorSetError),
    PersistentDescriptorSetBuildError(vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuildError),
    FlushError(vulkano::sync::FlushError),
    SamplerCreationError(vulkano::sampler::SamplerCreationError),
    ImageCreationError(vulkano::image::ImageCreationError),
    BeginRenderPassError(vulkano::command_buffer::BeginRenderPassError),
    AutoCommandBufferBuilderContextError(vulkano::command_buffer::AutoCommandBufferBuilderContextError),
    WriteLockError(vulkano::buffer::cpu_access::WriteLockError),
    FramebufferCreationError(vulkano::framebuffer::FramebufferCreationError),
    CopyBufferError(vulkano::command_buffer::CopyBufferError),
    BadTexture(TextureId),
    BadImageDimensions(Dimensions),
}

impl fmt::Display for RendererError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            &Self::GraphicsPipelineCreationError(ref e) => {
                write!(f, "Error creating rendering pipeline: {}", e)
            },
            &Self::DeviceMemoryAllocError(ref e) => {
                write!(f, "Error allocating memory on the device: {}", e)
            },
            &Self::OomError(ref e) => {
                write!(f, "Out of memory: {}", e)
            },
            &Self::DrawIndexedError(ref e) => {
                write!(f, "Error creating draw_indexed command: {}", e)
            },
            &Self::PersistentDescriptorSetError(ref e) => {
                write!(f, "Error creating descriptor set: {}", e)
            },
            &Self::PersistentDescriptorSetBuildError(ref e) => {
                write!(f, "Error error building descriptor set: {}", e)
            },
            &Self::FlushError(ref e) => {
                write!(f, "Error flushing futures: {}", e)
            },
            &Self::SamplerCreationError(ref e) => {
                write!(f, "Error creating sampler: {}", e)
            },
            &Self::ImageCreationError(ref e) => {
                write!(f, "Error creating image: {}", e)
            },
            &Self::BeginRenderPassError(ref e) => {
                write!(f, "Error starting render pass: {}", e)
            },
            &Self::AutoCommandBufferBuilderContextError(ref e) => {
                write!(f, "Error adding to command buffer: {}", e)
            },
            &Self::WriteLockError(ref e) => {
                write!(f, "Could not acquire buffer write lock: {}", e)
            },
            &Self::FramebufferCreationError(ref e) => {
                write!(f, "Error creating framebuffer {}", e)
            },
            &Self::CopyBufferError(ref e) => {
                write!(f, "Error copying buffer: {:?}", e)
            },
            &Self::BadTexture(ref t) => {
                write!(f, "The Texture ID could not be found: {:?}", t)
            },
            &Self::BadImageDimensions(d) => {
                write!(f, "Image Dimensions not supported (must be Dim2d): {:?}", d)
            },
        }
    }
}

impl std::error::Error for RendererError {}

impl From<GraphicsPipelineCreationError> for RendererError {
    fn from(e : GraphicsPipelineCreationError) -> Self {
        RendererError::GraphicsPipelineCreationError(e)
    }
}
impl From<vulkano::memory::DeviceMemoryAllocError> for RendererError {
    fn from(e : vulkano::memory::DeviceMemoryAllocError) -> Self {
        RendererError::DeviceMemoryAllocError(e)
    }
}
impl From<vulkano::OomError> for RendererError {
    fn from(e : vulkano::OomError) -> Self {
        RendererError::OomError(e)
    }
}
impl From<vulkano::command_buffer::DrawIndexedError> for RendererError {
    fn from(e : vulkano::command_buffer::DrawIndexedError) -> Self {
        RendererError::DrawIndexedError(e)
    }
}
impl From<vulkano::descriptor::descriptor_set::PersistentDescriptorSetError> for RendererError {
    fn from(e : vulkano::descriptor::descriptor_set::PersistentDescriptorSetError) -> Self {
        RendererError::PersistentDescriptorSetError(e)
    }
}
impl From<vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuildError> for RendererError {
    fn from(e : vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuildError) -> Self {
        RendererError::PersistentDescriptorSetBuildError(e)
    }
}
impl From<vulkano::sync::FlushError> for RendererError {
    fn from(e : vulkano::sync::FlushError) -> Self {
        RendererError::FlushError(e)
    }
}
impl From<vulkano::sampler::SamplerCreationError> for RendererError {
    fn from(e : vulkano::sampler::SamplerCreationError) -> Self {
        RendererError::SamplerCreationError(e)
    }
}
impl From<vulkano::command_buffer::BeginRenderPassError> for RendererError {
    fn from(e : vulkano::command_buffer::BeginRenderPassError) -> Self {
        RendererError::BeginRenderPassError(e)
    }
}
impl From<vulkano::command_buffer::AutoCommandBufferBuilderContextError> for RendererError {
    fn from(e : vulkano::command_buffer::AutoCommandBufferBuilderContextError) -> Self {
        RendererError::AutoCommandBufferBuilderContextError(e)
    }
}
impl From<vulkano::buffer::cpu_access::WriteLockError> for RendererError {
    fn from(e : vulkano::buffer::cpu_access::WriteLockError) -> Self {
        RendererError::WriteLockError(e)
    }
}
impl From<vulkano::framebuffer::FramebufferCreationError> for RendererError {
    fn from(e : vulkano::framebuffer::FramebufferCreationError) -> Self {
        RendererError::FramebufferCreationError(e)
    }
}
impl From<vulkano::image::ImageCreationError> for RendererError {
    fn from(e : vulkano::image::ImageCreationError) -> Self {
        RendererError::ImageCreationError(e)
    }
}
impl From<vulkano::command_buffer::CopyBufferError> for RendererError {
    fn from(e : vulkano::command_buffer::CopyBufferError) -> Self {
        RendererError::CopyBufferError(e)
    }
}


pub type Texture = (Arc<dyn ImageViewAccess + Send + Sync>, Arc<Sampler>);

pub struct Renderer {
    render_pass : Arc<dyn RenderPassAbstract + Send + Sync>,
    pipeline : Arc<dyn GraphicsPipelineAbstract + Send + Sync>,
    font_texture : Texture,
    textures : Textures<Texture>,
    ds: Vec<Arc<dyn DescriptorSet>>,
    framebuffer: Option<Arc<dyn FramebufferAbstract>>,
    vrt_buffer_pool : CpuBufferPool<Vertex>,
    idx_buffer_pool : CpuBufferPool<u16>,
}

impl Renderer {

    /// Initialize the renderer object, including vertex buffers, ImGui font textures,
    /// and the Vulkan graphics pipeline.
    /// 
    /// ---
    /// 
    /// `ctx`: the ImGui `Context` object
    /// 
    /// `device`: the Vulkano `Device` object for the device you want to render the UI on.
    /// 
    /// `queue`: the Vulkano `Queue` object for the queue the font atlas texture will be created on.
    /// 
    /// `format`: the Vulkano `Format` that the render pass will use when storing the frame in the target image.
    pub fn init(ctx: &mut imgui::Context, device : Arc<Device>, queue : Arc<Queue>, format : Format) -> Result<Renderer, RendererError> {

        let vs = shader::vs::Shader::load(device.clone()).unwrap();
        let fs = shader::fs::Shader::load(device.clone()).unwrap();

        let render_pass = Arc::new(
            vulkano::single_pass_renderpass!(
                device.clone(),
                attachments: {
                    color: {
                        load: Load,
                        store: Store,
                        format: format,
                        samples: 1,
                    }
                },
                pass: {
                    color: [color],
                    depth_stencil: {}
                }
            )
            .unwrap(),
        );

        let pipeline = Arc::new(GraphicsPipeline::start()
            .vertex_input_single_buffer::<Vertex>()
            .vertex_shader(vs.main_entry_point(), ())
            .triangle_list()
            .viewports_scissors_dynamic(1)
            .fragment_shader(fs.main_entry_point(), ())
            .blend_alpha_blending()
            .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
            .build(device.clone())?);


        let textures = Textures::new();

        let font_texture = Self::upload_font_texture(ctx.fonts(), device.clone(), queue.clone())?;


        ctx.set_renderer_name(Some(ImString::from(format!("imgui-vulkano-renderer {}", env!("CARGO_PKG_VERSION")))));

        let vrt_buffer_pool = CpuBufferPool::new(device.clone(), BufferUsage::vertex_buffer_transfer_destination());
        let idx_buffer_pool = CpuBufferPool::new(device.clone(), BufferUsage::index_buffer_transfer_destination());

        Ok(Renderer {
            render_pass : Arc::new(render_pass),
            pipeline : pipeline as Arc<dyn GraphicsPipelineAbstract + Send + Sync>,
            font_texture,
            textures,
            ds: vec![],
            framebuffer: None,
            vrt_buffer_pool,
            idx_buffer_pool,
        })
    }

    /// Appends the draw commands for the UI frame to an `AutoCommandBufferBuilder`.
    /// 
    /// ---
    /// 
    /// `cmd_buf_builder`: An `AutoCommandBufferBuilder` from vulkano to add commands to
    /// 
    /// `device`: the Vulkano `Device` object for the device you want to render the UI on
    /// 
    /// `queue`: the Vulkano `Queue` object for buffer creation
    /// 
    /// `target`: the target image to render to
    /// 
    /// `draw_data`: the ImGui `DrawData` that each UI frame creates
    pub fn draw_commands<I, P>(&mut self, cmd_buf_builder : &mut AutoCommandBufferBuilder<P>, _queue : Arc<Queue>, target : I, draw_data : &imgui::DrawData) -> Result<(), RendererError> 
    where I: ImageViewAccess + Send + Sync + 'static {

        let fb_width = draw_data.display_size[0] * draw_data.framebuffer_scale[0];
        let fb_height = draw_data.display_size[1] * draw_data.framebuffer_scale[1];
        if !(fb_width > 0.0 && fb_height > 0.0) {
            return Ok(());
        }
        let left = draw_data.display_pos[0];
        let right = draw_data.display_pos[0] + draw_data.display_size[0];
        let top = draw_data.display_pos[1];
        let bottom = draw_data.display_pos[1] + draw_data.display_size[1];

        let pc = shader::vs::ty::VertPC {
            matrix : [
                [(2.0 / (right - left)), 0.0, 0.0, 0.0],
                [0.0, (2.0 / (bottom - top)), 0.0, 0.0],
                [0.0, 0.0, -1.0, 0.0],
                [
                    (right + left) / (left - right),
                    (top + bottom) / (top - bottom),
                    0.0,
                    1.0,
                ],
            ]
        };

        let dims = match target.dimensions() {
            Dimensions::Dim2d {width, height} => {[width, height]},
            d => { return Err(RendererError::BadImageDimensions(d));}
        };

        let mut dynamic_state = DynamicState::default();
        dynamic_state.viewports = Some(vec![
            Viewport {
                origin: [0.0, 0.0],
                dimensions: [dims[0] as f32, dims[1] as f32],
                depth_range: 0.0 .. 1.0,
            }
        ]);
        dynamic_state.scissors = Some(vec![
            Scissor::default()
        ]);

        let clip_off = draw_data.display_pos;
        let clip_scale = draw_data.framebuffer_scale;

        
        let layout = self.pipeline.descriptor_set_layout(0).unwrap();

        let framebuffer = Arc::new(Framebuffer::start(self.render_pass.clone())
            .add(target)?.build()?);

        cmd_buf_builder.begin_render_pass(framebuffer, SubpassContents::Inline, vec![ClearValue::None])?;

        for draw_list in draw_data.draw_lists() {
            
            let vertex_buffer = Arc::new(self.vrt_buffer_pool.chunk(draw_list.vtx_buffer().iter().map(|&v| Vertex::from(v))).unwrap());
            let index_buffer  = Arc::new(self.idx_buffer_pool.chunk(draw_list.idx_buffer().iter().cloned()).unwrap());

            for cmd in draw_list.commands() {
                match cmd {
                    DrawCmd::Elements {
                        count,
                        cmd_params:
                            DrawCmdParams {
                                clip_rect,
                                texture_id,
                                // vtx_offset,
                                idx_offset,
                                ..
                            },
                    } => {
                        let clip_rect = [
                            (clip_rect[0] - clip_off[0]) * clip_scale[0],
                            (clip_rect[1] - clip_off[1]) * clip_scale[1],
                            (clip_rect[2] - clip_off[0]) * clip_scale[0],
                            (clip_rect[3] - clip_off[1]) * clip_scale[1],
                        ];

                        if clip_rect[0] < fb_width
                            && clip_rect[1] < fb_height
                            && clip_rect[2] >= 0.0
                            && clip_rect[3] >= 0.0
                        {

                            if let Some(ref mut scissors) = dynamic_state.scissors {
                                scissors[0] = Scissor {
                                    origin: [
                                        f32::max(0.0, clip_rect[0]).floor() as i32,
                                        f32::max(0.0, clip_rect[1]).floor() as i32
                                    ],
                                    dimensions: [
                                        (clip_rect[2] - clip_rect[0]).abs().ceil() as u32,
                                        (clip_rect[3] - clip_rect[1]).abs().ceil() as u32
                                    ],
                                };
                            }

                            let tex = self.lookup_texture(texture_id)?;

                            let set = Arc::new(PersistentDescriptorSet::start(layout.clone())
                                .add_sampled_image(tex.0.clone(), tex.1.clone())?
                                .build()?
                            );

                            cmd_buf_builder.draw_indexed(
                                self.pipeline.clone(), 
                                &dynamic_state, 
                                vec![vertex_buffer.clone()], 
                                index_buffer.clone().into_buffer_slice().slice(idx_offset..(idx_offset+count)).unwrap(),
                                set,
                                pc)?;
                        }
                    }
                    DrawCmd::ResetRenderState => (), // TODO
                    DrawCmd::RawCallback { callback, raw_cmd } => unsafe {
                        callback(draw_list.raw(), raw_cmd)
                    },
                }
            }
        }
        cmd_buf_builder.end_render_pass()?;

        Ok(())
    }

    pub unsafe fn unsafe_draw_commands<I, P>(&mut self, cmd_buf_builder : &mut UnsafeCommandBufferBuilder<P>, _queue : Arc<Queue>, target : I, draw_data : &imgui::DrawData) -> Result<(), RendererError> 
     where I: ImageViewAccess + Send + Sync + 'static {

        let fb_width = draw_data.display_size[0] * draw_data.framebuffer_scale[0];
        let fb_height = draw_data.display_size[1] * draw_data.framebuffer_scale[1];
        if !(fb_width > 0.0 && fb_height > 0.0) {
            return Ok(());
        }
        let left = draw_data.display_pos[0];
        let right = draw_data.display_pos[0] + draw_data.display_size[0];
        let top = draw_data.display_pos[1];
        let bottom = draw_data.display_pos[1] + draw_data.display_size[1];

        let pc = shader::vs::ty::VertPC {
            matrix : [
                [(2.0 / (right - left)), 0.0, 0.0, 0.0],
                [0.0, (2.0 / (bottom - top)), 0.0, 0.0],
                [0.0, 0.0, -1.0, 0.0],
                [
                    (right + left) / (left - right),
                    (top + bottom) / (top - bottom),
                    0.0,
                    1.0,
                ],
            ]
        };

        let dims = match ImageViewAccess::dimensions(&target) {
            Dimensions::Dim2d {width, height} => {[width, height]},
            d => { return Err(RendererError::BadImageDimensions(d));}
        };

        let mut dynamic_state = DynamicState::default();
        dynamic_state.viewports = Some(vec![
            Viewport {
                origin: [0.0, 0.0],
                dimensions: [dims[0] as f32, dims[1] as f32],
                depth_range: 0.0 .. 1.0,
            }
        ]);
        dynamic_state.scissors = Some(vec![
            Scissor::default()
        ]);

        let clip_off = draw_data.display_pos;
        let clip_scale = draw_data.framebuffer_scale;

        // let vtx_buf_len = draw_data.total_vtx_count as usize;
        // let idx_buf_len = draw_data.total_idx_count as usize;

        let mut vertexes = vec![];
        let mut indexes = vec![];

        for draw_list in draw_data.draw_lists() {
            // update the vertex and index buffers
            vertexes.extend(draw_list.vtx_buffer().iter().map(|&v| Vertex::from(v)));
            indexes.extend(draw_list.idx_buffer().iter().cloned());
        }

        let layout = self.pipeline.descriptor_set_layout(0).unwrap();

        let framebuffer = Arc::new(Framebuffer::start(self.render_pass.clone())
            .add(target)?.build()?);

        self.framebuffer = Some(framebuffer.clone());
        // cmd_buf_builder.copy_buffer(self.vertex_buffer.clone(), self.vertex_dev_buffer.clone())?;
        // cmd_buf_builder.copy_buffer(self.index_buffer.clone(), self.index_dev_buffer.clone())?;

        unsafe{
            cmd_buf_builder.begin_render_pass(&framebuffer, SubpassContents::Inline, vec![ClearValue::None].iter().map(|c| *c));
        }

        let mut dl_vtx_offset = 0;
        let mut dl_idx_offset = 0;

        self.ds.clear();
        for draw_list in draw_data.draw_lists() {

            let vertex_buffer = Arc::new(self.vrt_buffer_pool.chunk(draw_list.vtx_buffer().iter().map(|&v| Vertex::from(v))).unwrap());
            let index_buffer  = Arc::new(self.idx_buffer_pool.chunk(draw_list.idx_buffer().iter().cloned()).unwrap());

            for cmd in draw_list.commands() {
                match cmd {
                    DrawCmd::Elements {
                        count,
                        cmd_params:
                            DrawCmdParams {
                                clip_rect,
                                texture_id,
                                vtx_offset,
                                idx_offset,
                                ..
                            },
                    } => {
                        let clip_rect = [
                            (clip_rect[0] - clip_off[0]) * clip_scale[0],
                            (clip_rect[1] - clip_off[1]) * clip_scale[1],
                            (clip_rect[2] - clip_off[0]) * clip_scale[0],
                            (clip_rect[3] - clip_off[1]) * clip_scale[1],
                        ];

                        if clip_rect[0] < fb_width
                            && clip_rect[1] < fb_height
                            && clip_rect[2] >= 0.0
                            && clip_rect[3] >= 0.0
                        {

                            if let Some(ref mut scissors) = dynamic_state.scissors {
                                scissors[0] = Scissor {
                                    origin: [
                                        f32::max(0.0, clip_rect[0]).floor() as i32,
                                        f32::max(0.0, clip_rect[1]).floor() as i32
                                    ],
                                    dimensions: [
                                        (clip_rect[2] - clip_rect[0]).abs().ceil() as u32,
                                        (clip_rect[3] - clip_rect[1]).abs().ceil() as u32
                                    ],
                                };
                            }

                            let tex = self.lookup_texture(texture_id)?;

                            let set = Arc::new(PersistentDescriptorSet::start(layout.clone())
                                .add_sampled_image(tex.0.clone(), tex.1.clone())?
                                .build()?
                            );
                            self.ds.push(set.clone());

                            unsafe {
                                cmd_buf_builder.bind_pipeline_graphics(&self.pipeline.clone());
                                cmd_buf_builder.bind_vertex_buffers(0, {
                                    let mut builder = UnsafeCommandBufferBuilderBindVertexBuffer::new();
                                    builder.add(&vertex_buffer);
                                    builder
                                });
                                cmd_buf_builder.bind_index_buffer(&index_buffer, IndexType::U16);
                                cmd_buf_builder.bind_descriptor_sets(
                                    true,
                                    &self.pipeline,
                                    0,
                                    [set.inner()].iter().cloned(),
                                    [].iter().map(|d| *d),
                                );
                                cmd_buf_builder.push_constants(
                                    &self.pipeline,
                                    ShaderStages {
                                        vertex: true,
                                        .. ShaderStages::none()
                                    },
                                    0,
                                    std::mem::size_of::<shader::vs::ty::VertPC>() as u32,
                                    &pc,
                                );
                                cmd_buf_builder.set_viewport(0, dynamic_state.viewports.as_ref().unwrap().iter().cloned());
                                cmd_buf_builder.set_scissor(0, dynamic_state.scissors.as_ref().unwrap().iter().cloned());

                                cmd_buf_builder.draw_indexed(index_buffer.size()  as u32 / 2, 1, 0, 0, 0);
                            }
                        }
                    }
                    DrawCmd::ResetRenderState => (), // TODO
                    DrawCmd::RawCallback { callback, raw_cmd } => unsafe {
                        callback(draw_list.raw(), raw_cmd)
                    },
                }
            }

            dl_vtx_offset += draw_list.vtx_buffer().len();
            dl_idx_offset += draw_list.idx_buffer().len();
        }
        unsafe {
            cmd_buf_builder.end_render_pass();
        }

        Ok(())
    }
    
    /// Update the ImGui font atlas texture.
    /// 
    /// ---
    /// 
    /// `ctx`: the ImGui `Context` object
    /// 
    /// `device`: the Vulkano `Device` object for the device you want to render the UI on.
    /// 
    /// `queue`: the Vulkano `Queue` object for the queue the font atlas texture will be created on.
    pub fn reload_font_texture(
        &mut self,
        ctx: &mut imgui::Context,
        device : Arc<Device>,
        queue : Arc<Queue>,
    ) -> Result<(), RendererError> {
        self.font_texture = Self::upload_font_texture(ctx.fonts(), device, queue)?;
        Ok(())
    }

    /// Get the texture library that the renderer uses
    pub fn textures(&mut self) -> &mut Textures<Texture> {
        &mut self.textures
    }

    fn upload_font_texture(
        mut fonts: imgui::FontAtlasRefMut,
        device : Arc<Device>,
        queue : Arc<Queue>,
    ) -> Result<Texture, RendererError> {
        let texture = fonts.build_rgba32_texture();

        let (image, fut) = ImmutableImage::from_iter(
            texture.data.iter().cloned(),
            Dimensions::Dim2d{
                width : texture.width,
                height : texture.height,
            },
            vulkano::image::MipmapsCount::One,
            Format::R8G8B8A8Srgb,
            queue.clone(),
            )?;

        fut.then_signal_fence_and_flush()?
            .wait(None)?;

        let sampler = Sampler::simple_repeat_linear(device.clone());

        fonts.tex_id = TextureId::from(usize::MAX);
        Ok((image, sampler))
    }

    fn lookup_texture(&self, texture_id: TextureId) -> Result<&Texture, RendererError> {
        if texture_id.id() == usize::MAX {
            Ok(&self.font_texture)
        } else if let Some(texture) = self.textures.get(texture_id) {
            Ok(texture)
        } else {
            Err(RendererError::BadTexture(texture_id))
        }
    }
}