blob: 832859420c1f3249d58edd21d5beea8cab31f6e1 [file] [log] [blame]
Jason Macnakbe603432021-08-30 13:58:30 -07001use crate::device::Device;
2use crate::prelude::*;
3use crate::vk;
4use crate::RawPtr;
5use std::mem;
6use std::os::raw::c_char;
7use std::ptr;
8
9#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkInstance.html>"]
10#[derive(Clone)]
11pub struct Instance {
12 pub(crate) handle: vk::Instance,
13
14 pub(crate) instance_fn_1_0: vk::InstanceFnV1_0,
15 pub(crate) instance_fn_1_1: vk::InstanceFnV1_1,
16 pub(crate) instance_fn_1_2: vk::InstanceFnV1_2,
17}
18
19impl Instance {
20 pub unsafe fn load(static_fn: &vk::StaticFn, instance: vk::Instance) -> Self {
21 let load_fn = |name: &std::ffi::CStr| {
22 mem::transmute(static_fn.get_instance_proc_addr(instance, name.as_ptr()))
23 };
24
25 Instance {
26 handle: instance,
27
28 instance_fn_1_0: vk::InstanceFnV1_0::load(load_fn),
29 instance_fn_1_1: vk::InstanceFnV1_1::load(load_fn),
30 instance_fn_1_2: vk::InstanceFnV1_2::load(load_fn),
31 }
32 }
33
34 pub fn handle(&self) -> vk::Instance {
35 self.handle
36 }
37}
38
39/// Vulkan core 1.2
40#[allow(non_camel_case_types)]
41impl Instance {
42 pub fn fp_v1_2(&self) -> &vk::InstanceFnV1_2 {
43 &self.instance_fn_1_2
44 }
45}
46
47/// Vulkan core 1.1
48#[allow(non_camel_case_types)]
49impl Instance {
50 pub fn fp_v1_1(&self) -> &vk::InstanceFnV1_1 {
51 &self.instance_fn_1_1
52 }
53
54 pub unsafe fn enumerate_physical_device_groups_len(&self) -> VkResult<usize> {
55 let mut group_count = 0;
56 self.instance_fn_1_1
57 .enumerate_physical_device_groups(self.handle(), &mut group_count, ptr::null_mut())
58 .result_with_success(group_count as usize)
59 }
60
61 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumeratePhysicalDeviceGroups.html>"]
62 pub fn enumerate_physical_device_groups(
63 &self,
64 out: &mut [vk::PhysicalDeviceGroupProperties],
65 ) -> VkResult<()> {
66 unsafe {
67 let mut group_count = out.len() as u32;
68 self.instance_fn_1_1
69 .enumerate_physical_device_groups(self.handle(), &mut group_count, out.as_mut_ptr())
70 .into()
71 }
72 }
73
74 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceFeatures2.html>"]
75 pub unsafe fn get_physical_device_features2(
76 &self,
77 physical_device: vk::PhysicalDevice,
78 features: &mut vk::PhysicalDeviceFeatures2,
79 ) {
80 self.instance_fn_1_1
81 .get_physical_device_features2(physical_device, features);
82 }
83
84 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceProperties2.html>"]
85 pub unsafe fn get_physical_device_properties2(
86 &self,
87 physical_device: vk::PhysicalDevice,
88 prop: &mut vk::PhysicalDeviceProperties2,
89 ) {
90 self.instance_fn_1_1
91 .get_physical_device_properties2(physical_device, prop);
92 }
93
94 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceFormatProperties2.html>"]
95 pub unsafe fn get_physical_device_format_properties2(
96 &self,
97 physical_device: vk::PhysicalDevice,
98 format: vk::Format,
99 out: &mut vk::FormatProperties2,
100 ) {
101 self.instance_fn_1_1
102 .get_physical_device_format_properties2(physical_device, format, out);
103 }
104
105 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceImageFormatProperties2.html>"]
106 pub unsafe fn get_physical_device_image_format_properties2(
107 &self,
108 physical_device: vk::PhysicalDevice,
109 format_info: &vk::PhysicalDeviceImageFormatInfo2,
110 image_format_prop: &mut vk::ImageFormatProperties2,
111 ) -> VkResult<()> {
112 self.instance_fn_1_1
113 .get_physical_device_image_format_properties2(
114 physical_device,
115 format_info,
116 image_format_prop,
117 )
118 .into()
119 }
120
121 pub unsafe fn get_physical_device_queue_family_properties2_len(
122 &self,
123 physical_device: vk::PhysicalDevice,
124 ) -> usize {
125 let mut queue_count = 0;
126 self.instance_fn_1_1
127 .get_physical_device_queue_family_properties2(
128 physical_device,
129 &mut queue_count,
130 ptr::null_mut(),
131 );
132 queue_count as usize
133 }
134
135 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceQueueFamilyProperties2.html>"]
136 pub unsafe fn get_physical_device_queue_family_properties2(
137 &self,
138 physical_device: vk::PhysicalDevice,
139 queue_family_props: &mut [vk::QueueFamilyProperties2],
140 ) {
141 let mut queue_count = queue_family_props.len() as u32;
142 self.instance_fn_1_1
143 .get_physical_device_queue_family_properties2(
144 physical_device,
145 &mut queue_count,
146 queue_family_props.as_mut_ptr(),
147 );
148 }
149
150 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceMemoryProperties2.html>"]
151 pub unsafe fn get_physical_device_memory_properties2(
152 &self,
153 physical_device: vk::PhysicalDevice,
154 out: &mut vk::PhysicalDeviceMemoryProperties2,
155 ) {
156 self.instance_fn_1_1
157 .get_physical_device_memory_properties2(physical_device, out);
158 }
159
160 pub unsafe fn get_physical_device_sparse_image_format_properties2_len(
161 &self,
162 physical_device: vk::PhysicalDevice,
163 format_info: &vk::PhysicalDeviceSparseImageFormatInfo2,
164 ) -> usize {
165 let mut format_count = 0;
166 self.instance_fn_1_1
167 .get_physical_device_sparse_image_format_properties2(
168 physical_device,
169 format_info,
170 &mut format_count,
171 ptr::null_mut(),
172 );
173 format_count as usize
174 }
175
176 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceSparseImageFormatProperties2.html>"]
177 pub unsafe fn get_physical_device_sparse_image_format_properties2(
178 &self,
179 physical_device: vk::PhysicalDevice,
180 format_info: &vk::PhysicalDeviceSparseImageFormatInfo2,
181 out: &mut [vk::SparseImageFormatProperties2],
182 ) {
183 let mut format_count = out.len() as u32;
184 self.instance_fn_1_1
185 .get_physical_device_sparse_image_format_properties2(
186 physical_device,
187 format_info,
188 &mut format_count,
189 out.as_mut_ptr(),
190 );
191 }
192
193 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceExternalBufferProperties.html>"]
194 pub unsafe fn get_physical_device_external_buffer_properties(
195 &self,
196 physical_device: vk::PhysicalDevice,
197 external_buffer_info: &vk::PhysicalDeviceExternalBufferInfo,
198 out: &mut vk::ExternalBufferProperties,
199 ) {
200 self.instance_fn_1_1
201 .get_physical_device_external_buffer_properties(
202 physical_device,
203 external_buffer_info,
204 out,
205 );
206 }
207
208 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceExternalFenceProperties.html>"]
209 pub unsafe fn get_physical_device_external_fence_properties(
210 &self,
211 physical_device: vk::PhysicalDevice,
212 external_fence_info: &vk::PhysicalDeviceExternalFenceInfo,
213 out: &mut vk::ExternalFenceProperties,
214 ) {
215 self.instance_fn_1_1
216 .get_physical_device_external_fence_properties(
217 physical_device,
218 external_fence_info,
219 out,
220 );
221 }
222
223 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceExternalSemaphoreProperties.html>"]
224 pub unsafe fn get_physical_device_external_semaphore_properties(
225 &self,
226 physical_device: vk::PhysicalDevice,
227 external_semaphore_info: &vk::PhysicalDeviceExternalSemaphoreInfo,
228 out: &mut vk::ExternalSemaphoreProperties,
229 ) {
230 self.instance_fn_1_1
231 .get_physical_device_external_semaphore_properties(
232 physical_device,
233 external_semaphore_info,
234 out,
235 );
236 }
237}
238
239/// Vulkan core 1.0
240#[allow(non_camel_case_types)]
241impl Instance {
242 pub fn fp_v1_0(&self) -> &vk::InstanceFnV1_0 {
243 &self.instance_fn_1_0
244 }
245
246 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkCreateDevice.html>"]
247 ///
248 /// # Safety
249 /// In order for the created [`Device`] to be valid for the duration of its
250 /// usage, the [`Instance`] this was called on must be dropped later than the
251 /// resulting [`Device`].
252 pub unsafe fn create_device(
253 &self,
254 physical_device: vk::PhysicalDevice,
255 create_info: &vk::DeviceCreateInfo,
256 allocation_callbacks: Option<&vk::AllocationCallbacks>,
257 ) -> VkResult<Device> {
258 let mut device = mem::zeroed();
259 self.instance_fn_1_0
260 .create_device(
261 physical_device,
262 create_info,
263 allocation_callbacks.as_raw_ptr(),
264 &mut device,
265 )
266 .result()?;
267 Ok(Device::load(&self.instance_fn_1_0, device))
268 }
269
270 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetDeviceProcAddr.html>"]
271 pub unsafe fn get_device_proc_addr(
272 &self,
273 device: vk::Device,
274 p_name: *const c_char,
275 ) -> vk::PFN_vkVoidFunction {
276 self.instance_fn_1_0.get_device_proc_addr(device, p_name)
277 }
278
279 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkDestroyInstance.html>"]
280 pub unsafe fn destroy_instance(&self, allocation_callbacks: Option<&vk::AllocationCallbacks>) {
281 self.instance_fn_1_0
282 .destroy_instance(self.handle(), allocation_callbacks.as_raw_ptr());
283 }
284
285 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceFormatProperties.html>"]
286 pub unsafe fn get_physical_device_format_properties(
287 &self,
288 physical_device: vk::PhysicalDevice,
289 format: vk::Format,
290 ) -> vk::FormatProperties {
291 let mut format_prop = mem::zeroed();
292 self.instance_fn_1_0.get_physical_device_format_properties(
293 physical_device,
294 format,
295 &mut format_prop,
296 );
297 format_prop
298 }
299
300 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceImageFormatProperties.html>"]
301 pub unsafe fn get_physical_device_image_format_properties(
302 &self,
303 physical_device: vk::PhysicalDevice,
304 format: vk::Format,
305 typ: vk::ImageType,
306 tiling: vk::ImageTiling,
307 usage: vk::ImageUsageFlags,
308 flags: vk::ImageCreateFlags,
309 ) -> VkResult<vk::ImageFormatProperties> {
310 let mut image_format_prop = mem::zeroed();
311 self.instance_fn_1_0
312 .get_physical_device_image_format_properties(
313 physical_device,
314 format,
315 typ,
316 tiling,
317 usage,
318 flags,
319 &mut image_format_prop,
320 )
321 .result_with_success(image_format_prop)
322 }
323
324 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceMemoryProperties.html>"]
325 pub unsafe fn get_physical_device_memory_properties(
326 &self,
327 physical_device: vk::PhysicalDevice,
328 ) -> vk::PhysicalDeviceMemoryProperties {
329 let mut memory_prop = mem::zeroed();
330 self.instance_fn_1_0
331 .get_physical_device_memory_properties(physical_device, &mut memory_prop);
332 memory_prop
333 }
334
335 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceProperties.html>"]
336 pub unsafe fn get_physical_device_properties(
337 &self,
338 physical_device: vk::PhysicalDevice,
339 ) -> vk::PhysicalDeviceProperties {
340 let mut prop = mem::zeroed();
341 self.instance_fn_1_0
342 .get_physical_device_properties(physical_device, &mut prop);
343 prop
344 }
345
346 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceQueueFamilyProperties.html>"]
347 pub unsafe fn get_physical_device_queue_family_properties(
348 &self,
349 physical_device: vk::PhysicalDevice,
350 ) -> Vec<vk::QueueFamilyProperties> {
351 read_into_uninitialized_vector(|count, data| {
352 self.instance_fn_1_0
353 .get_physical_device_queue_family_properties(physical_device, count, data);
354 vk::Result::SUCCESS
355 })
356 // The closure always returns SUCCESS
357 .unwrap()
358 }
359
360 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceFeatures.html>"]
361 pub unsafe fn get_physical_device_features(
362 &self,
363 physical_device: vk::PhysicalDevice,
364 ) -> vk::PhysicalDeviceFeatures {
365 let mut prop = mem::zeroed();
366 self.instance_fn_1_0
367 .get_physical_device_features(physical_device, &mut prop);
368 prop
369 }
370
371 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumeratePhysicalDevices.html>"]
372 pub unsafe fn enumerate_physical_devices(&self) -> VkResult<Vec<vk::PhysicalDevice>> {
373 read_into_uninitialized_vector(|count, data| {
374 self.instance_fn_1_0
375 .enumerate_physical_devices(self.handle(), count, data)
376 })
377 }
378
379 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumerateDeviceExtensionProperties.html>"]
380 pub unsafe fn enumerate_device_extension_properties(
381 &self,
382 device: vk::PhysicalDevice,
383 ) -> VkResult<Vec<vk::ExtensionProperties>> {
384 read_into_uninitialized_vector(|count, data| {
385 self.instance_fn_1_0.enumerate_device_extension_properties(
386 device,
387 ptr::null(),
388 count,
389 data,
390 )
391 })
392 }
393
394 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumerateDeviceLayerProperties.html>"]
395 pub unsafe fn enumerate_device_layer_properties(
396 &self,
397 device: vk::PhysicalDevice,
398 ) -> VkResult<Vec<vk::LayerProperties>> {
399 read_into_uninitialized_vector(|count, data| {
400 self.instance_fn_1_0
401 .enumerate_device_layer_properties(device, count, data)
402 })
403 }
404
405 #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkGetPhysicalDeviceSparseImageFormatProperties.html>"]
406 pub unsafe fn get_physical_device_sparse_image_format_properties(
407 &self,
408 physical_device: vk::PhysicalDevice,
409 format: vk::Format,
410 typ: vk::ImageType,
411 samples: vk::SampleCountFlags,
412 usage: vk::ImageUsageFlags,
413 tiling: vk::ImageTiling,
414 ) -> Vec<vk::SparseImageFormatProperties> {
415 read_into_uninitialized_vector(|count, data| {
416 self.instance_fn_1_0
417 .get_physical_device_sparse_image_format_properties(
418 physical_device,
419 format,
420 typ,
421 samples,
422 usage,
423 tiling,
424 count,
425 data,
426 );
427 vk::Result::SUCCESS
428 })
429 // The closure always returns SUCCESS
430 .unwrap()
431 }
432}