@@ -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,22 +684,13 @@ 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,
687+ Ok ( unsafe {
688+ self . texture_from_raw (
689+ image. raw ,
690+ desc,
691+ None ,
692+ super :: TextureMemory :: Dedicated ( memory) ,
693+ )
714694 } )
715695 }
716696
@@ -1186,38 +1166,27 @@ impl crate::Device for super::Device {
11861166 unsafe { self . shared . raw . destroy_image ( image. raw , None ) } ;
11871167 } ) ?;
11881168
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,
1169+ Ok ( unsafe {
1170+ self . texture_from_raw ( image. raw , desc, None , super :: TextureMemory :: Block ( block) )
12051171 } )
12061172 }
1173+
12071174 unsafe fn destroy_texture ( & self , texture : super :: Texture ) {
12081175 if texture. drop_guard . is_none ( ) {
12091176 unsafe { self . shared . raw . destroy_image ( texture. raw , None ) } ;
1177+ self . counters . textures . sub ( 1 ) ;
12101178 }
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 ) ;
12161179
1217- unsafe { self . mem_allocator . lock ( ) . dealloc ( & * self . shared , block) } ;
1180+ match texture. memory {
1181+ super :: TextureMemory :: Block ( block) => unsafe {
1182+ self . counters . texture_memory . sub ( block. size ( ) as isize ) ;
1183+ self . mem_allocator . lock ( ) . dealloc ( & * self . shared , block) ;
1184+ } ,
1185+ super :: TextureMemory :: Dedicated ( memory) => unsafe {
1186+ self . shared . raw . free_memory ( memory, None ) ;
1187+ } ,
1188+ super :: TextureMemory :: External => { }
12181189 }
1219-
1220- self . counters . textures . sub ( 1 ) ;
12211190 }
12221191
12231192 unsafe fn add_raw_texture ( & self , _texture : & super :: Texture ) {
@@ -2879,5 +2848,4 @@ fn handle_unexpected(err: vk::Result) -> ! {
28792848struct ImageWithoutMemory {
28802849 raw : vk:: Image ,
28812850 requirements : vk:: MemoryRequirements ,
2882- copy_size : crate :: CopyExtent ,
28832851}
0 commit comments