Skip to content

Commit 59b7d72

Browse files
committed
add create_image_without_memory
1 parent 8be2b18 commit 59b7d72

File tree

1 file changed

+61
-76
lines changed

1 file changed

+61
-76
lines changed

wgpu-hal/src/vulkan/device.rs

Lines changed: 61 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,7 @@ impl super::Device {
702702
.get_physical_device_memory_properties(self.shared.physical_device)
703703
};
704704

705-
for (i, mem_ty) in mem_properties
706-
.memory_types_as_slice()
707-
.into_iter()
708-
.enumerate()
709-
{
705+
for (i, mem_ty) in mem_properties.memory_types_as_slice().iter().enumerate() {
710706
if type_bits & (1 << i) != 0 && mem_ty.property_flags & flags == flags {
711707
return Some(i);
712708
}
@@ -715,14 +711,20 @@ impl super::Device {
715711
None
716712
}
717713

718-
/// # Safety
719-
/// The `d3d11_shared_handle` must be valid and respecting `desc`.
720-
#[cfg(windows)]
721-
pub unsafe fn texture_from_d3d11_shared_handle(
714+
fn create_image_without_memory(
722715
&self,
723-
d3d11_shared_handle: *mut std::ffi::c_void,
724716
desc: &crate::TextureDescriptor,
725-
) -> ash::prelude::VkResult<super::Texture> {
717+
external_memory_image_create_info: Option<&mut vk::ExternalMemoryImageCreateInfo>,
718+
) -> Result<
719+
(
720+
vk::Image,
721+
vk::MemoryRequirements,
722+
crate::CopyExtent,
723+
Vec<wgt::TextureFormat>,
724+
vk::ImageCreateFlags,
725+
),
726+
crate::DeviceError,
727+
> {
726728
let copy_size = desc.copy_extent();
727729

728730
let mut raw_flags = vk::ImageCreateFlags::empty();
@@ -770,12 +772,39 @@ impl super::Device {
770772
vk_info = vk_info.push_next(&mut format_list_info);
771773
}
772774

775+
if let Some(ext_info) = external_memory_image_create_info {
776+
vk_info = vk_info.push_next(ext_info);
777+
}
778+
779+
let raw = unsafe {
780+
self.shared
781+
.raw
782+
.create_image(&vk_info, None)
783+
.map_err(map_err)?
784+
};
785+
fn map_err(err: vk::Result) -> crate::DeviceError {
786+
// We don't use VK_EXT_image_compression_control
787+
// VK_ERROR_COMPRESSION_EXHAUSTED_EXT
788+
super::map_host_device_oom_and_ioca_err(err)
789+
}
790+
let req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };
791+
792+
Ok((raw, req, copy_size, wgt_view_formats, raw_flags))
793+
}
794+
795+
/// # Safety
796+
/// The `d3d11_shared_handle` must be valid and respecting `desc`.
797+
#[cfg(windows)]
798+
pub unsafe fn texture_from_d3d11_shared_handle(
799+
&self,
800+
d3d11_shared_handle: *mut std::ffi::c_void,
801+
desc: &crate::TextureDescriptor,
802+
) -> Result<super::Texture, crate::DeviceError> {
773803
let mut external_memory_image_info = vk::ExternalMemoryImageCreateInfo::default()
774804
.handle_types(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE_KMT);
775-
vk_info = vk_info.push_next(&mut external_memory_image_info);
776805

777-
let raw = unsafe { self.shared.raw.create_image(&vk_info, None)? };
778-
let req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };
806+
let (raw, req, copy_size, wgt_view_formats, raw_flags) =
807+
self.create_image_without_memory(desc, Some(&mut external_memory_image_info))?;
779808

780809
let mut import_memory_info = vk::ImportMemoryWin32HandleInfoKHR::default()
781810
.handle_type(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE_KMT)
@@ -784,7 +813,7 @@ impl super::Device {
784813
let Some(mem_type_index) = self
785814
.find_memory_type_index(req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)
786815
else {
787-
return Err(vk::Result::ERROR_UNKNOWN);
816+
return Err(crate::DeviceError::ResourceCreationFailed);
788817
};
789818

790819
let memory_allocate_info = vk::MemoryAllocateInfo::default()
@@ -794,9 +823,22 @@ impl super::Device {
794823
let memory = unsafe {
795824
self.shared
796825
.raw
797-
.allocate_memory(&memory_allocate_info, None)?
826+
.allocate_memory(&memory_allocate_info, None)
827+
.map_err(super::map_host_device_oom_err)?
828+
};
829+
830+
unsafe {
831+
self.shared
832+
.raw
833+
.bind_image_memory(raw, memory, 0)
834+
.map_err(super::map_host_device_oom_err)?
798835
};
799-
unsafe { self.shared.raw.bind_image_memory(raw, memory, 0)? };
836+
837+
if let Some(label) = desc.label {
838+
unsafe { self.shared.set_object_name(raw, label) };
839+
}
840+
841+
self.counters.textures.add(1);
800842

801843
Ok(super::Texture {
802844
raw,
@@ -1139,65 +1181,8 @@ impl crate::Device for super::Device {
11391181
&self,
11401182
desc: &crate::TextureDescriptor,
11411183
) -> Result<super::Texture, crate::DeviceError> {
1142-
let copy_size = desc.copy_extent();
1143-
1144-
let mut raw_flags = vk::ImageCreateFlags::empty();
1145-
if desc.is_cube_compatible() {
1146-
raw_flags |= vk::ImageCreateFlags::CUBE_COMPATIBLE;
1147-
}
1148-
1149-
let original_format = self.shared.private_caps.map_texture_format(desc.format);
1150-
let mut vk_view_formats = vec![];
1151-
let mut wgt_view_formats = vec![];
1152-
if !desc.view_formats.is_empty() {
1153-
raw_flags |= vk::ImageCreateFlags::MUTABLE_FORMAT;
1154-
wgt_view_formats.clone_from(&desc.view_formats);
1155-
wgt_view_formats.push(desc.format);
1156-
1157-
if self.shared.private_caps.image_format_list {
1158-
vk_view_formats = desc
1159-
.view_formats
1160-
.iter()
1161-
.map(|f| self.shared.private_caps.map_texture_format(*f))
1162-
.collect();
1163-
vk_view_formats.push(original_format)
1164-
}
1165-
}
1166-
if desc.format.is_multi_planar_format() {
1167-
raw_flags |= vk::ImageCreateFlags::MUTABLE_FORMAT;
1168-
}
1169-
1170-
let mut vk_info = vk::ImageCreateInfo::default()
1171-
.flags(raw_flags)
1172-
.image_type(conv::map_texture_dimension(desc.dimension))
1173-
.format(original_format)
1174-
.extent(conv::map_copy_extent(&copy_size))
1175-
.mip_levels(desc.mip_level_count)
1176-
.array_layers(desc.array_layer_count())
1177-
.samples(vk::SampleCountFlags::from_raw(desc.sample_count))
1178-
.tiling(vk::ImageTiling::OPTIMAL)
1179-
.usage(conv::map_texture_usage(desc.usage))
1180-
.sharing_mode(vk::SharingMode::EXCLUSIVE)
1181-
.initial_layout(vk::ImageLayout::UNDEFINED);
1182-
1183-
let mut format_list_info = vk::ImageFormatListCreateInfo::default();
1184-
if !vk_view_formats.is_empty() {
1185-
format_list_info = format_list_info.view_formats(&vk_view_formats);
1186-
vk_info = vk_info.push_next(&mut format_list_info);
1187-
}
1188-
1189-
let raw = unsafe {
1190-
self.shared
1191-
.raw
1192-
.create_image(&vk_info, None)
1193-
.map_err(map_err)?
1194-
};
1195-
fn map_err(err: vk::Result) -> crate::DeviceError {
1196-
// We don't use VK_EXT_image_compression_control
1197-
// VK_ERROR_COMPRESSION_EXHAUSTED_EXT
1198-
super::map_host_device_oom_and_ioca_err(err)
1199-
}
1200-
let req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };
1184+
let (raw, req, copy_size, wgt_view_formats, raw_flags) =
1185+
self.create_image_without_memory(desc, None)?;
12011186

12021187
let block = unsafe {
12031188
self.mem_allocator.lock().alloc(

0 commit comments

Comments
 (0)