Skip to content

Commit ba5436e

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 ba5436e

File tree

3 files changed

+49
-72
lines changed

3 files changed

+49
-72
lines changed

wgpu-hal/src/vulkan/device.rs

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -488,41 +488,31 @@ 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 drop_guard.is_some() {
505+
self.counters.textures.add(1);
506+
}
507+
508+
if let Some(label) = desc.label {
509+
unsafe { self.shared.set_object_name(vk_image, label) };
510+
}
511+
521512
super::Texture {
522513
raw: vk_image,
523514
drop_guard,
524-
external_memory,
525-
block: None,
515+
memory,
526516
format: desc.format,
527517
copy_size: desc.copy_extent(),
528518
identity,
@@ -634,7 +624,6 @@ impl super::Device {
634624
Ok(ImageWithoutMemory {
635625
raw,
636626
requirements: req,
637-
copy_size,
638627
})
639628
}
640629

@@ -695,23 +684,12 @@ impl super::Device {
695684
unsafe { self.shared.raw.bind_image_memory(image.raw, memory, 0) }
696685
.map_err(super::map_host_device_oom_err)?;
697686

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,
714-
})
687+
Ok(self.texture_from_raw(
688+
image.raw,
689+
desc,
690+
None,
691+
super::TextureMemory::Dedicated(memory),
692+
))
715693
}
716694

717695
fn create_shader_module_impl(
@@ -1186,38 +1164,27 @@ impl crate::Device for super::Device {
11861164
unsafe { self.shared.raw.destroy_image(image.raw, None) };
11871165
})?;
11881166

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,
1167+
Ok(unsafe {
1168+
self.texture_from_raw(image.raw, desc, None, super::TextureMemory::Block(block))
12051169
})
12061170
}
1171+
12071172
unsafe fn destroy_texture(&self, texture: super::Texture) {
12081173
if texture.drop_guard.is_none() {
12091174
unsafe { self.shared.raw.destroy_image(texture.raw, None) };
1175+
self.counters.textures.sub(1);
12101176
}
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);
12161177

1217-
unsafe { self.mem_allocator.lock().dealloc(&*self.shared, block) };
1178+
match texture.memory {
1179+
super::TextureMemory::Block(block) => unsafe {
1180+
self.counters.texture_memory.sub(block.size() as isize);
1181+
self.mem_allocator.lock().dealloc(&*self.shared, block);
1182+
},
1183+
super::TextureMemory::Dedicated(memory) => unsafe {
1184+
self.shared.raw.free_memory(memory, None);
1185+
},
1186+
super::TextureMemory::External => {}
12181187
}
1219-
1220-
self.counters.textures.sub(1);
12211188
}
12221189

12231190
unsafe fn add_raw_texture(&self, _texture: &super::Texture) {
@@ -2879,5 +2846,4 @@ fn handle_unexpected(err: vk::Result) -> ! {
28792846
struct ImageWithoutMemory {
28802847
raw: vk::Image,
28812848
requirements: vk::MemoryRequirements,
2882-
copy_size: crate::CopyExtent,
28832849
}

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)