From 740cec2c9b89365ca9fa21625db6ad44540e635d Mon Sep 17 00:00:00 2001 From: msiglreith Date: Sat, 9 Dec 2017 15:08:46 +0100 Subject: [PATCH] Implement vkGetPhysicalDeviceFormatProperties and parts of 06 - depth stencil sample --- README.md | 2 +- libportability-gfx/src/conv.rs | 64 ++++++++++++++++++++++++++++++++- libportability-gfx/src/impls.rs | 18 +++++----- libportability/src/lib.rs | 8 +++++ native/test.cpp | 42 ++++++++++++++++++++++ 5 files changed, 124 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cf6ecf4..c1e958f 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ make Build the Rust library (portability implementation): ``` -cargo build +cargo build -p portability ``` Build the native example: diff --git a/libportability-gfx/src/conv.rs b/libportability-gfx/src/conv.rs index 8089b6e..6e1e367 100644 --- a/libportability-gfx/src/conv.rs +++ b/libportability-gfx/src/conv.rs @@ -39,13 +39,75 @@ pub fn format_from_hal(format: format::Format) -> VkFormat { } } -pub fn hal_from_format(format: VkFormat) -> format::Format { +pub fn format_properties_from_hal(properties: format::Properties) -> VkFormatProperties { + VkFormatProperties { + linearTilingFeatures: image_features_from_hal(properties.linear_tiling), + optimalTilingFeatures: image_features_from_hal(properties.optimal_tiling), + bufferFeatures: buffer_features_from_hal(properties.buffer_features), + } +} + +fn image_features_from_hal(features: format::ImageFeature) -> VkFormatFeatureFlags { + let mut flags = 0; + + if features.contains(format::ImageFeature::SAMPLED) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT as _; + } + if features.contains(format::ImageFeature::STORAGE) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT as _; + } + if features.contains(format::ImageFeature::STORAGE_ATOMIC) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT as _; + } + if features.contains(format::ImageFeature::COLOR_ATTACHMENT) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT as _; + } + if features.contains(format::ImageFeature::COLOR_ATTACHMENT_BLEND) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT as _; + } + if features.contains(format::ImageFeature::DEPTH_STENCIL_ATTACHMENT) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT as _; + } + if features.contains(format::ImageFeature::BLIT_SRC) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_BLIT_SRC_BIT as _; + } + if features.contains(format::ImageFeature::BLIT_DST) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_BLIT_DST_BIT as _; + } + if features.contains(format::ImageFeature::SAMPLED_LINEAR) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT as _; + } + + flags +} + +fn buffer_features_from_hal(features: format::BufferFeature) -> VkFormatFeatureFlags { + let mut flags = 0; + + if features.contains(format::BufferFeature::UNIFORM_TEXEL) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT as _; + } + if features.contains(format::BufferFeature::STORAGE_TEXEL) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT as _; + } + if features.contains(format::BufferFeature::STORAGE_TEXEL_ATOMIC) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT as _; + } + if features.contains(format::BufferFeature::VERTEX) { + flags |= VkFormatFeatureFlagBits::VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT as _; + } + + flags +} + +pub fn map_format(format: VkFormat) -> format::Format { use VkFormat::*; use hal::format::ChannelType::*; use hal::format::SurfaceType::*; let (sf, cf) = match format { VK_FORMAT_B8G8R8A8_UNORM => (B8_G8_R8_A8, Unorm), + VK_FORMAT_D16_UNORM => (D16, Unorm), _ => { panic!("format {:?}", format); } diff --git a/libportability-gfx/src/impls.rs b/libportability-gfx/src/impls.rs index 25f4e76..5e2d796 100644 --- a/libportability-gfx/src/impls.rs +++ b/libportability-gfx/src/impls.rs @@ -73,12 +73,14 @@ extern "C" { pFeatures: *mut VkPhysicalDeviceFeatures); } -extern "C" { - pub fn vkGetPhysicalDeviceFormatProperties(physicalDevice: - VkPhysicalDevice, - format: VkFormat, - pFormatProperties: - *mut VkFormatProperties); +#[inline] +pub extern fn gfxGetPhysicalDeviceFormatProperties( + adapter: VkPhysicalDevice, + format: VkFormat, + pFormatProperties: *mut VkFormatProperties, +) { + let properties = adapter.physical_device.format_properties(conv::map_format(format)); + unsafe { *pFormatProperties = conv::format_properties_from_hal(properties); } } extern "C" { pub fn vkGetPhysicalDeviceImageFormatProperties(physicalDevice: @@ -439,7 +441,7 @@ pub extern fn gfxCreateImageView( .device .create_image_view( &info.image, - conv::hal_from_format(info.format), + conv::map_format(info.format), conv::map_swizzle(info.components), conv::map_subresource_range(info.subresourceRange), ); @@ -1087,7 +1089,7 @@ pub extern fn gfxCreateSwapchainKHR( assert_eq!(info.imageSharingMode, VkSharingMode::VK_SHARING_MODE_EXCLUSIVE); // TODO let config = hal::SwapchainConfig { - color_format: conv::hal_from_format(info.imageFormat), + color_format: conv::map_format(info.imageFormat), depth_stencil_format: None, image_count: info.minImageCount, }; diff --git a/libportability/src/lib.rs b/libportability/src/lib.rs index f36b232..1b9f7e2 100644 --- a/libportability/src/lib.rs +++ b/libportability/src/lib.rs @@ -74,6 +74,14 @@ pub extern fn vkDestroyImageView( ) { gfxDestroyImageView(device, imageView, pAllocator) } +#[no_mangle] +pub extern fn vkGetPhysicalDeviceFormatProperties( + adapter: VkPhysicalDevice, + format: VkFormat, + pFormatProperties: *mut VkFormatProperties, +) { + gfxGetPhysicalDeviceFormatProperties(adapter, format, pFormatProperties) +} #[no_mangle] pub extern fn vkCreateCommandPool( diff --git a/native/test.cpp b/native/test.cpp index 60d6e77..1389e52 100644 --- a/native/test.cpp +++ b/native/test.cpp @@ -1,5 +1,25 @@ /// Sample code adopted from https://github.com/LunarG/VulkanSamples +/* + * Vulkan Samples + * + * Copyright (C) 2015-2016 Valve Corporation + * Copyright (C) 2015-2016 LunarG, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + #if defined(_WIN32) #define VK_USE_PLATFORM_WIN32_KHR #endif @@ -175,6 +195,28 @@ int main() { assert(!res); } + VkImageCreateInfo image_info = {}; + const VkFormat depth_format = VK_FORMAT_D16_UNORM; + VkFormatProperties props; + vkGetPhysicalDeviceFormatProperties(physical_devices[0], depth_format, &props); + printf("\tvkGetPhysicalDeviceFormatProperties\n"); + printf( + "\t\tlinear_tiling_features: %x\n" + "\t\toptimal_tiling_features: %x\n" + "\t\tbuffer_features: %x\n", + props.linearTilingFeatures, + props.optimalTilingFeatures, + props.bufferFeatures); + + if (props.linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) { + image_info.tiling = VK_IMAGE_TILING_LINEAR; + } else if (props.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) { + image_info.tiling = VK_IMAGE_TILING_OPTIMAL; + } else { + printf("VK_FORMAT_D16_UNORM unsupported.\n"); + return -1; + } + VkCommandPool cmd_pool = 0; VkCommandPoolCreateInfo cmd_pool_info = {}; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;