@@ -808,17 +808,6 @@ impl super::Device {
808
808
} )
809
809
}
810
810
811
- /// # Safety
812
- ///
813
- /// - `vk_buffer`'s memory must be managed by the caller
814
- /// - Externally imported buffers can't be mapped by `wgpu`
815
- pub unsafe fn buffer_from_raw ( vk_buffer : vk:: Buffer ) -> super :: Buffer {
816
- super :: Buffer {
817
- raw : vk_buffer,
818
- block : None ,
819
- }
820
- }
821
-
822
811
fn create_shader_module_impl (
823
812
& self ,
824
813
spv : & [ u32 ] ,
@@ -1153,15 +1142,22 @@ impl crate::Device for super::Device {
1153
1142
1154
1143
Ok ( super :: Buffer {
1155
1144
raw,
1156
- block : Some ( Mutex :: new ( block) ) ,
1145
+ block : Some ( Mutex :: new ( super :: BufferMemoryBacking :: Managed ( block) ) ) ,
1157
1146
} )
1158
1147
}
1159
1148
unsafe fn destroy_buffer ( & self , buffer : super :: Buffer ) {
1160
1149
unsafe { self . shared . raw . destroy_buffer ( buffer. raw , None ) } ;
1161
1150
if let Some ( block) = buffer. block {
1162
1151
let block = block. into_inner ( ) ;
1163
1152
self . counters . buffer_memory . sub ( block. size ( ) as isize ) ;
1164
- unsafe { self . mem_allocator . lock ( ) . dealloc ( & * self . shared , block) } ;
1153
+ match block {
1154
+ super :: BufferMemoryBacking :: Managed ( block) => unsafe {
1155
+ self . mem_allocator . lock ( ) . dealloc ( & * self . shared , block)
1156
+ } ,
1157
+ super :: BufferMemoryBacking :: VulkanMemory { memory, .. } => unsafe {
1158
+ self . shared . raw . free_memory ( memory, None ) ;
1159
+ } ,
1160
+ }
1165
1161
}
1166
1162
1167
1163
self . counters . buffers . sub ( 1 ) ;
@@ -1179,18 +1175,27 @@ impl crate::Device for super::Device {
1179
1175
if let Some ( ref block) = buffer. block {
1180
1176
let size = range. end - range. start ;
1181
1177
let mut block = block. lock ( ) ;
1182
- let ptr = unsafe { block. map ( & * self . shared , range. start , size as usize ) ? } ;
1183
- let is_coherent = block
1184
- . props ( )
1185
- . contains ( gpu_alloc:: MemoryPropertyFlags :: HOST_COHERENT ) ;
1186
- Ok ( crate :: BufferMapping { ptr, is_coherent } )
1178
+ if let super :: BufferMemoryBacking :: Managed ( ref mut block) = * block {
1179
+ let ptr = unsafe { block. map ( & * self . shared , range. start , size as usize ) ? } ;
1180
+ let is_coherent = block
1181
+ . props ( )
1182
+ . contains ( gpu_alloc:: MemoryPropertyFlags :: HOST_COHERENT ) ;
1183
+ Ok ( crate :: BufferMapping { ptr, is_coherent } )
1184
+ } else {
1185
+ crate :: hal_usage_error ( "tried to map externally created buffer" )
1186
+ }
1187
1187
} else {
1188
1188
crate :: hal_usage_error ( "tried to map external buffer" )
1189
1189
}
1190
1190
}
1191
1191
unsafe fn unmap_buffer ( & self , buffer : & super :: Buffer ) {
1192
1192
if let Some ( ref block) = buffer. block {
1193
- unsafe { block. lock ( ) . unmap ( & * self . shared ) } ;
1193
+ match & mut * block. lock ( ) {
1194
+ super :: BufferMemoryBacking :: Managed ( block) => unsafe { block. unmap ( & * self . shared ) } ,
1195
+ super :: BufferMemoryBacking :: VulkanMemory { .. } => {
1196
+ crate :: hal_usage_error ( "tried to unmap externally created buffer" )
1197
+ }
1198
+ } ;
1194
1199
} else {
1195
1200
crate :: hal_usage_error ( "tried to unmap external buffer" )
1196
1201
}
0 commit comments