Vala プログラミング

Rust プログラミング

おながのブログ

Rust Vulkano Cubemap

f:id:onagat12:20181229021533g:plain

#[macro_use]
extern crate vulkano;
extern crate vulkano_shaders;
extern crate winit;
extern crate vulkano_win;
extern crate arcball;
extern crate cgmath;
extern crate image;

use vulkano_win::VkSurfaceBuild;
use vulkano::buffer::BufferUsage;
use vulkano::buffer::cpu_access::CpuAccessibleBuffer;
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use vulkano::device::{Device, DeviceExtensions};
use vulkano::framebuffer::{Framebuffer, Subpass, FramebufferAbstract, RenderPassAbstract};
use vulkano::format::Format;
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::image::{SwapchainImage, ImmutableImage, Dimensions};
use vulkano::image::attachment::AttachmentImage;
use vulkano::pipeline::{GraphicsPipeline, GraphicsPipelineAbstract};
use vulkano::pipeline::viewport::Viewport;
use vulkano::swapchain;
use vulkano::swapchain::{Swapchain, SurfaceTransform, PresentMode, AcquireError};
use vulkano::sync;
use vulkano::sync::GpuFuture;
use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};

use winit::{Window, EventsLoop, WindowBuilder, Event, WindowEvent};
use image::ImageFormat;
use std::sync::Arc;
use std::iter;
use std::time::Instant;
use cgmath::{Matrix3, Matrix4, Rad, Point3, Vector3};

#[derive(Debug, Clone)]
struct Vertex { position: [f32; 3] }
impl_vertex!(Vertex, position);

static WINDOW_NAME: &str = "Cubemap Test";
static WIN_WIDTH: f64 = 600.0;
static WIN_HEIGHT: f64 = 600.0;

struct Vulkan {
    images: Vec<std::sync::Arc<vulkano::image::SwapchainImage<winit::Window>>>,
    swapchain: Arc<vulkano::swapchain::Swapchain<winit::Window>>,
    device: Arc<vulkano::device::Device>,
    queue: Arc<vulkano::device::Queue>,
    events_loop: winit::EventsLoop,
    surface: Arc<vulkano::swapchain::Surface<winit::Window>>,
}

impl Vulkan {
    pub fn init_vk() -> Vulkan {
        let extensions = vulkano_win::required_extensions();
        let instance = Instance::new(None, &extensions, None).unwrap();

        let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
        println!("Using device: {} (type: {:?})", physical.name(), physical.ty());

        // events_loop, surface, window
        let  events_loop = EventsLoop::new();
        let surface = WindowBuilder::new()
            .with_dimensions(winit::dpi::LogicalSize {width:WIN_WIDTH, height:WIN_HEIGHT})
            .with_title(WINDOW_NAME.to_string())
            .build_vk_surface(&events_loop, instance.clone()).unwrap();
        
        // (device, queues), queue_family
        let queue_family = physical.queue_families().find(|&q| {
            q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
        }).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();
        // we use only one queue, first one
        let queue = queues.next().unwrap();

        let initial_dimensions  = [WIN_WIDTH as u32, WIN_HEIGHT as u32];
        let caps = surface.capabilities(physical).unwrap();
    
        let (swapchain, images) = {
            let usage = caps.supported_usage_flags;
            let alpha = caps.supported_composite_alpha.iter().next().unwrap();
            let format = caps.supported_formats[0].0;
            //
            Swapchain::new(device.clone(), surface.clone(), caps.min_image_count, format,
                initial_dimensions, 1, usage, &queue, SurfaceTransform::Identity, alpha,
                PresentMode::Fifo, true, None).unwrap()
        };
      
        Vulkan {
            images,
            swapchain,
            device,
            queue,
            surface,
            events_loop,
        } 
    }
}

fn main() {
    // Vulkan Object initialization
    let mut vk = Vulkan::init_vk();

    let skybox_vertex_buffer = {
        let side2: f32 = 20.0 / 2.0;

        CpuAccessibleBuffer::from_iter(vk.device.clone(), BufferUsage::all(), [
            // Front
            Vertex { position: [-side2, -side2,  side2] },
    	    Vertex { position: [ side2, -side2,  side2] },
    	    Vertex { position: [ side2,  side2,  side2] },
            Vertex { position: [-side2,  side2,  side2] },
    	    // Right
    	    Vertex { position: [ side2, -side2,  side2] },
    	    Vertex { position: [ side2, -side2, -side2] },
    	    Vertex { position: [ side2,  side2, -side2] },
            Vertex { position: [ side2,  side2,  side2] },
    	    // Back
    	    Vertex { position: [-side2, -side2, -side2] },
    	    Vertex { position: [-side2,  side2, -side2] },
    	    Vertex { position: [ side2,  side2, -side2] },
            Vertex { position: [ side2, -side2, -side2] },
    	    // Left
    	    Vertex { position: [-side2, -side2,  side2] },
    	    Vertex { position: [-side2,  side2,  side2] },
            Vertex { position: [-side2,  side2, -side2] },
            Vertex { position: [-side2, -side2, -side2] },
            // Bottom
    	    Vertex { position: [-side2, -side2,  side2] },
    	    Vertex { position: [-side2, -side2, -side2] },
    	    Vertex { position: [ side2, -side2, -side2] },
            Vertex { position: [ side2, -side2,  side2] },
    	    // Top
            Vertex { position: [-side2,  side2,  side2] },
    	    Vertex { position: [ side2,  side2,  side2] },
    	    Vertex { position: [ side2,  side2, -side2] },
            Vertex { position: [-side2,  side2, -side2] }
        ].iter().cloned()).expect("failed to create buffer")
    };

    let skybox_index_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer
        ::from_iter(vk.device.clone(), vulkano::buffer::BufferUsage::all(), [
            // Front
            0u16, 1, 2, 2, 3, 0,
            // Right
            4, 5, 6, 6, 7, 4,
            // Back
            8, 9, 10, 10, 11, 8,
            // Left
            12, 13, 14, 14, 15, 12,
            // Bottom
            16, 17, 18, 18, 19, 16,
            // Top
            20, 21, 22, 22, 23, 20,
        ].iter().cloned()).expect("failed to create buffer");

    // uniform buffer
    let uniform_buffer = vulkano::buffer::cpu_pool::CpuBufferPool::<vs::ty::Data>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());

    let vs = vs::Shader::load(vk.device.clone()).expect("failed to create shader module");
    let fs = fs::Shader::load(vk.device.clone()).expect("failed to create shader module");

    // render pass
    let render_pass = Arc::new(single_pass_renderpass!(vk.device.clone(),
        attachments: {
            color: {
                load: Clear,
                store: Store,
                format: vk.swapchain.format(),
                samples: 1,
            },
            depth: {
                load: Clear,
                store: DontCare,
                format: vulkano::format::Format::D16Unorm,
                samples: 1,
            } // depth
        },
        pass: {
            color: [color],
            depth_stencil: {depth} // depth
        }
    ).unwrap());

    let window = vk.surface.window();

    let mut dimensions = if let Some(dimensions) = window.get_inner_size() {
        let dimensions: (u32, u32) = dimensions.to_physical(window.get_hidpi_factor()).into();
        [dimensions.0, dimensions.1]
    } else {
        return;
    };

    let img_posx = image::load_from_memory_with_format(include_bytes!("images/posx512.jpg"),
        ImageFormat::JPEG).unwrap().to_rgba();
    let img_negx = image::load_from_memory_with_format(include_bytes!("images/negx512.jpg"),
        ImageFormat::JPEG).unwrap().to_rgba();
    let img_posy = image::load_from_memory_with_format(include_bytes!("images/posy512.jpg"),
        ImageFormat::JPEG).unwrap().to_rgba();
    let img_negy = image::load_from_memory_with_format(include_bytes!("images/negy512.jpg"),
        ImageFormat::JPEG).unwrap().to_rgba();
    let img_posz = image::load_from_memory_with_format(include_bytes!("images/posz512.jpg"),
        ImageFormat::JPEG).unwrap().to_rgba();
    let img_negz = image::load_from_memory_with_format(include_bytes!("images/negz512.jpg"),
        ImageFormat::JPEG).unwrap().to_rgba();
    
    let cubemap_images = [img_posx, img_negx, img_posy, img_negy, img_posz, img_negz];
    let mut image_data: Vec<u8> = Vec::new();

    for image in cubemap_images.into_iter() {
        let mut image0 = image.clone().into_raw().clone();
        image_data.append(&mut image0);
    }
    
    let (texture, tex_future) = {
        ImmutableImage::from_iter(
            image_data.iter().cloned(),
            Dimensions::Cubemap { size: 512 },
            Format::R8G8B8A8Srgb,
            vk.queue.clone()
        ).unwrap()
    };

    let sampler = Sampler::new(vk.device.clone(), Filter::Linear, Filter::Linear,
        MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat,
        SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap();

    // pipeline and framebuffer
    let (mut pipeline, mut framebuffers) = 
        window_size_dependent_setup(vk.device.clone(), &vs, &fs, &vk.images, render_pass.clone());

    let mut recreate_swapchain = false;
    let mut previous_frame = Box::new(tex_future) as Box<GpuFuture>;
    let rotation_start = Instant::now();

    loop {
        previous_frame.cleanup_finished();

        if recreate_swapchain {
            dimensions = if let Some(dimensions) = window.get_inner_size() {
                let dimensions: (u32, u32) =
                    dimensions.to_physical(window.get_hidpi_factor()).into();
                [dimensions.0, dimensions.1]
            } else {
                return;
            };
            
            let (new_swapchain, new_images) = vk.swapchain.recreate_with_dimension(dimensions)
                .expect("swapcahain not recreate");
            
            vk.swapchain = new_swapchain;

            let (new_pipeline, new_framebuffers) = window_size_dependent_setup(vk.device.clone(), &vs, &fs,
                &new_images, render_pass.clone());
            
            pipeline = new_pipeline;
            framebuffers = new_framebuffers;

            recreate_swapchain = false;
        }

        let uniform_buffer_subbuffer = {
            let elapsed = rotation_start.elapsed();
            let rotation = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1_000_000_000.0;
            let rotation = Matrix3::from_angle_y(Rad(0.6 * rotation as f32));

            let world  = Matrix4::from(rotation);
            let view = Matrix4::look_at(Point3::new(0.0, 0.0, 1.0), Point3::new(0.0, 0.0, 0.0), Vector3::new(0.0, -1.0, 0.0));
            let scale = Matrix4::from_scale(1.0);
            let aspect_ratio = dimensions[0] as f32 / dimensions[1] as f32;
            let proj = cgmath::perspective(Rad(std::f32::consts::FRAC_PI_2), aspect_ratio, 0.01, 100.0);

            let uniform_data = vs::ty::Data {
                world: world.into(),
                view : (view * scale).into(),
                proj : proj.into(),
            };

            uniform_buffer.next(uniform_data).unwrap()
        };

        let set0 = Arc::new(vulkano::descriptor::descriptor_set::PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_buffer(uniform_buffer_subbuffer).unwrap()
            .build().unwrap()
        );

        let set1 = Arc::new(vulkano::descriptor::descriptor_set::PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
            .build().unwrap()
        );
        
        let (image_num, acquire_future) =
            match swapchain::acquire_next_image(vk.swapchain.clone(), None) {
                Ok(r) => r,
                Err(AcquireError::OutOfDate) => {
                    recreate_swapchain = true;
                    continue;
                },
                Err(err) => panic!("{:?}", err)
            };

        let command_buffer =
            AutoCommandBufferBuilder::primary_one_time_submit(vk.device.clone(), vk.queue.family()).unwrap() // Ok
                .begin_render_pass(framebuffers[image_num].clone(), false,
                vec![[0.1, 0.1, 0.1, 1.0].into(), 1f32.into()]).unwrap()
            .draw_indexed(pipeline.clone(),
                &DynamicState::none(),
                vec!(skybox_vertex_buffer.clone()), skybox_index_buffer.clone(),
                (set0.clone(), set1.clone()), ()).unwrap()
            .end_render_pass().unwrap()
            .build().unwrap();

        let _future = previous_frame.join(acquire_future)
            .then_execute(vk.queue.clone(), command_buffer).unwrap()
            .then_swapchain_present(vk.queue.clone(), vk.swapchain.clone(), image_num)
            .then_signal_fence_and_flush();
        
        previous_frame = Box::new(sync::now(vk.device.clone())) as Box<_>;

        let mut done = false;
        vk.events_loop.poll_events(|ev| {
            match ev {
                Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => done = true,
                Event::WindowEvent { event: WindowEvent::Resized(_), .. } => recreate_swapchain = true,
                _ => ()
            }
        });
        if done { return; }
    }
}

fn window_size_dependent_setup(
    device: Arc<Device>,
    vs: &vs::Shader,
    fs: &fs::Shader,
    images: &[Arc<SwapchainImage<Window>>],
    render_pass: Arc<RenderPassAbstract + Send + Sync>,
) -> (Arc<GraphicsPipelineAbstract + Send + Sync>, Vec<Arc<FramebufferAbstract + Send + Sync>> ) {
    let dimensions = images[0].dimensions();

    let depth_buffer = AttachmentImage::transient(device.clone(), dimensions, Format::D16Unorm).unwrap();

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

    let pipeline = Arc::new(GraphicsPipeline::start()
        .vertex_input_single_buffer::<Vertex>()
        .vertex_shader(vs.main_entry_point(), ())
        .triangle_list()
        .viewports_dynamic_scissors_irrelevant(1)
        .viewports(iter::once(Viewport {
            origin: [0.0, 0.0],
            dimensions: [dimensions[0] as f32, dimensions[1] as f32],
            depth_range: 0.0 .. 1.0,
        }))
        .fragment_shader(fs.main_entry_point(), ())
        .depth_stencil_simple_depth()
        .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
        .build(device.clone())
        .unwrap());

    (pipeline, framebuffers)
}

mod vs {
    vulkano_shaders::shader!{
        ty: "vertex",
        src: "
            #version 450

            layout(location = 0) in vec3 position;
            layout (location = 0) out vec3 ReflectDir;

            layout(set = 0, binding = 0) uniform Data {
                mat4 world;
                mat4 view;
                mat4 proj;
            } ubo;

            void main() 
            {
	        ReflectDir = position;
                gl_Position = ubo.proj * ubo.view * ubo.world * vec4(position, 1.0);
            }"
    }
}

mod fs {
    vulkano_shaders::shader!{
        ty: "fragment",
        src: "
            #version 450

            layout(location = 0) in vec3 ReflectDir;
            layout(location = 0) out vec4 f_color;

            layout(set = 1, binding = 0) uniform samplerCube cubetex;

            void main() 
            {
	         f_color = texture(cubetex, ReflectDir);
            }"
    }
}

Rust Vulkano Cube and Texture ( Depth, Two Uniforms )

f:id:onagat12:20181227003925g:plain

#[macro_use]
extern crate vulkano;
extern crate vulkano_shaders;
extern crate winit;
extern crate vulkano_win;
extern crate arcball;
extern crate cgmath;
extern crate image;

use vulkano_win::VkSurfaceBuild;
use vulkano::buffer::BufferUsage;
use vulkano::buffer::cpu_access::CpuAccessibleBuffer;
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use vulkano::device::{Device, DeviceExtensions};
use vulkano::framebuffer::{Framebuffer, Subpass, FramebufferAbstract, RenderPassAbstract};
use vulkano::format::Format;
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::image::{SwapchainImage, ImmutableImage, Dimensions};
use vulkano::image::attachment::AttachmentImage;
use vulkano::pipeline::{GraphicsPipeline, GraphicsPipelineAbstract};
use vulkano::pipeline::viewport::Viewport;
use vulkano::swapchain;
use vulkano::swapchain::{Swapchain, SurfaceTransform, PresentMode, AcquireError};
use vulkano::sync;
use vulkano::sync::GpuFuture;
use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};

use winit::{Window, EventsLoop, WindowBuilder};
use image::ImageFormat;
use std::sync::Arc;
use std::iter;

#[derive(Debug, Clone)]
struct Vertex { position: [f32; 3],  tex_coords: [f32; 2]}
impl_vertex!(Vertex, position, tex_coords);

static WINDOW_NAME: &str = "Cube Texture Test";
static WIN_WIDTH: f64 = 700.0;
static WIN_HEIGHT: f64 = 650.0;

struct Vulkan {
    images: Vec<std::sync::Arc<vulkano::image::SwapchainImage<winit::Window>>>,
    swapchain: Arc<vulkano::swapchain::Swapchain<winit::Window>>,
    device: Arc<vulkano::device::Device>,
    queue: Arc<vulkano::device::Queue>,
    events_loop: winit::EventsLoop,
    surface: Arc<vulkano::swapchain::Surface<winit::Window>>,
}

impl Vulkan {
    pub fn init_vk() -> Vulkan {
        let extensions = vulkano_win::required_extensions();
        let instance = Instance::new(None, &extensions, None).unwrap();

        let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
        println!("Using device: {} (type: {:?})", physical.name(), physical.ty());

        // events_loop, surface, window
        let  events_loop = EventsLoop::new();
        let surface = WindowBuilder::new()
            .with_dimensions(winit::dpi::LogicalSize {width:WIN_WIDTH, height:WIN_HEIGHT})
            .with_title(WINDOW_NAME.to_string())
            .build_vk_surface(&events_loop, instance.clone()).unwrap();
        
        // (device, queues), queue_family
        let queue_family = physical.queue_families().find(|&q| {
            q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
        }).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();
        // we use only one queue, first one
        let queue = queues.next().unwrap();

        let initial_dimensions  = [WIN_WIDTH as u32, WIN_HEIGHT as u32];
        let caps = surface.capabilities(physical).unwrap();
    
        let (swapchain, images) = {
            let usage = caps.supported_usage_flags;
            let alpha = caps.supported_composite_alpha.iter().next().unwrap();
            let format = caps.supported_formats[0].0;
            //
            Swapchain::new(device.clone(), surface.clone(), caps.min_image_count, format,
                initial_dimensions, 1, usage, &queue, SurfaceTransform::Identity, alpha,
                PresentMode::Fifo, true, None).unwrap()
        };
      
        Vulkan {
            images,
            swapchain,
            device,
            queue,
            surface,
            events_loop,
        } 
    }
}

fn main() {
    // Vulkan Object initialization
    let mut vk = Vulkan::init_vk();

    let vertex_buffer = {
        let side2: f32 = 0.8 / 2.0;

        CpuAccessibleBuffer::from_iter(vk.device.clone(), BufferUsage::all(), [
            // Front
            Vertex { position: [-side2, -side2,  side2], tex_coords: [0.0, 0.0] },
            Vertex { position: [ side2, -side2,  side2], tex_coords: [1.0, 0.0] },
            Vertex { position: [ side2,  side2,  side2], tex_coords: [1.0, 1.0] },
            Vertex { position: [-side2,  side2,  side2], tex_coords: [0.0, 1.0] },
    	    // Right
    	    Vertex { position: [ side2, -side2,  side2], tex_coords: [0.0, 0.0] },
    	    Vertex { position: [ side2, -side2, -side2], tex_coords: [1.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], tex_coords: [1.0, 1.0] },
            Vertex { position: [ side2,  side2,  side2], tex_coords: [0.0, 1.0] },
    	    // Back
    	    Vertex { position: [-side2, -side2, -side2], tex_coords: [0.0, 0.0] },
    	    Vertex { position: [-side2,  side2, -side2], tex_coords: [1.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], tex_coords: [1.0, 1.0] },
            Vertex { position: [ side2, -side2, -side2], tex_coords: [0.0, 1.0] },
    	    // Left
    	    Vertex { position: [-side2, -side2,  side2], tex_coords: [0.0, 0.0] },
    	    Vertex { position: [-side2,  side2,  side2], tex_coords: [1.0, 0.0] },
            Vertex { position: [-side2,  side2, -side2], tex_coords: [1.0, 1.0] },
            Vertex { position: [-side2, -side2, -side2], tex_coords: [0.0, 1.0] },
            // Bottom
    	    Vertex { position: [-side2, -side2,  side2], tex_coords: [0.0, 0.0] },
    	    Vertex { position: [-side2, -side2, -side2], tex_coords: [1.0, 0.0] },
    	    Vertex { position: [ side2, -side2, -side2], tex_coords: [1.0, 1.0] },
            Vertex { position: [ side2, -side2,  side2], tex_coords: [0.0, 1.0] },
    	    // Top
            Vertex { position: [-side2,  side2,  side2], tex_coords: [0.0, 0.0] },
    	    Vertex { position: [ side2,  side2,  side2], tex_coords: [1.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], tex_coords: [1.0, 1.0] },
            Vertex { position: [-side2,  side2, -side2], tex_coords: [0.0, 1.0] }
        ].iter().cloned()).expect("failed to create buffer")
    };

    let index_buffer = vulkano::buffer::cpu_access::CpuAccessibleBuffer
        ::from_iter(vk.device.clone(), vulkano::buffer::BufferUsage::all(), [
            // Front
            0u16, 1, 2, 2, 3, 0,
            // Right
            4, 5, 6, 6, 7, 4,
            // Back
            8, 9, 10, 10, 11, 8,
            // Left
            12, 13, 14, 14, 15, 12,
            // Bottom
            16, 17, 18, 18, 19, 16,
            // Top
            20, 21, 22, 22, 23, 20,
        ].iter().cloned()).expect("failed to create buffer");

    // uniform buffer
    let uniform_buffer = vulkano::buffer::cpu_pool::CpuBufferPool::<vs::ty::Data>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());

    let vs = vs::Shader::load(vk.device.clone()).expect("failed to create shader module");
    let fs = fs::Shader::load(vk.device.clone()).expect("failed to create shader module");

    // render pass
    let render_pass = Arc::new(single_pass_renderpass!(vk.device.clone(),
        attachments: {
            color: {
                load: Clear,
                store: Store,
                format: vk.swapchain.format(),
                samples: 1,
            },
            depth: {
                load: Clear,
                store: DontCare,
                format: vulkano::format::Format::D16Unorm,
                samples: 1,
            } // depth
        },
        pass: {
            color: [color],
            depth_stencil: {depth} // depth
        }
    ).unwrap());

    let window = vk.surface.window();

    let mut dimensions = if let Some(dimensions) = window.get_inner_size() {
        let dimensions: (u32, u32) = dimensions.to_physical(window.get_hidpi_factor()).into();
        [dimensions.0, dimensions.1]
    } else {
        return;
    };

    let (texture, tex_future) = {
        let image = image::load_from_memory_with_format(include_bytes!("ume-300x200.png"),
            ImageFormat::PNG).unwrap().to_rgba();
        let image_data = image.into_raw().clone();

        ImmutableImage::from_iter(
            image_data.iter().cloned(),
            Dimensions::Dim2d { width: 300, height: 200 },
            Format::R8G8B8A8Srgb,
            vk.queue.clone()
        ).unwrap()
    };

    let sampler = Sampler::new(vk.device.clone(), Filter::Linear, Filter::Linear,
        MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat,
        SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap();

    // pipeline and framebuffer
    let (mut pipeline, mut framebuffers) = 
        window_size_dependent_setup(vk.device.clone(), &vs, &fs, &vk.images, render_pass.clone());

    let mut recreate_swapchain = false;
    let mut previous_frame = Box::new(tex_future) as Box<GpuFuture>;

    let persp_proj:cgmath::Matrix4<f32> = cgmath::perspective(cgmath::Deg(65.0),
        dimensions[0] as f32 / dimensions[1] as f32, 0.01, 100.0);
    let mut arcball_camera = {
        let look_at = cgmath::Matrix4::look_at(cgmath::Point3::new(0.0, 0.0, 2.0),
                      cgmath::Point3::new(0.0, 0.0, 0.0), cgmath::Vector3::new(0.0, 1.0, 0.0));
        arcball::ArcballCamera::new(&look_at, 0.05, 4.0, [dimensions[0] as f32,
        dimensions[1] as f32])
    };
    //
    let mut arcball_camera_mat4: [[f32;4];4] = arcball_camera.get_mat4().into();

    let mut mouse_pressed = [false, false];
    let mut prev_mouse: Option<(f64,f64)> = None;

    loop {
        previous_frame.cleanup_finished();

        if recreate_swapchain {
            dimensions = if let Some(dimensions) = window.get_inner_size() {
                let dimensions: (u32, u32) =
                    dimensions.to_physical(window.get_hidpi_factor()).into();
                [dimensions.0, dimensions.1]
            } else {
                return;
            };
            
            let (new_swapchain, new_images) = vk.swapchain.recreate_with_dimension(dimensions)
                .expect("swapcahain not recreate");
            
            vk.swapchain = new_swapchain;

            let (new_pipeline, new_framebuffers) = window_size_dependent_setup(vk.device.clone(), &vs, &fs,
                &new_images, render_pass.clone());
            
            pipeline = new_pipeline;
            framebuffers = new_framebuffers;

            recreate_swapchain = false;
        }

        let proj = (persp_proj * arcball_camera.get_mat4()).into();

        let uniform_buffer_subbuffer = {
            let uniform_data = vs::ty::Data {
                proj : proj,
            };

            uniform_buffer.next(uniform_data).unwrap()
        };

        let set0 = Arc::new(vulkano::descriptor::descriptor_set::PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_buffer(uniform_buffer_subbuffer).unwrap()
            .build().unwrap()
        );

        let set1 = Arc::new(vulkano::descriptor::descriptor_set::PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
            .build().unwrap()
        );
        
        let (image_num, acquire_future) =
            match swapchain::acquire_next_image(vk.swapchain.clone(), None) {
                Ok(r) => r,
                Err(AcquireError::OutOfDate) => {
                    recreate_swapchain = true;
                    continue;
                },
                Err(err) => panic!("{:?}", err)
            };

        let command_buffer =
            AutoCommandBufferBuilder::primary_one_time_submit(vk.device.clone(), vk.queue.family()).unwrap() // Ok
                .begin_render_pass(framebuffers[image_num].clone(), false,
                vec![[0.1, 0.1, 0.1, 1.0].into(), 1f32.into()]).unwrap()
            .draw_indexed(pipeline.clone(),
                &DynamicState::none(),
                vec!(vertex_buffer.clone()), index_buffer.clone(),
                (set0.clone(), set1.clone()), ()).unwrap()
            .end_render_pass().unwrap()
            .build().unwrap();

        let _future = previous_frame.join(acquire_future)
            .then_execute(vk.queue.clone(), command_buffer).unwrap()
            .then_swapchain_present(vk.queue.clone(), vk.swapchain.clone(), image_num)
            .then_signal_fence_and_flush();
        
        previous_frame = Box::new(sync::now(vk.device.clone())) as Box<_>;

        let mut done = false;
        vk.events_loop.poll_events(|ev| {
            match ev {
                winit::Event::WindowEvent {
                    event: winit::WindowEvent::CursorMoved { position: winit::dpi::LogicalPosition {x, y}, ..}, ..}
                       if prev_mouse.is_none() => {
                                    prev_mouse = Some((x, y));
                },
                winit::Event::WindowEvent {
                    event: winit::WindowEvent::CursorMoved { position: winit::dpi::LogicalPosition {x, y}, .. }, ..}
                        => {
                            //println!("MouseMoved {},{}", x, y);
                            let prev = prev_mouse.unwrap();
                            if mouse_pressed[0] {
                                arcball_camera.rotate(cgmath::Vector2::new(prev.0 as f32, prev.1 as f32),
                                        cgmath::Vector2::new(x as f32, y as f32));
                                arcball_camera_mat4 = arcball_camera.get_mat4().into();
                                //println!("rotate mat4: {:?}", arcball_camera_mat4);
                            } else if mouse_pressed[1] {
                                let mouse_delta = cgmath::Vector2::new((x - prev.0) as f32, -(y - prev.1) as f32);
                                arcball_camera.pan(mouse_delta, 0.16);
                                arcball_camera_mat4 = arcball_camera.get_mat4().into();
                                //println!("pan mat4: {:?}", arcball_camera_mat4);
                            }
                            prev_mouse = Some((x, y));
                    },
                winit::Event::WindowEvent {
                    event: winit::WindowEvent::MouseInput { state: _state, button: _button, ..}, ..} => {
                        //println!("button {:?}", _button);
                        if _button == winit::MouseButton::Left {
                            mouse_pressed[0] = _state == winit::ElementState::Pressed;
                        } else if _button == winit::MouseButton::Right {
                            mouse_pressed[1] = _state == winit::ElementState::Pressed;
                        }
                },
                winit::Event::WindowEvent {
                    event: winit::WindowEvent::MouseWheel {
                        delta: winit::MouseScrollDelta::LineDelta(_, y), .. }, ..}  => {
                            //println!("ScrollDelta {}", y);
                            arcball_camera.zoom(y, 0.1);
                            arcball_camera_mat4 = arcball_camera.get_mat4().into();
                            //println!("zoom mat4: {:?}", arcball_camera_mat4);
                },
                winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. } => done = true,
                _ => ()
            }
        });
        if done { return; }
    }
}

fn window_size_dependent_setup(
    device: Arc<Device>,
    vs: &vs::Shader,
    fs: &fs::Shader,
    images: &[Arc<SwapchainImage<Window>>],
    render_pass: Arc<RenderPassAbstract + Send + Sync>,
) -> (Arc<GraphicsPipelineAbstract + Send + Sync>, Vec<Arc<FramebufferAbstract + Send + Sync>> ) {
    let dimensions = images[0].dimensions();

    let depth_buffer = AttachmentImage::transient(device.clone(), dimensions, Format::D16Unorm).unwrap();

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

    let pipeline = Arc::new(GraphicsPipeline::start()
        .vertex_input_single_buffer::<Vertex>()
        .vertex_shader(vs.main_entry_point(), ())
        .triangle_list()
        .viewports_dynamic_scissors_irrelevant(1)
        .viewports(iter::once(Viewport {
            origin: [0.0, 0.0],
            dimensions: [dimensions[0] as f32, dimensions[1] as f32],
            depth_range: 0.0 .. 1.0,
        }))
        .fragment_shader(fs.main_entry_point(), ())
        .depth_stencil_simple_depth()
        .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
        .build(device.clone())
        .unwrap());

    (pipeline, framebuffers)
}

mod vs {
    vulkano_shaders::shader!{
        ty: "vertex",
        src: "
            #version 450

            layout(location = 0) in vec3 position;
            layout(location = 1) in vec2 tex_coords;
            layout(location = 0) out vec2 uv;

            layout(set = 0, binding = 0) uniform Data {
                mat4 proj;
            } uniforms;

            void main() 
            {
	            uv = tex_coords;
                gl_Position = uniforms.proj * vec4(position, 1.0);
            }"
    }
}

mod fs {
    vulkano_shaders::shader!{
        ty: "fragment",
        src: "
            #version 450

            layout(location = 0) in vec2 uv;
            layout(location = 0) out vec4 f_color;

            layout(set = 1, binding = 0) uniform sampler2D tex;

            void main() 
            {
	            f_color = texture(tex, uv);
            }"
    }
}

Vulkano Two Uniforms

f:id:onagat12:20181226014007g:plain
This is a Vulkano two uniforms example, which is based on Vulkano image example.
・first uniform : (set = 0, binding = 0) uniform sampler2D
・second uniform : (set = 0, binding = 1) uniform Data

#[macro_use]
extern crate vulkano;
extern crate vulkano_shaders;
extern crate vulkano_win;
extern crate winit;
extern crate cgmath;
extern crate image;

use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use vulkano::device::{Device, DeviceExtensions};
use vulkano::format::Format;
use vulkano::framebuffer::{Framebuffer, FramebufferAbstract, Subpass, RenderPassAbstract};
use vulkano::image::{SwapchainImage, ImmutableImage, Dimensions};
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::pipeline::GraphicsPipeline;
use vulkano::pipeline::viewport::Viewport;
use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};
use vulkano::swapchain::{AcquireError, PresentMode, SurfaceTransform, Swapchain, SwapchainCreationError};
use vulkano::swapchain;
use vulkano::sync::{GpuFuture, FlushError};
use vulkano::sync;
use vulkano_win::VkSurfaceBuild;
use winit::{EventsLoop, Window, WindowBuilder, Event, WindowEvent};

use image::ImageFormat;
use std::sync::Arc;

static WINDOW_NAME: &str = "Image Test";
static WIN_WIDTH: f64 = 700.0;
static WIN_HEIGHT: f64 = 650.0;

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

    let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
    println!("Using device: {} (type: {:?})", physical.name(), physical.ty());

    let mut events_loop = EventsLoop::new();
    let surface = WindowBuilder::new()
        .with_dimensions(winit::dpi::LogicalSize {width:WIN_WIDTH, height:WIN_HEIGHT})
        .with_title(WINDOW_NAME.to_string())
        .build_vk_surface(&events_loop, instance.clone()).unwrap();
    let window = surface.window();

    let queue_family = physical.queue_families().find(|&q|
        q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
    ).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();

    // initial_dimensions -> dimensions
    let mut dimensions = if let Some(dimensions) = window.get_inner_size() {
        let dimensions: (u32, u32) = dimensions.to_physical(window.get_hidpi_factor()).into();
        [dimensions.0, dimensions.1]
    } else {
        return;
    };

    let (mut swapchain, images) = {
        let caps = surface.capabilities(physical).unwrap();

        let usage = caps.supported_usage_flags;
        let alpha = caps.supported_composite_alpha.iter().next().unwrap();
        let format = caps.supported_formats[0].0;

        Swapchain::new(device.clone(), surface.clone(), caps.min_image_count, format,
            dimensions, 1, usage, &queue, SurfaceTransform::Identity, alpha,
            PresentMode::Fifo, true, None).unwrap()
    };


    #[derive(Debug, Clone)]
    struct Vertex { position: [f32; 2] }
    impl_vertex!(Vertex, position);

    let vertex_buffer = CpuAccessibleBuffer::<[Vertex]>::from_iter(
        device.clone(),
        BufferUsage::all(),
        [
            Vertex { position: [-0.5, -0.5 ] },
            Vertex { position: [-0.5,  0.5 ] },
            Vertex { position: [ 0.5, -0.5 ] },
            Vertex { position: [ 0.5,  0.5 ] },
        ].iter().cloned()
    ).unwrap();

    // uniform buffer
    let uniform_buffer = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Data>
        ::new(device.clone(), vulkano::buffer::BufferUsage::all());

    let vs = vs::Shader::load(device.clone()).unwrap();
    let fs = fs::Shader::load(device.clone()).unwrap();

    let render_pass = Arc::new(
        single_pass_renderpass!(device.clone(),
            attachments: {
                color: {
                    load: Clear,
                    store: Store,
                    format: swapchain.format(),
                    samples: 1,
                }
            },
            pass: {
                color: [color],
                depth_stencil: {}
            }
        ).unwrap()
    );

    let (texture, tex_future) = {
        let image = image::load_from_memory_with_format(include_bytes!("ume-300x200.png"),
            ImageFormat::PNG).unwrap().to_rgba();
        let image_data = image.into_raw().clone();

        ImmutableImage::from_iter(
            image_data.iter().cloned(),
            // note Dimensions: image dimensions
            Dimensions::Dim2d { width: 300, height: 200 },
            Format::R8G8B8A8Srgb,
            queue.clone()
        ).unwrap()
    };

    let 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();

    let pipeline = Arc::new(GraphicsPipeline::start()
        .vertex_input_single_buffer::<Vertex>()
        .vertex_shader(vs.main_entry_point(), ())
        .triangle_strip()
        .viewports_dynamic_scissors_irrelevant(1)
        .fragment_shader(fs.main_entry_point(), ())
        .blend_alpha_blending()
        .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
        .build(device.clone())
        .unwrap());

    let mut dynamic_state = DynamicState { line_width: None, viewports: None, scissors: None };
    let mut framebuffers = window_size_dependent_setup(&images, render_pass.clone(), &mut dynamic_state);

    let mut recreate_swapchain = false;
    let mut previous_frame_end = Box::new(tex_future) as Box<GpuFuture>;

    let mut mouse_pressed = false;
    let mut mouse_position = [0.5, 0.5];
    let mut center = [0.5, 0.5];

    loop {
        previous_frame_end.cleanup_finished();
        if recreate_swapchain {
            dimensions = if let Some(dimensions) = window.get_inner_size() {
                let dimensions: (u32, u32) = dimensions.to_physical(window.get_hidpi_factor()).into();
                [dimensions.0, dimensions.1]
            } else {
                return;
            };

            let (new_swapchain, new_images) = match swapchain.recreate_with_dimension(dimensions) {
                Ok(r) => r,
                Err(SwapchainCreationError::UnsupportedDimensions) => continue,
                Err(err) => panic!("{:?}", err)
            };

            swapchain = new_swapchain;
            framebuffers = window_size_dependent_setup(&new_images, render_pass.clone(), &mut dynamic_state);

            recreate_swapchain = false;
        }

        // mouse position
        if mouse_pressed {
            //println!("position: {} {}", mouse_position[0] as u32, mouse_position[1] as u32);
            let uvx = 2.0/(dimensions[0] as f32)* mouse_position[0] - 0.5;
            let uvy = 2.0/(dimensions[1] as f32)* mouse_position[1] - 0.5;
            //println!("uv: {} {}", uvx, uvy);
            center = [uvx, uvy];
        }
        // uniform buffer
        let uniform_buffer_subbuffer = {
            let uniform_data = fs::ty::Data {
                center: center,
            };

            uniform_buffer.next(uniform_data).unwrap()
        };

        let set = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
            .add_buffer(uniform_buffer_subbuffer).unwrap()
            .build().unwrap()
        );


        let (image_num, future) = match swapchain::acquire_next_image(swapchain.clone(), None) {
            Ok(r) => r,
            Err(AcquireError::OutOfDate) => {
                recreate_swapchain = true;
                continue;
            }
            Err(err) => panic!("{:?}", err)
        };

        let clear_values = vec!([0.0, 0.0, 0.0, 1.0].into());
        let cb = AutoCommandBufferBuilder::primary_one_time_submit(device.clone(), queue.family())
            .unwrap()
            .begin_render_pass(framebuffers[image_num].clone(), false, clear_values).unwrap()
            .draw(pipeline.clone(), &dynamic_state, vertex_buffer.clone(), set.clone(), ()).unwrap()
            .end_render_pass().unwrap()
            .build().unwrap();

        let future = previous_frame_end.join(future)
            .then_execute(queue.clone(), cb).unwrap()
            .then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
            .then_signal_fence_and_flush();

        match future {
            Ok(future) => {
                previous_frame_end = Box::new(future) as Box<_>;
            }
            Err(FlushError::OutOfDate) => {
                recreate_swapchain = true;
                previous_frame_end = Box::new(sync::now(device.clone())) as Box<_>;
            }
            Err(e) => {
                println!("{:?}", e);
                previous_frame_end = Box::new(sync::now(device.clone())) as Box<_>;
            }
        }

        let mut done = false;
        events_loop.poll_events(|ev| {
            match ev {
                winit::Event::WindowEvent {
                    event: winit::WindowEvent::CursorMoved { position: winit::dpi::LogicalPosition {x, y}, .. }, ..}  => {
                        mouse_position = [x as f32, y as f32];
                        //if mouse_pressed {
                        //    println!("position: {} {}", mouse_position[0] as u32, mouse_position[1] as u32);
                        //}
                },
                winit::Event::WindowEvent {
                    event: winit::WindowEvent::MouseInput { state: _state, button: _button, ..}, ..} => {
                        mouse_pressed =
                            _button == winit::MouseButton::Left && _state == winit::ElementState::Pressed;
                        if mouse_pressed {println!("Mouse {:?} {:?}", _button, _state);}
                },

                Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => done = true,
                Event::WindowEvent { event: WindowEvent::Resized(_), .. } => recreate_swapchain = true,
                _ => ()
            }
        });
        if done { return; }
    }
}

/// This method is called once during initialization, then again whenever the window is resized
fn window_size_dependent_setup(
    images: &[Arc<SwapchainImage<Window>>],
    render_pass: Arc<RenderPassAbstract + Send + Sync>,
    dynamic_state: &mut DynamicState
) -> Vec<Arc<FramebufferAbstract + Send + Sync>> {
    let dimensions = images[0].dimensions();

    let viewport = Viewport {
        origin: [0.0, 0.0],
        dimensions: [dimensions[0] as f32, dimensions[1] as f32],
        depth_range: 0.0 .. 1.0,
    };
    dynamic_state.viewports = Some(vec!(viewport));

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

mod vs {
    vulkano_shaders::shader!{
        ty: "vertex",
        src: "
#version 450

layout(location = 0) in vec2 position;
layout(location = 0) out vec2 tex_coords;

void main() {
    gl_Position = vec4(position, 0.0, 1.0);
    tex_coords = position + vec2(0.5);
}"
    }
}

mod fs {
    vulkano_shaders::shader!{
        ty: "fragment",
        src: "
#version 450

layout(location = 0) in vec2 tex_coords;
layout(location = 0) out vec4 f_color;

layout(set = 0, binding = 0) uniform sampler2D tex;
layout(set = 0, binding = 1) uniform Data {
    vec2 center;
} uniforms;

const float refractance = 1.4;
const vec3 eyeVector = vec3(0., 0., -1.);
const float r = 0.3;

bool inCircle(vec2 position, vec2 center, float size) {
    float len = length(position - center);
    return (len < size)? true: false;
}

void main() {
    vec2 center = uniforms.center;
    vec2 new_coords = tex_coords - center;
    float length_xy = length(new_coords);
    float z = sqrt(pow(r,2) - pow(length_xy,2));
    float sin_thetai = sqrt(pow(r,2) - pow(z,2)) / r;
    float thetai = asin(sin_thetai);
    float sin_thetar = sin_thetai / refractance;
    float thetar = asin(sin_thetar);
    float lxy = z*tan(thetai - thetar);
    float deltax = lxy*new_coords.x/length_xy;
    float deltay = lxy*new_coords.y/length_xy;
    
    vec2 uv = (inCircle(tex_coords, center, r)) ? tex_coords - vec2(deltax, deltay): tex_coords;
    f_color = texture(tex, uv);
}"
    }
}

Rust Vulkano Tutorial(1) Window Creation(3) Resize

前回のプログラムは、ウィンドウのリサイズに対応していません。
ウインドウを拡大したとき、描画領域ののサイズが対応していません。

3 ウィンドウのリサイズ
 ウインドウのリサイズに対応するには、ウィンドウをリサイズしたとき、
スワップチェーンとフレームバッファを再作成し、スワップチェーン・
イメージの取得、コマンドの実行を行います。これらの処理は、リサイズが
行われる度に実行するので、繰り返し処理(loop)の中で行います。
また、ウィンドウのリサイズに対応して、スワップチェーンの再作成を
recreate_swapchainフラッグでコントロールします。

ウィンドウのリサイズに関する部分は次のようになります。

let window = surface.window();

let mut recreate_swapchain = false;

loop {
    previous_frame_end.cleanup_finished();

    if recreate_swapchain {
        let dimensions = window.get_inner_size().unwrap();
        let win_width = dimensions.width;
        let win_height = dimensions.height;

        let (new_swapchain, new_images) =
            swapchain.recreate_with_dimension([win_width as u32, win_height as u32])
            .expect("swapcahain not recreate");
        swapchain = new_swapchain;

        framebuffer = new_images.iter().map(|image| {
            Arc::new(Framebuffer::start(render_pass.clone())
            .add(image.clone()).unwrap()
            .build().unwrap())
        }).collect::<Vec<_>>();

        recreate_swapchain = false;
    }

    let (image_num, acquire_future) =
        match swapchain::acquire_next_image(swapchain.clone(), None) {
            Ok(r) => r,
            Err(AcquireError::OutOfDate) => {
                recreate_swapchain = true;
                continue;
            },
            Err(err) => panic!("{:?}", err)
        };
        
    let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
        device.clone(), queue.family()).unwrap()
        .begin_render_pass(framebuffer[image_num].clone(), false, vec![[0.0, 0.0, 1.0, 1.0].into()])
        .unwrap()
        .end_render_pass()
        .unwrap()
        .build()
        .unwrap();

    let _future = previous_frame_end.join(acquire_future)
        .then_execute(queue.clone(), command_buffer).unwrap()
        .then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
        .then_signal_fence_and_flush();

    previous_frame_end = Box::new(sync::now(device.clone())) as Box<_>;

    events_loop.poll_events(|event| {
        match event {
            winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. }
                 => done = true,
                
            winit::Event::WindowEvent { event: winit::WindowEvent::Resized(_), .. }
                => recreate_swapchain = true,
               
            _ => ()
        }
    });

    if done { return; }
}


全体のプログラム

#[macro_use]
extern crate vulkano;
extern crate vulkano_win;
extern crate winit;

use vulkano_win::VkSurfaceBuild;
use winit::EventsLoop;
use winit::WindowBuilder;
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::device::Device;
use vulkano::swapchain;
use vulkano::swapchain::{AcquireError, Swapchain, SurfaceTransform, PresentMode};
use vulkano::framebuffer::Framebuffer;
use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::sync;
use vulkano::sync::GpuFuture;
use std::sync::Arc;

fn main() {
    let instance = {
        let extensions = vulkano_win::required_extensions();
        Instance::new(None, &extensions, None).expect("failed to create Vulkan instance")
    };

    let mut events_loop = EventsLoop::new();
    let surface = WindowBuilder::new().build_vk_surface(&events_loop, instance.clone()).unwrap();

    let window = surface.window();

    let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
    let queue_family = physical.queue_families().find(|&q| {
        q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
    }).unwrap();

    let (device, mut queues) = {
        let device_ext = vulkano::device::DeviceExtensions {
            khr_swapchain: true,
            .. vulkano::device::DeviceExtensions::none()
        };

        Device::new(physical, physical.supported_features(), &device_ext,
            [(queue_family, 0.5)].iter().cloned()).expect("failed to create device")
    };
    let queue = queues.next().unwrap();

    let caps = surface.capabilities(physical)
        .expect("failed to get surface capabilities");
    let dimensions = caps.current_extent.unwrap_or([1280, 1024]);
    let alpha = caps.supported_composite_alpha.iter().next().unwrap();
    let format = caps.supported_formats[0].0;

    let (mut swapchain, images) = Swapchain::new(device.clone(), surface.clone(),
        caps.min_image_count, format, dimensions, 1, caps.supported_usage_flags, &queue,
        SurfaceTransform::Identity, alpha, PresentMode::Fifo, true, None)
        .expect("failed to create swapchain");

    let render_pass = Arc::new(single_pass_renderpass!(device.clone(),
        attachments: {
            color: {
                load: Clear,
                store: Store,
                format: swapchain.format(),
                samples: 1,
            }
        },
        pass: {
            color: [color],
            depth_stencil: {}
        }
    ).unwrap());

    let mut framebuffer = images.iter().map(|image| {
        Arc::new(Framebuffer::start(render_pass.clone())
            .add(image.clone()).unwrap()
            .build().unwrap())
    }).collect::<Vec<_>>();

    let mut previous_frame_end = Box::new(sync::now(device.clone())) as Box<GpuFuture>;

    let mut recreate_swapchain = false;

    let mut done = false;
    loop {
        previous_frame_end.cleanup_finished();

        if recreate_swapchain {
            let dimensions = window.get_inner_size().unwrap();
            let win_width = dimensions.width;
            let win_height = dimensions.height;

            let (new_swapchain, new_images) =
                swapchain.recreate_with_dimension([win_width as u32, win_height as u32])
                .expect("swapcahain not recreate");
            swapchain = new_swapchain;

            framebuffer = new_images.iter().map(|image| {
                Arc::new(Framebuffer::start(render_pass.clone())
                .add(image.clone()).unwrap()
                .build().unwrap())
            }).collect::<Vec<_>>();

            recreate_swapchain = false;
        }

        let (image_num, acquire_future) =
            match swapchain::acquire_next_image(swapchain.clone(), None) {
                Ok(r) => r,
                Err(AcquireError::OutOfDate) => {
                    recreate_swapchain = true;
                    continue;
                },
                Err(err) => panic!("{:?}", err)
            };
        
        let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
            device.clone(), queue.family()).unwrap()
            .begin_render_pass(framebuffer[image_num].clone(), false, vec![[0.0, 0.0, 1.0, 1.0].into()])
            .unwrap()
            .end_render_pass()
            .unwrap()
            .build()
            .unwrap();

        let _future = previous_frame_end.join(acquire_future)
            .then_execute(queue.clone(), command_buffer).unwrap()
            .then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
            .then_signal_fence_and_flush();

        previous_frame_end = Box::new(sync::now(device.clone())) as Box<_>;

        events_loop.poll_events(|event| {
            match event {
                winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. }
                    => done = true,
                winit::Event::WindowEvent { event: winit::WindowEvent::Resized(_), .. }
                    => recreate_swapchain = true,
                _ => ()
            }
        });

        if done { return; }
    }
}

Rust Vulkano Tutorial(1) Window Creation(2)

 前回のWindow Creation(1)のつづきです。

2 ウィンドウの初期化
 つづけて、ウィンドウの初期化を行います。次の処理が続きます。
(6)スワップチェーン(Swapchain)とイメージ(SwapchainImage)の作成
(7)レンダーパスと(render_pass)フレームバッファ(framebuffer)の作成
(8)スワップチェーンイメージの取得
(9)コマンドバッファ(command_buffer)の作成
(10)コマンドバッファの実行と同期処理

(6)スワップチェーンとイメージの作成
  ウィンドウサーフェイスへの描画には、スワップチェーンが必要です。
 スワップチェーンは、スワップチェーン・イメージとともに作成します。
 先に、作成に必要となる引数を設定します。

let caps = surface.capabilities(physical)
    .expect("failed to get surface capabilities");
let dimensions = caps.current_extent.unwrap_or([1280, 1024]);
let alpha = caps.supported_composite_alpha.iter().next().unwrap();
let format = caps.supported_formats[0].0;

let (swapchain, images) = Swapchain::new(device.clone(), surface.clone(),
    caps.min_image_count, format, dimensions, 1, caps.supported_usage_flags, &queue,
    SurfaceTransform::Identity, alpha, PresentMode::Fifo, true, None)
    .expect("failed to create swapchain");

(7)レンダーパスとフレームバッファの作成
  スワップチェーン・イメージは初期化して使用します。初期化は、
 レンダーパスとフレームバッファを使って行います。スワップチェーン・
 イメージの各イメージにレンダーパスを設定し、これらをイメージリスト
 としてまとめたフレームバッファを作成します。

let render_pass = Arc::new(single_pass_renderpass!(device.clone(),
    attachments: {
        color: {
             load: Clear,
             store: Store,
             format: swapchain.format(),
             samples: 1,
        }
    },
    pass: {
         color: [color],
            depth_stencil: {}
    }
).unwrap());

let framebuffer = images.iter().map(|image| {
    Arc::new(Framebuffer::start(render_pass.clone())
        .add(image.clone()).unwrap()
        .build().unwrap())
}).collect::<Vec<_>>();

(8)スワップチェーンイメージの取得
  スワップチェーンから、スワップチェーンイメージのインデックス
 (image_num)とGPUの描画を管理するオブジェクト(acquire_future)
 を取得します。

let (image_num, acquire_future) = swapchain::acquire_next_image(swapchain.clone(), None).unwrap();

(9)コマンドバッファの作成
  コマンドバッファは、描画等のコマンドを記述したコマンドリストです。
 ここでは、begin_render_passに青色([0.0, 0.0, 1.0, 1.0])で画面を初期化する
 処理を記述しています。

let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
    device.clone(), queue.family()).unwrap()
    .begin_render_pass(framebuffer[image_num].clone(), false, vec![[0.0, 0.0, 1.0, 1.0].into()])
    .unwrap()
    .end_render_pass()
    .unwrap()
    .build()
    .unwrap();

(10)コマンドバッファの実行と同期処理
  初めの行は、GPUの描画を管理するオブジェクト(GpuFuture)のメモリを
 確保(Box)するものです。ここに、先程スワップチェーンから取得した
 acquire_futureを結合します。次にコマンドバッファを実行(then_execute)し、
 スワップチェーンイメージを表示(then_swapchain_present)します。
 最後にこれらの同期処理(then_signal_fence_and_flush)を行います。

let previous_frame_end = Box::new(sync::now(device.clone())) as Box<GpuFuture>;

let _future = previous_frame_end.join(acquire_future)
        .then_execute(queue.clone(), command_buffer).unwrap()
        .then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
        .then_signal_fence_and_flush();

全体のプログラムは以下のようになります。
初期化したウィンドウを表示します。

#[macro_use]
extern crate vulkano;
extern crate vulkano_win;
extern crate winit;

use vulkano_win::VkSurfaceBuild;
use winit::EventsLoop;
use winit::WindowBuilder;
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::device::Device;
use vulkano::swapchain::{Swapchain, SurfaceTransform, PresentMode};
use vulkano::swapchain;
use std::sync::Arc;
use vulkano::framebuffer::Framebuffer;
use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::sync;
use vulkano::sync::GpuFuture;

fn main() {
    let instance = {
        let extensions = vulkano_win::required_extensions();
        Instance::new(None, &extensions, None).expect("failed to create Vulkan instance")
    };

    let mut events_loop = EventsLoop::new();
    let surface = WindowBuilder::new().build_vk_surface(&events_loop, instance.clone()).unwrap();

    // 02
    let physical = PhysicalDevice::enumerate(&instance).next().unwrap();
    let queue_family = physical.queue_families().find(|&q| {
        q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
    }).unwrap();

    let (device, mut queues) = {
        let device_ext = vulkano::device::DeviceExtensions {
            khr_swapchain: true,
            .. vulkano::device::DeviceExtensions::none()
        };

        Device::new(physical, physical.supported_features(), &device_ext,
            [(queue_family, 0.5)].iter().cloned()).expect("failed to create device")
    };
    let queue = queues.next().unwrap();
    //

    // 03
    let caps = surface.capabilities(physical)
        .expect("failed to get surface capabilities");
    let dimensions = caps.current_extent.unwrap_or([1280, 1024]);
    let alpha = caps.supported_composite_alpha.iter().next().unwrap();
    let format = caps.supported_formats[0].0;

    let (swapchain, images) = Swapchain::new(device.clone(), surface.clone(),
        caps.min_image_count, format, dimensions, 1, caps.supported_usage_flags, &queue,
        SurfaceTransform::Identity, alpha, PresentMode::Fifo, true, None)
        .expect("failed to create swapchain");
    //

    // 04
    let render_pass = Arc::new(single_pass_renderpass!(device.clone(),
        attachments: {
            color: {
                load: Clear,
                store: Store,
                format: swapchain.format(),
                samples: 1,
            }
        },
        pass: {
            color: [color],
            depth_stencil: {}
        }
    ).unwrap());

    let framebuffer = images.iter().map(|image| {
        Arc::new(Framebuffer::start(render_pass.clone())
            .add(image.clone()).unwrap()
            .build().unwrap())
    }).collect::<Vec<_>>();
    //

    // 05
    let (image_num, acquire_future) = swapchain::acquire_next_image(swapchain.clone(), None).unwrap();

    let command_buffer = AutoCommandBufferBuilder::primary_one_time_submit(
        device.clone(), queue.family()).unwrap()
        .begin_render_pass(framebuffer[image_num].clone(), false, vec![[0.0, 0.0, 1.0, 1.0].into()])
        .unwrap()
        .end_render_pass()
        .unwrap()
        .build()
        .unwrap();
    //

    // 06
    let previous_frame_end = Box::new(sync::now(device.clone())) as Box<GpuFuture>;

    let _future = previous_frame_end.join(acquire_future)
            .then_execute(queue.clone(), command_buffer).unwrap()
            .then_swapchain_present(queue.clone(), swapchain.clone(), image_num)
            .then_signal_fence_and_flush();
    //

    let mut done = false;
    loop {
        events_loop.poll_events(|event| {
            match event {
                winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. }
                    => done = true,
                _ => ()
            }
        });

        if done { return; }
    }
}


Cargo.tomlの [dependencies] 部分です。

[dependencies]
vulkano-win = "0.11"
winit = "0.18"
vulkano = "0.11"

Rust Vulkano Tutorial(1) Window Creation(1)

Rust Vulkano のチュートリアルです。
今回は初期化したウィンドウ(Window)の作成について記述しています。
Vulkanoガイド(https://vulkano.rs/guide/introduction)とVulkano Triangle Example
https://github.com/vulkano-rs/vulkano)を参考にしています。

1 Vulkanオブジェクトの初期化
 Vulkanでは直接GPUを扱います。GPUにアクセスできるように、Vulkanデバイス
(Device)を作成します。
 Vulkanデバイスの作成には、決まった手続きがあります。次のようにして
 作成します。
 (1)インスタンス(Instance)の作成
 (2)サーフェイス(Surface)の作成
 (3)物理デバイス(Physical_device)の取得
 (4)キュー・ファミリー(Queue_family)の取得
 (5)Vulkanデバイス(Device)とキュー(Queue)の作成

(1)インスタンスの作成

let instance = {
    let extensions = vulkano_win::required_extensions();
    Instance::new(None, &extensions, None).expect("failed to create Vulkan instance")
};

(2)サーフェイスの作成
  キュー・ファミリーの取得時に必要となるので、ここでサーフェイスを
 作成します。同時にウィンドウも作成されます。

let mut events_loop = EventsLoop::new();
let surface = WindowBuilder::new().build_vk_surface(&events_loop,
    instance.clone()).unwrap();

  サーフェイスの作成時にイベント・ループ(EventsLoop)が必要となるので、
 先にイベント・ループを作成します。

(3)物理デバイスの取得
  使用する物理デバイスを取得します。

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

  使用可能な物理デバイスを列挙(enumerate)し、最初のデバイスを取得
 (next)します。

(4)キュー・ファミリーの取得
  Vulkanデバイスの作成時に必要となるので、ここで物理デバイスからキュー・
 ファミリーを取得します。
  条件「q.supports_graphics() && surface.is_supported(q)」を満たすキュー・
 ファミリー(グラフィックス用でサーフェイスに描画できる
 キュー・ファミリー)を取り出します。

let queue_family = physical.queue_families().find(|&q| {
    q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
}).unwrap();

(5)Vulkanデバイスとキューの作成
  Vulkanデバイスとキューを作成します。

let (device, mut queues) = {
    let device_ext = vulkano::device::DeviceExtensions {
        khr_swapchain: true,
        .. vulkano::device::DeviceExtensions::none()
    };

    Device::new(physical, physical.supported_features(), &device_ext,
         [(queue_family, 0.5)].iter().cloned()).expect("failed to create device")
};
let queue = queues.next().unwrap();

  最後の行で、作成したqueues(キューのイテレータ)から、最初の要素
 (next)を使用するキューとして取り出します。

 ここまでの段階では、作成されたウィンドウは瞬時に閉じてしまいます。
そこで、events_loopを使ってウィンドウの表示を行います。

let mut done = false;
loop {
    events_loop.poll_events(|event| {
        match event {
            winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. }
                => done = true,
            _ => ()
        }
    });

    if done { return; }
}

 ウィンドウの終了フラッグ(done)を用いて、ウィンドウのクローズボタンが
押されたときにウィンドウを閉じるようにします。

 これで、黒い画面(初期化されていない)のウィンドウが表示されます。

 全体のプログラムは、次のようになります。

extern crate vulkano_win;
extern crate winit;
extern crate vulkano;

use vulkano_win::VkSurfaceBuild;
use winit::EventsLoop;
use winit::WindowBuilder;
use vulkano::instance::{Instance, PhysicalDevice};
use vulkano::device::Device;

fn main() {
    // Instance 
    let instance = {
        let extensions = vulkano_win::required_extensions();
        Instance::new(None, &extensions, None).expect("failed to create Vulkan instance")
    };

    // Surface and Window
    let mut events_loop = EventsLoop::new();
    let surface = WindowBuilder::new().build_vk_surface(&events_loop, instance.clone()).unwrap();

    // Physical Device
    let physical = PhysicalDevice::enumerate(&instance).next().unwrap();

    // QueueFamily
    let queue_family = physical.queue_families().find(|&q| {
        q.supports_graphics() && surface.is_supported(q).unwrap_or(false)
    }).unwrap();

    // Device and Queues
    let (device, mut queues) = {
        let device_ext = vulkano::device::DeviceExtensions {
            khr_swapchain: true,
            .. vulkano::device::DeviceExtensions::none()
        };

        Device::new(physical, physical.supported_features(), &device_ext,
            [(queue_family, 0.5)].iter().cloned()).expect("failed to create device")
    };
    let queue = queues.next().unwrap();


    let mut done = false;
    loop {
        // EventsLoop
        events_loop.poll_events(|event| {
            match event {
                winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. }
                    => done = true,
                _ => ()
            }
        });

        if done { return; }
    }
}

Rust Vulkano Triangle (3)

Rust Vulkano Triangle (2)の続きです。

スワップチェインとイメージの作成
 screen(サーフェイス)にイメージを描画するには、Swapchain
スワップチェイン)が必要になります。
 スワップチェインの作成と同時にSwapchainImage(イメージ)が作成され
ます。スワップチェインは、イメージをサーフェイスに描画する役目をします。

 スワップチェインは、Struct vulkano::swapchain::Swapchainのnewメソッドで
作成する。
書式

fn new(device, surface, num_images, format, dimensions,
    layers, usage, sharing, transform, alpha, mode,
    clipped, old_swapchain)
  -> Result<(Swapchain, SwapchainImage), SwapchainCreationError>

引数には、
device(使用するVulkanデバイス)、
surface(使用するサーフェイス)、
num_images(イメージバッファの数)、
format(イメージフォーマット)、
dimensions(イメージのサイズ)、
layers(各イメージのレイヤーの数)、
usage(イメージの使用形式)、
sharing(sharing mode、キューファミリーの使用モードを指定)、
transform(surface transform、スクリーンに描画する前のイメージ変換)、
alpha(Composite alphaの指定)、
mode(present mode、イメージの表示モードの指定)、
clipped(ブッファの非表示部分をクリップ)、
old_swapchain(以前のスワップチェインの使用)
を指定する。
戻り値はResult型で、Okの場合はスワップチェインとスワップチェインイメージ
のタプル ( Swapchain, SwapcainImage ) を返す。

 引数 num_images、format、dimensions、usage、alphaは、サーフェイスの
capabilitiesメソッドを用いて取得した値を指定する。
capabilitiesの取得と引数

let caps = window.surface().capabilities(physical)
    .expect("failed to get surface capabilities");

num_images : caps.min_image_count
format : caps.supported_formats[0].0 (タプルの最初の値)
dimensions : caps.current_extent.unwrap()
usage : caps.supported_usage_flags
alpha : caps.supported_composite_alpha.iter().next().unwrap()

その他の引数は、次のようにする。

・device : device.clone()
・surface : window.surface().clone()
・layers : 1
・sharing : &queue
・transform : SurfaceTransform::Identity
・present : PresentMode::Fifo
・clipped : true
・old_swapchain : None

スワップチェインとイメージの作成は次のようになる。

// dimension
let mut dimensions;

// Swapchain and images  
let (mut swapchain, mut images) = {
    // Surface capabilities
    let caps = window.surface().capabilities(physical)
        .expect("failed to get surface capabilities");
    // dimension
    dimensions = caps.current_extent.unwrap();
    // composite alpha
    let alpha = caps.supported_composite_alpha.iter().next().unwrap();
    // format
    let format = caps.supported_formats[0].0;
    //  Swapchain and images
    Swapchain::new(device.clone(), window.surface().clone(), caps.min_image_count, format,
        dimensions, 1, caps.supported_usage_flags, &queue, SurfaceTransform::Identity, alpha,
        PresentMode::Fifo, true, None).expect("failed to create swapchain")
};