Change FramebufferAbstract trait to be faster

This commit is contained in:
Pierre Krieger 2017-07-03 13:37:51 +02:00
parent b1abbbd7cd
commit 572ace793a
4 changed files with 46 additions and 22 deletions

View File

@ -613,14 +613,14 @@ impl<P> SyncCommandBufferBuilder<P> {
where F: FramebufferAbstract + Send + Sync + 'static where F: FramebufferAbstract + Send + Sync + 'static
{ {
fn image(&self, num: usize) -> &ImageAccess { fn image(&self, num: usize) -> &ImageAccess {
self.0.attachments()[num].parent() // TODO: slow self.0.attached_image_view(num).unwrap().parent()
} }
} }
Box::new(Fin(self.framebuffer)) Box::new(Fin(self.framebuffer))
} }
fn image(&self, num: usize) -> &ImageAccess { fn image(&self, num: usize) -> &ImageAccess {
self.framebuffer.attachments()[num].parent() // TODO: slow self.framebuffer.attached_image_view(num).unwrap().parent()
} }
} }

View File

@ -6,7 +6,7 @@
// at your option. All files in the project carrying such // at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except // notice may not be copied, modified, or distributed except
// according to those terms. // according to those terms.
use SafeDeref; use SafeDeref;
use image::ImageViewAccess; use image::ImageViewAccess;
use std::sync::Arc; use std::sync::Arc;
@ -16,8 +16,9 @@ use std::sync::Arc;
/// A list of attachments. /// A list of attachments.
// TODO: rework this trait // TODO: rework this trait
pub unsafe trait AttachmentsList { pub unsafe trait AttachmentsList {
// TODO: meh for API fn num_attachments(&self) -> usize;
fn as_image_view_accesses(&self) -> Vec<&ImageViewAccess>;
fn as_image_view_access(&self, index: usize) -> Option<&ImageViewAccess>;
} }
unsafe impl<T> AttachmentsList for T unsafe impl<T> AttachmentsList for T
@ -25,22 +26,37 @@ unsafe impl<T> AttachmentsList for T
T::Target: AttachmentsList T::Target: AttachmentsList
{ {
#[inline] #[inline]
fn as_image_view_accesses(&self) -> Vec<&ImageViewAccess> { fn num_attachments(&self) -> usize {
(**self).as_image_view_accesses() (**self).num_attachments()
}
#[inline]
fn as_image_view_access(&self, index: usize) -> Option<&ImageViewAccess> {
(**self).as_image_view_access(index)
} }
} }
unsafe impl AttachmentsList for () { unsafe impl AttachmentsList for () {
#[inline] #[inline]
fn as_image_view_accesses(&self) -> Vec<&ImageViewAccess> { fn num_attachments(&self) -> usize {
vec![] 0
}
#[inline]
fn as_image_view_access(&self, _: usize) -> Option<&ImageViewAccess> {
None
} }
} }
unsafe impl AttachmentsList for Vec<Arc<ImageViewAccess + Send + Sync>> { unsafe impl AttachmentsList for Vec<Arc<ImageViewAccess + Send + Sync>> {
#[inline] #[inline]
fn as_image_view_accesses(&self) -> Vec<&ImageViewAccess> { fn num_attachments(&self) -> usize {
self.iter().map(|p| &**p as &ImageViewAccess).collect() self.len()
}
#[inline]
fn as_image_view_access(&self, index: usize) -> Option<&ImageViewAccess> {
self.get(index).map(|v| &**v as &_)
} }
} }
@ -49,9 +65,16 @@ unsafe impl<A, B> AttachmentsList for (A, B)
B: ImageViewAccess B: ImageViewAccess
{ {
#[inline] #[inline]
fn as_image_view_accesses(&self) -> Vec<&ImageViewAccess> { fn num_attachments(&self) -> usize {
let mut list = self.0.as_image_view_accesses(); self.0.num_attachments() + 1
list.push(&self.1); }
list
#[inline]
fn as_image_view_access(&self, index: usize) -> Option<&ImageViewAccess> {
if index == self.0.num_attachments() {
Some(&self.1)
} else {
self.0.as_image_view_access(index)
}
} }
} }

View File

@ -376,8 +376,8 @@ unsafe impl<Rp, A> FramebufferAbstract for Framebuffer<Rp, A>
} }
#[inline] #[inline]
fn attachments(&self) -> Vec<&ImageViewAccess> { fn attached_image_view(&self, index: usize) -> Option<&ImageViewAccess> {
self.resources.as_image_view_accesses() self.resources.as_image_view_access(index)
} }
} }

View File

@ -28,9 +28,10 @@ pub unsafe trait FramebufferAbstract: RenderPassAbstract {
/// Returns the width, height and array layers of the framebuffer. /// Returns the width, height and array layers of the framebuffer.
fn dimensions(&self) -> [u32; 3]; fn dimensions(&self) -> [u32; 3];
/// Returns all the attachments of the framebuffer. /// Returns the attachment of the framebuffer with the given index.
// TODO: meh for trait object ///
fn attachments(&self) -> Vec<&ImageViewAccess>; /// If the `index` is not between `0` and `num_attachments`, then `None` should be returned.
fn attached_image_view(&self, index: usize) -> Option<&ImageViewAccess>;
/// Returns the width of the framebuffer in pixels. /// Returns the width of the framebuffer in pixels.
#[inline] #[inline]
@ -66,8 +67,8 @@ unsafe impl<T> FramebufferAbstract for T
} }
#[inline] #[inline]
fn attachments(&self) -> Vec<&ImageViewAccess> { fn attached_image_view(&self, index: usize) -> Option<&ImageViewAccess> {
FramebufferAbstract::attachments(&**self) (**self).attached_image_view(index)
} }
} }