easel-rs 1.0.4

A shader playground for creating high resolution digital paintings.
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
use crate::dashboard::DashboardMessage;
use crate::push_constants::{load_push_constants_from_json, PushConstant};
use crate::texture::{default_color_sampler, AssetTexture, Texture};
use crate::uniforms::{load_uniforms_from_json, Uniforms, UserUniform};
use crate::vector::{IntVector2, IntVector4, Vector2, Vector4};
use chrono::Datelike;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::vec::Vec;
use stopwatch::Stopwatch;
use wgpu::util::{BufferInitDescriptor, DeviceExt};
use wgpu::{
    BindGroupDescriptor, BindGroupEntry, BindGroupLayoutDescriptor, BindGroupLayoutEntry,
    BindingResource, Extent3d, LoadOp, Operations, Origin3d, PowerPreference,
    RequestAdapterOptions,
};
use winit::{event::*, window::Window};

pub mod message;
use crate::postprocessing::PostProcess;
use log::{error, info, warn};
use message::CanvasMessage;
use notify::{DebouncedEvent, RecommendedWatcher, Watcher};

/// Pre-compile vertex shader that renders a full-screen quad.
pub static VS_MODULE_BYTES: &[u8] = include_bytes!("../../shaders/vert.spv");
/// The [wgpu::TextureFormat] used when rendering to screen.
pub static RENDER_TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
/// The [wgpu::TextureFormat] used when rendering off-screen painting to write to disk.
pub static PAINTING_TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
/// Built-in shader used as a post-processing effect to apply gamma sRGB conversion for painting.
/// This is needed as the [PAINTING_TEXTURE_FORMAT] does not perform automatic sRGB conversion for us.
static POST_PROCESS_SRGB_SHADER_BYTES: &[u8] =
    include_bytes!("../../shaders/post-process-srgb.spv");

/// Central class for the painting on the Easel.
/// Sends & receives messages to/from Dashboard.
/// Provides file watching capabilities for shader and/or custom uniforms.
pub struct Canvas {
    /// Handle to winit Window.
    pub window: Window,
    /// Handle to WebGPU Instance
    pub instance: wgpu::Instance,
    /// Handle to WebGPU render surface
    pub surface: wgpu::Surface,
    /// Handle to WebGPU Adapter
    pub adapter: wgpu::Adapter,
    /// Handle to WebGPU Device. Attempts to use highest performance GPU on system.
    pub device: wgpu::Device,
    /// Handle to command dispatch queue on device.
    pub queue: wgpu::Queue,
    /// Descriptor is kept around for window resizing events.
    sc_desc: wgpu::SwapChainDescriptor,
    /// Handle to swap chain for on-screen rendering.
    swap_chain: wgpu::SwapChain,
    /// Render pipeline used for on-screen rendering. May include post-processing effects, if provided.
    render_pipeline: wgpu::RenderPipeline,
    /// Render pipeline used for off-screen rendering. Will always include sRGB conversion post-processing effect.
    /// May also include other post-processing effects, if provided.
    painting_pipeline: wgpu::RenderPipeline,
    /// The pipeline use to render output of [Self::render_pipeline] to screen.
    swap_chain_pipeline: wgpu::RenderPipeline,
    /// Color with which to [wgpu::LoadOp::Clear] attachments to render passes.
    clear_color: wgpu::Color,
    /// Resolution of render canvas.
    /// **Note:** Distinct from the painting render resolution.
    size: winit::dpi::PhysicalSize<u32>,
    /// Uniforms provided by Canvas to all shaders.
    uniforms: Uniforms,
    /// Handle to device buffer where [Self::uniforms] are copied over.
    uniforms_device_buffer: wgpu::Buffer,
    /// Optional device buffer of user-provided uniforms.
    user_uniforms_buffer: Option<wgpu::Buffer>,
    /// Optional size of device buffer holding user-provided uniforms.
    user_uniforms_buffer_size: Option<usize>,
    /// Optional list of user-provided uniforms from JSON file.
    user_uniforms: Vec<Box<dyn UserUniform>>,
    /// Optional list of user-provided push constants from JSON file.
    push_constants: Option<Vec<Box<dyn PushConstant>>>,

    bind_groups: [wgpu::BindGroup; 2],
    bind_group_layouts: [wgpu::BindGroupLayout; 2],

    /// List of texture handles and their destination binding locations in the shader.
    #[allow(dead_code)]
    textures: Vec<Box<dyn Texture>>,
    /// List of post-processing shaders.
    /// By default includes sRGB Gamma application.
    postprocess_ops: Vec<PostProcess>,
    /// Stopwatch used for calculating time elapsed and other uniforms.
    stop_watch: Stopwatch,
    /// Pause/Play state. Also pauses [Self::stop_watch], which sets time data in [Self::uniforms].
    paused: bool,
    /// Time of last update. Use to calculate time deltas in [Self::uniforms].
    last_update: std::time::Instant,

    /// Used to send messages to Dashboard.
    transmitter: Sender<CanvasMessage>,
    /// Use to receive messages from Dashboard.
    receiver: Receiver<DashboardMessage>,
    /// Whether to show the window titlebar.
    show_titlebar: bool,

    /// Optional file watcher used to watch the fragment shader.
    shader_file_watcher: Option<RecommendedWatcher>,
    /// Optional receiver of file watcher events for the fragment shader.
    shader_file_watcher_receiver: Option<Receiver<DebouncedEvent>>,
    /// Optional file watcher used to watch the JSON file.
    json_file_watcher: Option<RecommendedWatcher>,
    /// Optional receiver of file watcher events for the JSON file.
    json_file_watcher_receiver: Option<Receiver<DebouncedEvent>>,
}

impl Canvas {
    /// Construct a new Canvas object
    /// * `window` - [winit::window::Window] to render to. Takes ownership
    /// * `fs_spirv_data` - Binary data of compiled fragment shader
    /// * `images` - Optional array of images to bind to shader. Images are bound in the same order as specified here.
    /// * `user_uniforms` - Optional array of user-specified uniforms to bind in shader. Uniforms are bound in same order as specified here.
    /// * `push_constants` - Optional array of push constants to bind in shader. Constants are bound in same order as specified here.
    /// * `transmitter` - [std::sync::mpsc::Sender] object used for sending [CanvasMessage]s to interested parties.
    /// * `receiver` - [std::sync::mpsc::Receiver] object used to received messages from [crate::dashboard::Dashboard]
    pub async fn new(
        window: Window,
        fs_spirv_data: Vec<u8>,
        images: Option<Vec<image::DynamicImage>>,
        user_uniforms: Option<Vec<Box<dyn UserUniform>>>,
        push_constants: Option<Vec<Box<dyn PushConstant>>>,
        transmitter: Sender<CanvasMessage>,
        receiver: Receiver<DashboardMessage>,
    ) -> Self {
        let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
        let size = window.inner_size();

        let surface: wgpu::Surface;
        unsafe {
            surface = instance.create_surface(&window);
        }

        let adapter = instance
            .request_adapter(&RequestAdapterOptions {
                compatible_surface: Some(&surface),
                power_preference: PowerPreference::HighPerformance,
            })
            .await
            .unwrap();
        // From: https://docs.rs/wgpu/0.6.2/wgpu/struct.Limits.html#structfield.max_push_constant_size
        let max_push_constant_size = match wgpu::BackendBit::PRIMARY {
            wgpu::BackendBit::VULKAN => 256,
            wgpu::BackendBit::DX12 => 256,
            wgpu::BackendBit::METAL => 4096,
            _ => 128,
        };
        let limits = wgpu::Limits {
            max_push_constant_size,
            ..Default::default()
        };
        let device_desc = wgpu::DeviceDescriptor {
            features: adapter.features(),
            limits,
            shader_validation: true,
        };

        let (device, queue) = adapter.request_device(&device_desc, None).await.unwrap();

        //------------------------------------------------------------------------------------------
        // Create uniforms, device buffer, and bindings.
        let mut uniforms = Uniforms::new();
        uniforms.resolution = Vector4::new(size.width as f32, size.height as f32, 0.0, 0.0);
        uniforms.num_textures = match &images {
            Some(vec) => vec.len() as u32,
            None => 0,
        };
        let descriptor = BufferInitDescriptor {
            label: Some("Uniforms Buffer"),
            contents: bytemuck::bytes_of(&uniforms),
            usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
        };
        let u_buffer = device.create_buffer_init(&descriptor);

        //------------------------------------------------------------------------------------------
        // Bind custom uniforms, if provided
        let mut custom_uniforms_buffer = None;
        let mut custom_uniforms_buffer_size = 0;
        if let Some(dem_uniforms) = &user_uniforms {
            let mut total_size = 0;
            for a_uniform in dem_uniforms {
                total_size += a_uniform.size();
            }

            custom_uniforms_buffer_size = total_size;
            let mut bytes = Vec::with_capacity(total_size);
            for a_uniform in dem_uniforms {
                bytes.extend_from_slice(&a_uniform.bytes());
            }

            let desc = BufferInitDescriptor {
                label: Some("Custom Uniforms Buffer"),
                contents: &bytes,
                usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
            };

            custom_uniforms_buffer = Some(device.create_buffer_init(&desc));
        }

        //------------------------------------------------------------------------------------------
        // Load textures.
        let mut asset_textures = Vec::<Box<dyn Texture>>::new();
        if let Some(vec) = images {
            for an_image in &vec {
                asset_textures.push(Box::new(AssetTexture::new_with_image(
                    an_image, &device, &queue,
                )));
            }
        }

        //------------------------------------------------------------------------------------------
        // Setup swap chain
        let sc_desc = wgpu::SwapChainDescriptor {
            usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
            format: wgpu::TextureFormat::Bgra8UnormSrgb,
            width: size.width,
            height: size.height,
            present_mode: wgpu::PresentMode::Fifo,
        };
        let swap_chain = device.create_swap_chain(&surface, &sc_desc);

        //------------------------------------------------------------------------------------------
        // Load shaders.
        let vs_module = device.create_shader_module(wgpu::util::make_spirv(VS_MODULE_BYTES));
        let fs_module = device.create_shader_module(wgpu::util::make_spirv(&fs_spirv_data));

        //------------------------------------------------------------------------------------------
        // Create the bind group layout and entries.
        // Uniforms and our generated textures are set 0
        let primary_bind_group_layout: wgpu::BindGroupLayout;
        {
            let mut bind_group_layout_entries = Vec::<wgpu::BindGroupLayoutEntry>::new();
            // Uniforms are first.
            bind_group_layout_entries.push(wgpu::BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStage::FRAGMENT,
                ty: wgpu::BindingType::UniformBuffer {
                    dynamic: false,
                    min_binding_size: None,
                },
                count: None,
            });
            if let Some(_) = custom_uniforms_buffer {
                bind_group_layout_entries.push(wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStage::FRAGMENT,
                    ty: wgpu::BindingType::UniformBuffer {
                        dynamic: false,
                        min_binding_size: None,
                    },
                    count: None,
                });
            }
            primary_bind_group_layout =
                device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: None,
                    entries: &bind_group_layout_entries,
                });
        }

        // In set 1, bind provided textures.
        let secondary_bind_group_layout: wgpu::BindGroupLayout;
        {
            let mut bind_group_layout_entries = Vec::<wgpu::BindGroupLayoutEntry>::new();
            // For now, we only have 1 sampler per set
            bind_group_layout_entries.push(BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStage::FRAGMENT,
                ty: wgpu::BindingType::Sampler { comparison: false },
                count: None,
            });
            for i in 1..=asset_textures.len() {
                bind_group_layout_entries.push(BindGroupLayoutEntry {
                    binding: i as u32,
                    visibility: wgpu::ShaderStage::FRAGMENT,
                    ty: wgpu::BindingType::SampledTexture {
                        component_type: wgpu::TextureComponentType::Float,
                        multisampled: true,
                        dimension: wgpu::TextureViewDimension::D2,
                    },
                    count: None,
                });
            }
            // Create the Bind Group Layout.
            secondary_bind_group_layout =
                device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                    label: None,
                    entries: &bind_group_layout_entries,
                });
        }

        //------------------------------------------------------------------------------------------
        // Create Bind Groups from layouts.
        let primary_bind_group: wgpu::BindGroup;
        {
            let mut primary_bind_group_entries: Vec<BindGroupEntry> = Vec::new();
            // Provided Uniforms first.
            primary_bind_group_entries.push(wgpu::BindGroupEntry {
                binding: 0,
                resource: BindingResource::Buffer(
                    u_buffer.slice(0..(std::mem::size_of_val(&uniforms) as u64)),
                ),
            });
            // Custom Uniforms next, if enabled.
            if let Some(cu_buffer) = &custom_uniforms_buffer {
                primary_bind_group_entries.push(wgpu::BindGroupEntry {
                    binding: 1,
                    resource: BindingResource::Buffer(
                        cu_buffer.slice(0..(custom_uniforms_buffer_size as u64)),
                    ),
                });
            }

            // Finally create the bind group.
            primary_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Primary Bind Group"),
                layout: &primary_bind_group_layout,
                entries: &primary_bind_group_entries,
            });
        }

        let secondary_bind_group: wgpu::BindGroup;
        {
            let mut secondary_bind_group_entries: Vec<BindGroupEntry> = Vec::new();
            let default_sampler = default_color_sampler(&device);
            secondary_bind_group_entries.push(BindGroupEntry {
                binding: 0,
                resource: BindingResource::Sampler(&default_sampler),
            });
            // Create texture views.
            let mut tex_views = Vec::<wgpu::TextureView>::new();
            for tex in &asset_textures {
                let texture_view = tex.get_view(0);
                tex_views.push(texture_view);
            }
            // Add texture view bindings.
            for tex_bind_idx in 1..=tex_views.len() {
                secondary_bind_group_entries.push(BindGroupEntry {
                    binding: tex_bind_idx as u32,
                    resource: BindingResource::TextureView(&tex_views[tex_bind_idx - 1]),
                });
            }
            secondary_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
                label: Some("Secondary Bind Group"),
                layout: &secondary_bind_group_layout,
                entries: &secondary_bind_group_entries,
            });
        }

        //------------------------------------------------------------------------------------------
        // Create render pipeline.
        let mut constants_for_pipeline = vec![];
        if let Some(constants) = push_constants.as_ref() {
            let mut size = 0;
            for a_constant in constants {
                size += a_constant.size();
            }
            constants_for_pipeline.push(wgpu::PushConstantRange {
                stages: wgpu::ShaderStage::FRAGMENT,
                range: 0..(size as u32),
            });
        }
        let render_pipeline_layout =
            device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("Canvas Pipeline Layout"),
                bind_group_layouts: &[&primary_bind_group_layout, &secondary_bind_group_layout],
                push_constant_ranges: &constants_for_pipeline,
            });
        let (render_pipeline, painting_pipeline) = crate::utils::create_pipelines(
            &device,
            &render_pipeline_layout,
            &vs_module,
            &fs_module,
            (RENDER_TEXTURE_FORMAT, PAINTING_TEXTURE_FORMAT),
        );
        // Swap chain pipeline will never change and is separate from others.
        let swap_chain_pipeline =
            crate::utils::create_swap_chain_pipeline(&device, &vs_module, sc_desc.format);

        let postprocess_ops: Vec<PostProcess> = vec![PostProcess::new(
            &device,
            Vec::from(POST_PROCESS_SRGB_SHADER_BYTES),
            custom_uniforms_buffer.is_some(),
        )];
        let mut custom_size = None;
        if custom_uniforms_buffer_size > 0 {
            custom_size = Some(custom_uniforms_buffer_size);
        }

        // Inform dashboard of our window size so that it has a sensible default for painting res.
        transmitter
            .send(CanvasMessage::UpdatePaintingResolutioninGUI(
                IntVector2::new(size.width as i32, size.height as i32),
            ))
            .unwrap();
        Self {
            window,
            instance,
            surface,
            adapter,
            device,
            queue,
            sc_desc,
            swap_chain,
            render_pipeline,
            painting_pipeline,
            swap_chain_pipeline,
            clear_color: wgpu::Color {
                r: 0.1,
                g: 0.2,
                b: 0.3,
                a: 1.0,
            },
            size,
            uniforms,
            user_uniforms_buffer: custom_uniforms_buffer,
            user_uniforms_buffer_size: custom_size,
            user_uniforms: match user_uniforms {
                Some(uni) => uni,
                None => vec![],
            },
            push_constants,
            uniforms_device_buffer: u_buffer,
            bind_groups: [primary_bind_group, secondary_bind_group],
            bind_group_layouts: [primary_bind_group_layout, secondary_bind_group_layout],
            textures: asset_textures,
            postprocess_ops,
            stop_watch: Stopwatch::start_new(),
            paused: false,
            last_update: std::time::Instant::now(),
            transmitter,
            receiver,
            show_titlebar: true,
            shader_file_watcher: None,
            shader_file_watcher_receiver: None,
            json_file_watcher: None,
            json_file_watcher_receiver: None,
        }
    }

    /// Expected to be called from main thread when user resizes canvas window.
    pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
        self.size = new_size;
        self.sc_desc.width = new_size.width;
        self.sc_desc.height = new_size.height;
        self.swap_chain = self.device.create_swap_chain(&self.surface, &self.sc_desc);
        self.uniforms.resolution.x = new_size.width as f32;
        self.uniforms.resolution.y = new_size.height as f32;
        self.transmitter
            .send(CanvasMessage::WindowResized(IntVector2::new(
                new_size.width as i32,
                new_size.height as i32,
            )))
            .unwrap();
    }

    /// Expected to be called from main thread to handle IO events.
    /// This fn assumes the incoming events are from the Canvas' window.
    pub fn input(&mut self, event: &WindowEvent) -> bool {
        match event {
            WindowEvent::KeyboardInput { input, .. } => match input {
                KeyboardInput {
                    state: ElementState::Pressed,
                    virtual_keycode: Some(VirtualKeyCode::Space),
                    ..
                } => {
                    self.paused = !self.paused;
                    if self.paused {
                        self.stop_watch.stop();
                    } else {
                        self.stop_watch.start();
                    }
                    self.transmitter
                        .send(CanvasMessage::PausePlayChanged)
                        .unwrap();
                    true
                }
                KeyboardInput {
                    state: ElementState::Pressed,
                    virtual_keycode: Some(VirtualKeyCode::Escape),
                    ..
                } => false,
                _ => true,
            },
            WindowEvent::CursorMoved { position, .. } => {
                self.uniforms.mouse_info.z = self.uniforms.mouse_info.x;
                self.uniforms.mouse_info.w = self.uniforms.mouse_info.y;
                self.uniforms.mouse_info.x = position.x as f32;
                self.uniforms.mouse_info.y = position.y as f32;
                // Send message.
                self.transmitter
                    .send(CanvasMessage::MouseMoved(Vector2::new(
                        self.uniforms.mouse_info.x,
                        self.uniforms.mouse_info.y,
                    )))
                    .unwrap();
                true
            }
            WindowEvent::Resized(physical_size) => {
                self.resize(*physical_size);
                true
            }
            WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
                // new_inner_size is &mut so w have to dereference it twice
                self.resize(**new_inner_size);
                true
            }
            _ => false,
        }
    }

    /// Used to parse messages received from Dashboard and act accordingly.
    fn dashboard_signal_received(&mut self, message: DashboardMessage) {
        match message {
            DashboardMessage::PausePlayChanged => {
                self.paused = !self.paused;
                if self.paused {
                    self.stop_watch.stop();
                } else {
                    self.stop_watch.start();
                }
            }
            DashboardMessage::Pause => {
                self.paused = true;
                self.stop_watch.stop();
            }
            DashboardMessage::Play => {
                self.paused = false;
                self.stop_watch.start();
            }
            DashboardMessage::TitlebarStatusChanged => {
                self.show_titlebar = !self.show_titlebar;
                self.window.set_decorations(self.show_titlebar);
            }
            DashboardMessage::RasterOutputRequested(resolution) => self.create_painting(resolution),
            DashboardMessage::UniformUpdatedViaGUI(modified_uniform) => {
                let user_uniforms = &mut self.user_uniforms;
                if let Some(index) = user_uniforms
                    .iter()
                    .position(|uniform| uniform.name() == modified_uniform.name())
                {
                    user_uniforms[index] = modified_uniform;
                }
            }
        }
    }

    /// Called every frame prior to render.
    /// Updates uniforms, checks watched files (if any), examines messages from Dashboard.
    pub fn update(&mut self) {
        // Receive messages from Dashboard and act accordingly
        loop {
            let msg_result = self.receiver.try_recv();
            match msg_result {
                Ok(msg) => self.dashboard_signal_received(msg),
                Err(_) => break,
            }
        }

        {
            // Check if shader file watcher reports file updated.
            let mut file_events = Vec::new();
            match &self.shader_file_watcher_receiver {
                Some(rx) => loop {
                    let msg_result = rx.try_recv();
                    match msg_result {
                        Ok(event) => file_events.push(event),
                        Err(_) => break,
                    }
                },
                None => {}
            }
            for an_event in file_events {
                self.update_shader_pipeline(an_event);
            }
        }
        {
            let mut file_events = Vec::new();
            // Check if json file watcher reports file updated.
            match &self.json_file_watcher_receiver {
                Some(rx) => loop {
                    let msg_result = rx.try_recv();
                    match msg_result {
                        Ok(event) => file_events.push(event),
                        Err(_) => break,
                    }
                },
                None => {}
            }
            for an_event in file_events {
                self.update_custom_uniforms_from_file(an_event);
            }
        }
        // Referesh user uniforms buffer
        if let Some(buffer) = &self.user_uniforms_buffer {
            let mut total_size = 0;
            for a_uniform in &self.user_uniforms {
                total_size += a_uniform.size();
            }
            let mut bytes = Vec::with_capacity(total_size);
            for a_uniform in &self.user_uniforms {
                bytes.extend_from_slice(&a_uniform.bytes());
            }
            self.queue.write_buffer(&buffer, 0, &bytes);
        }

        // Only actually update uniforms if not paused, but we always update buffer.
        if !self.paused {
            self.uniforms.frame_num += 1;
            self.uniforms.time = self.stop_watch.elapsed().as_secs_f32();
            let now = std::time::Instant::now();
            let delta_duration = now.duration_since(self.last_update);
            self.uniforms.time_delta = delta_duration.as_secs_f32();
            let today = chrono::Local::now();
            self.uniforms.date =
                IntVector4::new(today.year(), today.month() as i32, today.day() as i32, 0);
            self.last_update = now;
        }
        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Update Uniforms Encoder"),
            });
        // Copy uniforms from CPU to staging buffer, then copy from staging buffer to main buf.
        let descriptor = BufferInitDescriptor {
            label: Some("Uniforms Buffer"),
            contents: bytemuck::bytes_of(&self.uniforms),
            usage: wgpu::BufferUsage::COPY_SRC,
        };
        let staging_buffer = self.device.create_buffer_init(&descriptor);

        encoder.copy_buffer_to_buffer(
            &staging_buffer,
            0,
            &self.uniforms_device_buffer,
            0,
            std::mem::size_of::<Uniforms>() as u64,
        );
        let command_buffer = encoder.finish();
        self.queue.submit(Some(command_buffer));
    }

    /// Render the shader on the canvas.
    pub fn render_canvas(&mut self) {
        if self.paused {
            return;
        }
        let frame = match self.swap_chain.get_current_frame() {
            Ok(frame) => frame,
            Err(frame_err) => {
                self.transmitter
                    .send(CanvasMessage::SwapChainFrameError(frame_err))
                    .unwrap();
                return;
            }
        };
        // Create the texture to render to.
        let tex_desc = wgpu::TextureDescriptor {
            size: Extent3d {
                width: self.size.width,
                height: self.size.height,
                depth: 1,
            },
            format: RENDER_TEXTURE_FORMAT,
            usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
            label: Some("Canvas Render"),
            dimension: wgpu::TextureDimension::D2,
            mip_level_count: 1,
            sample_count: 1,
        };
        let render_tex = self.device.create_texture(&tex_desc);
        let render_tex_view = render_tex.create_view(&wgpu::TextureViewDescriptor::default());

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Render Encoder"),
            });

        // First, render using the shader.
        {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
                    attachment: &render_tex_view,
                    resolve_target: None,
                    ops: Operations {
                        load: LoadOp::Clear(self.clear_color),
                        store: true,
                    },
                }],
                depth_stencil_attachment: None,
            });

            for i in 0..self.bind_groups.len() {
                render_pass.set_bind_group(i as u32, &self.bind_groups[i], &[]);
            }
            render_pass.set_pipeline(&self.render_pipeline);
            // Set push constants, if any.
            if let Some(constants) = self.push_constants.as_ref() {
                let mut offset: usize = 0;
                for a_constant in constants {
                    let bytes = a_constant.bytes();
                    render_pass.set_push_constants(
                        wgpu::ShaderStage::FRAGMENT,
                        offset as u32,
                        &bytes,
                    );
                    offset += a_constant.size();
                }
            }
            render_pass.draw(0..3, 0..1);
        }

        // We can't create bind groups with swap chain textures, so have to create another temp tex.
        let postprocessing_tex = self.device.create_texture(&tex_desc);
        let postprocessing_tex_view =
            postprocessing_tex.create_view(&wgpu::TextureViewDescriptor::default());

        // Then render any post-processing effects.
        let mut stage_in = &render_tex_view;
        let mut stage_out = &postprocessing_tex_view;
        // The last op is always the sRGB conversion, which we don't need when rendering to screen.
        for i in 0..self.postprocess_ops.len() - 1 {
            let postprocess_op = &self.postprocess_ops[i];
            // If user has provided custom uniforms, pass them to the post-processing stage as well.
            let mut custom_data = None;
            if let Some(custom_buffer) = self.user_uniforms_buffer.as_ref() {
                custom_data = Some((custom_buffer, self.user_uniforms_buffer_size.unwrap()));
            }
            postprocess_op.post_process(
                stage_in,
                stage_out,
                (
                    &self.uniforms_device_buffer,
                    std::mem::size_of_val(&self.uniforms),
                ),
                custom_data,
                &self.device,
                &mut encoder,
                self.clear_color,
                false,
            );
            // Swap input and output textures handles
            std::mem::swap(&mut stage_in, &mut stage_out);
        }
        // Swap one more time to get final output tex (undo last swap).
        std::mem::swap(&mut stage_in, &mut stage_out);

        // Render back to swap chain texture.
        // Build new specialized bind groups for this render pass.
        let sc_layout = self
            .device
            .create_bind_group_layout(&BindGroupLayoutDescriptor {
                label: None,
                entries: &[
                    BindGroupLayoutEntry {
                        binding: 0,
                        count: None,
                        visibility: wgpu::ShaderStage::FRAGMENT,
                        ty: wgpu::BindingType::Sampler { comparison: false },
                    },
                    BindGroupLayoutEntry {
                        binding: 1,
                        count: None,
                        visibility: wgpu::ShaderStage::FRAGMENT,
                        ty: wgpu::BindingType::SampledTexture {
                            component_type: wgpu::TextureComponentType::Float,
                            dimension: wgpu::TextureViewDimension::D2,
                            multisampled: true,
                        },
                    },
                ],
            });
        let sc_bind_group = self.device.create_bind_group(&BindGroupDescriptor {
            label: Some("Swap Chain Render Pass Bind Group"),
            layout: &sc_layout,
            entries: &[
                BindGroupEntry {
                    binding: 0,
                    resource: BindingResource::Sampler(&default_color_sampler(&self.device)),
                },
                BindGroupEntry {
                    binding: 1,
                    resource: BindingResource::TextureView(stage_out),
                },
            ],
        });
        {
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
                    attachment: &frame.output.view,
                    resolve_target: None,
                    ops: Operations {
                        load: LoadOp::Clear(self.clear_color),
                        store: true,
                    },
                }],
                depth_stencil_attachment: None,
            });

            render_pass.set_bind_group(0, &sc_bind_group, &[]);

            render_pass.set_pipeline(&self.swap_chain_pipeline);
            render_pass.draw(0..3, 0..1);
        }

        let command_buffer = encoder.finish();
        self.queue.submit(Some(command_buffer));

        self.transmitter
            .send(CanvasMessage::RenderPassSubmitted)
            .unwrap();
        self.transmitter.send(CanvasMessage::FrameStep).unwrap();
    }

    /// Similar to [Self::render_canvas()], but renders to a very high bit-depth texture and writes output to file.
    /// **Note:** File is written to disk asynchronously.
    pub fn create_painting(&mut self, resolution: IntVector2) {
        let painting_tex_desc = wgpu::TextureDescriptor {
            size: Extent3d {
                width: resolution.x as u32,
                height: resolution.y as u32,
                depth: 1,
            },
            format: PAINTING_TEXTURE_FORMAT,
            usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT
                | wgpu::TextureUsage::COPY_SRC
                | wgpu::TextureUsage::SAMPLED,
            label: Some("Painting"),
            dimension: wgpu::TextureDimension::D2,
            mip_level_count: 1,
            sample_count: 1,
        };

        // Texture to render the painting too.
        let painting = self.device.create_texture(&painting_tex_desc);
        // Create the output texture for post-processing.
        let post_process_tex = self.device.create_texture(&painting_tex_desc);

        // Buffer to copy texture into after all rendering finishes.
        let buffer_desc = wgpu::BufferDescriptor {
            label: Some("Painting Staging Buffer"),
            usage: wgpu::BufferUsage::COPY_DST | wgpu::BufferUsage::MAP_READ,
            size: ((resolution.x * resolution.y) as usize * std::mem::size_of::<half::f16>() * 4)
                as u64,
            mapped_at_creation: false,
        };
        let buffer = self.device.create_buffer(&buffer_desc);

        let mut encoder = self
            .device
            .create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("Painting Encoder"),
            });

        let painting_start_time = std::time::Instant::now();
        // First run the pipeline.
        {
            let painting_view = painting.create_view(&wgpu::TextureViewDescriptor::default());
            let mut render_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
                    attachment: &painting_view,
                    resolve_target: None,
                    ops: Operations {
                        load: LoadOp::Clear(self.clear_color),
                        store: true,
                    },
                }],
                depth_stencil_attachment: None,
            });

            for i in 0..self.bind_groups.len() {
                render_pass.set_bind_group(i as u32, &self.bind_groups[i], &[]);
            }
            render_pass.set_pipeline(&self.painting_pipeline);
            // Set push constants, if any.
            if let Some(constants) = self.push_constants.as_ref() {
                let mut offset: usize = 0;
                for a_constant in constants {
                    let bytes = a_constant.bytes();
                    render_pass.set_push_constants(
                        wgpu::ShaderStage::FRAGMENT,
                        offset as u32,
                        &bytes,
                    );
                    offset += a_constant.size();
                }
            }
            render_pass.draw(0..3, 0..1);
        }

        // Then run all post-processing steps, in order.
        let mut stage_in = &painting;
        let mut stage_out = &post_process_tex;
        for postprocess_op in &mut self.postprocess_ops {
            // If user has provided custom uniforms, pass them to the post-processing stage as well.
            let mut custom_data = None;
            if let Some(custom_buffer) = self.user_uniforms_buffer.as_ref() {
                custom_data = Some((custom_buffer, self.user_uniforms_buffer_size.unwrap()));
            }
            let input_view = stage_in.create_view(&wgpu::TextureViewDescriptor::default());
            let output_view = stage_out.create_view(&wgpu::TextureViewDescriptor::default());
            postprocess_op.post_process(
                &input_view,
                &output_view,
                (
                    &self.uniforms_device_buffer,
                    std::mem::size_of_val(&self.uniforms),
                ),
                custom_data,
                &self.device,
                &mut encoder,
                self.clear_color,
                true,
            );
            // Swap input and output textures handles
            std::mem::swap(&mut stage_in, &mut stage_out);
        }
        // Swap one more time to get final output tex (undo last swap).
        std::mem::swap(&mut stage_in, &mut stage_out);

        // Then encode a copy of the texture to the buffer.
        {
            let tex_copy_view = wgpu::TextureCopyView {
                mip_level: 0,
                origin: Origin3d::ZERO,
                texture: stage_out,
            };
            let buf_copy_view = wgpu::BufferCopyView {
                buffer: &buffer,
                layout: wgpu::TextureDataLayout {
                    bytes_per_row: ((resolution.x * 4) as usize * std::mem::size_of::<half::f16>())
                        as u32,
                    offset: 0,
                    rows_per_image: resolution.y as u32,
                },
            };
            encoder.copy_texture_to_buffer(
                tex_copy_view,
                buf_copy_view,
                Extent3d {
                    width: resolution.x as u32,
                    height: resolution.y as u32,
                    depth: 1,
                },
            );
        }

        let command_buffer = encoder.finish();
        self.queue.submit(Some(command_buffer));

        self.transmitter
            .send(CanvasMessage::PaintingStarted(
                buffer,
                resolution,
                painting_start_time,
            ))
            .unwrap();
    }

    /// Use to trigger automatic reload when shader is changed on disk.
    /// Works for both text source and SPIR-V binaries
    pub fn watch_shader_file(&mut self, file: &str, interval_ms: u64) {
        let (tx, rx) = channel();
        let mut file_watcher =
            notify::watcher(tx, std::time::Duration::from_millis(interval_ms)).unwrap();
        file_watcher
            .watch(file, notify::RecursiveMode::NonRecursive)
            .expect("Invalid file provided.");

        self.shader_file_watcher = Some(file_watcher);
        self.shader_file_watcher_receiver = Some(rx);
    }

    /// Use to trigger automatic reload when uniforms file is changed on disk.
    pub fn watch_uniforms_file(&mut self, file: &str, interval_ms: u64) {
        let (tx, rx) = channel();
        let mut file_watcher =
            notify::watcher(tx, std::time::Duration::from_millis(interval_ms)).unwrap();
        file_watcher
            .watch(file, notify::RecursiveMode::NonRecursive)
            .expect("Invalid file provided.");

        self.json_file_watcher = Some(file_watcher);
        self.json_file_watcher_receiver = Some(rx);
    }

    /// Reload uniforms file from disk and update render pipelines.
    fn update_custom_uniforms_from_file(&mut self, event: DebouncedEvent) {
        let mut disable = false;
        match event {
            DebouncedEvent::Create(path_buf) | DebouncedEvent::Write(path_buf) => {
                let file = path_buf.to_str().unwrap();
                info!("Detected uniforms JSON file changed, reloading {}", file);
                let text =
                    std::fs::read_to_string(file).expect("Error reading uniforms from file.");
                let json_data = json::parse(&text).expect("Error parsing JSON");
                self.user_uniforms = load_uniforms_from_json(&json_data);
                self.push_constants = Some(load_push_constants_from_json(&json_data));
            }
            DebouncedEvent::Remove(path_buf) => {
                info!(
                    "Uniforms JSON file {} removed, disabling file watcher.",
                    path_buf.to_str().unwrap()
                );
                disable = true;
            }
            DebouncedEvent::Rename(src, _) => {
                info!(
                    "Uniforms JSON file {} renamed, disabling file watcher.",
                    src.to_str().unwrap()
                );
                disable = true;
            }
            DebouncedEvent::Error(err, buf) => {
                warn!("Encountered error {:?}", err);
                match buf {
                    Some(path) => warn!("File: {}", path.to_str().unwrap()),
                    None => {}
                }
                warn!("Disabling file watcher.");
                disable = true;
            }
            _ => {}
        }
        if disable {
            self.json_file_watcher_receiver = None;
            self.json_file_watcher = None
        }
    }

    /// Reload shader from disk and update render pipelines
    fn update_shader_pipeline(&mut self, event: DebouncedEvent) {
        let mut disable = false;
        match event {
            DebouncedEvent::Create(path_buf) | DebouncedEvent::Write(path_buf) => {
                let file = path_buf.to_str().unwrap();
                let fs_spirv_data = match crate::utils::load_shader(file) {
                    Ok(data) => data,
                    Err(e) => {
                        error!("Error compiling shader: {}", e);
                        self.transmitter
                            .send(CanvasMessage::ShaderCompilationFailed(e.to_string()))
                            .unwrap();
                        return;
                    }
                };
                let fs_module = self
                    .device
                    .create_shader_module(wgpu::util::make_spirv(&fs_spirv_data));
                let vs_module = self
                    .device
                    .create_shader_module(wgpu::util::make_spirv(VS_MODULE_BYTES));

                let layouts = [&self.bind_group_layouts[0], &self.bind_group_layouts[1]];
                let mut constants_for_pipeline = vec![];
                if let Some(constants) = self.push_constants.as_ref() {
                    let mut size = 0;
                    for a_constant in constants {
                        size += a_constant.size();
                    }
                    constants_for_pipeline.push(wgpu::PushConstantRange {
                        stages: wgpu::ShaderStage::FRAGMENT,
                        range: 0..(size as u32),
                    });
                }
                let render_pipeline_layout =
                    self.device
                        .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                            label: Some("Canvas Pipeline Layout"),
                            bind_group_layouts: &layouts,
                            push_constant_ranges: &constants_for_pipeline,
                        });
                let (render_pipeline, painting_pipeline) = crate::utils::create_pipelines(
                    &self.device,
                    &render_pipeline_layout,
                    &vs_module,
                    &fs_module,
                    (RENDER_TEXTURE_FORMAT, PAINTING_TEXTURE_FORMAT),
                );

                self.render_pipeline = render_pipeline;
                self.painting_pipeline = painting_pipeline;

                self.transmitter
                    .send(CanvasMessage::ShaderCompilationSucceeded)
                    .unwrap();
                info!("Detected shader file changed, reloading {}", file);
            }
            DebouncedEvent::Remove(path_buf) => {
                info!(
                    "Shader file {} removed, disabling file watcher.",
                    path_buf.to_str().unwrap()
                );
                disable = true;
            }
            DebouncedEvent::Rename(src, _) => {
                info!(
                    "Shader file {} renamed, disabling file watcher.",
                    src.to_str().unwrap()
                );
                disable = true;
            }
            DebouncedEvent::Error(err, buf) => {
                warn!("Encountered error {:?}", err);
                match buf {
                    Some(path) => warn!("File: {}", path.to_str().unwrap()),
                    None => {}
                }
                warn!("Disabling file watcher.");
                disable = true;
            }
            _ => {}
        }
        if disable {
            self.shader_file_watcher_receiver = None;
            self.shader_file_watcher = None
        }
    }

    pub fn add_post_processing_shader(&mut self, shader_data: Vec<u8>) {
        let postprocess = PostProcess::new(
            &self.device,
            shader_data,
            self.user_uniforms_buffer.is_some(),
        );
        // We have a default included post-processing stage that is run in the painting pipeline
        // for doing sRGB conversion. That must always run last.
        self.postprocess_ops
            .insert(self.postprocess_ops.len() - 1, postprocess);
    }

    /// Time to exit, cleanup resources.
    pub fn exit_requested(&mut self) {
        self.shader_file_watcher = None;
        self.shader_file_watcher_receiver = None;
        self.json_file_watcher = None;
        self.json_file_watcher_receiver = None;
    }

    /// Expected to be called immediately after the render() function.
    pub fn post_render(&mut self) {
        // Inform Dashboard of each of our user-provided uniforms.
        for a_uniform in &self.user_uniforms {
            let uni = a_uniform.copy();
            self.transmitter
                .send(CanvasMessage::UniformForGUI(uni))
                .unwrap();
        }
    }
}