diff --git a/native/test.cpp b/native/test.cpp index cda45a4..76beade 100644 --- a/native/test.cpp +++ b/native/test.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "window.hpp" int main() { @@ -145,13 +146,35 @@ int main() { printf("\tvkCreateSwapchainKHR (query): res=%d image_count=%d\n", res, image_count); assert(!res); - VkImage *swapchain_images = new VkImage[image_count]; - assert(swapchain_images); - - res = vkGetSwapchainImagesKHR(device, swapchain, &image_count, swapchain_images); + std::vector swapchain_images(image_count); + res = vkGetSwapchainImagesKHR(device, swapchain, &image_count, &swapchain_images[0]); printf("\tvkCreateSwapchainKHR: res=%d\n", res); assert(!res); + std::vector 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; VkCommandPoolCreateInfo cmd_pool_info = {}; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; @@ -180,7 +203,7 @@ int main() { } - delete[] swapchain_images; + // TODO: destroy image views vkDestroySwapchainKHR(device, swapchain, NULL); printf("\tvkDestroySwapchainKHR\n"); vkFreeCommandBuffers(device, cmd_pool, 1, &cmd_buffer); diff --git a/src/conv.rs b/src/conv.rs index f94584e..8089b6e 100644 --- a/src/conv.rs +++ b/src/conv.rs @@ -1,7 +1,5 @@ -use {VkExtent2D, VkFormat}; - -use hal::format; -use hal::window; +use super::*; +use hal::{format, image, window}; pub fn format_from_hal(format: format::Format) -> VkFormat { use VkFormat::*; @@ -62,3 +60,55 @@ pub fn extent2d_from_hal(extent: window::Extent2d) -> VkExtent2D { 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 +} diff --git a/src/lib.rs b/src/lib.rs index 9e9bfef..21c69c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -519,12 +519,9 @@ pub struct VkBufferView_T { _unused: [u8; 0], } pub type VkBufferView = *mut VkBufferView_T; -#[repr(C)] -#[derive(Debug, Copy, Clone)] -pub struct VkImageView_T { - _unused: [u8; 0], -} -pub type VkImageView = *mut VkImageView_T; + +pub type VkImageView = Handle<::ImageView>; + #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct VkShaderModule_T { @@ -4675,11 +4672,35 @@ extern "C" { *const VkImageSubresource, pLayout: *mut VkSubresourceLayout); } -extern "C" { - pub fn vkCreateImageView(device: VkDevice, - pCreateInfo: *const VkImageViewCreateInfo, - pAllocator: *const VkAllocationCallbacks, - pView: *mut VkImageView) -> VkResult; +#[no_mangle] +pub extern fn vkCreateImageView( + gpu: VkDevice, + pCreateInfo: *const VkImageViewCreateInfo, + pAllocator: *const VkAllocationCallbacks, + 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,