11use super :: backend:: { BufferConfig , BufferUsage , ComputeBackend } ;
2- use anyhow:: { bail , Context , Result } ;
2+ use anyhow:: { Context , Result , bail } ;
33use ash:: vk;
44use std:: ffi:: { CStr , CString } ;
55
@@ -14,7 +14,11 @@ pub struct AshBackend {
1414}
1515
1616impl AshBackend {
17- fn find_memory_type ( & self , type_filter : u32 , properties : vk:: MemoryPropertyFlags ) -> Option < u32 > {
17+ fn find_memory_type (
18+ & self ,
19+ type_filter : u32 ,
20+ properties : vk:: MemoryPropertyFlags ,
21+ ) -> Option < u32 > {
1822 for i in 0 ..self . memory_properties . memory_type_count {
1923 if ( type_filter & ( 1 << i) ) != 0
2024 && self . memory_properties . memory_types [ i as usize ]
@@ -37,10 +41,13 @@ impl AshBackend {
3741
3842 let buffer_create_info = vk:: BufferCreateInfo :: default ( )
3943 . size ( config. size )
40- . usage ( usage | vk:: BufferUsageFlags :: TRANSFER_SRC | vk:: BufferUsageFlags :: TRANSFER_DST )
44+ . usage (
45+ usage | vk:: BufferUsageFlags :: TRANSFER_SRC | vk:: BufferUsageFlags :: TRANSFER_DST ,
46+ )
4147 . sharing_mode ( vk:: SharingMode :: EXCLUSIVE ) ;
4248
43- let buffer = self . device
49+ let buffer = self
50+ . device
4451 . create_buffer ( & buffer_create_info, None )
4552 . context ( "Failed to create buffer" ) ?;
4653
@@ -57,7 +64,8 @@ impl AshBackend {
5764 . allocation_size ( memory_requirements. size )
5865 . memory_type_index ( memory_type_index) ;
5966
60- let memory = self . device
67+ let memory = self
68+ . device
6169 . allocate_memory ( & allocate_info, None )
6270 . context ( "Failed to allocate memory" ) ?;
6371
@@ -67,12 +75,13 @@ impl AshBackend {
6775
6876 // Initialize buffer if initial data provided
6977 if let Some ( data) = & config. initial_data {
70- let mapped_ptr = self . device
78+ let mapped_ptr = self
79+ . device
7180 . map_memory ( memory, 0 , config. size , vk:: MemoryMapFlags :: empty ( ) )
7281 . context ( "Failed to map memory" ) ?;
73-
82+
7483 std:: ptr:: copy_nonoverlapping ( data. as_ptr ( ) , mapped_ptr as * mut u8 , data. len ( ) ) ;
75-
84+
7685 self . device . unmap_memory ( memory) ;
7786 }
7887
@@ -85,7 +94,7 @@ impl ComputeBackend for AshBackend {
8594 fn init ( ) -> Result < Self > {
8695 unsafe {
8796 let entry = ash:: Entry :: load ( ) . context ( "Failed to load Vulkan entry" ) ?;
88-
97+
8998 // Create instance
9099 let app_info = vk:: ApplicationInfo :: default ( )
91100 . application_name ( CStr :: from_bytes_with_nul_unchecked ( b"difftest\0 " ) )
@@ -94,8 +103,8 @@ impl ComputeBackend for AshBackend {
94103 . engine_version ( vk:: make_api_version ( 0 , 1 , 0 , 0 ) )
95104 . api_version ( vk:: API_VERSION_1_2 ) ;
96105
97- let instance_create_info = vk :: InstanceCreateInfo :: default ( )
98- . application_info ( & app_info) ;
106+ let instance_create_info =
107+ vk :: InstanceCreateInfo :: default ( ) . application_info ( & app_info) ;
99108
100109 let instance = entry
101110 . create_instance ( & instance_create_info, None )
@@ -105,7 +114,7 @@ impl ComputeBackend for AshBackend {
105114 let physical_devices = instance
106115 . enumerate_physical_devices ( )
107116 . context ( "Failed to enumerate physical devices" ) ?;
108-
117+
109118 if physical_devices. is_empty ( ) {
110119 bail ! ( "No Vulkan devices found" ) ;
111120 }
@@ -114,7 +123,8 @@ impl ComputeBackend for AshBackend {
114123 let memory_properties = instance. get_physical_device_memory_properties ( physical_device) ;
115124
116125 // Find compute queue family
117- let queue_family_properties = instance. get_physical_device_queue_family_properties ( physical_device) ;
126+ let queue_family_properties =
127+ instance. get_physical_device_queue_family_properties ( physical_device) ;
118128 let queue_family_index = queue_family_properties
119129 . iter ( )
120130 . enumerate ( )
@@ -129,7 +139,7 @@ impl ComputeBackend for AshBackend {
129139 . queue_priorities ( & priorities) ;
130140
131141 let device_features = vk:: PhysicalDeviceFeatures :: default ( ) ;
132-
142+
133143 let queue_create_infos = [ queue_create_info] ;
134144 let device_create_info = vk:: DeviceCreateInfo :: default ( )
135145 . queue_create_infos ( & queue_create_infos)
@@ -196,18 +206,20 @@ impl ComputeBackend for AshBackend {
196206 . map ( |chunk| u32:: from_le_bytes ( [ chunk[ 0 ] , chunk[ 1 ] , chunk[ 2 ] , chunk[ 3 ] ] ) )
197207 . collect ( ) ;
198208
199- let shader_module_create_info = vk:: ShaderModuleCreateInfo :: default ( )
200- . code ( & spirv_u32) ;
209+ let shader_module_create_info = vk:: ShaderModuleCreateInfo :: default ( ) . code ( & spirv_u32) ;
201210
202- let shader_module = self . device
211+ let shader_module = self
212+ . device
203213 . create_shader_module ( & shader_module_create_info, None )
204214 . context ( "Failed to create shader module" ) ?;
205215
206216 // Create descriptor set layout
207217 let mut layout_bindings = Vec :: new ( ) ;
208218 for ( i, buffer) in buffers. iter ( ) . enumerate ( ) {
209219 let descriptor_type = match buffer. usage {
210- BufferUsage :: Storage | BufferUsage :: StorageReadOnly => vk:: DescriptorType :: STORAGE_BUFFER ,
220+ BufferUsage :: Storage | BufferUsage :: StorageReadOnly => {
221+ vk:: DescriptorType :: STORAGE_BUFFER
222+ }
211223 BufferUsage :: Uniform => vk:: DescriptorType :: UNIFORM_BUFFER ,
212224 } ;
213225 let binding = vk:: DescriptorSetLayoutBinding :: default ( )
@@ -218,19 +230,21 @@ impl ComputeBackend for AshBackend {
218230 layout_bindings. push ( binding) ;
219231 }
220232
221- let descriptor_set_layout_create_info = vk :: DescriptorSetLayoutCreateInfo :: default ( )
222- . bindings ( & layout_bindings) ;
233+ let descriptor_set_layout_create_info =
234+ vk :: DescriptorSetLayoutCreateInfo :: default ( ) . bindings ( & layout_bindings) ;
223235
224- let descriptor_set_layout = self . device
236+ let descriptor_set_layout = self
237+ . device
225238 . create_descriptor_set_layout ( & descriptor_set_layout_create_info, None )
226239 . context ( "Failed to create descriptor set layout" ) ?;
227240
228241 // Create pipeline layout
229242 let set_layouts = [ descriptor_set_layout] ;
230- let pipeline_layout_create_info = vk :: PipelineLayoutCreateInfo :: default ( )
231- . set_layouts ( & set_layouts) ;
243+ let pipeline_layout_create_info =
244+ vk :: PipelineLayoutCreateInfo :: default ( ) . set_layouts ( & set_layouts) ;
232245
233- let pipeline_layout = self . device
246+ let pipeline_layout = self
247+ . device
234248 . create_pipeline_layout ( & pipeline_layout_create_info, None )
235249 . context ( "Failed to create pipeline layout" ) ?;
236250
@@ -245,8 +259,13 @@ impl ComputeBackend for AshBackend {
245259 . stage ( stage_create_info)
246260 . layout ( pipeline_layout) ;
247261
248- let pipelines = self . device
249- . create_compute_pipelines ( vk:: PipelineCache :: null ( ) , & [ compute_pipeline_create_info] , None )
262+ let pipelines = self
263+ . device
264+ . create_compute_pipelines (
265+ vk:: PipelineCache :: null ( ) ,
266+ & [ compute_pipeline_create_info] ,
267+ None ,
268+ )
250269 . map_err ( |( _, e) | e)
251270 . context ( "Failed to create compute pipeline" ) ?;
252271
@@ -255,7 +274,7 @@ impl ComputeBackend for AshBackend {
255274 // Create buffers
256275 let mut vk_buffers = Vec :: new ( ) ;
257276 let mut buffer_memories = Vec :: new ( ) ;
258-
277+
259278 for buffer_config in & buffers {
260279 let ( buffer, memory) = self . create_buffer ( buffer_config) ?;
261280 vk_buffers. push ( buffer) ;
@@ -268,7 +287,8 @@ impl ComputeBackend for AshBackend {
268287 . descriptor_pool ( self . descriptor_pool )
269288 . set_layouts ( & set_layouts) ;
270289
271- let descriptor_sets = self . device
290+ let descriptor_sets = self
291+ . device
272292 . allocate_descriptor_sets ( & descriptor_set_allocate_info)
273293 . context ( "Failed to allocate descriptor sets" ) ?;
274294
@@ -292,7 +312,9 @@ impl ComputeBackend for AshBackend {
292312 . enumerate ( )
293313 . map ( |( i, ( buffer_info, config) ) | {
294314 let descriptor_type = match config. usage {
295- BufferUsage :: Storage | BufferUsage :: StorageReadOnly => vk:: DescriptorType :: STORAGE_BUFFER ,
315+ BufferUsage :: Storage | BufferUsage :: StorageReadOnly => {
316+ vk:: DescriptorType :: STORAGE_BUFFER
317+ }
296318 BufferUsage :: Uniform => vk:: DescriptorType :: UNIFORM_BUFFER ,
297319 } ;
298320
@@ -312,10 +334,11 @@ impl ComputeBackend for AshBackend {
312334 . level ( vk:: CommandBufferLevel :: PRIMARY )
313335 . command_buffer_count ( 1 ) ;
314336
315- let command_buffers = self . device
337+ let command_buffers = self
338+ . device
316339 . allocate_command_buffers ( & command_buffer_allocate_info)
317340 . context ( "Failed to allocate command buffer" ) ?;
318-
341+
319342 let command_buffer = command_buffers[ 0 ] ;
320343
321344 // Begin command buffer
@@ -327,11 +350,8 @@ impl ComputeBackend for AshBackend {
327350 . context ( "Failed to begin command buffer" ) ?;
328351
329352 // Bind pipeline and descriptor set
330- self . device . cmd_bind_pipeline (
331- command_buffer,
332- vk:: PipelineBindPoint :: COMPUTE ,
333- pipeline,
334- ) ;
353+ self . device
354+ . cmd_bind_pipeline ( command_buffer, vk:: PipelineBindPoint :: COMPUTE , pipeline) ;
335355
336356 self . device . cmd_bind_descriptor_sets (
337357 command_buffer,
@@ -343,7 +363,8 @@ impl ComputeBackend for AshBackend {
343363 ) ;
344364
345365 // Dispatch compute
346- self . device . cmd_dispatch ( command_buffer, dispatch[ 0 ] , dispatch[ 1 ] , dispatch[ 2 ] ) ;
366+ self . device
367+ . cmd_dispatch ( command_buffer, dispatch[ 0 ] , dispatch[ 1 ] , dispatch[ 2 ] ) ;
347368
348369 // End command buffer
349370 self . device
@@ -352,8 +373,7 @@ impl ComputeBackend for AshBackend {
352373
353374 // Submit command buffer
354375 let command_buffers = [ command_buffer] ;
355- let submit_info = vk:: SubmitInfo :: default ( )
356- . command_buffers ( & command_buffers) ;
376+ let submit_info = vk:: SubmitInfo :: default ( ) . command_buffers ( & command_buffers) ;
357377
358378 self . device
359379 . queue_submit ( self . queue , & [ submit_info] , vk:: Fence :: null ( ) )
@@ -368,27 +388,34 @@ impl ComputeBackend for AshBackend {
368388 let mut results = Vec :: new ( ) ;
369389 for ( _i, ( memory, config) ) in buffer_memories. iter ( ) . zip ( & buffers) . enumerate ( ) {
370390 let mut data = vec ! [ 0u8 ; config. size as usize ] ;
371-
372- let mapped_ptr = self . device
391+
392+ let mapped_ptr = self
393+ . device
373394 . map_memory ( * memory, 0 , config. size , vk:: MemoryMapFlags :: empty ( ) )
374395 . context ( "Failed to map memory for reading" ) ?;
375-
376- std:: ptr:: copy_nonoverlapping ( mapped_ptr as * const u8 , data. as_mut_ptr ( ) , config. size as usize ) ;
377-
396+
397+ std:: ptr:: copy_nonoverlapping (
398+ mapped_ptr as * const u8 ,
399+ data. as_mut_ptr ( ) ,
400+ config. size as usize ,
401+ ) ;
402+
378403 self . device . unmap_memory ( * memory) ;
379-
404+
380405 results. push ( data) ;
381406 }
382407
383408 // Clean up
384- self . device . free_command_buffers ( self . command_pool , & [ command_buffer] ) ;
409+ self . device
410+ . free_command_buffers ( self . command_pool , & [ command_buffer] ) ;
385411 for ( buffer, memory) in vk_buffers. iter ( ) . zip ( & buffer_memories) {
386412 self . device . destroy_buffer ( * buffer, None ) ;
387413 self . device . free_memory ( * memory, None ) ;
388414 }
389415 self . device . destroy_pipeline ( pipeline, None ) ;
390416 self . device . destroy_pipeline_layout ( pipeline_layout, None ) ;
391- self . device . destroy_descriptor_set_layout ( descriptor_set_layout, None ) ;
417+ self . device
418+ . destroy_descriptor_set_layout ( descriptor_set_layout, None ) ;
392419 self . device . destroy_shader_module ( shader_module, None ) ;
393420
394421 Ok ( results)
@@ -399,10 +426,11 @@ impl ComputeBackend for AshBackend {
399426impl Drop for AshBackend {
400427 fn drop ( & mut self ) {
401428 unsafe {
402- self . device . destroy_descriptor_pool ( self . descriptor_pool , None ) ;
429+ self . device
430+ . destroy_descriptor_pool ( self . descriptor_pool , None ) ;
403431 self . device . destroy_command_pool ( self . command_pool , None ) ;
404432 self . device . destroy_device ( None ) ;
405433 self . instance . destroy_instance ( None ) ;
406434 }
407435 }
408- }
436+ }
0 commit comments