Vala プログラミング

Rust プログラミング

おながのブログ

Rust Vulkano Texture Mapping

f:id:onagat12:20190209181103p:plain

Texture Mapping
前回の Two Objects に Texture Mapping を適用しました。
Texture Mapping の方法は、David Wolff「OpenGL 4.0 シェーディング言語
( OpenGL 4.0 Shading Language Cookbook )」を参考にしています。

追加・変更する箇所
1 shader
・vertex shader

(1) layout(location = 2) out vec4 ProjTexCoord;
(2) layout(set = 0, binding = 0) uniform MVP {
       mat4 view;
       mat4 mvp;
       mat4 t_matrix;
    } mvp;
(3) ProjTexCoord = mvp.t_matrix * pos4;

・fragment shader

(1) layout(location = 2) in vec4 ProjTexCoord;
(2) layout(set = 1, binding = 0) uniform sampler2D ProjectorTex;
(3) vec4 projTexColor = vec4(0.0);
    if( ProjTexCoord.z > 0.0 )
        projTexColor = textureProj( ProjectorTex, ProjTexCoord );
    f_color = vec4(color,1.0) + projTexColor * 0.6;

2 texture の設定

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

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

let sampler = Sampler::new(vk.device.clone(), Filter::Linear, Filter::Linear,
    MipmapMode::Nearest, SamplerAddressMode::ClampToBorder(FloatTransparentBlack),
    SamplerAddressMode::ClampToBorder(FloatTransparentBlack),
    SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap();
* 3つの SamplerAddressMode(u, v, w)は以下のように設定しています。
  u,v:ClampToBorder(FloatTransparentBlack)
   uv座標が[0.0, 1.0]の範囲外は指定したボーダー色(FloatTransparentBlack)にする。
  w:Repeat(textureを繰り返し表示する)

let mut previous_frame = Box::new(tex_future) as Box<GpuFuture>;
* 前回使用した Box::new(now(vk.device.clone())) の代わりに、
  Box::new(tex_future) を使用します。これでメモリの確保とtextureの初期化が
 行われます。

3 texture 射影(Projection)行列
texture mapping を行うための射影行列を準備します。

// Projection Matrix (t Matrix)
let mut t_matrix = Matrix4::from_cols(
    Vector4::new(0.5, 0.0, 0.0, 0.0),
    Vector4::new(0.0, 0.5, 0.0, 0.0),
    Vector4::new(0.0, 0.0, 0.5, 0.0),
    Vector4::new(0.5, 0.5, 0.5, 1.0));

// Projection View Matrix (tv Matrix)
let tv_matrix = {
   let projector_position = Point3::new(1.0, 2.0, 2.0);
    let projector_at = Point3::new(-1.0, -1.0, 0.0);
    let projector_up = Vector3::new(0.0, 1.0, 0.0);

    Matrix4::look_at(projector_position, projector_at, projector_up)
};

// Projection Projection(perspective) Matrix (tp Matrix)
let tp_matrix = cgmath::perspective(Rad(std::f32::consts::FRAC_PI_6),
       aspect_ratio, 0.2, 100.0);

t_matrix = t_matrix * tp_matrix * tv_matrix;

4 uniform データと DescripterSet の設定
・uniform ダータ

let ubo_mvp_subbuffer = {
    let uniform_data = vs::ty::MVP {
        view : view.into(),
        mvp : mvp.into(),
        t_matrix: t_matrix.into(),
    };
    ubo_mvp.next(uniform_data).unwrap()
 };

・DescripterSet
fragment shader の3つの uniform(set=1, binding=0, 1, 2)を設定する。

let set11 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
            .add_buffer(ubo_material1_subbuffer).unwrap()
            .add_buffer(ubo_light_subbuffer.clone()).unwrap()
            .build().unwrap()
        );
* obj-2 も同様


プログラム

#[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;
use vulkano::pipeline::viewport::Viewport;
use vulkano::swapchain;
use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, AcquireError,
        SwapchainCreationError};
use vulkano::sync;
use vulkano::sync::GpuFuture;
use vulkano::sampler::{Sampler, SamplerAddressMode, Filter, MipmapMode};
use vulkano::sampler::BorderColor::FloatTransparentBlack;
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;

use winit::{Window, EventsLoop, WindowBuilder};
use image::ImageFormat;
use std::sync::Arc;
use cgmath::{Matrix4, Rad, Point3, Vector3, Vector4};

static WINDOW_NAME: &str = "Texture Mapping Tow Objects";
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();
        // first queue
        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[1].0; // formats[1]
            //
            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() {
    let mut vk = Vulkan::init_vk();

    #[derive(Debug, Clone)]
    struct Vertex { position: [f32; 3], normal: [f32; 3] }
    impl_vertex!(Vertex, position, normal);
    
    // obj-1
    let vertex_buffer1 = {
        let side2: f32 = 0.5 / 2.0;

        CpuAccessibleBuffer::from_iter(vk.device.clone(), BufferUsage::all(), [
            // Front
            Vertex { position: [-side2, -side2,  side2], normal: [0.0, 0.0, 1.0] },
    	    Vertex { position: [ side2, -side2,  side2], normal: [0.0, 0.0, 1.0] },
    	    Vertex { position: [ side2,  side2,  side2], normal: [0.0, 0.0, 1.0] },
            Vertex { position: [-side2,  side2,  side2], normal: [0.0, 0.0, 1.0] },
    	    // Right
    	    Vertex { position: [ side2, -side2,  side2], normal: [1.0, 0.0, 0.0] },
    	    Vertex { position: [ side2, -side2, -side2], normal: [1.0, 0.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], normal: [1.0, 0.0, 0.0] },
            Vertex { position: [ side2,  side2,  side2], normal: [1.0, 0.0, 0.0] },
    	    // Back
    	    Vertex { position: [-side2, -side2, -side2], normal: [0.0, 0.0, -1.0] },
    	    Vertex { position: [-side2,  side2, -side2], normal: [0.0, 0.0, -1.0] },
    	    Vertex { position: [ side2,  side2, -side2], normal: [0.0, 0.0, -1.0] },
            Vertex { position: [ side2, -side2, -side2], normal: [0.0, 0.0, -1.0] },
    	    // Left
    	    Vertex { position: [-side2, -side2,  side2], normal: [-1.0, 0.0, 0.0] },
    	    Vertex { position: [-side2,  side2,  side2], normal: [-1.0, 0.0, 0.0] },
            Vertex { position: [-side2,  side2, -side2], normal: [-1.0, 0.0, 0.0] },
            Vertex { position: [-side2, -side2, -side2], normal: [-1.0, 0.0, 0.0] },
            // Bottom
    	    Vertex { position: [-side2, -side2,  side2], normal: [0.0, -1.0, 0.0] },
    	    Vertex { position: [-side2, -side2, -side2], normal: [0.0, -1.0, 0.0] },
    	    Vertex { position: [ side2, -side2, -side2], normal: [0.0, -1.0, 0.0] },
            Vertex { position: [ side2, -side2,  side2], normal: [0.0, -1.0, 0.0] },
    	    // Top
            Vertex { position: [-side2,  side2,  side2], normal: [0.0, 1.0, 0.0] },
    	    Vertex { position: [ side2,  side2,  side2], normal: [0.0, 1.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], normal: [0.0, 1.0, 0.0] },
            Vertex { position: [-side2,  side2, -side2], normal: [0.0, 1.0, 0.0] }
        ].iter().cloned()).expect("failed to create buffer")
    };
    
    let index_buffer1 = 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");
    
    // obj-2
    let vertex_buffer2 = {    
        CpuAccessibleBuffer::from_iter(vk.device.clone(), BufferUsage::all(),
        [ Vertex { position: [-0.5, -0.5,  0.5], normal: [ 0.0, 1.0, 0.0] },
          Vertex { position: [ 0.5, -0.5,  0.5], normal: [ 0.0, 1.0, 0.0] },
          Vertex { position: [ 0.5, -0.5, -0.5], normal: [ 0.0, 1.0, 0.0] },
          Vertex { position: [-0.5, -0.5, -0.5], normal: [ 0.0, 1.0, 0.0] },
          //
          Vertex { position: [ 0.5, -0.5,  0.5], normal: [-1.0, 0.0, 0.0] },
          Vertex { position: [ 0.5,  0.5,  0.5], normal: [-1.0, 0.0, 0.0] },
          Vertex { position: [ 0.5,  0.5, -0.5], normal: [-1.0, 0.0, 0.0] },
          Vertex { position: [ 0.5, -0.5, -0.5], normal: [-1.0, 0.0, 0.0] },
          //
          Vertex { position: [-0.5, -0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
          Vertex { position: [ 0.5, -0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
          Vertex { position: [ 0.5,  0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
          Vertex { position: [-0.5,  0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
        ].iter().cloned()).expect("failed to create buffer")
    };
    
    let index_buffer2 = vulkano::buffer::cpu_access::CpuAccessibleBuffer::from_iter(
        vk.device.clone(), vulkano::buffer::BufferUsage::all(), [
            0u16, 1, 2, 2, 3, 0,
            4, 5, 6, 6, 7, 4,
            8, 9, 10, 10, 11, 8
        ].iter().cloned()).expect("failed to create buffer");
    
    let ubo_mvp = vulkano::buffer::cpu_pool::CpuBufferPool::<vs::ty::MVP>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());
    //
    let ubo_material = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Material>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());
    //
    let ubo_light = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Light>
        ::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-200x200.png"),
            ImageFormat::PNG).unwrap().to_rgba();
        let image_data = image.into_raw().clone();

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

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

    // framebuffer(use dynamic_state viewport)
    let mut dynamic_state = DynamicState { line_width: None, viewports: None, scissors: None };
    let mut framebuffers = window_size_dependent_setup(vk.device.clone(),
        &vk.images, render_pass.clone(), &mut dynamic_state);

    // pipeliner(use dynamic_state viewport)
    let pipeline = Arc::new(GraphicsPipeline::start()
        .vertex_input_single_buffer()
        .vertex_shader(vs.main_entry_point(), ())
        .triangle_list()
        .viewports_dynamic_scissors_irrelevant(1)
        .fragment_shader(fs.main_entry_point(), ())
        .depth_stencil_simple_depth()
        .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
        .build(vk.device.clone())
        .unwrap());
    
    let mut recreate_swapchain = false;
    let mut previous_frame = Box::new(tex_future) as Box<GpuFuture>;

    let aspect_ratio = dimensions[0] as f32 / dimensions[1] as f32;

    let persp_proj:cgmath::Matrix4<f32> = cgmath::perspective(cgmath::Deg(65.0),
        aspect_ratio, 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();

    // Projection Matrix (t Matrix)
    let mut t_matrix = Matrix4::from_cols(
            Vector4::new(0.5, 0.0, 0.0, 0.0),
            Vector4::new(0.0, 0.5, 0.0, 0.0),
            Vector4::new(0.0, 0.0, 0.5, 0.0),
            Vector4::new(0.5, 0.5, 0.5, 1.0));
    // Projection View Matrix (tv Matrix)
    let tv_matrix = {
        let projector_position = Point3::new(1.0, 2.0, 2.0);
        let projector_at = Point3::new(-1.0, -1.0, 0.0);
        let projector_up = Vector3::new(0.0, 1.0, 0.0);

        Matrix4::look_at(projector_position, projector_at, projector_up)
    };
    // Projection Projection(perspective) Matrix (tp Matrix)
    let tp_matrix = cgmath::perspective(Rad(std::f32::consts::FRAC_PI_6),
               aspect_ratio, 0.2, 100.0);
    t_matrix = t_matrix * tp_matrix * tv_matrix;
    
    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) = match vk.swapchain.recreate_with_dimension(dimensions) {
                Ok(r) => r,
                Err(SwapchainCreationError::UnsupportedDimensions) => continue,
                Err(err) => panic!("{:?}", err)
            };

            vk.swapchain = new_swapchain;

            framebuffers = window_size_dependent_setup(vk.device.clone(),
               &new_images, render_pass.clone(), &mut dynamic_state);

            recreate_swapchain = false;
        }

        // arcball: world transformation -> Identity Matrix
        let view = arcball_camera.get_mat4();
        let mvp = persp_proj * view;
        
        // uniform_buffer
        // mvp obj-1 and obj-2
        let ubo_mvp_subbuffer = {
            let uniform_data = vs::ty::MVP {
                view : view.into(),
                mvp : mvp.into(),
                t_matrix: t_matrix.into(),
            };
            ubo_mvp.next(uniform_data).unwrap()
        };
        // material obj-1
        let ubo_material1_subbuffer = {
            let uniform_data = fs::ty::Material {
                kd : [0.5, 0.2, 0.1],
                shininess : 100.0,
                ks : [0.95, 0.95, 0.95],
                shininess2 : 100.0, // dummy
                ka : [0.1, 0.1, 0.1],
            };
            ubo_material.next(uniform_data).unwrap()
        };

        // material obj-2
        let ubo_material2_subbuffer = {
            let uniform_data = fs::ty::Material {
                kd : [0.4, 0.4, 0.4],
                shininess : 1.0,
                ks : [0.0, 0.0, 0.0],
                shininess2 : 1.0, // dummy
                ka : [0.1, 0.1, 0.1],
            };
            ubo_material.next(uniform_data).unwrap()
        };
        
        // light obj-1 and obj-2
        let ubo_light_subbuffer = {
            let uniform_data = fs::ty::Light {
                position : [0.0, 0.0, 0.0, 1.0],
                intensity : [1.0, 1.0, 1.0],
            };
            ubo_light.next(uniform_data).unwrap()
        };

        // uniform set
        // obj-1 set10, set11
        let set10 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_buffer(ubo_mvp_subbuffer.clone()).unwrap()
            .build().unwrap()
        );
        let set11 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
            .add_buffer(ubo_material1_subbuffer).unwrap()
            .add_buffer(ubo_light_subbuffer.clone()).unwrap()
            .build().unwrap()
        );
        // obj-2 set20, set21
        let set20 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_buffer(ubo_mvp_subbuffer).unwrap()
            .build().unwrap()
        );
        let set21 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_sampled_image(texture.clone(), sampler.clone()).unwrap()
            .add_buffer(ubo_material2_subbuffer).unwrap()
            .add_buffer(ubo_light_subbuffer.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.2, 0.2, 0.3, 1.0].into(), 1f32.into()]).unwrap()
            // draw obj-2: first draw
            .draw_indexed(pipeline.clone(), &dynamic_state,
                vertex_buffer2.clone(), index_buffer2.clone(),
                (set20.clone(), set21.clone()), ()).unwrap()
            // draw obj-1: second draw
            .draw_indexed(pipeline.clone(), &dynamic_state,
                vertex_buffer1.clone(), index_buffer1.clone(),
                (set10.clone(), set11.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();

        match future {
            Ok(future) => {
                previous_frame = Box::new(future) as Box<_>;
            }
            Err(sync::FlushError::OutOfDate) => {
                recreate_swapchain = true;
                previous_frame = Box::new(sync::now(vk.device.clone())) as Box<_>;
            }
            Err(e) => {
                println!("{:?}", e);
                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>,
    images: &[Arc<SwapchainImage<Window>>],
    render_pass: Arc<RenderPassAbstract + Send + Sync>,
    dynamic_state: &mut DynamicState
) -> Vec<Arc<FramebufferAbstract + Send + Sync>> {
    let dimensions = images[0].dimensions();
    // dynamic viewport
    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));

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

    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<_>>()
}

// obj-1 and obj-2
mod vs {
    vulkano_shaders::shader!{
        ty: "vertex",
        src: "
            #version 450

            layout(location = 0) in vec3 position;
            layout(location = 1) in vec3 normal;

            layout(location = 0) out vec4 EyePosition;
            layout(location = 1) out vec3 EyeNormal;
            layout(location = 2) out vec4 ProjTexCoord;

            layout(set = 0, binding = 0) uniform MVP {
                mat4 view;
                mat4 mvp;
                mat4 t_matrix;
            } mvp;

            void main()
            {
                vec4 pos4 = vec4(position, 1.0);

                EyeNormal = normalize((mvp.view * vec4(normal,0)).xyz);
                EyePosition = mvp.view * pos4;
                ProjTexCoord = mvp.t_matrix * pos4;
                gl_Position = mvp.mvp * vec4(position, 1.0);
            }"
    }
}

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

            layout(location = 0) in vec4 EyePosition;
            layout(location = 1) in vec3 EyeNormal;
            layout(location = 2) in vec4 ProjTexCoord;

            layout(location = 0) out vec4 f_color;

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

            layout(set = 1, binding = 1) uniform Material {
                vec3 kd;
                float shininess;
                vec3 ks;
                float shininess2; // dummy
                vec3 ka;
            } material;

            layout(set = 1, binding = 2) uniform Light {
                vec4 position;
                vec3 intensity;
            } light;

            vec3 phongModel( vec3 pos, vec3 norm ) {
                vec3 s = normalize(vec3(light.position) - pos);
                vec3 v = normalize(-pos.xyz);
                vec3 r = reflect( -s, norm );
                vec3 ambient = light.intensity * material.ka;
                float sDotN = max( dot(s,norm), 0.0 );
                vec3 diffuse = light.intensity * material.kd * sDotN;
                vec3 spec = vec3(0.0);
                if( sDotN > 0.0 )
                    spec = light.intensity * material.ks *
                    pow( max( dot(r,v), 0.0 ), material.shininess );

                return ambient + diffuse + spec;
            }

            void main()
            {
                vec3 color = phongModel(vec3(EyePosition), EyeNormal);

	            vec4 projTexColor = vec4(0.0);
                if( ProjTexCoord.z > 0.0 )
                    projTexColor = textureProj( ProjectorTex, ProjTexCoord );

                f_color = vec4(color,1.0) + projTexColor * 0.6;
            }"
    }
}

Rust Vulkano Two Objects

f:id:onagat12:20190209180312p:plain

2つのオブジェクト(Two Objects)の描画方法
オブジェクト1(obj-1):キューブ(cube)
オブジェクト2(obj-2):床と壁(floor and Walls)

1 Vertex buffer と Index buffer
Vertex buffer と Index buffer はそれぞれのオブジェクトについて準備します。
オブジェクト1: vertex_buffer1, index_buffer1
オブジェクト2: vertex_buffer2, index_buffer2

2 shader と pipeline
・shader
2つのオブジェクトで同じvertex shader と fragmant shader を使用します。
vertex shader と fragment shader は、David Wolff「OpenGL 4.0
シェーディング言語 ( OpenGL 4.0 Shading Language Cookbook )」を
参考にしています。(オブジェクトの描画には、phongモデルを使用して
います。)
・pipeline
2つのオブジェクトで同じvertex shader と fragmant shader を使うので、
pipelineは一つだけになります。

3 uniform buffer
・shader内 uniform の記述

vertex shader
layout(set = 0, binding = 0) uniform MVP {
    mat4 view;
    mat4 mvp;
} mvp;

fragment shader
layout(set = 1, binding = 0) uniform Material {
    vec3 kd;
    float shininess;
    vec3 ks;
    float shininess2; // dummy
    vec3 ka;
} material;
  注)変数を上記のような順番にし、さらにdummy変数を追加しないとビルドできません。
layout(set = 1, binding = 1) uniform Light {
    vec4 position;
    vec3 intensity;
} light;

これらに対応した uniform buffer を生成します。

let ubo_mvp = vulkano::buffer::cpu_pool::CpuBufferPool::<vs::ty::MVP>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());

let ubo_material = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Material>
    ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());

let ubo_light = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Light>
    ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());

・uniform データの設定

// uniform_buffer
        // mvp obj-1 and obj-2
        let ubo_mvp_subbuffer = {
            let uniform_data = vs::ty::MVP {
                view : view.into(),
                mvp : mvp.into(),
            };
            ubo_mvp.next(uniform_data).unwrap()
        };
        // material obj-1
        let ubo_material1_subbuffer = {
            let uniform_data = fs::ty::Material {
                kd : [0.5, 0.2, 0.1],
                shininess : 100.0,
                ks : [0.95, 0.95, 0.95],
                shininess2 : 100.0, // dummy
                ka : [0.1, 0.1, 0.1],
            };
            ubo_material.next(uniform_data).unwrap()
        };

        // material obj-2
        let ubo_material2_subbuffer = {
            let uniform_data = fs::ty::Material {
                kd : [0.4, 0.4, 0.4],
                shininess : 1.0,
                ks : [0.0, 0.0, 0.0],
                shininess2 : 1.0, // dummy
                ka : [0.1, 0.1, 0.1],
            };
            ubo_material.next(uniform_data).unwrap()
        };
        
        // light obj-1 and obj-2
        let ubo_light_subbuffer = {
            let uniform_data = fs::ty::Light {
                position : [0.0, 0.0, 0.0, 1.0],
                intensity : [1.0, 1.0, 1.0],
            };
            ubo_light.next(uniform_data).unwrap()
        };

・DescriptorSet
DescriptorSetの記述は以下のようにします。
obj-1の設定
vertex shaderの
layout(set = 0, binding = 0) uniform MVP
に対応させて、

let set10 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
   .add_buffer(ubo_mvp_subbuffer.clone()).unwrap()
   .build().unwrap()
);

と記述する。

fragment shaderの
layout(set = 1, binding = 0) uniform Material
layout(set = 1, binding = 1) uniform Light
に対応させて、

let set11 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 1)
   .add_buffer(ubo_material1_subbuffer).unwrap()
   .add_buffer(ubo_light_subbuffer.clone()).unwrap()
   .build().unwrap()
);

と記述する。
obj-2も同様に記述する。

4 2つのオブジェクトの描画
command buffer における draw コマンドは次のように記述する。

// draw obj-2: first draw
.draw_indexed(pipeline.clone(), &dynamic_state,
    vertex_buffer2.clone(), index_buffer2.clone(),
    (set20.clone(), set21.clone()), ()).unwrap()
// draw obj-1: second draw
.draw_indexed(pipeline.clone(), &dynamic_state,
    vertex_buffer1.clone(), index_buffer1.clone(),
    (set10.clone(), set11.clone()), ()).unwrap()


プログラム

#[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;
use vulkano::image::attachment::AttachmentImage;
use vulkano::pipeline::GraphicsPipeline;
use vulkano::pipeline::viewport::Viewport;
use vulkano::swapchain;
use vulkano::swapchain::{Swapchain, PresentMode, SurfaceTransform, AcquireError,
        SwapchainCreationError};
use vulkano::sync;
use vulkano::sync::now;
use vulkano::sync::GpuFuture;
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;

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

static WINDOW_NAME: &str = "Tow Objects";
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();
        // first queue
        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[1].0; // formats[1]
            //
            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() {
    let mut vk = Vulkan::init_vk();

    #[derive(Debug, Clone)]
    struct Vertex { position: [f32; 3], normal: [f32; 3] }
    impl_vertex!(Vertex, position, normal);
    
    // obj-1
    let vertex_buffer1 = {
        let side2: f32 = 0.5 / 2.0;

        CpuAccessibleBuffer::from_iter(vk.device.clone(), BufferUsage::all(), [
            // Front
            Vertex { position: [-side2, -side2,  side2], normal: [0.0, 0.0, 1.0] },
    	    Vertex { position: [ side2, -side2,  side2], normal: [0.0, 0.0, 1.0] },
    	    Vertex { position: [ side2,  side2,  side2], normal: [0.0, 0.0, 1.0] },
            Vertex { position: [-side2,  side2,  side2], normal: [0.0, 0.0, 1.0] },
    	    // Right
    	    Vertex { position: [ side2, -side2,  side2], normal: [1.0, 0.0, 0.0] },
    	    Vertex { position: [ side2, -side2, -side2], normal: [1.0, 0.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], normal: [1.0, 0.0, 0.0] },
            Vertex { position: [ side2,  side2,  side2], normal: [1.0, 0.0, 0.0] },
    	    // Back
    	    Vertex { position: [-side2, -side2, -side2], normal: [0.0, 0.0, -1.0] },
    	    Vertex { position: [-side2,  side2, -side2], normal: [0.0, 0.0, -1.0] },
    	    Vertex { position: [ side2,  side2, -side2], normal: [0.0, 0.0, -1.0] },
            Vertex { position: [ side2, -side2, -side2], normal: [0.0, 0.0, -1.0] },
    	    // Left
    	    Vertex { position: [-side2, -side2,  side2], normal: [-1.0, 0.0, 0.0] },
    	    Vertex { position: [-side2,  side2,  side2], normal: [-1.0, 0.0, 0.0] },
            Vertex { position: [-side2,  side2, -side2], normal: [-1.0, 0.0, 0.0] },
            Vertex { position: [-side2, -side2, -side2], normal: [-1.0, 0.0, 0.0] },
            // Bottom
    	    Vertex { position: [-side2, -side2,  side2], normal: [0.0, -1.0, 0.0] },
    	    Vertex { position: [-side2, -side2, -side2], normal: [0.0, -1.0, 0.0] },
    	    Vertex { position: [ side2, -side2, -side2], normal: [0.0, -1.0, 0.0] },
            Vertex { position: [ side2, -side2,  side2], normal: [0.0, -1.0, 0.0] },
    	    // Top
            Vertex { position: [-side2,  side2,  side2], normal: [0.0, 1.0, 0.0] },
    	    Vertex { position: [ side2,  side2,  side2], normal: [0.0, 1.0, 0.0] },
    	    Vertex { position: [ side2,  side2, -side2], normal: [0.0, 1.0, 0.0] },
            Vertex { position: [-side2,  side2, -side2], normal: [0.0, 1.0, 0.0] }
        ].iter().cloned()).expect("failed to create buffer")
    };
    
    let index_buffer1 = 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");
    
    // obj-2
    let vertex_buffer2 = {    
        CpuAccessibleBuffer::from_iter(vk.device.clone(), BufferUsage::all(),
        [ Vertex { position: [-0.5, -0.5,  0.5], normal: [ 0.0, 1.0, 0.0] },
          Vertex { position: [ 0.5, -0.5,  0.5], normal: [ 0.0, 1.0, 0.0] },
          Vertex { position: [ 0.5, -0.5, -0.5], normal: [ 0.0, 1.0, 0.0] },
          Vertex { position: [-0.5, -0.5, -0.5], normal: [ 0.0, 1.0, 0.0] },
          //
          Vertex { position: [ 0.5, -0.5,  0.5], normal: [-1.0, 0.0, 0.0] },
          Vertex { position: [ 0.5,  0.5,  0.5], normal: [-1.0, 0.0, 0.0] },
          Vertex { position: [ 0.5,  0.5, -0.5], normal: [-1.0, 0.0, 0.0] },
          Vertex { position: [ 0.5, -0.5, -0.5], normal: [-1.0, 0.0, 0.0] },
          //
          Vertex { position: [-0.5, -0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
          Vertex { position: [ 0.5, -0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
          Vertex { position: [ 0.5,  0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
          Vertex { position: [-0.5,  0.5, -0.5], normal: [ 0.0, 0.0, 1.0] },
        ].iter().cloned()).expect("failed to create buffer")
    };
    
    let index_buffer2 = vulkano::buffer::cpu_access::CpuAccessibleBuffer::from_iter(
        vk.device.clone(), vulkano::buffer::BufferUsage::all(), [
            0u16, 1, 2, 2, 3, 0,
            4, 5, 6, 6, 7, 4,
            8, 9, 10, 10, 11, 8
        ].iter().cloned()).expect("failed to create buffer");
    
    let ubo_mvp = vulkano::buffer::cpu_pool::CpuBufferPool::<vs::ty::MVP>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());
    //
    let ubo_material = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Material>
        ::new(vk.device.clone(), vulkano::buffer::BufferUsage::all());
    //
    let ubo_light = vulkano::buffer::cpu_pool::CpuBufferPool::<fs::ty::Light>
        ::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;
    };

    // framebuffer(use dynamic_state viewport) 
    let mut dynamic_state = DynamicState { line_width: None, viewports: None, scissors: None };
    let mut framebuffers = window_size_dependent_setup(vk.device.clone(),
        &vk.images, render_pass.clone(), &mut dynamic_state);

    // pipeliner(use dynamic_state viewport)
    let pipeline = Arc::new(GraphicsPipeline::start()
        .vertex_input_single_buffer()
        .vertex_shader(vs.main_entry_point(), ())
        .triangle_list()
        .viewports_dynamic_scissors_irrelevant(1)
        .fragment_shader(fs.main_entry_point(), ())
        .depth_stencil_simple_depth()
        .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
        .build(vk.device.clone())
        .unwrap());
    
    let mut recreate_swapchain = false;
    let mut previous_frame = Box::new(now(vk.device.clone())) 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) = match vk.swapchain.recreate_with_dimension(dimensions) {
                Ok(r) => r,
                Err(SwapchainCreationError::UnsupportedDimensions) => continue,
                Err(err) => panic!("{:?}", err)
            };

            vk.swapchain = new_swapchain;

            framebuffers = window_size_dependent_setup(vk.device.clone(),
               &new_images, render_pass.clone(), &mut dynamic_state);

            recreate_swapchain = false;
        }

        let view = arcball_camera.get_mat4();
        let mvp = persp_proj * view;
        
        // uniform_buffer
        // mvp obj-1 and obj-2
        let ubo_mvp_subbuffer = {
            let uniform_data = vs::ty::MVP {
                view : view.into(),
                mvp : mvp.into(),
            };
            ubo_mvp.next(uniform_data).unwrap()
        };
        // material obj-1
        let ubo_material1_subbuffer = {
            let uniform_data = fs::ty::Material {
                kd : [0.5, 0.2, 0.1],
                shininess : 100.0,
                ks : [0.95, 0.95, 0.95],
                shininess2 : 100.0, // dummy
                ka : [0.1, 0.1, 0.1],
            };
            ubo_material.next(uniform_data).unwrap()
        };

        // material obj-2
        let ubo_material2_subbuffer = {
            let uniform_data = fs::ty::Material {
                kd : [0.4, 0.4, 0.4],
                shininess : 1.0,
                ks : [0.0, 0.0, 0.0],
                shininess2 : 1.0, // dummy
                ka : [0.1, 0.1, 0.1],
            };
            ubo_material.next(uniform_data).unwrap()
        };
        
        // light obj-1 and obj-2
        let ubo_light_subbuffer = {
            let uniform_data = fs::ty::Light {
                position : [0.0, 0.0, 0.0, 1.0],
                intensity : [1.0, 1.0, 1.0],
            };
            ubo_light.next(uniform_data).unwrap()
        };

        // uniform set
        // obj-1 set10, set11
        let set10 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_buffer(ubo_mvp_subbuffer.clone()).unwrap()
            .build().unwrap()
        );
        let set11 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_buffer(ubo_material1_subbuffer).unwrap()
            .add_buffer(ubo_light_subbuffer.clone()).unwrap()
            .build().unwrap()
        );
        // obj-2 set20, set21
        let set20 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 0)
            .add_buffer(ubo_mvp_subbuffer).unwrap()
            .build().unwrap()
        );
        let set21 = Arc::new(PersistentDescriptorSet::start(pipeline.clone(), 1)
            .add_buffer(ubo_material2_subbuffer).unwrap()
            .add_buffer(ubo_light_subbuffer.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.2, 0.2, 0.3, 1.0].into(), 1f32.into()]).unwrap()
            // draw obj-2: first draw
            .draw_indexed(pipeline.clone(), &dynamic_state,
                vertex_buffer2.clone(), index_buffer2.clone(),
                (set20.clone(), set21.clone()), ()).unwrap()
            // draw obj-1: second draw
            .draw_indexed(pipeline.clone(), &dynamic_state,
                vertex_buffer1.clone(), index_buffer1.clone(),
                (set10.clone(), set11.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();

        match future {
            Ok(future) => {
                previous_frame = Box::new(future) as Box<_>;
            }
            Err(sync::FlushError::OutOfDate) => {
                recreate_swapchain = true;
                previous_frame = Box::new(sync::now(vk.device.clone())) as Box<_>;
            }
            Err(e) => {
                println!("{:?}", e);
                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>,
    images: &[Arc<SwapchainImage<Window>>],
    render_pass: Arc<RenderPassAbstract + Send + Sync>,
    dynamic_state: &mut DynamicState
) -> Vec<Arc<FramebufferAbstract + Send + Sync>> {
    let dimensions = images[0].dimensions();
    // dynamic viewport
    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));

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

    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<_>>()
}

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

            layout(location = 0) in vec3 position;
            layout(location = 1) in vec3 normal;

            layout(location = 0) out vec4 EyePosition;
            layout(location = 1) out vec3 EyeNormal;

            layout(set = 0, binding = 0) uniform MVP {
                mat4 view;
                mat4 mvp;
            } mvp;

            void main()
            {
                vec4 pos4 = vec4(position, 1.0);

                EyeNormal = normalize((mvp.view * vec4(normal,0)).xyz);
                EyePosition = mvp.view * pos4;
                gl_Position = mvp.mvp * vec4(position, 1.0);
            }"
    }
}
//
mod fs {
    vulkano_shaders::shader!{
        ty: "fragment",
        src: "
            #version 450

            layout(location = 0) in vec4 EyePosition;
            layout(location = 1) in vec3 EyeNormal;

            layout(location = 0) out vec4 f_color;

            layout(set = 1, binding = 0) uniform Material {
                vec3 kd;
                float shininess;
                vec3 ks;
                float shininess2; // dummy
                vec3 ka;
            } material;

            layout(set = 1, binding = 1) uniform Light {
                vec4 position;
                vec3 intensity;
            } light;

            vec3 phongModel( vec3 pos, vec3 norm ) {
                vec3 s = normalize(vec3(light.position) - pos);
                vec3 v = normalize(-pos.xyz);
                vec3 r = reflect( -s, norm );
                vec3 ambient = light.intensity * material.ka;
                float sDotN = max( dot(s,norm), 0.0 );
                vec3 diffuse = light.intensity * material.kd * sDotN;
                vec3 spec = vec3(0.0);
                if( sDotN > 0.0 )
                    spec = light.intensity * material.ks *
                    pow( max( dot(r,v), 0.0 ), material.shininess );

                return ambient + diffuse + spec;
            }

            void main()
            {
                vec3 color = phongModel(vec3(EyePosition), EyeNormal);

	            f_color = vec4(color, 1.0);
            }"
    }
}

Rust Vulkano Cubemap

f:id:onagat12:20181229021533g:plain

cubemapの描画
Vulkano issue #922 (Usage of CubeMap) を参考にしました。
1) The data for the buffer is the image data of six images appended to each other
in the order of: left, right, bottom, top, back, and front (at least that is the order
I have). The size of the cubemap is the width of one of the square images.
2) Say you load an image into a Vec of u8's, where each value is the color
component r, g, b, or a; or any other variant. Just append the next image's data
onto that Vec.

1 cubemap imageデータの生成
 まず、次のようにして各部分の imageデータ(img_posx, img_negx, ...)を
生成します。

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

生成される imageデータの型は Vec<u8> になる。
 次に、空の Vec<u8> データを準備し、これに posx, negx, posy, negy, posz,
negz の順に各imageデータを追加して、 cubemap 全体の imageデータ
(一つの Vec<u8> データ image_data)にする。
 これから cubemap texture を生成する。

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);
}


2 cubemap texture の生成
 cubemap texture の生成には、通常の texture 生成と同じ文を使用する。
ただし、from_iter()メソッドの dimensions 引数は Cubemap を設定する。
また、image_data(型は Vec<u8> )には、1で生成した cubemap imageデータを
設定する。

let (texture, tex_future) = {
    ImmutableImage::from_iter(
        image_data.iter().cloned(),
        Dimensions::Cubemap { size: 512 },
        Format::R8G8B8A8Srgb,
        vk.queue.clone()
    ).unwrap()
};

cubemap プログラム

#[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";
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);
            }"
    }
}

2019-2-7 update

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"