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
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue};
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::sync::{GpuFuture, FlushError, NowFuture};
use vulkano::sync::now;
use vulkano::sync;
use std::sync::Arc;
use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, Surface, SwapchainCreationError, AcquireError, Capabilities};
use vulkano::image::swapchain::SwapchainImage;
use winit::Window;
use crate::compute::compu_state::CompuState;
use vulkano::image::ImageUsage;
use crate::compute::compu_frame::CompuFrame;
use crate::canvas::canvas_frame::{CanvasFrame};
use std::time::Duration;
use vulkano::pipeline::depth_stencil::{DynamicStencilValue, StencilFaceFlags};
use vulkano::pipeline::vertex::{OneVertexOneInstanceDefinition, SingleBufferDefinition};
use crate::canvas::canvas_state::CanvasState;
use crate::canvas::managed::shader::generic_shader::GenericShader;
use crate::canvas::managed::shader::text_shader::TextShader;
use crate::canvas::managed::handles::{CanvasTextureHandle, CompiledShaderHandle, CanvasFontHandle, CanvasImageHandle};
use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle};
use crate::util::vertex::{VertexTypes, ColorVertex3D, TextVertex3D, TextureVertex3D, ImageVertex3D};


/// VKProcessor holds the vulkan instance information, the swapchain,
/// and the compute and canvas states
pub struct VkProcessor<'a> {
    // Vulkan state fields
    pub instance: Arc<Instance>,
    pub physical: PhysicalDevice<'a>,
    pub device: Arc<Device>,
    pub queues: QueuesIter,
    pub queue: Arc<Queue>,

    pub swapchain: Option<Arc<Swapchain<Window>>>,
    pub swapchain_images: Option<Vec<Arc<SwapchainImage<Window>>>>,

    swapchain_recreate_needed: bool,

    /// State holding textures, images, and their related vertex buffers
    canvas_state: CanvasState,
    /// State holding
    compute_state: CompuState,

    capabilities: Capabilities,

}


impl<'a> VkProcessor<'a> {

    /// Creates a new VkProcessor from an instance and surface
    /// This includes the physical device, queues, compute and canvas state
    pub fn new(instance: &'a Arc<Instance>, surface: &'a Arc<Surface<Window>>) -> VkProcessor<'a> {

        let physical = PhysicalDevice::enumerate(instance).next().unwrap();

        let queue_family = physical.queue_families().find(|&q| {
            // We take the first queue that supports drawing to our window.
            q.supports_graphics() &&
                surface.is_supported(q).unwrap_or(false) &&
                q.supports_compute()
        }).unwrap();

        let device_ext = DeviceExtensions { khr_swapchain: true, ..DeviceExtensions::none() };

        let (device, mut queues) = Device::new(physical,
                                               physical.supported_features(),
                                               &device_ext,
                                               [(queue_family, 0.5)].iter().cloned()).unwrap();
        let queue = queues.next().unwrap();

        let capabilities = surface.capabilities(physical).unwrap();

        VkProcessor {
            instance: instance.clone(),
            physical: physical.clone(),
            device: device.clone(),
            queue: queue.clone(),
            queues: queues,
            swapchain: None,
            swapchain_images: None,
            swapchain_recreate_needed: false,
            compute_state: CompuState::new(),
            capabilities: capabilities.clone(),
            canvas_state: CanvasState::new(queue, device, physical, capabilities),
        }
    }

    /// VKProcessor controls the window. So it will let the main loop know when it is done
    pub fn is_open(&mut self) -> bool {
       // self.surface
        true
    }

    /// Using the surface, we calculate the surface capabilities and create the swapchain and swapchain images
    pub fn create_swapchain(&mut self, surface: &'a Arc<Surface<Window>>) {
        let (mut swapchain, images) = {
            let capabilities = surface.capabilities(self.physical).unwrap();
            let usage = capabilities.supported_usage_flags;
            let alpha = capabilities.supported_composite_alpha.iter().next().unwrap();
            // Choosing the internal format that the images will have.
            let format = capabilities.supported_formats[0].0;

            // Set the swapchains window dimensions
            let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
                // convert to physical pixels
                let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
                [dimensions.0, dimensions.1]
            } else {
                // The window no longer exists so exit the application.
                panic!("window closed");
            };

            Swapchain::new(self.device.clone(),
                           surface.clone(),
                           capabilities.min_image_count, // number of attachment images
                           format,
                           initial_dimensions,
                           1, // Layers
                           usage,
                           &self.queue,
                           SurfaceTransform::Identity,
                           alpha,
                           PresentMode::Immediate, true, None).unwrap()
        };

        self.swapchain = Some(swapchain);
        self.swapchain_images = Some(images);
    }

    /// On screen resizes, the swapchain and images must be recreated
    pub fn recreate_swapchain(&mut self, surface: &'a Arc<Surface<Window>>) {
        let dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
            let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
            [dimensions.0, dimensions.1]
        } else {
            return;
        };

        let (new_swapchain, new_images) = match self.swapchain.clone().unwrap().clone().recreate_with_dimension(dimensions) {
            Ok(r) => r,
            // This error tends to happen when the user is manually resizing the window.
            // Simply restarting the loop is the easiest way to fix this issue.
            Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"),
            Err(err) => panic!("{:?}", err)
        };

        self.swapchain = Some(new_swapchain);
        self.swapchain_images = Some(new_images);
    }

    /// A hardcoded list of textures which can be preloaded from this function
    pub fn preload_textures(&mut self) {
        self.canvas_state.load_texture(String::from("funky-bird.jpg"));
        self.canvas_state.load_texture(String::from("button.png"));
        self.canvas_state.load_texture(String::from("background.jpg"));
        self.canvas_state.load_texture(String::from("test2.png"));
        self.canvas_state.load_texture(String::from("sfml.png"));
    }

    /// A hardcoded list of kernels which can be preloaded from this function
    pub fn preload_kernels(&mut self) {
        self.compute_state.new_kernel(String::from("simple-homogenize.compute"), self.device.clone());
        self.compute_state.new_kernel(String::from("simple-edge.compute"), self.device.clone());
    }

    /// A hardcoded list of shaders which can be preloaded from this function
    pub fn preload_shaders(&mut self) {
        self.canvas_state.load_shader::<GenericShader, ColorVertex3D>(String::from("color-passthrough"), self.physical.clone(), self.capabilities.clone());
        self.canvas_state.load_shader::<GenericShader, TextureVertex3D>(String::from("simple_texture"), self.physical.clone(), self.capabilities.clone());
        self.canvas_state.load_shader::<GenericShader, ImageVertex3D>(String::from("simple_image"), self.physical.clone(), self.capabilities.clone());
    //    self.canvas_state.load_shader::<TextShader, TextVertex3D>(String::from("simple_text"), self.physical.clone(), self.capabilities.clone());
    }

    /// A hardcoded list of shaders which can be proloaded from this function
    pub fn preload_fonts(&mut self) {
        self.canvas_state.load_font(String::from("sansation.ttf"));
    }

    /// O(n) Lookup for the matching texture string
    pub fn get_texture_handle(&self, texture_name: String) -> Option<Arc<CanvasTextureHandle>> {
        self.canvas_state.get_texture_handle(texture_name)
    }

    /// O(n) Lookup for the matching kernel string
    pub fn get_kernel_handle(&self, kernel_name: String) -> Option<Arc<CompuKernelHandle>> {
        self.compute_state.get_kernel_handle(kernel_name)
    }

    /// O(n) Lookup for the matching shader string
    pub fn get_shader_handle(&self, shader_name: String) -> Option<Arc<CompiledShaderHandle>> {
        self.canvas_state.get_shader_handle(shader_name)
    }

    pub fn get_font_handle(&self, font_name: String) -> Option<Arc<CanvasFontHandle>> {
        self.canvas_state.get_font_handle(font_name)
    }

    /// Create a new image which has the transfer usage
    pub fn new_swap_image(&mut self, dimensions: (u32, u32)) -> Arc<CanvasImageHandle> {
        let mut usage = ImageUsage::none();
        usage.transfer_destination = true;
        usage.storage = true;

        self.canvas_state.create_image(dimensions, usage)
    }

    /// Builds a compute buffer and returns it's handle
    pub fn new_compute_buffer(&mut self, data: Vec<u8>, dimensions: (u32, u32), stride: u32) -> Arc<CompuBufferHandle> {
        self.compute_state.new_compute_buffer(data, dimensions, stride, self.device.clone())
    }

    /// Takes a compute buffer handle and returns the read data
    pub fn read_compute_buffer(&mut self, handle: Arc<CompuBufferHandle>) -> Vec<u8> {
        self.compute_state.read_compute_buffer(handle)
    }

    /// Takes a compute buffer handle and writes the received data
    pub fn write_compute_buffer(&self, handle: Arc<CompuBufferHandle>, data: Vec<u8>) {
        self.compute_state.write_compute_buffer(handle, data)
    }

    ///
    pub fn run(&mut self,
               surface: &'a Arc<Surface<Window>>,
               //canvas_frame: CanvasFrame,
               canvas_frame: CanvasFrame,
               compute_frame: CompuFrame,
    ) {

        {
            let g = hprof::enter("Waiting at queue");
            self.queue.wait();
        }

        let g = hprof::enter("Frame buffer, future, swapchain recreate");
        let mut framebuffers =
            self.canvas_state.window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone());

        // Whenever the window resizes we need to recreate everything dependent on the window size.
        // In this example that includes the swapchain, the framebuffers and the dynamic state viewport.
        if self.swapchain_recreate_needed {
            self.recreate_swapchain(surface);
            framebuffers =
                self.canvas_state.window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone());
            self.swapchain_recreate_needed = false;
        }

        // This function can block if no image is available. The parameter is an optional timeout
        // after which the function call will return an error.
        let (image_num, acquire_future) =
            match vulkano::swapchain::acquire_next_image(
                self.swapchain.clone().unwrap().clone(),
                None,
            ) {
                Ok(r) => r,
                Err(AcquireError::OutOfDate) => {
                    self.swapchain_recreate_needed = true;
                    return;
                }
                Err(err) => panic!("{:?}", err)
            };

        drop(g);

        let allocated_buffers = {
            // take the canvas frame and create the vertex buffers
            // TODO: This performs gpu buffer creation. Shouldn't be in hotpath??
            let g = hprof::enter("Canvas creates GPU buffers");
            self.canvas_state.allocate(canvas_frame)
        };

        let mut command_buffer =
            AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.queue.family()).unwrap();

        let g = hprof::enter("Push compute commands to command buffer");
        // Add the compute commands
        let mut command_buffer = self.compute_state.compute_commands(compute_frame, command_buffer, &self.canvas_state);
        drop(g);

        let g = hprof::enter("Push draw commands to command buffer");
        
        // Add the draw commands
        //let mut command_buffer = self.canvas_state.draw_commands(command_buffer, framebuffers, image_num);
        let mut command_buffer =
            self.canvas_state.draw_commands(command_buffer, framebuffers, image_num, allocated_buffers);

        // And build
        let command_buffer = command_buffer.build().unwrap();
        drop(g);

        // Wait on the previous frame, then execute the command buffer and present the image
        {

            let g = hprof::enter("Joining on the framebuffer");
            let mut future = sync::now(self.device.clone())
                .join(acquire_future);
            drop(g);

            let g = hprof::enter("Running the kernel and waiting on the future");

            let future = future
                .then_execute(self.queue.clone(), command_buffer).unwrap()
                .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num)
                .then_signal_fence_and_flush();

            match future {
                Ok(future) => {
                    future.wait(None).unwrap();
                }
                Err(FlushError::OutOfDate) => {
                    self.swapchain_recreate_needed = true;
                }
                Err(e) => {
                    println!("{:?}", e);
                }
            }
        }
    }
}