Link textures to swap chains

This commit is contained in:
Dzmitry Malyshau 2019-01-09 09:14:40 -05:00
parent 1859141a99
commit 630e86f120
3 changed files with 95 additions and 45 deletions

View File

@ -14,11 +14,10 @@ use hal::command::RawCommandBuffer;
use hal::queue::RawCommandQueue;
use hal::{self, Device as _Device, Surface as _Surface};
//use rendy_memory::{allocator, Config, Heaps};
use parking_lot::{Mutex, RwLock};
use std::collections::hash_map::{Entry, HashMap};
use std::{ffi, slice};
use parking_lot::Mutex;
use std::collections::hash_map::{Entry, HashMap};
use std::sync::atomic::Ordering;
#[derive(Hash, PartialEq)]
@ -257,24 +256,26 @@ pub extern "C" fn wgpu_device_create_texture(
let life_guard = LifeGuard::new();
let ref_count = life_guard.ref_count.clone();
let id = HUB.textures.write().register(resource::Texture {
raw: bound_image,
device_id: Stored {
value: device_id,
ref_count: device.life_guard.ref_count.clone(),
},
kind,
format: desc.format,
full_range,
life_guard,
});
let query = device.texture_tracker.lock().query(
&Stored {
value: id,
ref_count,
},
TextureUsageFlags::WRITE_ALL,
);
let id = HUB.textures
.write()
.register(resource::Texture {
raw: bound_image,
device_id: Stored {
value: device_id,
ref_count: device.life_guard.ref_count.clone(),
},
kind,
format: desc.format,
full_range,
swap_chain_link: RwLock::new(None),
life_guard,
});
let query = device.texture_tracker
.lock()
.query(
&Stored { value: id, ref_count },
TextureUsageFlags::WRITE_ALL,
);
assert!(query.initialized);
id
@ -891,6 +892,7 @@ pub extern "C" fn wgpu_device_create_swap_chain(
levels: 0 .. 1,
layers: 0 .. 1,
},
swap_chain_link: RwLock::new(None),
life_guard: LifeGuard::new(),
};
swap_chain::Frame {
@ -898,6 +900,9 @@ pub extern "C" fn wgpu_device_create_swap_chain(
ref_count: texture.life_guard.ref_count.clone(),
value: texture_guard.register(texture),
},
fence: device.raw.create_fence(true).unwrap(),
sem_available: device.raw.create_semaphore().unwrap(),
sem_present: device.raw.create_semaphore().unwrap(),
}
})
.collect(),
@ -908,7 +913,12 @@ pub extern "C" fn wgpu_device_create_swap_chain(
.write()
.register(swap_chain::SwapChain {
raw,
device_id: Stored {
value: device_id,
ref_count: device.life_guard.ref_count.clone(),
},
frames,
next_frame_index: 0,
sem_available: device.raw.create_semaphore().unwrap(),
epoch: 1,
})
}

View File

@ -1,6 +1,14 @@
use crate::{DeviceId, Extent3d, LifeGuard, Stored, TextureId};
use bitflags::bitflags;
use parking_lot::RwLock;
use hal;
use crate::{
Extent3d, LifeGuard, Stored,
DeviceId, TextureId,
};
use crate::swap_chain::SwapChainLink;
bitflags! {
#[repr(transparent)]
@ -78,6 +86,7 @@ pub(crate) struct Texture<B: hal::Backend> {
pub kind: hal::image::Kind,
pub format: TextureFormat,
pub full_range: hal::image::SubresourceRange,
pub swap_chain_link: RwLock<Option<SwapChainLink>>,
pub life_guard: LifeGuard,
}

View File

@ -1,24 +1,40 @@
use hal;
use crate::{Stored, WeaklyStored,
DeviceId, SwapChainId, TextureId,
};
use crate::registry::{HUB, Items};
use crate::resource;
use crate::{Stored,
SwapChainId, TextureId,
};
use hal;
use hal::{Device as _Device, Swapchain as _Swapchain};
use std::mem;
pub(crate) struct Surface<B: hal::Backend> {
pub raw: B::Surface,
pub type Epoch = u16;
pub(crate) struct SwapChainLink {
swap_chain_id: WeaklyStored<SwapChainId>, //TODO: strongly
epoch: Epoch,
image_index: hal::SwapImageIndex,
}
pub(crate) struct Frame {
pub texture: Stored<TextureId>,
pub(crate) struct Surface<B: hal::Backend> {
pub raw: B::Surface,
}
pub(crate) struct Frame<B: hal::Backend> {
pub texture: Stored<TextureId>,
pub fence: B::Fence,
pub sem_available: B::Semaphore,
pub sem_present: B::Semaphore,
}
pub(crate) struct SwapChain<B: hal::Backend> {
pub raw: B::Swapchain,
pub frames: Vec<Frame>,
pub next_frame_index: usize,
pub raw: B::Swapchain,
pub device_id: Stored<DeviceId>,
pub frames: Vec<Frame<B>>,
pub sem_available: B::Semaphore,
pub epoch: Epoch,
}
#[repr(C)]
@ -33,15 +49,30 @@ pub struct SwapChainDescriptor {
pub extern "C" fn wgpu_swap_chain_get_next_texture(
swap_chain_id: SwapChainId,
) -> TextureId {
let mut swap_chain_guard = HUB.swap_chains.write();
let swap_chain = swap_chain_guard.get_mut(swap_chain_id);
let mut swap_chain_guard = HUB.swap_chains.write();
let swap_chain = swap_chain_guard.get_mut(swap_chain_id);
let device_guard = HUB.devices.read();
let device = device_guard.get(swap_chain.device_id.value);
let frame = &swap_chain.frames[swap_chain.next_frame_index];
swap_chain.next_frame_index += 1;
if swap_chain.next_frame_index == swap_chain.frames.len() {
swap_chain.next_frame_index = 0;
}
let image_index = unsafe {
let sync = hal::FrameSync::Semaphore(&swap_chain.sem_available);
swap_chain.raw.acquire_image(!0, sync).unwrap()
};
//TODO: actual synchronization
frame.texture.value
let frame = &mut swap_chain.frames[image_index as usize];
unsafe {
device.raw.wait_for_fence(&frame.fence, !0).unwrap();
}
mem::swap(&mut frame.sem_available, &mut swap_chain.sem_available);
let texture_guard = HUB.textures.read();
let texture = texture_guard.get(frame.texture.value);
*texture.swap_chain_link.write() = Some(SwapChainLink {
swap_chain_id: WeaklyStored(swap_chain_id),
epoch: swap_chain.epoch,
image_index,
});
frame.texture.value
}