Skip to content

Commit fbda323

Browse files
committed
add ImageWithoutMemory struct
1 parent 47298e2 commit fbda323

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

wgpu-hal/src/vulkan/device.rs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ impl super::Device {
709709
.get_physical_device_memory_properties(self.shared.physical_device)
710710
};
711711

712+
// https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkPhysicalDeviceMemoryProperties.html
712713
for (i, mem_ty) in mem_properties.memory_types_as_slice().iter().enumerate() {
713714
if type_bits & (1 << i) != 0 && mem_ty.property_flags & flags == flags {
714715
return Some(i);
@@ -722,16 +723,7 @@ impl super::Device {
722723
&self,
723724
desc: &crate::TextureDescriptor,
724725
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-
> {
726+
) -> Result<ImageWithoutMemory, crate::DeviceError> {
735727
let copy_size = desc.copy_extent();
736728

737729
let mut raw_flags = vk::ImageCreateFlags::empty();
@@ -791,7 +783,13 @@ impl super::Device {
791783
}
792784
let req = unsafe { self.shared.raw.get_image_memory_requirements(raw) };
793785

794-
Ok((raw, req, copy_size, wgt_view_formats, raw_flags))
786+
Ok(ImageWithoutMemory {
787+
raw,
788+
requirements: req,
789+
copy_size,
790+
view_formats: wgt_view_formats,
791+
raw_flags,
792+
})
795793
}
796794

797795
/// # Safety
@@ -802,29 +800,38 @@ impl super::Device {
802800
#[cfg(windows)]
803801
pub unsafe fn texture_from_d3d11_shared_handle(
804802
&self,
805-
d3d11_shared_handle: *mut std::ffi::c_void,
803+
d3d11_shared_handle: windows::Win32::Foundation::HANDLE,
806804
desc: &crate::TextureDescriptor,
807805
) -> Result<super::Texture, crate::DeviceError> {
808806
if !self.shared.private_caps.external_memory_win32 {
807+
log::error!("VK_KHR_external_memory extension is required");
809808
return Err(crate::DeviceError::ResourceCreationFailed);
810809
}
811810

812811
let mut external_memory_image_info = vk::ExternalMemoryImageCreateInfo::default()
813812
.handle_types(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE);
814813

815-
let (raw, req, copy_size, wgt_view_formats, raw_flags) =
816-
self.create_image_without_memory(desc, Some(&mut external_memory_image_info))?;
814+
let ImageWithoutMemory {
815+
raw,
816+
requirements,
817+
copy_size,
818+
view_formats,
819+
raw_flags,
820+
} = self.create_image_without_memory(desc, Some(&mut external_memory_image_info))?;
817821

818822
let mut import_memory_info = vk::ImportMemoryWin32HandleInfoKHR::default()
819823
.handle_type(vk::ExternalMemoryHandleTypeFlags::D3D11_TEXTURE)
820-
.handle(d3d11_shared_handle as _);
824+
.handle(d3d11_shared_handle.0 as _);
821825

822826
let mem_type_index = self
823-
.find_memory_type_index(req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)
827+
.find_memory_type_index(
828+
requirements.memory_type_bits,
829+
vk::MemoryPropertyFlags::DEVICE_LOCAL,
830+
)
824831
.ok_or(crate::DeviceError::ResourceCreationFailed)?;
825832

826833
let memory_allocate_info = vk::MemoryAllocateInfo::default()
827-
.allocation_size(req.size)
834+
.allocation_size(requirements.size)
828835
.memory_type_index(mem_type_index as _)
829836
.push_next(&mut import_memory_info);
830837
let memory = unsafe { self.shared.raw.allocate_memory(&memory_allocate_info, None) }
@@ -848,7 +855,7 @@ impl super::Device {
848855
format: desc.format,
849856
raw_flags,
850857
copy_size,
851-
view_formats: wgt_view_formats,
858+
view_formats,
852859
})
853860
}
854861

@@ -1184,17 +1191,22 @@ impl crate::Device for super::Device {
11841191
&self,
11851192
desc: &crate::TextureDescriptor,
11861193
) -> Result<super::Texture, crate::DeviceError> {
1187-
let (raw, req, copy_size, wgt_view_formats, raw_flags) =
1188-
self.create_image_without_memory(desc, None)?;
1194+
let ImageWithoutMemory {
1195+
raw,
1196+
requirements,
1197+
copy_size,
1198+
view_formats,
1199+
raw_flags,
1200+
} = self.create_image_without_memory(desc, None)?;
11891201

11901202
let block = unsafe {
11911203
self.mem_allocator.lock().alloc(
11921204
&*self.shared,
11931205
gpu_alloc::Request {
1194-
size: req.size,
1195-
align_mask: req.alignment - 1,
1206+
size: requirements.size,
1207+
align_mask: requirements.alignment - 1,
11961208
usage: gpu_alloc::UsageFlags::FAST_DEVICE_ACCESS,
1197-
memory_types: req.memory_type_bits & self.valid_ash_memory_types,
1209+
memory_types: requirements.memory_type_bits & self.valid_ash_memory_types,
11981210
},
11991211
)?
12001212
};
@@ -1223,7 +1235,7 @@ impl crate::Device for super::Device {
12231235
format: desc.format,
12241236
raw_flags,
12251237
copy_size,
1226-
view_formats: wgt_view_formats,
1238+
view_formats,
12271239
})
12281240
}
12291241
unsafe fn destroy_texture(&self, texture: super::Texture) {
@@ -2687,3 +2699,11 @@ impl From<gpu_descriptor::AllocationError> for crate::DeviceError {
26872699
fn handle_unexpected(err: vk::Result) -> ! {
26882700
panic!("Unexpected Vulkan error: `{err}`")
26892701
}
2702+
2703+
struct ImageWithoutMemory {
2704+
raw: vk::Image,
2705+
requirements: vk::MemoryRequirements,
2706+
copy_size: crate::CopyExtent,
2707+
view_formats: Vec<wgt::TextureFormat>,
2708+
raw_flags: vk::ImageCreateFlags,
2709+
}

0 commit comments

Comments
 (0)