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
use crate::vertex_2d::{ColoredVertex2D, Vertex2D};
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use std::collections::HashMap;
use vulkano::buffer::{BufferAccess, BufferUsage, ImmutableBuffer, CpuAccessibleBuffer};
use std::sync::Arc;
use vulkano::format::{ClearValue, Format};
use vulkano::framebuffer::{FramebufferAbstract, Framebuffer, RenderPass, RenderPassAbstract};
use vulkano::device::{Device, Queue};
use vulkano::instance::PhysicalDevice;
use vulkano::image::immutable::ImmutableImage;
use vulkano::image::{Dimensions, ImageAccess, ImageDimensions, SwapchainImage, ImageUsage, AttachmentImage};
use vulkano::sampler::{Sampler, SamplerAddressMode, MipmapMode, Filter};
use vulkano::descriptor::DescriptorSet;
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use std::path::PathBuf;
use image::GenericImageView;
use std::iter::FromIterator;
use vulkano::swapchain::Capabilities;
use winit::Window;
use vulkano::pipeline::viewport::Viewport;
use vulkano::descriptor::descriptor::DescriptorDescTy::TexelBuffer;
use crate::canvas_frame::CanvasFrame;
use std::hash::Hash;
use crate::canvas_shader::{CanvasShader, CanvasShaderHandle};
use crate::canvas_buffer::{CanvasImage, CanvasTexture};

/// Vertex trait for Drawable Vertices.
pub trait Vertex {
    fn position(&self) -> (f32, f32) {
        (0.0, 0.0)
    }
    fn color(&self) -> Option<(f32, f32, f32, f32)> {
        Some((0., 0., 0., 0.))
    }
}

impl Vertex for ColoredVertex2D {
    fn position(&self) -> (f32, f32) {
        (0.0, 0.0)
    }

    fn color(&self) -> Option<(f32, f32, f32, f32)> {
        Some((0., 0., 0., 0.))
    }
}

/// A drawable object can be passed into a CanvasFrame to be rendered
/// Allows Texture or Image drawing via their handles
pub trait Drawable {
    fn get_vertices(&self) -> Vec<(f32, f32)>;
    fn get_color(&self) -> (f32, f32, f32, f32);
    fn get_texture_handle(&self) -> Option<Arc<CanvasTextureHandle>>;
    fn get_image_handle(&self) -> Option<Arc<CanvasImageHandle>>;
}

/// Legacy ShaderType enum for single type shaders.
#[derive(PartialEq, Eq, Hash, Clone)]
pub enum ShaderType {
    SOLID = 0,
    TEXTURED = 1,
    IMAGE = 2,
}

/// Typed wrapper for a u32 texture handle (index id)
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct CanvasTextureHandle {
    pub handle: u32
}

/// Typed wrapper for a u32 image handle (index id)
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct CanvasImageHandle {
    pub handle: u32
}

/// Canvas state is used for storage of texture and image buffers in addition to vertex buffers
/// Canvas state also contains logic for writing the stored buffers to the command_buffer
#[derive(Clone)]
pub struct CanvasState {

    /// Generated during new()
    dynamic_state: DynamicState,
    /// Generated during new()
    sampler: Arc<Sampler>,

    // hold the image, texture, and shader buffers the same was as we do CompuState
    image_buffers: Vec<Arc<CanvasImage>>,
    texture_buffers: Vec<Arc<CanvasTexture>>,
    shader_buffers: Vec<Arc<CanvasShader>>,

    // Hold onto the vertices we get from the Compu and Canvas Frames
    // When the run comes around, push the vertices to the GPU
    colored_drawables: Vec<ColoredVertex2D>,
    colored_vertex_buffer: Vec<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,

    textured_drawables: HashMap<Arc<CanvasTextureHandle>, Vec<Vec<Vertex2D>>>,
    textured_vertex_buffer: HashMap<Arc<CanvasTextureHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,

    image_drawables: HashMap<Arc<CanvasImageHandle>, Vec<Vec<Vertex2D>>>,
    image_vertex_buffer: HashMap<Arc<CanvasImageHandle>, Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync)>>,

    // Looks like we gotta hold onto the queue for managing textures
    queue: Arc<Queue>,
    device: Arc<Device>,
    render_pass: Arc<dyn RenderPassAbstract + Send + Sync>,
}


impl CanvasState {

    /// This method is called once during initialization, then again whenever the window is resized
    pub fn window_size_dependent_setup(&mut self, images: &[Arc<SwapchainImage<Window>>])
                                       -> Vec<Arc<dyn FramebufferAbstract + Send + Sync>> {

        let dimensions = images[0].dimensions();

        self.dynamic_state.viewports =
            Some(vec![Viewport {
                origin: [0.0, 0.0],
                dimensions: [dimensions.width() as f32, dimensions.height() as f32],
                depth_range: 0.0..1.0,
            }]);

        images.iter().map(|image| {
            Arc::new(
                Framebuffer::start(self.render_pass.clone())
                    .add(image.clone()).unwrap()
                    .build().unwrap()
            ) as Arc<dyn FramebufferAbstract + Send + Sync>
        }).collect::<Vec<_>>()
    }

    /// Creates a Canvas State. Which at this point is pretty empty
    pub fn new(queue: Arc<Queue>,
               device: Arc<Device>,
               physical: PhysicalDevice,
               capabilities: Capabilities) -> CanvasState {


        let format = capabilities.supported_formats[0].0;

        let render_pass = Arc::new(vulkano::single_pass_renderpass!(
            device.clone(),

            // Attachments are outgoing like f_color
            attachments: {
                // `color` is a custom name we give to the first and only attachment.
                color: {
                    // `load: Clear` means that we ask the GPU to clear the content of this
                    // attachment at the start of the drawing.
                    load: Clear,
                    // `store: Store` means that we ask the GPU to store the output of the draw
                    // in the actual image. We could also ask it to discard the result.
                    store: Store,
                    // `format: <ty>` indicates the type of the format of the image. This has to
                    // be one of the types of the `vulkano::format` module (or alternatively one
                    // of your structs that implements the `FormatDesc` trait). Here we use the
                    // same format as the swapchain.
                    format: format,
                    // TODO:
                    samples: 1,
                }
            },
            pass: {
                // We use the attachment named `color` as the one and only color attachment.
                color: [color],
                //color: [],
                // No depth-stencil attachment is indicated with empty brackets.
                depth_stencil: {}
            }
        ).unwrap());


        CanvasState {
            dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None },
            sampler: Sampler::new(device.clone(), Filter::Linear, Filter::Linear,
                                  MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat,
                                  SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(),
            image_buffers: vec![],
            texture_buffers: vec![],
            shader_buffers: vec![],

            colored_drawables: vec![],
            colored_vertex_buffer: vec![],
            textured_drawables: HashMap::default(),
            textured_vertex_buffer: Default::default(),
            image_drawables: Default::default(),
            image_vertex_buffer: Default::default(),

            queue: queue.clone(),
            device: device.clone(),
            render_pass: render_pass.clone(),
        }
    }

    /// Using the dimensions and suggested usage, load a CanvasImage and return it's handle
    pub fn create_image(&mut self, dimensions: (u32, u32), usage: ImageUsage) -> Arc<CanvasImageHandle> {
        let handle = Arc::new(CanvasImageHandle { handle: self.image_buffers.len() as u32 });

        let image = CanvasImage {
            handle: handle.clone(),
            buffer: AttachmentImage::with_usage(
                self.device.clone(),
                [dimensions.0, dimensions.1],
                Format::R8G8B8A8Uint,
                usage).unwrap(),
            size: dimensions,
        };

        self.image_buffers.push(Arc::new(image));

        handle
    }

    /// Return the image buffer from an input image handle
    pub fn get_image(&self, image_handle: Arc<CanvasImageHandle>) -> Arc<AttachmentImage> {
        self.image_buffers.get((*image_handle).clone().handle as usize).unwrap()
            .clone().buffer.clone()
    }

    /// Load a texture buffer from an input filename
    fn get_texture_from_file(&self, image_filename: String) -> Arc<ImmutableImage<Format>> {
        let project_root =
            std::env::current_dir()
                .expect("failed to get root directory");

        let mut compute_path = project_root.clone();
        compute_path.push(PathBuf::from("resources/images/"));
        compute_path.push(PathBuf::from(image_filename));

        let img = image::open(compute_path).expect("Couldn't find image");

        let xy = img.dimensions();

        let data_length = xy.0 * xy.1 * 4;
        let pixel_count = img.raw_pixels().len();

        let mut image_buffer = Vec::new();

        if pixel_count != data_length as usize {
            println!("Creating apha channel...");
            for i in img.raw_pixels().iter() {
                if (image_buffer.len() + 1) % 4 == 0 {
                    image_buffer.push(255);
                }
                image_buffer.push(*i);
            }
            image_buffer.push(255);
        } else {
            image_buffer = img.raw_pixels();
        }

        let (texture, tex_future) = ImmutableImage::from_iter(
            image_buffer.iter().cloned(),
            Dimensions::Dim2d { width: xy.0, height: xy.1 },
            Format::R8G8B8A8Srgb,
            self.queue.clone(),
        ).unwrap();

        texture
    }

    /// Load a texture using it's filename from a file. Returns the handle of the loaded texture
    pub fn load_texture(&mut self, filename: String) -> Option<Arc<CanvasTextureHandle>> {
        let texture_buffer = self.get_texture_from_file(filename.clone());

        let handle = Arc::new(CanvasTextureHandle {
            handle: self.texture_buffers.len() as u32
        });

        let texture = Arc::new(CanvasTexture {
            handle: handle.clone(),
            buffer: self.get_texture_from_file(filename.clone()),
            name: filename.clone(),
            size: (0, 0),
        });

        self.texture_buffers.push(texture);

        Some(handle)
    }

    /// Load and Compile a shader with the filename at resources/shaders
    /// Takes physical and capabilities as we don't store that in Canvas
    pub fn load_shader(&mut self,
                       filename: String,
                       physical: PhysicalDevice,
                       capabilities: Capabilities) -> Option<Arc<CanvasShaderHandle>> {

        let handle = Arc::new(CanvasShaderHandle {
            handle: self.shader_buffers.len() as u32
        });

        let shader = Arc::new(CanvasShader::new_colored(
            filename.clone(),
            capabilities.clone(),
            self.queue.clone(),
            physical.clone(),
            self.device.clone(),
            handle.clone(),
            self.render_pass.clone())
        );

        self.shader_buffers.push(shader.clone());

        Some(handle)
    }

    /// Using the texture name, iterates through the stored textures and matches by the name
    pub fn get_texture_handle(&self, texture_name: String)
                              -> Option<Arc<CanvasTextureHandle>> {
        for i in self.texture_buffers.clone() {
            if i.name == texture_name {
                return Some(i.handle.clone());
            }
        }
        None
    }

    /// Using the shader name, iterates through the stored textures and matches by the name
    pub fn get_shader_handle(&self, shader_name: String)
                             -> Option<Arc<CanvasShaderHandle>> {
        for shader in self.shader_buffers.clone() {
            if shader.name == shader_name {
                return Some(shader.handle.clone());
            }
        }
        None
    }

    /// Using the texture handle, grab the stored texture and return the buffer
    pub fn get_texture(&self, texture_handle: Arc<CanvasTextureHandle>)
                       -> Arc<ImmutableImage<Format>> {
        let handle = texture_handle.handle as usize;

        if let Some(i) = self.texture_buffers.get(handle) {
            return i.clone().buffer.clone();
        } else {
            panic!("{} : Texture not loaded", handle);
        }
    }

    /// Scrape all the values from the CanvasFrame and then allocate the vertex buffers
    pub fn draw(&mut self, canvas_frame: CanvasFrame) {
        self.textured_drawables = canvas_frame.textured_drawables;
        self.colored_drawables = canvas_frame.colored_drawables;
        self.image_drawables = canvas_frame.image_drawables;

        self.allocate_vertex_buffers();
    }

    /// draw(canvas_fame) stored all the intermediate information, this function
    /// allocates the vertex buffers using that information
    fn allocate_vertex_buffers(&mut self) {
        self.colored_vertex_buffer.clear();
        {
            let g = hprof::enter("Colored Vertex Buffer");
            self.colored_vertex_buffer.push(
                ImmutableBuffer::from_iter(
                    self.colored_drawables.iter().cloned(),
                    BufferUsage::vertex_buffer(),
                    self.queue.clone(),
                ).unwrap().0
            );
        }

        self.textured_vertex_buffer.clear();
        {
            let g = hprof::enter("Textured Vertex Buffer");
            for (k, v) in self.textured_drawables.drain() {
                self.textured_vertex_buffer.insert(
                    k.clone(),
                    ImmutableBuffer::from_iter(
                        v.first().unwrap().iter().cloned(),
                        BufferUsage::vertex_buffer(),
                        self.queue.clone(),
                    ).unwrap().0,
                );
            }
        }

        self.image_vertex_buffer.clear();
        {
            let g = hprof::enter("Image Vertex Buffer");
            for (k, v) in self.image_drawables.drain() {
                self.image_vertex_buffer.insert(
                    k.clone(),
                    ImmutableBuffer::from_iter(
                        v.first().unwrap().iter().cloned(),
                        BufferUsage::vertex_buffer(),
                        self.queue.clone(),
                    ).unwrap().0,
                );
            }
        }
    }

    /// Builds the descriptor set for solid colors using the input kernel (needs to support solid colors)
    fn get_solid_color_descriptor_set(&self, kernel: Arc<CanvasShader>) -> Box<dyn DescriptorSet + Send + Sync> {
        let o: Box<dyn DescriptorSet + Send + Sync> = Box::new(
            PersistentDescriptorSet::start(
                kernel.clone().get_pipeline().clone(), 0,
            ).build().unwrap());
        o
    }

    /// Pushes the draw commands to the command buffer. Requires the framebuffers and
    /// image number to be passed in as they are taken care of by the vkprocessor
    pub fn draw_commands(&self,
                         mut command_buffer: AutoCommandBufferBuilder,
                         framebuffers: Vec<Arc<dyn FramebufferAbstract + Send + Sync>>,
                         image_num: usize) -> AutoCommandBufferBuilder {

        // Specify the color to clear the framebuffer with i.e. blue
        let clear_values = vec!(ClearValue::Float([0.0, 0.0, 1.0, 1.0]));

        let mut command_buffer = command_buffer.begin_render_pass(
            framebuffers[image_num].clone(), false, clear_values.clone(),
        ).unwrap();

        // Solid colors
        let mut shader = self.shader_buffers.get(
            self.get_shader_handle(String::from("color-passthrough"))
                .unwrap().clone().handle as usize
        ).unwrap();

        // This looks a little weird as colored_vertex_buffer is a vec of GPU allocated vecs.
        // But we can pass in multiple vertex buffers
        if !self.colored_vertex_buffer.is_empty() {
            command_buffer = command_buffer.draw(
                shader.get_pipeline().clone(),
                &self.dynamic_state.clone(),
                self.colored_vertex_buffer.clone(),
                (), (),
            ).unwrap();
        }

        // Textures
        let mut shader = self.shader_buffers.get(
            self.get_shader_handle(String::from("simple_texture"))
                .unwrap().clone().handle as usize
        ).unwrap();

        if !self.textured_vertex_buffer.is_empty() {
            for (texture_handle, vertex_buffer) in self.textured_vertex_buffer.clone() {
                let handle = texture_handle.clone().handle as usize;
                let descriptor_set = self.texture_buffers.get(handle).clone().unwrap().clone()
                    .get_descriptor_set(shader.clone(), self.sampler.clone());

                command_buffer = command_buffer.draw(
                    shader.get_pipeline().clone(),
                    &self.dynamic_state.clone(), vec![vertex_buffer],
                    vec![descriptor_set], (),
                ).unwrap();
            }
        }

        // Images
        let mut shader = self.shader_buffers.get(
            self.get_shader_handle(String::from("simple_image"))
                .unwrap().clone().handle as usize
        ).unwrap();

        if !self.image_vertex_buffer.is_empty() {
            for (image_handle, vertex_buffer) in self.image_vertex_buffer.clone() {
                let handle = image_handle.clone().handle as usize;
                let descriptor_set = self.image_buffers.get(handle).clone().unwrap().clone()
                    .get_descriptor_set(shader.clone());

                command_buffer = command_buffer.draw(
                    shader.get_pipeline().clone(),
                    &self.dynamic_state.clone(), vec![vertex_buffer],
                    vec![descriptor_set], (),
                ).unwrap();
            }
        }

        command_buffer
            .end_render_pass()
            .unwrap()
    }
}