@@ -36,7 +36,15 @@ mod semaphore_list;
3636pub use adapter:: PhysicalDeviceFeatures ;
3737
3838use alloc:: { boxed:: Box , ffi:: CString , sync:: Arc , vec:: Vec } ;
39- use core:: { borrow:: Borrow , ffi:: CStr , fmt, marker:: PhantomData , mem, num:: NonZeroU32 } ;
39+ use core:: {
40+ borrow:: Borrow ,
41+ ffi:: CStr ,
42+ fmt,
43+ marker:: PhantomData ,
44+ mem,
45+ num:: NonZeroU32 ,
46+ sync:: atomic:: { AtomicU64 , Ordering } ,
47+ } ;
4048
4149use arrayvec:: ArrayVec ;
4250use ash:: { ext, khr, vk} ;
@@ -651,6 +659,9 @@ struct DeviceShared {
651659 render_passes : Mutex < FastHashMap < RenderPassKey , vk:: RenderPass > > ,
652660 sampler_cache : Mutex < sampler:: SamplerCache > ,
653661 memory_allocations_counter : InternalCounter ,
662+
663+ texture_identity_factory : ResourceIdentityFactory < vk:: Image > ,
664+ texture_view_identity_factory : ResourceIdentityFactory < vk:: ImageView > ,
654665}
655666
656667impl Drop for DeviceShared {
@@ -864,6 +875,7 @@ pub struct Texture {
864875 block : Option < gpu_alloc:: MemoryBlock < vk:: DeviceMemory > > ,
865876 format : wgt:: TextureFormat ,
866877 copy_size : crate :: CopyExtent ,
878+ identity : ResourceIdentity < vk:: Image > ,
867879}
868880
869881impl crate :: DynTexture for Texture { }
@@ -886,6 +898,8 @@ pub struct TextureView {
886898 raw_format : vk:: Format ,
887899 base_mip_level : u32 ,
888900 dimension : wgt:: TextureViewDimension ,
901+ texture_identity : ResourceIdentity < vk:: Image > ,
902+ view_identity : ResourceIdentity < vk:: ImageView > ,
889903}
890904
891905impl crate :: DynTextureView for TextureView { }
@@ -897,6 +911,13 @@ impl TextureView {
897911 pub unsafe fn raw_handle ( & self ) -> vk:: ImageView {
898912 self . raw
899913 }
914+
915+ fn identified_raw_view ( & self ) -> IdentifiedTextureView {
916+ IdentifiedTextureView {
917+ raw : self . raw ,
918+ identity : self . view_identity ,
919+ }
920+ }
900921}
901922
902923#[ derive( Debug ) ]
@@ -956,16 +977,59 @@ impl Temp {
956977 }
957978}
958979
980+ struct ResourceIdentityFactory < T > {
981+ next_id : AtomicU64 ,
982+ _phantom : PhantomData < T > ,
983+ }
984+
985+ impl < T > ResourceIdentityFactory < T > {
986+ fn new ( ) -> Self {
987+ Self {
988+ next_id : AtomicU64 :: new ( 1 ) ,
989+ _phantom : PhantomData ,
990+ }
991+ }
992+
993+ /// Returns a new unique ID for a resource of type `T`.
994+ fn next ( & self ) -> ResourceIdentity < T > {
995+ ResourceIdentity {
996+ id : self . next_id . fetch_add ( 1 , Ordering :: Relaxed ) ,
997+ _phantom : PhantomData ,
998+ }
999+ }
1000+ }
1001+
1002+ #[ derive( Debug , Copy , Clone , Eq , Hash , PartialEq ) ]
1003+ struct ResourceIdentity < T > {
1004+ id : u64 ,
1005+ _phantom : PhantomData < T > ,
1006+ }
1007+
9591008#[ derive( Clone , Eq , Hash , PartialEq ) ]
9601009struct FramebufferKey {
9611010 raw_pass : vk:: RenderPass ,
962- attachments : ArrayVec < vk:: ImageView , { MAX_TOTAL_ATTACHMENTS } > ,
1011+ attachment_identities : ArrayVec < ResourceIdentity < vk:: ImageView > , { MAX_TOTAL_ATTACHMENTS } > ,
1012+ attachment_views : ArrayVec < vk:: ImageView , { MAX_TOTAL_ATTACHMENTS } > ,
9631013 extent : wgt:: Extent3d ,
9641014}
9651015
1016+ impl FramebufferKey {
1017+ fn push_view ( & mut self , view : IdentifiedTextureView ) {
1018+ self . attachment_identities . push ( view. identity ) ;
1019+ self . attachment_views . push ( view. raw ) ;
1020+ }
1021+ }
1022+
1023+ #[ derive( Copy , Clone ) ]
1024+ struct IdentifiedTextureView {
1025+ raw : vk:: ImageView ,
1026+ identity : ResourceIdentity < vk:: ImageView > ,
1027+ }
1028+
9661029#[ derive( Clone , Eq , Hash , PartialEq ) ]
9671030struct TempTextureViewKey {
9681031 texture : vk:: Image ,
1032+ texture_identity : ResourceIdentity < vk:: Image > ,
9691033 format : vk:: Format ,
9701034 mip_level : u32 ,
9711035 depth_slice : u32 ,
@@ -1008,7 +1072,7 @@ pub struct CommandEncoder {
10081072 end_of_pass_timer_query : Option < ( vk:: QueryPool , u32 ) > ,
10091073
10101074 framebuffers : FastHashMap < FramebufferKey , vk:: Framebuffer > ,
1011- temp_texture_views : FastHashMap < TempTextureViewKey , vk :: ImageView > ,
1075+ temp_texture_views : FastHashMap < TempTextureViewKey , IdentifiedTextureView > ,
10121076
10131077 counters : Arc < wgt:: HalCounters > ,
10141078}
@@ -1037,7 +1101,7 @@ impl Drop for CommandEncoder {
10371101 }
10381102
10391103 for ( _, view) in self . temp_texture_views . drain ( ) {
1040- unsafe { self . device . raw . destroy_image_view ( view, None ) } ;
1104+ unsafe { self . device . raw . destroy_image_view ( view. raw , None ) } ;
10411105 }
10421106
10431107 self . counters . command_encoders . sub ( 1 ) ;
0 commit comments