Merge pull request #7 from msiglreith/image_view

Image view creation/destruction
This commit is contained in:
Dzmitry Malyshau 2017-12-07 19:40:26 -05:00 committed by GitHub
commit 1797c73f8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 124 additions and 23 deletions

View file

@ -7,6 +7,7 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <vector>
#include "window.hpp" #include "window.hpp"
int main() { int main() {
@ -145,13 +146,35 @@ int main() {
printf("\tvkCreateSwapchainKHR (query): res=%d image_count=%d\n", res, image_count); printf("\tvkCreateSwapchainKHR (query): res=%d image_count=%d\n", res, image_count);
assert(!res); assert(!res);
VkImage *swapchain_images = new VkImage[image_count]; std::vector<VkImage> swapchain_images(image_count);
assert(swapchain_images); res = vkGetSwapchainImagesKHR(device, swapchain, &image_count, &swapchain_images[0]);
res = vkGetSwapchainImagesKHR(device, swapchain, &image_count, swapchain_images);
printf("\tvkCreateSwapchainKHR: res=%d\n", res); printf("\tvkCreateSwapchainKHR: res=%d\n", res);
assert(!res); assert(!res);
std::vector<VkImageView> swapchain_views(image_count);
for(auto i = 0; i < image_count; i++) {
VkImageViewCreateInfo color_image_view = {};
color_image_view.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
color_image_view.pNext = NULL;
color_image_view.flags = 0;
color_image_view.image = swapchain_images[i];
color_image_view.viewType = VK_IMAGE_VIEW_TYPE_2D;
color_image_view.format = swapchain_ci.imageFormat;
color_image_view.components.r = VK_COMPONENT_SWIZZLE_R;
color_image_view.components.g = VK_COMPONENT_SWIZZLE_G;
color_image_view.components.b = VK_COMPONENT_SWIZZLE_B;
color_image_view.components.a = VK_COMPONENT_SWIZZLE_A;
color_image_view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
color_image_view.subresourceRange.baseMipLevel = 0;
color_image_view.subresourceRange.levelCount = 1;
color_image_view.subresourceRange.baseArrayLayer = 0;
color_image_view.subresourceRange.layerCount = 1;
res = vkCreateImageView(device, &color_image_view, NULL, &swapchain_views[i]);
printf("\tvkCreateImageView: res=%d\n", res);
assert(!res);
}
VkCommandPool cmd_pool = 0; VkCommandPool cmd_pool = 0;
VkCommandPoolCreateInfo cmd_pool_info = {}; VkCommandPoolCreateInfo cmd_pool_info = {};
cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
@ -180,7 +203,10 @@ int main() {
} }
delete[] swapchain_images; for(auto view : swapchain_views) {
vkDestroyImageView(device, view, NULL);
printf("\tvkDestroyImageView\n");
}
vkDestroySwapchainKHR(device, swapchain, NULL); vkDestroySwapchainKHR(device, swapchain, NULL);
printf("\tvkDestroySwapchainKHR\n"); printf("\tvkDestroySwapchainKHR\n");
vkFreeCommandBuffers(device, cmd_pool, 1, &cmd_buffer); vkFreeCommandBuffers(device, cmd_pool, 1, &cmd_buffer);

View file

@ -1,7 +1,5 @@
use {VkExtent2D, VkFormat}; use super::*;
use hal::{format, image, window};
use hal::format;
use hal::window;
pub fn format_from_hal(format: format::Format) -> VkFormat { pub fn format_from_hal(format: format::Format) -> VkFormat {
use VkFormat::*; use VkFormat::*;
@ -62,3 +60,55 @@ pub fn extent2d_from_hal(extent: window::Extent2d) -> VkExtent2D {
height: extent.height, height: extent.height,
} }
} }
pub fn map_swizzle(components: VkComponentMapping) -> format::Swizzle {
format::Swizzle(
map_swizzle_component(components.r, format::Component::R),
map_swizzle_component(components.g, format::Component::G),
map_swizzle_component(components.b, format::Component::B),
map_swizzle_component(components.a, format::Component::A),
)
}
fn map_swizzle_component(
component: VkComponentSwizzle,
identity: format::Component,
) -> format::Component {
use VkComponentSwizzle::*;
match component {
VK_COMPONENT_SWIZZLE_IDENTITY => identity,
VK_COMPONENT_SWIZZLE_ZERO => format::Component::Zero,
VK_COMPONENT_SWIZZLE_ONE => format::Component::One,
VK_COMPONENT_SWIZZLE_R => format::Component::R,
VK_COMPONENT_SWIZZLE_G => format::Component::G,
VK_COMPONENT_SWIZZLE_B => format::Component::B,
VK_COMPONENT_SWIZZLE_A => format::Component::A,
_ => panic!("Unsupported swizzle component: {:?}", component),
}
}
pub fn map_subresource_range(subresource: VkImageSubresourceRange) -> image::SubresourceRange {
image::SubresourceRange {
aspects: map_aspect(subresource.aspectMask),
levels: subresource.baseMipLevel as _ .. (subresource.baseMipLevel+subresource.levelCount) as _,
layers: subresource.baseArrayLayer as _ .. (subresource.baseArrayLayer+subresource.layerCount) as _,
}
}
fn map_aspect(aspects: VkImageAspectFlags) -> image::AspectFlags {
let mut flags = image::AspectFlags::empty();
if aspects & VkImageAspectFlagBits::VK_IMAGE_ASPECT_COLOR_BIT as u32 != 0 {
flags |= image::AspectFlags::COLOR;
}
if aspects & VkImageAspectFlagBits::VK_IMAGE_ASPECT_DEPTH_BIT as u32 != 0 {
flags |= image::AspectFlags::DEPTH;
}
if aspects & VkImageAspectFlagBits::VK_IMAGE_ASPECT_STENCIL_BIT as u32 != 0 {
flags |= image::AspectFlags::DEPTH;
}
if aspects & VkImageAspectFlagBits::VK_IMAGE_ASPECT_METADATA_BIT as u32 != 0 {
unimplemented!()
}
flags
}

View file

@ -519,12 +519,9 @@ pub struct VkBufferView_T {
_unused: [u8; 0], _unused: [u8; 0],
} }
pub type VkBufferView = *mut VkBufferView_T; pub type VkBufferView = *mut VkBufferView_T;
#[repr(C)]
#[derive(Debug, Copy, Clone)] pub type VkImageView = Handle<<B as hal::Backend>::ImageView>;
pub struct VkImageView_T {
_unused: [u8; 0],
}
pub type VkImageView = *mut VkImageView_T;
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct VkShaderModule_T { pub struct VkShaderModule_T {
@ -4675,15 +4672,43 @@ extern "C" {
*const VkImageSubresource, *const VkImageSubresource,
pLayout: *mut VkSubresourceLayout); pLayout: *mut VkSubresourceLayout);
} }
extern "C" { #[no_mangle]
pub fn vkCreateImageView(device: VkDevice, pub extern fn vkCreateImageView(
gpu: VkDevice,
pCreateInfo: *const VkImageViewCreateInfo, pCreateInfo: *const VkImageViewCreateInfo,
pAllocator: *const VkAllocationCallbacks, pAllocator: *const VkAllocationCallbacks,
pView: *mut VkImageView) -> VkResult; pView: *mut VkImageView,
) -> VkResult {
let info = unsafe { &*pCreateInfo };
assert!(info.subresourceRange.levelCount != VK_REMAINING_MIP_LEVELS as _); // TODO
assert!(info.subresourceRange.layerCount != VK_REMAINING_ARRAY_LAYERS as _); // TODO
let view = gpu
.device
.create_image_view(
&info.image,
conv::hal_from_format(info.format),
conv::map_swizzle(info.components),
conv::map_subresource_range(info.subresourceRange),
);
match view {
Ok(view) => {
unsafe { *pView = Handle::new(view) };
VkResult::VK_SUCCESS
},
Err(err) => {
panic!("Unexpected image view creation error: {:?}", err)
},
} }
extern "C" { }
pub fn vkDestroyImageView(device: VkDevice, imageView: VkImageView, #[no_mangle]
pAllocator: *const VkAllocationCallbacks); pub extern fn vkDestroyImageView(
gpu: VkDevice,
imageView: VkImageView,
pAllocator: *const VkAllocationCallbacks,
) {
gpu.device.destroy_image_view(*imageView.unwrap())
} }
extern "C" { extern "C" {
pub fn vkCreateShaderModule(device: VkDevice, pub fn vkCreateShaderModule(device: VkDevice,