From 5bf1114fe795ba8be9cd88f5f4af9339dd58cd90 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Sat, 4 Mar 2017 18:28:12 +0100 Subject: [PATCH] Implement GPU locks for attachment and storage images --- vulkano/src/image/attachment.rs | 20 +++++++++++++++++--- vulkano/src/image/storage.rs | 19 ++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/vulkano/src/image/attachment.rs b/vulkano/src/image/attachment.rs index c0b02619..0f8bc3f6 100644 --- a/vulkano/src/image/attachment.rs +++ b/vulkano/src/image/attachment.rs @@ -9,6 +9,8 @@ use std::iter::Empty; use std::sync::Arc; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; use device::Device; use device::Queue; @@ -79,6 +81,9 @@ pub struct AttachmentImage> where A: MemoryPool { // Layout to use when the image is used as a framebuffer attachment. // Must be either "depth-stencil optimal" or "color optimal". attachment_layout: Layout, + + // Number of times this image is locked on the GPU side. + gpu_lock: AtomicUsize, } impl AttachmentImage { @@ -168,6 +173,7 @@ impl AttachmentImage { format: format, attachment_layout: if is_depth { Layout::DepthStencilAttachmentOptimal } else { Layout::ColorAttachmentOptimal }, + gpu_lock: AtomicUsize::new(0), })) } } @@ -193,13 +199,20 @@ unsafe impl Image for AttachmentImage where F: 'static + Send + Sync } #[inline] - fn try_gpu_lock(&self, exclusive_access: bool, queue: &Queue) -> bool { - false // FIXME: + fn try_gpu_lock(&self, _: bool, _: &Queue) -> bool { + let val = self.gpu_lock.fetch_add(1, Ordering::SeqCst); + if val == 1 { + true + } else { + self.gpu_lock.fetch_sub(1, Ordering::SeqCst); + false + } } #[inline] unsafe fn increase_gpu_lock(&self) { - // FIXME: + let val = self.gpu_lock.fetch_add(1, Ordering::SeqCst); + debug_assert!(val >= 1); } } @@ -270,6 +283,7 @@ unsafe impl ImageView for AttachmentImage mod tests { use super::AttachmentImage; use format::Format; + use image::Image; #[test] fn create_regular() { diff --git a/vulkano/src/image/storage.rs b/vulkano/src/image/storage.rs index 41b661e8..6a1b2fe2 100644 --- a/vulkano/src/image/storage.rs +++ b/vulkano/src/image/storage.rs @@ -9,6 +9,8 @@ use std::iter::Empty; use std::sync::Arc; +use std::sync::atomic::AtomicUsize; +use std::sync::atomic::Ordering; use smallvec::SmallVec; use device::Device; @@ -54,6 +56,9 @@ pub struct StorageImage> where A: MemoryPool { // Queue families allowed to access this image. queue_families: SmallVec<[u32; 4]>, + + // Number of times this image is locked on the GPU side. + gpu_lock: AtomicUsize, } impl StorageImage { @@ -122,6 +127,7 @@ impl StorageImage { dimensions: dimensions, format: format, queue_families: queue_families, + gpu_lock: AtomicUsize::new(0), })) } } @@ -146,13 +152,20 @@ unsafe impl Image for StorageImage where F: 'static + Send + Sync, A } #[inline] - fn try_gpu_lock(&self, exclusive_access: bool, queue: &Queue) -> bool { - false // FIXME: + fn try_gpu_lock(&self, _: bool, _: &Queue) -> bool { + let val = self.gpu_lock.fetch_add(1, Ordering::SeqCst); + if val == 1 { + true + } else { + self.gpu_lock.fetch_sub(1, Ordering::SeqCst); + false + } } #[inline] unsafe fn increase_gpu_lock(&self) { - // FIXME: + let val = self.gpu_lock.fetch_add(1, Ordering::SeqCst); + debug_assert!(val >= 1); } }