let initial_data = std::slice::from_raw_parts(data.as_ptr() as *const u8, len);
device.create_buffer_with_data(
&BufferDesc {
- location: MemoryLocation::HostMapped,
+ memory_location: MemoryLocation::Host,
+ host_mapped: true,
usage,
size: len,
},
);
let blÄhaj_image = device.create_image(&ImageDesc {
- location: MemoryLocation::Device,
+ memory_location: MemoryLocation::Device,
usage: ImageUsageFlags::SAMPLED | ImageUsageFlags::TRANSFER,
dimension: ImageDimension::Type2d,
format: ImageFormat::RGBA8_SRGB,
);
let glyph_atlas = device.create_image(&ImageDesc {
- location: MemoryLocation::Device,
+ memory_location: MemoryLocation::Device,
usage: ImageUsageFlags::SAMPLED | ImageUsageFlags::TRANSFER,
dimension: ImageDimension::Type2d,
format: ImageFormat::R8_UNORM,
let mut buffers = (0..4096)
.map(|_| {
device.create_buffer(&BufferDesc {
- location: MemoryLocation::HostMapped,
+ memory_location: MemoryLocation::Host,
+ host_mapped: true,
usage: BufferUsageFlags::STORAGE,
size: 16 + rng.next_bound_usize(1024 - 16),
})
buffers.extend((0..512).map(|_| {
device.create_buffer(&BufferDesc {
- location: MemoryLocation::HostMapped,
+ memory_location: MemoryLocation::Host,
+ host_mapped: true,
usage: BufferUsageFlags::STORAGE,
size: 16 + rng.next_bound_usize(10 * 1024 * 1024 - 16),
})
if width != depth_width || height != depth_height {
device.destroy_image(&frame, depth_image);
depth_image = device.create_image(&ImageDesc {
- location: MemoryLocation::Device,
+ memory_location: MemoryLocation::Device,
usage: ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT,
dimension: ImageDimension::Type2d,
format: ImageFormat::DEPTH_F32,
impl<'a> MappedBuffer<'a> {
pub fn new(device: &'a dyn Device, usage: BufferUsageFlags, len: usize) -> Self {
let buffer = device.create_buffer(&BufferDesc {
- location: MemoryLocation::HostMapped,
+ memory_location: MemoryLocation::Host,
+ host_mapped: true,
usage,
size: len,
});
Buffer(vk::Buffer),
}
-pub struct VulkanMemoryDesc {
- pub requirements: vk::MemoryRequirements,
- pub memory_location: MemoryLocation,
- pub _linear: bool,
-}
-
#[derive(Clone)]
pub struct VulkanMemoryDedicated {
memory: vk::DeviceMemory,
pub fn allocate_memory_dedicated(
&self,
- desc: &VulkanMemoryDesc,
+ memory_location: MemoryLocation,
+ requirements: &vk::MemoryRequirements,
dedicated_desc: &VulkanMemoryDedicatedDesc,
) -> VulkanMemory {
- let memory_property_flags = match desc.memory_location {
- MemoryLocation::HostMapped => vk::MemoryPropertyFlags::HOST_VISIBLE,
+ let memory_property_flags = match memory_location {
+ MemoryLocation::Host => vk::MemoryPropertyFlags::HOST_VISIBLE,
MemoryLocation::Device => vk::MemoryPropertyFlags::DEVICE_LOCAL,
};
+ let size = requirements.size;
+
let memory_type_index =
- self.find_memory_type_index(desc.requirements.memory_type_bits, memory_property_flags);
+ self.find_memory_type_index(requirements.memory_type_bits, memory_property_flags);
let allocator = self.allocators[memory_type_index.widen()]
.as_ref()
.expect("returned a memory type index that has no associated allocator");
let mut allocate_info = vk::MemoryAllocateInfo {
- allocation_size: desc.requirements.size,
+ allocation_size: size,
memory_type_index,
..default()
};
VulkanMemory::Dedicated(VulkanMemoryDedicated {
memory,
mapped_ptr,
- size: desc.requirements.size,
+ size,
memory_type_index,
})
}
- pub fn allocate_memory(&self, desc: &VulkanMemoryDesc) -> VulkanMemory {
- let memory_property_flags = match desc.memory_location {
- MemoryLocation::HostMapped => vk::MemoryPropertyFlags::HOST_VISIBLE,
+ pub fn allocate_memory(
+ &self,
+ memory_location: MemoryLocation,
+ requirements: &vk::MemoryRequirements,
+ ) -> VulkanMemory {
+ let memory_property_flags = match memory_location {
+ MemoryLocation::Host => vk::MemoryPropertyFlags::HOST_VISIBLE,
MemoryLocation::Device => vk::MemoryPropertyFlags::DEVICE_LOCAL,
};
+ let size = requirements.size;
+ let align = requirements.alignment;
+
let memory_type_index =
- self.find_memory_type_index(desc.requirements.memory_type_bits, memory_property_flags);
+ self.find_memory_type_index(requirements.memory_type_bits, memory_property_flags);
let allocator = self.allocators[memory_type_index.widen()]
.as_ref()
let mut tlsf = allocator.tlsf.lock();
let allocation = {
- if let Some(allocation) =
- tlsf.alloc(desc.requirements.size, desc.requirements.alignment)
- {
+ if let Some(allocation) = tlsf.alloc(size, align) {
allocation
} else {
let allocate_info = vk::MemoryAllocateInfo {
VulkanAllocationInfo { memory, mapped_ptr },
);
- tlsf.alloc(desc.requirements.size, desc.requirements.alignment)
- .expect("failed to allocate")
+ tlsf.alloc(size, align).expect("failed to allocate")
}
};
VulkanMemory::SubAlloc(VulkanMemorySubAlloc {
allocation,
- size: desc.requirements.size,
+ size: requirements.size,
memory_type_index,
})
}
mod wsi;
use self::{
- allocator::{VulkanAllocator, VulkanMemory, VulkanMemoryDedicatedDesc, VulkanMemoryDesc},
+ allocator::{VulkanAllocator, VulkanMemory, VulkanMemoryDedicatedDesc},
barrier::{vulkan_image_memory_barrier, vulkan_memory_barrier},
convert::*,
wsi::{VulkanWsi, VulkanWsiFrame},
|| memory_dedicated_requirements.requires_dedicated_allocation == vk::Bool32::True
{
self.allocate_memory_dedicated(
- &VulkanMemoryDesc {
- requirements: memory_requirements.memory_requirements,
- memory_location: desc.location,
- _linear: true,
- },
+ desc.memory_location,
+ &memory_requirements.memory_requirements,
&VulkanMemoryDedicatedDesc::Buffer(buffer),
)
} else {
- self.allocate_memory(&VulkanMemoryDesc {
- requirements: memory_requirements.memory_requirements,
- memory_location: desc.location,
- _linear: true,
- })
+ self.allocate_memory(
+ desc.memory_location,
+ &memory_requirements.memory_requirements,
+ )
};
if let Some(initial_data) = initial_data {
|| memory_dedicated_requirements.requires_dedicated_allocation == vk::Bool32::True
{
self.allocate_memory_dedicated(
- &VulkanMemoryDesc {
- requirements: memory_requirements.memory_requirements,
- memory_location: desc.location,
- _linear: true,
- },
+ desc.memory_location,
+ &memory_requirements.memory_requirements,
&VulkanMemoryDedicatedDesc::Image(image),
)
} else {
- self.allocate_memory(&VulkanMemoryDesc {
- requirements: memory_requirements.memory_requirements,
- memory_location: desc.location,
- _linear: true,
- })
+ self.allocate_memory(
+ desc.memory_location,
+ &memory_requirements.memory_requirements,
+ )
};
unsafe {
&mut memory_requirements,
);
- let memory = self.allocate_memory(&VulkanMemoryDesc {
- requirements: memory_requirements.memory_requirements,
- memory_location: MemoryLocation::HostMapped,
- _linear: true,
- });
+ let memory = self.allocate_memory(
+ MemoryLocation::Host,
+ &memory_requirements.memory_requirements,
+ );
unsafe {
self.device_fn.bind_buffer_memory2(
&mut memory_requirements,
);
- let memory = self.allocate_memory(&VulkanMemoryDesc {
- requirements: memory_requirements.memory_requirements,
- memory_location: MemoryLocation::HostMapped,
- _linear: true,
- });
+ let memory = self.allocate_memory(
+ MemoryLocation::Host,
+ &memory_requirements.memory_requirements,
+ );
assert!(!memory.mapped_ptr().is_null());
// SAFETY: The memory has just been allocated, so as long as the pointer is
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MemoryLocation {
- HostMapped,
+ Host,
Device,
}
}
pub struct BufferDesc {
- pub location: MemoryLocation,
+ pub memory_location: MemoryLocation,
+ pub host_mapped: bool,
pub usage: BufferUsageFlags,
pub size: usize,
}
pub struct ImageDesc {
- pub location: MemoryLocation,
+ pub memory_location: MemoryLocation,
pub usage: ImageUsageFlags,
pub dimension: ImageDimension,
pub format: ImageFormat,