use super::{
sys::{Image, ImageMemory, RawImage},
traits::ImageContent,
ImageAccess, ImageDescriptorLayouts, ImageError, ImageInner, ImageLayout, ImageUsage,
SampleCount,
};
use crate::{
device::{Device, DeviceOwned},
format::Format,
image::{sys::ImageCreateInfo, ImageCreateFlags, ImageDimensions, ImageFormatInfo},
memory::{
allocator::{
AllocationCreateInfo, AllocationType, MemoryAllocatePreference, MemoryAllocator,
MemoryUsage,
},
DedicatedAllocation, DeviceMemoryError, ExternalMemoryHandleType,
ExternalMemoryHandleTypes,
},
DeviceSize,
};
use std::{
fs::File,
hash::{Hash, Hasher},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};
#[derive(Debug)]
pub struct AttachmentImage {
inner: Arc<Image>,
attachment_layout: ImageLayout,
layout_initialized: AtomicBool,
}
impl AttachmentImage {
#[inline]
pub fn new(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
ImageUsage::empty(),
SampleCount::Sample1,
)
}
#[inline]
pub fn input_attachment(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
input_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
base_usage,
SampleCount::Sample1,
)
}
#[inline]
pub fn multisampled(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
ImageUsage::empty(),
samples,
)
}
#[inline]
pub fn multisampled_input_attachment(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
input_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(allocator, dimensions, 1, format, base_usage, samples)
}
#[inline]
pub fn with_usage(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
usage: ImageUsage,
) -> Result<Arc<AttachmentImage>, ImageError> {
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
usage,
SampleCount::Sample1,
)
}
#[inline]
pub fn multisampled_with_usage(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
usage: ImageUsage,
) -> Result<Arc<AttachmentImage>, ImageError> {
AttachmentImage::new_impl(allocator, dimensions, 1, format, usage, samples)
}
#[inline]
pub fn multisampled_with_usage_with_layers(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
array_layers: u32,
samples: SampleCount,
format: Format,
usage: ImageUsage,
) -> Result<Arc<AttachmentImage>, ImageError> {
AttachmentImage::new_impl(allocator, dimensions, array_layers, format, usage, samples)
}
#[inline]
pub fn sampled(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
sampled: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
base_usage,
SampleCount::Sample1,
)
}
#[inline]
pub fn sampled_input_attachment(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
sampled: true,
input_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
base_usage,
SampleCount::Sample1,
)
}
#[inline]
pub fn sampled_multisampled(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
sampled: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(allocator, dimensions, 1, format, base_usage, samples)
}
#[inline]
pub fn sampled_multisampled_input_attachment(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
sampled: true,
input_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(allocator, dimensions, 1, format, base_usage, samples)
}
#[inline]
pub fn transient(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
transient_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
base_usage,
SampleCount::Sample1,
)
}
#[inline]
pub fn transient_input_attachment(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
transient_attachment: true,
input_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(
allocator,
dimensions,
1,
format,
base_usage,
SampleCount::Sample1,
)
}
#[inline]
pub fn transient_multisampled(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
transient_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(allocator, dimensions, 1, format, base_usage, samples)
}
#[inline]
pub fn transient_multisampled_input_attachment(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
samples: SampleCount,
format: Format,
) -> Result<Arc<AttachmentImage>, ImageError> {
let base_usage = ImageUsage {
transient_attachment: true,
input_attachment: true,
..ImageUsage::empty()
};
AttachmentImage::new_impl(allocator, dimensions, 1, format, base_usage, samples)
}
fn new_impl(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
array_layers: u32,
format: Format,
base_usage: ImageUsage,
samples: SampleCount,
) -> Result<Arc<AttachmentImage>, ImageError> {
let physical_device = allocator.device().physical_device();
let device_properties = physical_device.properties();
if dimensions[0] > device_properties.max_framebuffer_height {
panic!("AttachmentImage height exceeds physical device's max_framebuffer_height");
}
if dimensions[1] > device_properties.max_framebuffer_width {
panic!("AttachmentImage width exceeds physical device's max_framebuffer_width");
}
if array_layers > device_properties.max_framebuffer_layers {
panic!("AttachmentImage layer count exceeds physical device's max_framebuffer_layers");
}
let aspects = format.aspects();
let is_depth = aspects.depth || aspects.stencil;
if format.compression().is_some() {
panic!() }
let raw_image = RawImage::new(
allocator.device().clone(),
ImageCreateInfo {
dimensions: ImageDimensions::Dim2d {
width: dimensions[0],
height: dimensions[1],
array_layers,
},
format: Some(format),
samples,
usage: ImageUsage {
color_attachment: !is_depth,
depth_stencil_attachment: is_depth,
..base_usage
},
..Default::default()
},
)?;
let requirements = raw_image.memory_requirements()[0];
let create_info = AllocationCreateInfo {
requirements,
allocation_type: AllocationType::NonLinear,
usage: MemoryUsage::GpuOnly,
allocate_preference: MemoryAllocatePreference::Unknown,
dedicated_allocation: Some(DedicatedAllocation::Image(&raw_image)),
..Default::default()
};
match unsafe { allocator.allocate_unchecked(create_info) } {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.alignment == 0);
debug_assert!(alloc.size() == requirements.size);
let inner = Arc::new(unsafe {
raw_image
.bind_memory_unchecked([alloc])
.map_err(|(err, _, _)| err)?
});
Ok(Arc::new(AttachmentImage {
inner,
attachment_layout: if is_depth {
ImageLayout::DepthStencilAttachmentOptimal
} else {
ImageLayout::ColorAttachmentOptimal
},
layout_initialized: AtomicBool::new(false),
}))
}
Err(err) => Err(err.into()),
}
}
pub fn new_with_exportable_fd(
allocator: &(impl MemoryAllocator + ?Sized),
dimensions: [u32; 2],
array_layers: u32,
format: Format,
base_usage: ImageUsage,
samples: SampleCount,
) -> Result<Arc<AttachmentImage>, ImageError> {
let physical_device = allocator.device().physical_device();
let device_properties = physical_device.properties();
if dimensions[0] > device_properties.max_framebuffer_height {
panic!("AttachmentImage height exceeds physical device's max_framebuffer_height");
}
if dimensions[1] > device_properties.max_framebuffer_width {
panic!("AttachmentImage width exceeds physical device's max_framebuffer_width");
}
if array_layers > device_properties.max_framebuffer_layers {
panic!("AttachmentImage layer count exceeds physical device's max_framebuffer_layers");
}
let aspects = format.aspects();
let is_depth = aspects.depth || aspects.stencil;
let usage = ImageUsage {
color_attachment: !is_depth,
depth_stencil_attachment: is_depth,
..base_usage
};
let external_memory_properties = allocator
.device()
.physical_device()
.image_format_properties(ImageFormatInfo {
flags: ImageCreateFlags {
mutable_format: true,
..ImageCreateFlags::empty()
},
format: Some(format),
usage,
external_memory_handle_type: Some(ExternalMemoryHandleType::OpaqueFd),
..Default::default()
})
.unwrap()
.unwrap()
.external_memory_properties;
assert!(external_memory_properties.exportable);
let external_memory_handle_types = ExternalMemoryHandleTypes {
opaque_fd: true,
..ExternalMemoryHandleTypes::empty()
};
let raw_image = RawImage::new(
allocator.device().clone(),
ImageCreateInfo {
flags: ImageCreateFlags {
mutable_format: true,
..ImageCreateFlags::empty()
},
dimensions: ImageDimensions::Dim2d {
width: dimensions[0],
height: dimensions[1],
array_layers,
},
format: Some(format),
samples,
usage,
external_memory_handle_types,
..Default::default()
},
)?;
let requirements = raw_image.memory_requirements()[0];
let memory_type_index = allocator
.find_memory_type_index(requirements.memory_type_bits, MemoryUsage::GpuOnly.into())
.expect("failed to find a suitable memory type");
match unsafe {
allocator.allocate_dedicated_unchecked(
memory_type_index,
requirements.size,
Some(DedicatedAllocation::Image(&raw_image)),
external_memory_handle_types,
)
} {
Ok(alloc) => {
debug_assert!(alloc.offset() % requirements.alignment == 0);
debug_assert!(alloc.size() == requirements.size);
let inner = Arc::new(unsafe {
raw_image
.bind_memory_unchecked([alloc])
.map_err(|(err, _, _)| err)?
});
Ok(Arc::new(AttachmentImage {
inner,
attachment_layout: if is_depth {
ImageLayout::DepthStencilAttachmentOptimal
} else {
ImageLayout::ColorAttachmentOptimal
},
layout_initialized: AtomicBool::new(false),
}))
}
Err(err) => Err(err.into()),
}
}
#[inline]
pub fn export_posix_fd(&self) -> Result<File, DeviceMemoryError> {
let allocation = match self.inner.memory() {
ImageMemory::Normal(a) => &a[0],
_ => unreachable!(),
};
allocation
.device_memory()
.export_fd(ExternalMemoryHandleType::OpaqueFd)
}
#[inline]
pub fn mem_size(&self) -> DeviceSize {
let allocation = match self.inner.memory() {
ImageMemory::Normal(a) => &a[0],
_ => unreachable!(),
};
allocation.device_memory().allocation_size()
}
}
unsafe impl ImageAccess for AttachmentImage {
#[inline]
fn inner(&self) -> ImageInner<'_> {
ImageInner {
image: &self.inner,
first_layer: 0,
num_layers: self.inner.dimensions().array_layers(),
first_mipmap_level: 0,
num_mipmap_levels: 1,
}
}
#[inline]
fn initial_layout_requirement(&self) -> ImageLayout {
self.attachment_layout
}
#[inline]
fn final_layout_requirement(&self) -> ImageLayout {
self.attachment_layout
}
#[inline]
fn descriptor_layouts(&self) -> Option<ImageDescriptorLayouts> {
Some(ImageDescriptorLayouts {
storage_image: ImageLayout::General,
combined_image_sampler: ImageLayout::ShaderReadOnlyOptimal,
sampled_image: ImageLayout::ShaderReadOnlyOptimal,
input_attachment: ImageLayout::ShaderReadOnlyOptimal,
})
}
#[inline]
unsafe fn layout_initialized(&self) {
self.layout_initialized.store(true, Ordering::SeqCst);
}
#[inline]
fn is_layout_initialized(&self) -> bool {
self.layout_initialized.load(Ordering::SeqCst)
}
}
unsafe impl DeviceOwned for AttachmentImage {
#[inline]
fn device(&self) -> &Arc<Device> {
self.inner.device()
}
}
unsafe impl<P> ImageContent<P> for AttachmentImage {
fn matches_format(&self) -> bool {
true }
}
impl PartialEq for AttachmentImage {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.inner() == other.inner()
}
}
impl Eq for AttachmentImage {}
impl Hash for AttachmentImage {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner().hash(state);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::memory::allocator::StandardMemoryAllocator;
#[test]
fn create_regular() {
let (device, _) = gfx_dev_and_queue!();
let memory_allocator = StandardMemoryAllocator::new_default(device);
let _img =
AttachmentImage::new(&memory_allocator, [32, 32], Format::R8G8B8A8_UNORM).unwrap();
}
#[test]
fn create_transient() {
let (device, _) = gfx_dev_and_queue!();
let memory_allocator = StandardMemoryAllocator::new_default(device);
let _img = AttachmentImage::transient(&memory_allocator, [32, 32], Format::R8G8B8A8_UNORM)
.unwrap();
}
#[test]
fn d16_unorm_always_supported() {
let (device, _) = gfx_dev_and_queue!();
let memory_allocator = StandardMemoryAllocator::new_default(device);
let _img = AttachmentImage::new(&memory_allocator, [32, 32], Format::D16_UNORM).unwrap();
}
}