Debian Codegraph @

third_party/rust/gpu-allocator/examples/vulkan-buffer.rs

raw
1 use std::default::Default;
2
3 use ash::vk;
4 use gpu_allocator::{
5 vulkan::{AllocationCreateDesc, AllocationScheme, Allocator, AllocatorCreateDesc},
6 MemoryLocation,
7 };
8 use log::info;
9
10 fn main() {
11 env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
12
13 let entry = unsafe { ash::Entry::load() }.unwrap();
14
15 // Create Vulkan instance
16 let instance = {
17 let app_name = c"Vulkan gpu-allocator test";
18
19 let appinfo = vk::ApplicationInfo::default()
20 .application_name(app_name)
21 .application_version(0)
22 .engine_name(app_name)
23 .engine_version(0)
24 .api_version(vk::make_api_version(0, 1, 0, 0));
25
26 let layer_names_raw = [c"VK_LAYER_KHRONOS_validation".as_ptr()];
27
28 let create_info = vk::InstanceCreateInfo::default()
29 .application_info(&appinfo)
30 .enabled_layer_names(&layer_names_raw);
31
32 unsafe {
33 entry
34 .create_instance(&create_info, None)
35 .expect("Instance creation error")
36 }
37 };
38
39 // Look for vulkan physical device
40 let (pdevice, queue_family_index) = {
41 let pdevices = unsafe {
42 instance
43 .enumerate_physical_devices()
44 .expect("Physical device error")
45 };
46 pdevices
47 .iter()
48 .find_map(|pdevice| {
49 unsafe { instance.get_physical_device_queue_family_properties(*pdevice) }
50 .iter()
51 .enumerate()
52 .find_map(|(index, &info)| {
53 let supports_graphics = info.queue_flags.contains(vk::QueueFlags::GRAPHICS);
54 if supports_graphics {
55 Some((*pdevice, index))
56 } else {
57 None
58 }
59 })
60 })
61 .expect("Couldn't find suitable device.")
62 };
63
64 // Create vulkan device
65 let device = {
66 let device_extension_names_raw = vec![];
67 let features = vk::PhysicalDeviceFeatures {
68 shader_clip_distance: 1,
69 ..Default::default()
70 };
71 let priorities = [1.0];
72
73 let queue_info = vk::DeviceQueueCreateInfo::default()
74 .queue_family_index(queue_family_index as u32)
75 .queue_priorities(&priorities);
76
77 let create_info = vk::DeviceCreateInfo::default()
78 .queue_create_infos(std::slice::from_ref(&queue_info))
79 .enabled_extension_names(&device_extension_names_raw)
80 .enabled_features(&features);
81
82 unsafe { instance.create_device(pdevice, &create_info, None).unwrap() }
83 };
84
85 // Setting up the allocator
86 let mut allocator = Allocator::new(&AllocatorCreateDesc {
87 instance: instance.clone(),
88 device: device.clone(),
89 physical_device: pdevice,
90 debug_settings: Default::default(),
91 buffer_device_address: false,
92 allocation_sizes: Default::default(),
93 })
94 .unwrap();
95
96 // Test allocating Gpu Only memory
97 {
98 let test_buffer_info = vk::BufferCreateInfo::default()
99 .size(512)
100 .usage(vk::BufferUsageFlags::STORAGE_BUFFER)
101 .sharing_mode(vk::SharingMode::EXCLUSIVE);
102 let test_buffer = unsafe { device.create_buffer(&test_buffer_info, None) }.unwrap();
103 let requirements = unsafe { device.get_buffer_memory_requirements(test_buffer) };
104 let location = MemoryLocation::GpuOnly;
105
106 let allocation = allocator
107 .allocate(&AllocationCreateDesc {
108 requirements,
109 location,
110 linear: true,
111 allocation_scheme: AllocationScheme::GpuAllocatorManaged,
112 name: "Test allocation (Gpu Only)",
113 })
114 .unwrap();
115
116 unsafe {
117 device
118 .bind_buffer_memory(test_buffer, allocation.memory(), allocation.offset())
119 .unwrap()
120 };
121
122 allocator.free(allocation).unwrap();
123
124 unsafe { device.destroy_buffer(test_buffer, None) };
125
126 info!("Allocation and deallocation of GpuOnly memory was successful.");
127 }
128
129 // Test allocating Cpu to Gpu memory
130 {
131 let test_buffer_info = vk::BufferCreateInfo::default()
132 .size(512)
133 .usage(vk::BufferUsageFlags::STORAGE_BUFFER)
134 .sharing_mode(vk::SharingMode::EXCLUSIVE);
135 let test_buffer = unsafe { device.create_buffer(&test_buffer_info, None) }.unwrap();
136 let requirements = unsafe { device.get_buffer_memory_requirements(test_buffer) };
137 let location = MemoryLocation::CpuToGpu;
138
139 let allocation = allocator
140 .allocate(&AllocationCreateDesc {
141 requirements,
142 location,
143 linear: true,
144 allocation_scheme: AllocationScheme::GpuAllocatorManaged,
145 name: "Test allocation (Cpu to Gpu)",
146 })
147 .unwrap();
148
149 unsafe {
150 device
151 .bind_buffer_memory(test_buffer, allocation.memory(), allocation.offset())
152 .unwrap()
153 };
154
155 allocator.free(allocation).unwrap();
156
157 unsafe { device.destroy_buffer(test_buffer, None) };
158
159 info!("Allocation and deallocation of CpuToGpu memory was successful.");
160 }
161
162 // Test allocating Gpu to Cpu memory
163 {
164 let test_buffer_info = vk::BufferCreateInfo::default()
165 .size(512)
166 .usage(vk::BufferUsageFlags::STORAGE_BUFFER)
167 .sharing_mode(vk::SharingMode::EXCLUSIVE);
168 let test_buffer = unsafe { device.create_buffer(&test_buffer_info, None) }.unwrap();
169 let requirements = unsafe { device.get_buffer_memory_requirements(test_buffer) };
170 let location = MemoryLocation::GpuToCpu;
171
172 let allocation = allocator
173 .allocate(&AllocationCreateDesc {
174 requirements,
175 location,
176 linear: true,
177 allocation_scheme: AllocationScheme::GpuAllocatorManaged,
178 name: "Test allocation (Gpu to Cpu)",
179 })
180 .unwrap();
181
182 unsafe {
183 device
184 .bind_buffer_memory(test_buffer, allocation.memory(), allocation.offset())
185 .unwrap()
186 };
187
188 allocator.free(allocation).unwrap();
189
190 unsafe { device.destroy_buffer(test_buffer, None) };
191
192 info!("Allocation and deallocation of GpuToCpu memory was successful.");
193 }
194
195 drop(allocator); // Explicitly drop before destruction of device and instance.
196 unsafe { device.destroy_device(None) };
197 unsafe { instance.destroy_instance(None) };
198 }