      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

    #![allow(dead_code)]
    #![allow(unused_variables)]
    #![allow(unused_mut)]

    extern crate cgmath;
    extern crate image;
    extern crate nalgebra as na;
    extern crate rand;
    extern crate time;
    extern crate hprof;

    use vulkano::sync;
    use crate::util::timer::Timer;
    use vulkano::instance::Instance;
    use vulkano::sync::GpuFuture;
    use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualKeyCode, ElementState};
    use winit::dpi::LogicalSize;
    use vulkano_win::VkSurfaceBuild;

    use crate::util::load_raw;

    use vulkano::instance::debug::DebugCallback;
    use crate::compute::compu_frame::CompuFrame;
    use crate::canvas::canvas_frame::{CanvasFrame, Drawable};
    use std::sync::Arc;
    use crate::canvas::managed::handles::{CanvasTextureHandle, Handle, CanvasFontHandle};
    use crate::util::vertex::{VertexTypes, TextureVertex3D};
    use crate::compute::managed::handles::{CompuBufferHandle, CompuKernelHandle};
    use crate::drawables::sprite::Sprite;
    use crate::drawables::rect::Rect;
    use crate::drawables::compu_sprite::CompuSprite;

    pub mod util;
    pub mod vkprocessor;
    pub mod drawables;
    pub mod canvas;
    pub mod compute;

    pub fn main() {
        hprof::start_frame();

        let q1 = hprof::enter("setup");

        let instance = {
            let extensions = vulkano_win::required_extensions();
            Instance::new(None, &extensions, None).unwrap()
        };

        let _callback = DebugCallback::errors_and_warnings(&instance, |msg| {
            println!("Debug callback: {:?}", msg.description);
        }).ok();

        let mut events_loop = EventsLoop::new();
        let mut surface = WindowBuilder::new()
            .with_dimensions(LogicalSize::from((800, 800)))
            .build_vk_surface(&events_loop, instance.clone()).unwrap();
        let mut window = surface.window();

        let mut processor = vkprocessor::VkProcessor::new(&instance, &surface);

        {
            let g = hprof::enter("vulkan preload");
            processor.create_swapchain(&surface);

            processor.preload_kernels();
            processor.preload_shaders();
            processor.preload_textures();
            processor.preload_fonts();
        }

        let q2 = hprof::enter("Game Objects");

        let mut timer = Timer::new();
        let mut frame_future : Box<dyn GpuFuture> =
            Box::new(sync::now(processor.device.clone())) as Box<dyn GpuFuture>;

        let step_size: f32 = 0.005;
        let mut elapsed_time: f32;
        let mut delta_time: f32;
        let mut accumulator_time: f32 = 0.0;
        let mut current_time: f32 = timer.elap_time();

        let image_data = load_raw(String::from("funky-bird.jpg"));
        let image_dimensions_f : (f32, f32) = ((image_data.1).0 as f32, (image_data.1).1 as f32);
        let image_dimensions_u : (u32, u32) = image_data.1;
        let compu_sprite1 : CompuSprite =
            CompuSprite::new((0.0, -0.5), (0.4, 0.4), 0, image_dimensions_f,
                             // Swap image to render the result to. Must match dimensions
                             processor.new_swap_image(image_dimensions_u));

        let compute_buffer : Arc<CompuBufferHandle> =
            processor.new_compute_buffer(image_data.0, image_data.1, 4);

        let compute_kernel : Arc<CompuKernelHandle> =
            processor.get_kernel_handle(String::from("simple-edge.compute"))
            .expect("Can't find that kernel");

        // Get the handles for the assets
        let funky_handle : Arc<CanvasTextureHandle> =
            processor.get_texture_handle(String::from("funky-bird.jpg")).unwrap();
        let sfml_handle : Arc<CanvasTextureHandle> =
            processor.get_texture_handle(String::from("sfml.png")).unwrap();
        let font_handle : Arc<CanvasFontHandle> =
            processor.get_font_handle(String::from("sansation.ttf")).unwrap();

        let funky_sprite = Sprite::new((0.0, 0.5), (0.5, 0.5), 0, funky_handle.clone());
        let sfml_sprite  = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
        let rect  = Rect::new((-0.5, -0.5), (0.5, 0.5), 1);

        //let sfml_sprite  = Sprite::new((0.0, -0.5), (0.5, 0.5), 1, sfml_handle.clone());
        //let text_sprite = Text::new((-0.1,-0.1), (10.0, 10.0), font_handle.clone());
        //let test_polygon = Poly::new_with_color((-0.5, -0.5), (0.5, 0.5), 1, (1.0,0.0,0.0,0.0));

        drop(q2);
        drop(q1);

        let l = hprof::enter("Loop");

        let mut exit = false;

        let mut count = 0;

        while let true = processor.is_open() {

            // Take care of our timing
            {
                elapsed_time = timer.elap_time();
                delta_time = elapsed_time - current_time;
                current_time = elapsed_time;
                if delta_time > 0.02 {
                    delta_time = 0.02;
                }
                accumulator_time += delta_time;
            }

            while (accumulator_time - step_size) >= step_size {
                accumulator_time -= step_size;
            }

            // Events loop is borrowed from the surface
            events_loop.poll_events(|event| {
                match event {
                    Event::WindowEvent { event: WindowEvent::CloseRequested, .. } =>
                        {
                            exit = true;
                        }
                    Event::WindowEvent { event: WindowEvent::Resized(_), .. } => {
                        processor.recreate_swapchain(&surface);
                    }
                    Event::DeviceEvent { event: DeviceEvent::Key(keyboard_input), .. } => {
                        match keyboard_input.virtual_keycode.unwrap() {
                            VirtualKeyCode::A => {
                                if keyboard_input.state == ElementState::Pressed {
                                    // processor.save_edges_image();
                                }
                            }
                            _ => ()
                        }
                    }
    //                Event::DeviceEvent { event: DeviceEvent::Button(mouse_input), .. } => {
    //                    mouse_xy.x
    //                },
                    _ => ()
                }
            });

            if exit {
                break;
            }

            let mut canvas_frame = CanvasFrame::default();
            canvas_frame.draw(&funky_sprite);
            canvas_frame.draw(&compu_sprite1);
    //        canvas_frame.draw(&rect);

            let mut compu_frame = CompuFrame::new();
            //compu_frame.add(compute_buffer.clone(), compute_kernel.clone());
            compu_frame.add_with_image_swap(compute_buffer.clone(), compute_kernel.clone(), &compu_sprite1);

            {
                let g = hprof::enter("Run");
                processor.run(&surface,
                              canvas_frame,
                              compu_frame);
            }
        }

        drop(l);

        hprof::end_frame();
        hprof::profiler().print_timing();
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)