Skip to content

Commit 8e9f658

Browse files
committed
add create_image_without_memory
1 parent 1d8cdfb commit 8e9f658

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
@@ -709,11 +709,7 @@ impl super::Device {
709709
.get_physical_device_memory_properties(self.shared.physical_device)
710710
};
711711

712-
for (i, mem_ty) in mem_properties
713-
.memory_types_as_slice()
714-
.into_iter()
715-
.enumerate()
716-
{
712+
for (i, mem_ty) in mem_properties.memory_types_as_slice().iter().enumerate() {
717713
if type_bits & (1 << i) != 0 && mem_ty.property_flags & flags == flags {
718714
return Some(i);
719715
}
@@ -722,14 +718,20 @@ impl super::Device {
722718
None
723719
}
724720

725-
/// # Safety
726-
/// The `d3d11_shared_handle` must be valid and respecting `desc`.
727-
#[cfg(windows)]
728-
pub unsafe fn texture_from_d3d11_shared_handle(
721+
fn create_image_without_memory(
729722
&self,
730-
d3d11_shared_handle: *mut std::ffi::c_void,
731723
desc: &crate::TextureDescriptor,
732-
) -> ash::prelude::VkResult<super::Texture> {
724+
external_memory_image_create_info: Option<&mut vk::ExternalMemoryImageCreateInfo>,
725+
) -> Result<
726+
(
727+
vk::Image,
728+
vk::MemoryRequirements,
729+
crate::CopyExtent,
730+
Vec<wgt::TextureFormat>,
731+
vk::ImageCreateFlags,
732+
),
733+
crate::DeviceError,
734+
> {
733735
let copy_size = desc.copy_extent();
734736

735737
let mut raw_flags = vk::ImageCreateFlags::empty();
@@ -777,12 +779,39 @@ impl super::Device {
777779
vk_info = vk_info.push_next(&mut format_list_info);
778780
}
779781

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

784-
let raw = unsafe { self.shared.raw.create_image(&vk_info, None)? };
785-
let req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };
813+
let (raw, req, copy_size, wgt_view_formats, raw_flags) =
814+
self.create_image_without_memory(desc, Some(&mut external_memory_image_info))?;
786815

787816
let mut import_memory_info = vk::ImportMemoryWin32HandleInfoKHR::default()
788817
.handle_type(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE_KMT)
@@ -791,7 +820,7 @@ impl super::Device {
791820
let Some(mem_type_index) = self
792821
.find_memory_type_index(req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)
793822
else {
794-
return Err(vk::Result::ERROR_UNKNOWN);
823+
return Err(crate::DeviceError::ResourceCreationFailed);
795824
};
796825

797826
let memory_allocate_info = vk::MemoryAllocateInfo::default()
@@ -801,9 +830,22 @@ impl super::Device {
801830
let memory = unsafe {
802831
self.shared
803832
.raw
804-
.allocate_memory(&memory_allocate_info, None)?
833+
.allocate_memory(&memory_allocate_info, None)
834+
.map_err(super::map_host_device_oom_err)?
835+
};
836+
837+
unsafe {
838+
self.shared
839+
.raw
840+
.bind_image_memory(raw, memory, 0)
841+
.map_err(super::map_host_device_oom_err)?
805842
};
806-
unsafe { self.shared.raw.bind_image_memory(raw, memory, 0)? };
843+
844+
if let Some(label) = desc.label {
845+
unsafe { self.shared.set_object_name(raw, label) };
846+
}
847+
848+
self.counters.textures.add(1);
807849

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

12131198
let block = unsafe {
12141199
self.mem_allocator.lock().alloc(

0 commit comments

Comments
 (0)