1 use crate::prelude::*; 2 use crate::vk; 3 use crate::RawPtr; 4 use crate::{Entry, Instance}; 5 use std::ffi::CStr; 6 use std::mem; 7 8 #[derive(Clone)] 9 pub struct Surface { 10 handle: vk::Instance, 11 fp: vk::KhrSurfaceFn, 12 } 13 14 impl Surface { new(entry: &Entry, instance: &Instance) -> Self15 pub fn new(entry: &Entry, instance: &Instance) -> Self { 16 let handle = instance.handle(); 17 let fp = vk::KhrSurfaceFn::load(|name| unsafe { 18 mem::transmute(entry.get_instance_proc_addr(handle, name.as_ptr())) 19 }); 20 Self { handle, fp } 21 } 22 23 /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceSupportKHR.html> 24 #[inline] get_physical_device_surface_support( &self, physical_device: vk::PhysicalDevice, queue_family_index: u32, surface: vk::SurfaceKHR, ) -> VkResult<bool>25 pub unsafe fn get_physical_device_surface_support( 26 &self, 27 physical_device: vk::PhysicalDevice, 28 queue_family_index: u32, 29 surface: vk::SurfaceKHR, 30 ) -> VkResult<bool> { 31 let mut b = 0; 32 (self.fp.get_physical_device_surface_support_khr)( 33 physical_device, 34 queue_family_index, 35 surface, 36 &mut b, 37 ) 38 .result_with_success(b > 0) 39 } 40 41 /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfacePresentModesKHR.html> 42 #[inline] get_physical_device_surface_present_modes( &self, physical_device: vk::PhysicalDevice, surface: vk::SurfaceKHR, ) -> VkResult<Vec<vk::PresentModeKHR>>43 pub unsafe fn get_physical_device_surface_present_modes( 44 &self, 45 physical_device: vk::PhysicalDevice, 46 surface: vk::SurfaceKHR, 47 ) -> VkResult<Vec<vk::PresentModeKHR>> { 48 read_into_uninitialized_vector(|count, data| { 49 (self.fp.get_physical_device_surface_present_modes_khr)( 50 physical_device, 51 surface, 52 count, 53 data, 54 ) 55 }) 56 } 57 58 /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceCapabilitiesKHR.html> 59 #[inline] get_physical_device_surface_capabilities( &self, physical_device: vk::PhysicalDevice, surface: vk::SurfaceKHR, ) -> VkResult<vk::SurfaceCapabilitiesKHR>60 pub unsafe fn get_physical_device_surface_capabilities( 61 &self, 62 physical_device: vk::PhysicalDevice, 63 surface: vk::SurfaceKHR, 64 ) -> VkResult<vk::SurfaceCapabilitiesKHR> { 65 let mut surface_capabilities = mem::zeroed(); 66 (self.fp.get_physical_device_surface_capabilities_khr)( 67 physical_device, 68 surface, 69 &mut surface_capabilities, 70 ) 71 .result_with_success(surface_capabilities) 72 } 73 74 /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetPhysicalDeviceSurfaceFormatsKHR.html> 75 #[inline] get_physical_device_surface_formats( &self, physical_device: vk::PhysicalDevice, surface: vk::SurfaceKHR, ) -> VkResult<Vec<vk::SurfaceFormatKHR>>76 pub unsafe fn get_physical_device_surface_formats( 77 &self, 78 physical_device: vk::PhysicalDevice, 79 surface: vk::SurfaceKHR, 80 ) -> VkResult<Vec<vk::SurfaceFormatKHR>> { 81 read_into_uninitialized_vector(|count, data| { 82 (self.fp.get_physical_device_surface_formats_khr)(physical_device, surface, count, data) 83 }) 84 } 85 86 /// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkDestroySurfaceKHR.html> 87 #[inline] destroy_surface( &self, surface: vk::SurfaceKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>, )88 pub unsafe fn destroy_surface( 89 &self, 90 surface: vk::SurfaceKHR, 91 allocation_callbacks: Option<&vk::AllocationCallbacks>, 92 ) { 93 (self.fp.destroy_surface_khr)(self.handle, surface, allocation_callbacks.as_raw_ptr()); 94 } 95 96 #[inline] name() -> &'static CStr97 pub const fn name() -> &'static CStr { 98 vk::KhrSurfaceFn::name() 99 } 100 101 #[inline] fp(&self) -> &vk::KhrSurfaceFn102 pub fn fp(&self) -> &vk::KhrSurfaceFn { 103 &self.fp 104 } 105 106 #[inline] instance(&self) -> vk::Instance107 pub fn instance(&self) -> vk::Instance { 108 self.handle 109 } 110 } 111