Skip to content

Commit f22e16a

Browse files
committed
unify vk_hal::Texture::{block, external_memory} in enum TextureMemory
Instead of having two std::Option<> types that are logically mutually exclusive, this unifies them in an enum. As a result, vk::Device::texture_from_raw() becomes more generic and can be used more consistently in the wgpu-hal::vulkan implementation, which also fixes inconsistent usage of the "textures" counter.
1 parent efca3f5 commit f22e16a

File tree

3 files changed

+45
-69
lines changed

3 files changed

+45
-69
lines changed

wgpu-hal/src/vulkan/device.rs

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -488,41 +488,27 @@ impl super::Device {
488488
/// - If `drop_callback` is [`None`], wgpu-hal will take ownership of `vk_image`. If
489489
/// `drop_callback` is [`Some`], `vk_image` must be valid until the callback is called.
490490
/// - If the `ImageCreateFlags` does not contain `MUTABLE_FORMAT`, the `view_formats` of `desc` must be empty.
491-
/// - If `external_memory` is [`Some`], wgpu-hal will take ownership of the memory (which is presumed to back
492-
/// `vk_image`). If `external_memory` is [`None`], the memory must be valid until `drop_callback` is called.
491+
/// - If `external_memory` is not [`TextureMemory::External`], wgpu-hal will take ownership of the memory
492+
/// (which is presumed to back `vk_image`). Otherwise, the memory must remain valid until `drop_callback`
493+
/// is called.
493494
pub unsafe fn texture_from_raw(
494495
&self,
495496
vk_image: vk::Image,
496497
desc: &crate::TextureDescriptor,
497498
drop_callback: Option<crate::DropCallback>,
498-
external_memory: Option<vk::DeviceMemory>,
499+
memory: super::TextureMemory,
499500
) -> super::Texture {
500-
let mut raw_flags = vk::ImageCreateFlags::empty();
501-
let mut view_formats = vec![];
502-
for tf in desc.view_formats.iter() {
503-
if *tf == desc.format {
504-
continue;
505-
}
506-
view_formats.push(*tf);
507-
}
508-
if !view_formats.is_empty() {
509-
raw_flags |=
510-
vk::ImageCreateFlags::MUTABLE_FORMAT | vk::ImageCreateFlags::EXTENDED_USAGE;
511-
view_formats.push(desc.format)
512-
}
513-
if desc.format.is_multi_planar_format() {
514-
raw_flags |= vk::ImageCreateFlags::MUTABLE_FORMAT;
515-
}
516-
517501
let identity = self.shared.texture_identity_factory.next();
518-
519502
let drop_guard = crate::DropGuard::from_option(drop_callback);
520503

504+
if let Some(label) = desc.label {
505+
unsafe { self.shared.set_object_name(vk_image, label) };
506+
}
507+
521508
super::Texture {
522509
raw: vk_image,
523510
drop_guard,
524-
external_memory,
525-
block: None,
511+
memory,
526512
format: desc.format,
527513
copy_size: desc.copy_extent(),
528514
identity,
@@ -634,7 +620,6 @@ impl super::Device {
634620
Ok(ImageWithoutMemory {
635621
raw,
636622
requirements: req,
637-
copy_size,
638623
})
639624
}
640625

@@ -695,22 +680,13 @@ impl super::Device {
695680
unsafe { self.shared.raw.bind_image_memory(image.raw, memory, 0) }
696681
.map_err(super::map_host_device_oom_err)?;
697682

698-
if let Some(label) = desc.label {
699-
unsafe { self.shared.set_object_name(image.raw, label) };
700-
}
701-
702-
let identity = self.shared.texture_identity_factory.next();
703-
704-
self.counters.textures.add(1);
705-
706-
Ok(super::Texture {
707-
raw: image.raw,
708-
drop_guard: None,
709-
external_memory: Some(memory),
710-
block: None,
711-
format: desc.format,
712-
copy_size: image.copy_size,
713-
identity,
683+
Ok(unsafe {
684+
self.texture_from_raw(
685+
image.raw,
686+
desc,
687+
None,
688+
super::TextureMemory::Dedicated(memory),
689+
)
714690
})
715691
}
716692

@@ -1186,35 +1162,25 @@ impl crate::Device for super::Device {
11861162
unsafe { self.shared.raw.destroy_image(image.raw, None) };
11871163
})?;
11881164

1189-
if let Some(label) = desc.label {
1190-
unsafe { self.shared.set_object_name(image.raw, label) };
1191-
}
1192-
1193-
let identity = self.shared.texture_identity_factory.next();
1194-
1195-
self.counters.textures.add(1);
1196-
1197-
Ok(super::Texture {
1198-
raw: image.raw,
1199-
drop_guard: None,
1200-
external_memory: None,
1201-
block: Some(block),
1202-
format: desc.format,
1203-
copy_size: image.copy_size,
1204-
identity,
1165+
Ok(unsafe {
1166+
self.texture_from_raw(image.raw, desc, None, super::TextureMemory::Block(block))
12051167
})
12061168
}
1169+
12071170
unsafe fn destroy_texture(&self, texture: super::Texture) {
12081171
if texture.drop_guard.is_none() {
12091172
unsafe { self.shared.raw.destroy_image(texture.raw, None) };
12101173
}
1211-
if let Some(memory) = texture.external_memory {
1212-
unsafe { self.shared.raw.free_memory(memory, None) };
1213-
}
1214-
if let Some(block) = texture.block {
1215-
self.counters.texture_memory.sub(block.size() as isize);
12161174

1217-
unsafe { self.mem_allocator.lock().dealloc(&*self.shared, block) };
1175+
match texture.memory {
1176+
super::TextureMemory::Block(block) => unsafe {
1177+
self.counters.texture_memory.sub(block.size() as isize);
1178+
self.mem_allocator.lock().dealloc(&*self.shared, block);
1179+
},
1180+
super::TextureMemory::Dedicated(memory) => unsafe {
1181+
self.shared.raw.free_memory(memory, None);
1182+
},
1183+
super::TextureMemory::External => {}
12181184
}
12191185

12201186
self.counters.textures.sub(1);
@@ -2879,5 +2845,4 @@ fn handle_unexpected(err: vk::Result) -> ! {
28792845
struct ImageWithoutMemory {
28802846
raw: vk::Image,
28812847
requirements: vk::MemoryRequirements,
2882-
copy_size: crate::CopyExtent,
28832848
}

wgpu-hal/src/vulkan/mod.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,23 @@ pub struct AccelerationStructure {
695695

696696
impl crate::DynAccelerationStructure for AccelerationStructure {}
697697

698+
#[derive(Debug)]
699+
pub enum TextureMemory {
700+
// shared memory in GPU allocator (owned by wgpu-hal)
701+
Block(gpu_alloc::MemoryBlock<vk::DeviceMemory>),
702+
703+
// dedicated memory (owned by wgpu-hal)
704+
Dedicated(vk::DeviceMemory),
705+
706+
// memory not owned by wgpu
707+
External,
708+
}
709+
698710
#[derive(Debug)]
699711
pub struct Texture {
700712
raw: vk::Image,
701-
external_memory: Option<vk::DeviceMemory>,
702-
block: Option<gpu_alloc::MemoryBlock<vk::DeviceMemory>>,
713+
memory: TextureMemory,
714+
703715
format: wgt::TextureFormat,
704716
copy_size: crate::CopyExtent,
705717
identity: ResourceIdentity<vk::Image>,
@@ -722,8 +734,8 @@ impl Texture {
722734
/// # Safety
723735
///
724736
/// - The external memory must not be manually freed
725-
pub unsafe fn external_memory(&self) -> Option<vk::DeviceMemory> {
726-
self.external_memory
737+
pub unsafe fn memory(&self) -> &TextureMemory {
738+
&self.memory
727739
}
728740
}
729741

wgpu-hal/src/vulkan/swapchain/native.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,7 @@ impl Swapchain for NativeSwapchain {
498498
texture: crate::vulkan::Texture {
499499
raw: self.images[index as usize],
500500
drop_guard: None,
501-
block: None,
502-
external_memory: None,
501+
memory: crate::vulkan::TextureMemory::External,
503502
format: self.config.format,
504503
copy_size: crate::CopyExtent {
505504
width: self.config.extent.width,

0 commit comments

Comments
 (0)