Skip to content

Commit

Permalink
Update gfx with swapchain readback support
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Oct 29, 2020
1 parent 7c42b54 commit 2fcc563
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 113 deletions.
148 changes: 79 additions & 69 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions Makefile
@@ -1,11 +1,11 @@
CTS_DIR=../VK-GL-CTS
CHERRY_DIR=../cherry
BINDING=target/vulkan.rs
LIB_FILE_NAME=
LIB_VULKAN_NAME=
NATIVE_DIR=target/native
NATIVE_TARGET=$(NATIVE_DIR)/test
NATIVE_OBJECTS=$(NATIVE_DIR)/test.o $(NATIVE_DIR)/window.o
LIB_FILE_NAME=
LIB_VULKAN_NAME=
TEST_LIST=$(CURDIR)/conformance/deqp.txt
TEST_LIST_SOURCE=$(CTS_DIR)/external/vulkancts/mustpass/1.0.2/vk-default.txt
DEQP_DIR=$(CTS_DIR)/build/external/vulkancts/modules/vulkan/
Expand All @@ -18,7 +18,7 @@ ZIP_COMMAND=
DOTA_DIR=../dota2/bin/osx64
DOTA_EXE=$(DOTA_DIR)/dota2.app/Contents/MacOS/dota2
#possible command lines are : -vulkan_disable_occlusion_queries -vulkan_scene_system_job_cost 2 +vulkan_batch_submits 1 +vulkan_batch_size 500
DOTA_PARAMS:=-vulkan_disable_occlusion_queries
DOTA_PARAMS:=-vulkan -vulkan_disable_occlusion_queries -vulkan_descriptor_sets_per_pool 256
DOTA_DEMO_PHORONIX= "$(CURDIR)/../dota2/demos/dota2-pts-1971360796.dem"
DOTA_BENCHMARK=+timedemoquit +timedemo $(DOTA_DEMO_PHORONIX) +timedemo_start 40000 +timedemo_end 50000 +fps_max 0 -novconsole -high -autoconfig_level 3
DOTA_BENCH_RESULTS=../dota2/dota/Source2Bench.csv
Expand Down Expand Up @@ -135,15 +135,15 @@ $(BINDING): headers/vulkan/*.h
bindgen --no-layout-tests --rustfmt-bindings headers/vulkan/vulkan.h -o $(BINDING)

$(LIBRARY): dummy
cargo build --manifest-path libportability/Cargo.toml --features $(BACKEND)
cargo build --manifest-path libportability-icd/Cargo.toml --features $(BACKEND),portability-gfx/env_logger
mkdir -p target/native
cargo build --manifest-path libportability/Cargo.toml --features $(BACKEND),debug
cargo build --manifest-path libportability-icd/Cargo.toml --features $(BACKEND),debug

$(LIBRARY_FAST): dummy
cargo build --release --manifest-path libportability/Cargo.toml --features $(BACKEND)
cargo build --release --manifest-path libportability-icd/Cargo.toml --features $(BACKEND)

$(NATIVE_DIR)/%.o: native/%.cpp $(DEPS) Makefile
-mkdir $(NATIVE_DIR)
$(CC) -c -o $@ $< $(CFLAGS)

$(NATIVE_TARGET): $(LIBRARY) $(NATIVE_OBJECTS) Makefile
Expand Down
1 change: 0 additions & 1 deletion libportability-gfx/Cargo.toml
Expand Up @@ -38,7 +38,6 @@ git = "https://github.com/gfx-rs/gfx"
#path = "../../gfx/src/backend/empty"

[target.'cfg(not(any(target_os = "macos", target_os = "ios")))'.dependencies.gfx-backend-vulkan]
features = ["x11", "xcb"]
git = "https://github.com/gfx-rs/gfx"
#path = "../../gfx/src/backend/vulkan"
optional = true
Expand Down
12 changes: 11 additions & 1 deletion libportability-gfx/src/conv.rs
Expand Up @@ -422,7 +422,7 @@ pub fn map_image_usage(usage: VkImageUsageFlags) -> image::Usage {
image::Usage::from_bits_truncate(usage)
}

pub fn _map_image_usage_from_hal(usage: image::Usage) -> VkImageUsageFlags {
pub fn map_image_usage_from_hal(usage: image::Usage) -> VkImageUsageFlags {
usage.bits()
}

Expand Down Expand Up @@ -568,6 +568,16 @@ pub fn memory_properties_from_hal(properties: memory::Properties) -> VkMemoryPro
flags
}

pub fn memory_heap_flags_from_hal(hal_flags: memory::HeapFlags) -> VkMemoryHeapFlags {
let mut flags = 0;

if hal_flags.contains(memory::HeapFlags::DEVICE_LOCAL) {
flags |= VkMemoryHeapFlagBits::VK_MEMORY_HEAP_DEVICE_LOCAL_BIT as u32;
}

flags
}

pub fn map_descriptor_type(ty: VkDescriptorType) -> pso::DescriptorType {
use super::VkDescriptorType::*;

Expand Down
38 changes: 17 additions & 21 deletions libportability-gfx/src/impls.rs
Expand Up @@ -498,19 +498,20 @@ pub unsafe extern "C" fn gfxGetPhysicalDeviceMemoryProperties(
let num_types = properties.memory_types.len();
memory_properties.memoryTypeCount = num_types as _;
for i in 0..num_types {
let flags = conv::memory_properties_from_hal(properties.memory_types[i].properties);
let ty = &properties.memory_types[i];
memory_properties.memoryTypes[i] = VkMemoryType {
propertyFlags: flags, // propertyFlags
heapIndex: properties.memory_types[i].heap_index as _,
propertyFlags: conv::memory_properties_from_hal(ty.properties),
heapIndex: ty.heap_index as _,
};
}

let num_heaps = properties.memory_heaps.len();
memory_properties.memoryHeapCount = num_heaps as _;
for i in 0..num_heaps {
let heap = &properties.memory_heaps[i];
memory_properties.memoryHeaps[i] = VkMemoryHeap {
size: properties.memory_heaps[i],
flags: 0, // TODO
size: heap.size,
flags: conv::memory_heap_flags_from_hal(heap.flags),
};
}
}
Expand Down Expand Up @@ -1826,7 +1827,7 @@ pub unsafe extern "C" fn gfxCreateImage(
kind,
info.mipLevels as _,
conv::map_format(info.format)
.expect(&format!("Unsupported image format: {:?}", info.format)),
.unwrap_or_else(|| panic!("Unsupported image format: {:?}", info.format)),
conv::map_tiling(info.tiling),
conv::map_image_usage(info.usage),
conv::map_image_create_flags(info.flags),
Expand Down Expand Up @@ -4274,8 +4275,7 @@ pub unsafe extern "C" fn gfxGetPhysicalDeviceSurfaceCapabilitiesKHR(
as _,
currentTransform: VkSurfaceTransformFlagBitsKHR::VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,
supportedCompositeAlpha: caps.composite_alpha_modes.bits(),
// Ignoring `caps.usage` since we only work with the new swapchain model here.
supportedUsageFlags: VkImageUsageFlagBits::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT as _,
supportedUsageFlags: conv::map_image_usage_from_hal(caps.usage),
};

*pSurfaceCapabilities = output;
Expand Down Expand Up @@ -4428,16 +4428,14 @@ pub unsafe extern "C" fn gfxCreateSwapchainKHR(
) -> VkResult {
let info = &*pCreateInfo;
// TODO: more checks
assert_eq!(info.clipped, VK_TRUE); // TODO
if info.clipped == 0 {
warn!("Non-clipped swapchain requested");
}
assert_eq!(
info.imageSharingMode,
VkSharingMode::VK_SHARING_MODE_EXCLUSIVE
); // TODO

if info.imageUsage != VkImageUsageFlagBits::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT as _ {
warn!("Unsupported swapchain usage: {:?}", info.imageUsage);
}

let config = hal::window::SwapchainConfig {
present_mode: conv::map_present_mode(info.presentMode),
composite_alpha_mode: conv::map_composite_alpha(info.compositeAlpha),
Expand All @@ -4455,12 +4453,13 @@ pub unsafe extern "C" fn gfxCreateSwapchainKHR(
.configure_swapchain(&gpu.device, config)
{
Ok(()) => {
let count = info.minImageCount;
let swapchain = Swapchain {
gpu,
surface: info.surface,
count: info.minImageCount,
count,
current_index: 0,
active: Vec::new(),
active: (0 .. count).map(|_| None).collect(),
lazy_framebuffers: Mutex::new(Vec::with_capacity(1)),
};
*pSwapchain = Handle::new(swapchain);
Expand Down Expand Up @@ -4746,7 +4745,7 @@ pub unsafe extern "C" fn gfxAcquireNextImageKHR(
match swapchain.surface.acquire_image(timeout) {
Ok((frame, suboptimal)) => {
let index = (swapchain.current_index + 1) % swapchain.count;
swapchain.active.push((index, frame));
swapchain.active[index as usize] = Some(frame);
*pImageIndex = index;
swapchain.current_index = index;
match suboptimal {
Expand Down Expand Up @@ -4781,12 +4780,9 @@ pub unsafe extern "C" fn gfxQueuePresentKHR(

for (swapchain, &index) in swapchain_slice.iter().zip(index_slice) {
let sc = swapchain.as_mut().unwrap();
let active_pos = sc
.active
.iter()
.position(|&(frame, _)| frame == index)
let frame = sc.active[index as usize]
.take()
.expect("Frame was not acquired properly!");
let (_, frame) = sc.active.swap_remove(active_pos);
let sem = wait_semaphores.first().map(|s| &s.raw);
if let Err(_) = queue.present(&mut *sc.surface, frame, sem) {
return VkResult::VK_ERROR_SURFACE_LOST_KHR;
Expand Down
18 changes: 8 additions & 10 deletions libportability-gfx/src/lib.rs
Expand Up @@ -124,11 +124,14 @@ pub enum Image<B: hal::Backend> {
#[derive(Debug)]
struct UnexpectedSwapchainImage;

impl<B: hal::Backend> Image<B> {
fn as_native(&self) -> Result<&B::Image, UnexpectedSwapchainImage> {
impl Image<B> {
fn as_native(&self) -> Result<&<B as hal::Backend>::Image, UnexpectedSwapchainImage> {
//use std::borrow::Borrow;
match *self {
Image::Native { ref raw } => Ok(raw),
Image::SwapchainFrame { .. } => Err(UnexpectedSwapchainImage),
//Image::SwapchainFrame { ref swapchain, frame } =>
// Ok(swapchain.active[frame as usize].as_ref().unwrap().borrow()),
}
}
}
Expand Down Expand Up @@ -195,11 +198,9 @@ impl Framebuffer {
} => {
use std::borrow::Borrow;
swapchain
.active
.iter()
.find(|&&(index, _)| index == frame)
.active[frame as usize]
.as_ref()
.expect("Swapchain frame isn't acquired")
.1
.borrow()
}
});
Expand Down Expand Up @@ -249,10 +250,7 @@ pub struct Swapchain<B: hal::Backend> {
surface: VkSurfaceKHR,
count: hal::window::SwapImageIndex,
current_index: hal::window::SwapImageIndex,
active: Vec<(
hal::window::SwapImageIndex,
<B::Surface as hal::window::PresentationSurface<B>>::SwapchainImage,
)>,
active: Vec<Option<<B::Surface as hal::window::PresentationSurface<B>>::SwapchainImage>>,
lazy_framebuffers: parking_lot::Mutex<Vec<<B as hal::Backend>::Framebuffer>>,
}

Expand Down
5 changes: 2 additions & 3 deletions libportability-icd/src/lib.rs
Expand Up @@ -19,9 +19,8 @@ pub unsafe extern "C" fn vk_icdGetInstanceProcAddr(
pub unsafe extern "C" fn vk_icdNegotiateLoaderICDInterfaceVersion(
pSupportedVersion: *mut ::std::os::raw::c_uint,
) -> VkResult {
let supported_version = &mut *pSupportedVersion;
if *supported_version > ICD_VERSION {
*supported_version = ICD_VERSION;
if *pSupportedVersion > ICD_VERSION {
*pSupportedVersion = ICD_VERSION;
}

VkResult::VK_SUCCESS
Expand Down
4 changes: 2 additions & 2 deletions native/test.cpp
Expand Up @@ -381,8 +381,8 @@ int main() {
vkGetImageMemoryRequirements(device, depth_image, &mem_reqs);
printf("\tvkGetImageMemoryRequirements\n");
printf(
"\t\tsize: %llx\n"
"\t\talignment: %llx\n"
"\t\tsize: %zx\n"
"\t\talignment: %zx\n"
"\t\tmemoryTypeBits: %x\n",
mem_reqs.size,
mem_reqs.alignment,
Expand Down

0 comments on commit 2fcc563

Please sign in to comment.