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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
use conrod_core::{
    image,
    mesh::{self, Mesh},
    render,
    text::rt,
    Rect, Scalar,
};
use std::collections::{HashMap, HashSet};

/// A loaded wgpu texture and it's width/height
pub struct Image {
    /// The immutable image type, represents the data loaded onto the GPU.
    ///
    /// Uses a dynamic format for flexibility on the kinds of images that might be loaded.
    pub texture: wgpu::Texture,
    /// The format of the `texture`.
    pub texture_format: wgpu::TextureFormat,
    /// The width of the image.
    pub width: u32,
    /// The height of the image.
    pub height: u32,
}

/// The data associated with a single vertex.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vertex {
    /// The normalised position of the vertex within vector space.
    ///
    /// [-1.0, 1.0] is the leftmost, bottom position of the display.
    /// [1.0, -1.0] is the rightmost, top position of the display.
    pub position: [f32; 2],
    /// The coordinates of the texture used by this `Vertex`.
    ///
    /// [0.0, 0.0] is the leftmost, top position of the texture.
    /// [1.0, 1.0] is the rightmost, bottom position of the texture.
    pub tex_coords: [f32; 2],
    /// Linear sRGB with an alpha channel.
    pub rgba: [f32; 4],
    /// The mode with which the `Vertex` will be drawn within the fragment shader.
    ///
    /// `0` for rendering text.
    /// `1` for rendering an image.
    /// `2` for rendering non-textured 2D geometry.
    ///
    /// If any other value is given, the fragment shader will not output any color.
    pub mode: u32,
}

/// A helper type aimed at simplifying the rendering of conrod primitives via wgpu.
pub struct Renderer {
    vs_mod: wgpu::ShaderModule,
    fs_mod: wgpu::ShaderModule,
    glyph_cache_tex: wgpu::Texture,
    _default_image_tex: wgpu::Texture,
    default_bind_group: wgpu::BindGroup,
    sampler: wgpu::Sampler,
    mesh: Mesh,
    // The texture format of the output attachment.
    dst_format: wgpu::TextureFormat,
    // The sample count of the output attachment.
    dst_sample_count: u32,
    // In order to support a dynamic number of images we maintain a unique bind group for each.
    bind_groups: HashMap<image::Id, wgpu::BindGroup>,
    // We also need a unique
    render_pipelines: HashMap<wgpu::TextureComponentType, Pipeline>,
}

/// Data that must be unique per `wgpu::TextureComponentType`, i.e. bind group layout and render
/// pipeline.
struct Pipeline {
    bind_group_layout: wgpu::BindGroupLayout,
    render_pipeline: wgpu::RenderPipeline,
}

/// An command for uploading an individual glyph.
pub struct GlyphCacheCommand<'a> {
    /// The CPU buffer containing the pixel data.
    pub glyph_cache_pixel_buffer: &'a [u8],
    /// The GPU image to which the glyphs are cached.
    pub glyph_cache_texture: &'a wgpu::Texture,
    /// The width of the texture.
    pub width: u32,
    /// The height of the texture.
    pub height: u32,
}

/// A render produced by the `Renderer::render` method.
pub struct Render<'a> {
    pub vertex_buffer: wgpu::Buffer,
    pub commands: Vec<RenderPassCommand<'a>>,
}

/// A draw command that maps directly to the `wgpu::CommandEncoder` method. By returning
/// `RenderPassCommand`s, we can avoid consuming the entire `AutoCommandBufferBuilder` itself which might
/// not always be available from APIs that wrap Vulkan.
pub enum RenderPassCommand<'a> {
    /// Specify the rectangle to which drawing should be cropped.
    SetScissor {
        top_left: [u32; 2],
        dimensions: [u32; 2],
    },
    /// Draw the specified range of vertices.
    Draw { vertex_range: std::ops::Range<u32> },
    /// A new image requires drawing and in turn a new bind group requires setting.
    SetBindGroup { bind_group: &'a wgpu::BindGroup },
    /// An image requiring a different bind group layout requires drawing and in turn, we must set
    /// the necessary render pipeline.
    SetPipeline { pipeline: &'a wgpu::RenderPipeline },
}

const GLYPH_TEX_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;
const GLYPH_TEX_COMPONENT_TY: wgpu::TextureComponentType = wgpu::TextureComponentType::Uint;
const DEFAULT_IMAGE_TEX_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;

impl mesh::ImageDimensions for Image {
    fn dimensions(&self) -> [u32; 2] {
        [self.width, self.height]
    }
}

impl Image {
    pub fn texture_component_type(&self) -> wgpu::TextureComponentType {
        texture_format_to_component_type(self.texture_format)
    }
}

impl Renderer {
    /// Construct a new `Renderer`.
    ///
    /// The `dst_sample_count` and `dst_format` refer to the associated properties of the output
    /// attachment to which the `Renderer` will draw. Note that if the `dst_sample_count` or
    /// `dst_format` change at runtime, the `Renderer` should be reconstructed.
    pub fn new(
        device: &wgpu::Device,
        dst_sample_count: u32,
        dst_format: wgpu::TextureFormat,
    ) -> Self {
        let glyph_cache_dims = mesh::DEFAULT_GLYPH_CACHE_DIMS;
        Self::with_glyph_cache_dimensions(device, dst_sample_count, dst_format, glyph_cache_dims)
    }

    /// Create a renderer with a specific size for the glyph cache.
    ///
    /// The `dst_sample_count` and `dst_format` refer to the associated properties of the output
    /// attachment to which the `Renderer` will draw. Note that if the `dst_sample_count` or
    /// `dst_format` change at runtime, the `Renderer` should be reconstructed.
    pub fn with_glyph_cache_dimensions(
        device: &wgpu::Device,
        dst_sample_count: u32,
        dst_format: wgpu::TextureFormat,
        glyph_cache_dims: [u32; 2],
    ) -> Self {
        assert_eq!(
            glyph_cache_dims[0] % 256,
            0,
            "wgpu glyph cache width must be multiple of 256"
        );

        // The mesh for converting primitives into vertices.
        let mesh = Mesh::with_glyph_cache_dimensions(glyph_cache_dims);

        // Load shader modules.
        let vs = include_bytes!("shaders/vert.spv");
        let vs_spirv = wgpu::read_spirv(std::io::Cursor::new(&vs[..]))
            .expect("failed to read hard-coded SPIRV");
        let vs_mod = device.create_shader_module(&vs_spirv);
        let fs = include_bytes!("shaders/frag.spv");
        let fs_spirv = wgpu::read_spirv(std::io::Cursor::new(&fs[..]))
            .expect("failed to read hard-coded SPIRV");
        let fs_mod = device.create_shader_module(&fs_spirv);

        // Create the glyph cache texture.
        let glyph_cache_tex_desc = glyph_cache_tex_desc(glyph_cache_dims);
        let glyph_cache_tex = device.create_texture(&glyph_cache_tex_desc);

        // Create the default image that is bound to `image_texture` along with a default bind
        // group for use in the case that there are no user supplied images.
        let default_image_tex_desc = default_image_tex_desc();
        let default_image_tex = device.create_texture(&default_image_tex_desc);

        // Create the sampler for sampling from the glyph cache and image textures.
        let sampler_desc = sampler_desc();
        let sampler = device.create_sampler(&sampler_desc);

        // Create at least one render pipeline for the default texture.
        let mut render_pipelines = HashMap::new();

        let default_tex_component_ty = texture_format_to_component_type(DEFAULT_IMAGE_TEX_FORMAT);
        let bind_group_layout = bind_group_layout(device, default_tex_component_ty);
        let pipeline_layout = pipeline_layout(device, &bind_group_layout);
        let render_pipeline = render_pipeline(
            device,
            &pipeline_layout,
            &vs_mod,
            &fs_mod,
            dst_format,
            dst_sample_count,
        );
        let default_bind_group = bind_group(
            device,
            &bind_group_layout,
            &glyph_cache_tex,
            &sampler,
            &default_image_tex,
        );
        let default_pipeline = Pipeline {
            bind_group_layout,
            render_pipeline,
        };
        render_pipelines.insert(default_tex_component_ty, default_pipeline);

        // The empty set of bind groups to be associated with user images.
        let bind_groups = Default::default();

        Self {
            vs_mod,
            fs_mod,
            glyph_cache_tex,
            _default_image_tex: default_image_tex,
            default_bind_group,
            sampler,
            dst_format,
            dst_sample_count,
            bind_groups,
            render_pipelines,
            mesh,
        }
    }

    /// Produce an `Iterator` yielding `Command`s.
    pub fn commands(&self) -> mesh::Commands {
        self.mesh.commands()
    }

    /// Fill the inner vertex and command buffers by translating the given `primitives`.
    ///
    /// This method may return an `Option<GlyphCacheCommand>`, in which case the user should use
    /// the contained `glyph_cpu_buffer_pool` to write the pixel data to the GPU, and then use a
    /// `copy_buffer_to_image` command to write the data to the given `glyph_cache_texture` image.
    pub fn fill<'a, P>(
        &'a mut self,
        image_map: &image::Map<Image>,
        viewport: [f32; 4],
        scale_factor: f64,
        primitives: P,
    ) -> Result<Option<GlyphCacheCommand<'a>>, rt::gpu_cache::CacheWriteErr>
    where
        P: render::PrimitiveWalker,
    {
        // Convert the given primitives into vertices.
        let [vp_l, vp_t, vp_r, vp_b] = viewport;
        let lt = [vp_l as Scalar, vp_t as Scalar];
        let rb = [vp_r as Scalar, vp_b as Scalar];
        let viewport = Rect::from_corners(lt, rb);
        let fill = self
            .mesh
            .fill(viewport, scale_factor, image_map, primitives)?;

        // Check whether or not we need a glyph cache update.
        let glyph_cache_cmd = match fill.glyph_cache_requires_upload {
            false => None,
            true => {
                let (width, height) = self.mesh.glyph_cache().dimensions();
                Some(GlyphCacheCommand {
                    glyph_cache_pixel_buffer: self.mesh.glyph_cache_pixel_buffer(),
                    glyph_cache_texture: &self.glyph_cache_tex,
                    width,
                    height,
                })
            }
        };
        Ok(glyph_cache_cmd)
    }

    /// Converts the inner list of `Command`s generated via `fill` to a list of
    /// `RenderPassCommand`s that are easily digestible by a `wgpu::RenderPass` produced by a
    /// `wgpu::CommandEncoder`.
    pub fn render(&mut self, device: &wgpu::Device, image_map: &image::Map<Image>) -> Render {
        let Renderer {
            ref mut bind_groups,
            ref mut render_pipelines,
            ref mut mesh,
            ref vs_mod,
            ref fs_mod,
            ref default_bind_group,
            ref glyph_cache_tex,
            ref sampler,
            dst_format,
            dst_sample_count,
            ..
        } = *self;

        let mut commands = vec![];

        // Ensure we have:
        // - a bind group layout and render pipeline for each unique texture component type.
        // - a bind group ready for each image in the map.
        let default_tct = texture_format_to_component_type(DEFAULT_IMAGE_TEX_FORMAT);
        let unique_tex_component_types: HashSet<_> = image_map
            .values()
            .map(|img| img.texture_component_type())
            .chain(Some(default_tct))
            .collect();
        bind_groups.retain(|k, _| image_map.contains_key(k));
        render_pipelines.retain(|tct, _| unique_tex_component_types.contains(tct));
        for (id, img) in image_map.iter() {
            // If we already have a bind group for this image move on.
            if bind_groups.contains_key(id) {
                continue;
            }

            // Retrieve the bind group layout and pipeline for the image's texture component type.
            let tct = img.texture_component_type();
            let pipeline = render_pipelines.entry(tct).or_insert_with(|| {
                let bind_group_layout = bind_group_layout(device, tct);
                let pipeline_layout = pipeline_layout(device, &bind_group_layout);
                let render_pipeline = render_pipeline(
                    device,
                    &pipeline_layout,
                    vs_mod,
                    fs_mod,
                    dst_format,
                    dst_sample_count,
                );
                Pipeline {
                    bind_group_layout,
                    render_pipeline,
                }
            });

            // Create the bind
            let bind_group = bind_group(
                device,
                &pipeline.bind_group_layout,
                &glyph_cache_tex,
                sampler,
                &img.texture,
            );
            bind_groups.insert(*id, bind_group);
        }

        // Prepare a single vertex buffer containing all vertices for all geometry.
        let vertices = mesh.vertices();
        let vertices_bytes = vertices_as_bytes(vertices);
        let usage = wgpu::BufferUsage::VERTEX;
        let vertex_buffer = device.create_buffer_with_data(vertices_bytes, usage);

        // Keep track of the currently set bind group.
        #[derive(PartialEq)]
        enum BindGroup {
            Default,
            Image(image::Id),
        }
        let mut bind_group = None;

        for command in mesh.commands() {
            match command {
                // Update the `scizzor` before continuing to draw.
                mesh::Command::Scizzor(s) => {
                    let top_left = [s.top_left[0] as u32, s.top_left[1] as u32];
                    let dimensions = s.dimensions;
                    let cmd = RenderPassCommand::SetScissor {
                        top_left,
                        dimensions,
                    };
                    commands.push(cmd);
                }

                // Draw to the target with the given `draw` command.
                mesh::Command::Draw(draw) => match draw {
                    // Draw text and plain 2D geometry.
                    mesh::Draw::Plain(vertex_range) => {
                        let vertex_count = vertex_range.len();
                        if vertex_count <= 0 {
                            continue;
                        }
                        // Ensure a render pipeline and bind group is set.
                        if bind_group.is_none() {
                            bind_group = Some(BindGroup::Default);
                            let pipeline = &render_pipelines[&default_tct].render_pipeline;
                            let cmd = RenderPassCommand::SetPipeline { pipeline };
                            commands.push(cmd);
                            let cmd = RenderPassCommand::SetBindGroup {
                                bind_group: default_bind_group,
                            };
                            commands.push(cmd);
                        }
                        let cmd = RenderPassCommand::Draw {
                            vertex_range: vertex_range.start as u32..vertex_range.end as u32,
                        };
                        commands.push(cmd);
                    }

                    // Draw an image whose texture data lies within the `image_map` at the
                    // given `id`.
                    mesh::Draw::Image(image_id, vertex_range) => {
                        let vertex_count = vertex_range.len();
                        if vertex_count == 0 {
                            continue;
                        }
                        // Ensure the bind group matches this image.
                        let expected_bind_group = Some(BindGroup::Image(image_id));
                        if bind_group != expected_bind_group {
                            // Check whether or not we need to switch pipelines.
                            let expected_tct = image_map[&image_id].texture_component_type();
                            let current_tct = bind_group.as_ref().map(|bg| match *bg {
                                BindGroup::Default => default_tct,
                                BindGroup::Image(id) => image_map[&id].texture_component_type(),
                            });
                            if current_tct != Some(expected_tct) {
                                let pipeline = &render_pipelines[&expected_tct].render_pipeline;
                                let cmd = RenderPassCommand::SetPipeline { pipeline };
                                commands.push(cmd);
                            }

                            // Now update the bind group and add the new bind group command.
                            bind_group = expected_bind_group;
                            let cmd = RenderPassCommand::SetBindGroup {
                                bind_group: &bind_groups[&image_id],
                            };
                            commands.push(cmd);
                        }
                        let cmd = RenderPassCommand::Draw {
                            vertex_range: vertex_range.start as u32..vertex_range.end as u32,
                        };
                        commands.push(cmd);
                    }
                },
            }
        }

        Render {
            vertex_buffer,
            commands,
        }
    }
}

impl<'a> GlyphCacheCommand<'a> {
    /// Creates a buffer on the GPU loaded with the updated pixel data.
    ///
    /// Created with `BufferUsage::COPY_SRC`, ready to be copied to the texture.
    ///
    /// TODO: In the future, we should consider re-using the same buffer and writing to it via
    /// `Buffer::map_write_async`. When asking about how to ensure that the write completes before
    /// the following `copy_buffer_to_texture` command, I was advised to just create a new buffer
    /// each time instead for now.
    /// EDIT:
    /// > if you try to map an existing buffer, it will give it to you only after all the gpu use
    /// > of the buffer is over. So you can't do it every frame reasonably
    pub fn create_buffer(&self, device: &wgpu::Device) -> wgpu::Buffer {
        device.create_buffer_with_data(&self.glyph_cache_pixel_buffer, wgpu::BufferUsage::COPY_SRC)
    }

    /// Create the copy view ready for copying the pixel data to the texture.
    pub fn buffer_copy_view<'b>(&self, buffer: &'b wgpu::Buffer) -> wgpu::BufferCopyView<'b> {
        wgpu::BufferCopyView {
            buffer,
            offset: 0,
            bytes_per_row: self.width,
            rows_per_image: self.height,
        }
    }

    /// Create the texture copy view ready for receiving the pixel data from the buffer.
    pub fn texture_copy_view(&self) -> wgpu::TextureCopyView {
        wgpu::TextureCopyView {
            texture: &self.glyph_cache_texture,
            mip_level: 0,
            array_layer: 0,
            origin: wgpu::Origin3d::ZERO,
        }
    }

    /// Encode the command for copying the buffer's pixel data to the glyph cache texture.
    pub fn encode(&self, buffer: &wgpu::Buffer, encoder: &mut wgpu::CommandEncoder) {
        let buffer_copy_view = self.buffer_copy_view(&buffer);
        let texture_copy_view = self.texture_copy_view();
        let extent = self.extent();
        encoder.copy_buffer_to_texture(buffer_copy_view, texture_copy_view, extent);
    }

    /// The extent required for the copy command.
    pub fn extent(&self) -> wgpu::Extent3d {
        wgpu::Extent3d {
            width: self.width,
            height: self.height,
            depth: 1,
        }
    }

    /// Short-hand for `create_buffer` and `encode` in succession.
    pub fn load_buffer_and_encode(&self, device: &wgpu::Device, e: &mut wgpu::CommandEncoder) {
        let buffer = self.create_buffer(&device);
        self.encode(&buffer, e);
    }
}

fn glyph_cache_tex_desc([width, height]: [u32; 2]) -> wgpu::TextureDescriptor<'static> {
    let depth = 1;
    let texture_extent = wgpu::Extent3d {
        width,
        height,
        depth,
    };
    wgpu::TextureDescriptor {
        label: Some("conrod_wgpu_glyph_cache_texture"),
        size: texture_extent,
        array_layer_count: 1,
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: GLYPH_TEX_FORMAT,
        usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
    }
}

fn default_image_tex_desc() -> wgpu::TextureDescriptor<'static> {
    let width = 64;
    let height = 64;
    let depth = 1;
    let texture_extent = wgpu::Extent3d {
        width,
        height,
        depth,
    };
    wgpu::TextureDescriptor {
        label: Some("conrod_wgpu_image_texture"),
        size: texture_extent,
        array_layer_count: 1,
        mip_level_count: 1,
        sample_count: 1,
        dimension: wgpu::TextureDimension::D2,
        format: DEFAULT_IMAGE_TEX_FORMAT,
        usage: wgpu::TextureUsage::SAMPLED,
    }
}

fn sampler_desc() -> wgpu::SamplerDescriptor {
    wgpu::SamplerDescriptor {
        address_mode_u: wgpu::AddressMode::ClampToEdge,
        address_mode_v: wgpu::AddressMode::ClampToEdge,
        address_mode_w: wgpu::AddressMode::ClampToEdge,
        mag_filter: wgpu::FilterMode::Linear,
        min_filter: wgpu::FilterMode::Linear,
        mipmap_filter: wgpu::FilterMode::Linear,
        lod_min_clamp: -100.0,
        lod_max_clamp: 100.0,
        compare: wgpu::CompareFunction::Always,
    }
}

fn bind_group_layout(
    device: &wgpu::Device,
    img_tex_component_ty: wgpu::TextureComponentType,
) -> wgpu::BindGroupLayout {
    let glyph_cache_texture_binding = wgpu::BindGroupLayoutEntry {
        binding: 0,
        visibility: wgpu::ShaderStage::FRAGMENT,
        ty: wgpu::BindingType::SampledTexture {
            multisampled: false,
            component_type: GLYPH_TEX_COMPONENT_TY,
            dimension: wgpu::TextureViewDimension::D2,
        },
    };
    let sampler_binding = wgpu::BindGroupLayoutEntry {
        binding: 1,
        visibility: wgpu::ShaderStage::FRAGMENT,
        ty: wgpu::BindingType::Sampler { comparison: false },
    };
    let image_texture_binding = wgpu::BindGroupLayoutEntry {
        binding: 2,
        visibility: wgpu::ShaderStage::FRAGMENT,
        ty: wgpu::BindingType::SampledTexture {
            multisampled: false,
            component_type: img_tex_component_ty,
            dimension: wgpu::TextureViewDimension::D2,
        },
    };
    let bindings = &[
        glyph_cache_texture_binding,
        sampler_binding,
        image_texture_binding,
    ];
    let desc = wgpu::BindGroupLayoutDescriptor {
        label: Some("conrod_bind_group_layout"),
        bindings,
    };
    device.create_bind_group_layout(&desc)
}

fn bind_group(
    device: &wgpu::Device,
    layout: &wgpu::BindGroupLayout,
    glyph_cache_tex: &wgpu::Texture,
    sampler: &wgpu::Sampler,
    image: &wgpu::Texture,
) -> wgpu::BindGroup {
    // Glyph cache texture view.
    let glyph_cache_tex_view = glyph_cache_tex.create_default_view();
    let glyph_cache_tex_binding = wgpu::Binding {
        binding: 0,
        resource: wgpu::BindingResource::TextureView(&glyph_cache_tex_view),
    };

    // Sampler binding.
    let sampler_binding = wgpu::Binding {
        binding: 1,
        resource: wgpu::BindingResource::Sampler(&sampler),
    };

    // Image texture view.
    let image_tex_view = image.create_default_view();
    let image_tex_binding = wgpu::Binding {
        binding: 2,
        resource: wgpu::BindingResource::TextureView(&image_tex_view),
    };

    let bindings = &[glyph_cache_tex_binding, sampler_binding, image_tex_binding];
    let label = Some("conrod_bind_group");
    let desc = wgpu::BindGroupDescriptor {
        label,
        layout,
        bindings,
    };
    device.create_bind_group(&desc)
}

fn pipeline_layout(
    device: &wgpu::Device,
    bind_group_layout: &wgpu::BindGroupLayout,
) -> wgpu::PipelineLayout {
    let desc = wgpu::PipelineLayoutDescriptor {
        bind_group_layouts: &[&bind_group_layout],
    };
    device.create_pipeline_layout(&desc)
}

fn vertex_attrs() -> [wgpu::VertexAttributeDescriptor; 4] {
    let position_offset = 0;
    let position_size = std::mem::size_of::<[f32; 2]>() as wgpu::BufferAddress;
    let tex_coords_offset = position_offset + position_size;
    let tex_coords_size = position_size;
    let rgba_offset = tex_coords_offset + tex_coords_size;
    let rgba_size = std::mem::size_of::<[f32; 4]>() as wgpu::BufferAddress;
    let mode_offset = rgba_offset + rgba_size;
    [
        // position
        wgpu::VertexAttributeDescriptor {
            format: wgpu::VertexFormat::Float2,
            offset: position_offset,
            shader_location: 0,
        },
        // tex_coords
        wgpu::VertexAttributeDescriptor {
            format: wgpu::VertexFormat::Float2,
            offset: tex_coords_offset,
            shader_location: 1,
        },
        // rgba
        wgpu::VertexAttributeDescriptor {
            format: wgpu::VertexFormat::Float4,
            offset: rgba_offset,
            shader_location: 2,
        },
        // mode
        wgpu::VertexAttributeDescriptor {
            format: wgpu::VertexFormat::Uint,
            offset: mode_offset,
            shader_location: 3,
        },
    ]
}

fn render_pipeline(
    device: &wgpu::Device,
    layout: &wgpu::PipelineLayout,
    vs_mod: &wgpu::ShaderModule,
    fs_mod: &wgpu::ShaderModule,
    dst_format: wgpu::TextureFormat,
    dst_sample_count: u32,
) -> wgpu::RenderPipeline {
    let vs_desc = wgpu::ProgrammableStageDescriptor {
        module: &vs_mod,
        entry_point: "main",
    };
    let fs_desc = wgpu::ProgrammableStageDescriptor {
        module: &fs_mod,
        entry_point: "main",
    };
    let raster_desc = wgpu::RasterizationStateDescriptor {
        front_face: wgpu::FrontFace::Ccw,
        cull_mode: wgpu::CullMode::None,
        depth_bias: 0,
        depth_bias_slope_scale: 0.0,
        depth_bias_clamp: 0.0,
    };
    let color_state_desc = wgpu::ColorStateDescriptor {
        format: dst_format,
        color_blend: wgpu::BlendDescriptor {
            src_factor: wgpu::BlendFactor::SrcAlpha,
            dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
            operation: wgpu::BlendOperation::Add,
        },
        alpha_blend: wgpu::BlendDescriptor {
            src_factor: wgpu::BlendFactor::One,
            dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
            operation: wgpu::BlendOperation::Add,
        },
        write_mask: wgpu::ColorWrite::ALL,
    };
    let vertex_attrs = vertex_attrs();
    let vertex_buffer_desc = wgpu::VertexBufferDescriptor {
        stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
        step_mode: wgpu::InputStepMode::Vertex,
        attributes: &vertex_attrs[..],
    };
    let vertex_state_desc = wgpu::VertexStateDescriptor {
        index_format: wgpu::IndexFormat::Uint16,
        vertex_buffers: &[vertex_buffer_desc],
    };
    let desc = wgpu::RenderPipelineDescriptor {
        layout,
        vertex_stage: vs_desc,
        fragment_stage: Some(fs_desc),
        rasterization_state: Some(raster_desc),
        primitive_topology: wgpu::PrimitiveTopology::TriangleList,
        color_states: &[color_state_desc],
        depth_stencil_state: None,
        vertex_state: vertex_state_desc,
        sample_count: dst_sample_count,
        sample_mask: !0,
        alpha_to_coverage_enabled: false,
    };
    device.create_render_pipeline(&desc)
}

fn vertices_as_bytes(s: &[mesh::Vertex]) -> &[u8] {
    let len = s.len() * std::mem::size_of::<mesh::Vertex>();
    let ptr = s.as_ptr() as *const u8;
    unsafe { std::slice::from_raw_parts(ptr, len) }
}

// TODO: Remove this when this lands https://github.com/gfx-rs/wgpu/pull/628
fn texture_format_to_component_type(format: wgpu::TextureFormat) -> wgpu::TextureComponentType {
    match format {
        wgpu::TextureFormat::R8Uint
        | wgpu::TextureFormat::R16Uint
        | wgpu::TextureFormat::Rg8Uint
        | wgpu::TextureFormat::R32Uint
        | wgpu::TextureFormat::Rg16Uint
        | wgpu::TextureFormat::Rgba8Uint
        | wgpu::TextureFormat::Rg32Uint
        | wgpu::TextureFormat::Rgba16Uint
        | wgpu::TextureFormat::Rgba32Uint => wgpu::TextureComponentType::Uint,

        wgpu::TextureFormat::R8Sint
        | wgpu::TextureFormat::R16Sint
        | wgpu::TextureFormat::Rg8Sint
        | wgpu::TextureFormat::R32Sint
        | wgpu::TextureFormat::Rg16Sint
        | wgpu::TextureFormat::Rgba8Sint
        | wgpu::TextureFormat::Rg32Sint
        | wgpu::TextureFormat::Rgba16Sint
        | wgpu::TextureFormat::Rgba32Sint => wgpu::TextureComponentType::Sint,

        wgpu::TextureFormat::R8Unorm
        | wgpu::TextureFormat::R8Snorm
        | wgpu::TextureFormat::R16Float
        | wgpu::TextureFormat::R32Float
        | wgpu::TextureFormat::Rg8Unorm
        | wgpu::TextureFormat::Rg8Snorm
        | wgpu::TextureFormat::Rg16Float
        | wgpu::TextureFormat::Rg11b10Float
        | wgpu::TextureFormat::Rg32Float
        | wgpu::TextureFormat::Rgba8Snorm
        | wgpu::TextureFormat::Rgba16Float
        | wgpu::TextureFormat::Rgba32Float
        | wgpu::TextureFormat::Rgba8Unorm
        | wgpu::TextureFormat::Rgba8UnormSrgb
        | wgpu::TextureFormat::Bgra8Unorm
        | wgpu::TextureFormat::Bgra8UnormSrgb
        | wgpu::TextureFormat::Rgb10a2Unorm
        | wgpu::TextureFormat::Depth32Float
        | wgpu::TextureFormat::Depth24Plus
        | wgpu::TextureFormat::Depth24PlusStencil8 => wgpu::TextureComponentType::Float,
    }
}