Refactor Entry into EntryV1_0

This commit is contained in:
maik klein 2017-01-04 19:09:27 +01:00
parent 47420b1c1e
commit 0531d67e99
6 changed files with 76 additions and 53 deletions

View file

@ -47,6 +47,62 @@ pub enum InstanceError {
LoadError(Vec<&'static str>),
VkError(vk::Result),
}
pub trait EntryV1_0{
fn fp_v1_0(&self) -> &vk::EntryFnV1_0;
fn static_fn(&self) -> &vk::StaticFn;
fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> {
unsafe {
let mut num = 0;
self.fp_v1_0().enumerate_instance_layer_properties(&mut num, ptr::null_mut());
let mut v = Vec::with_capacity(num as usize);
let err_code = self
.fp_v1_0()
.enumerate_instance_layer_properties(&mut num, v.as_mut_ptr());
v.set_len(num as usize);
match err_code {
vk::Result::Success => Ok(v),
_ => Err(err_code),
}
}
}
fn enumerate_instance_extension_properties(&self)
-> VkResult<Vec<vk::ExtensionProperties>> {
unsafe {
let mut num = 0;
self
.fp_v1_0()
.enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut());
let mut data = Vec::with_capacity(num as usize);
let err_code = self
.fp_v1_0()
.enumerate_instance_extension_properties(ptr::null(), &mut num, data.as_mut_ptr());
data.set_len(num as usize);
match err_code {
vk::Result::Success => Ok(data),
_ => Err(err_code),
}
}
}
fn get_instance_proc_addr(&self,
instance: vk::Instance,
p_name: *const vk::c_char)
-> vk::PFN_vkVoidFunction {
unsafe { self.static_fn().get_instance_proc_addr(instance, p_name) }
}
}
impl EntryV1_0 for Entry<V1_0>{
fn fp_v1_0(&self) -> &vk::EntryFnV1_0{
self.entry_fn.fp_v1_0()
}
fn static_fn(&self) -> &vk::StaticFn{
&self.static_fn
}
}
impl<V: FunctionPointers> Entry<V> {
pub fn create_instance(&self,
create_info: &vk::InstanceCreateInfo,
@ -87,47 +143,4 @@ impl<V: FunctionPointers> Entry<V> {
_v: PhantomData,
})
}
pub fn enumerate_instance_layer_properties(&self) -> VkResult<Vec<vk::LayerProperties>> {
unsafe {
let mut num = 0;
self.entry_fn.fp_v1_0().enumerate_instance_layer_properties(&mut num, ptr::null_mut());
let mut v = Vec::with_capacity(num as usize);
let err_code = self.entry_fn
.fp_v1_0()
.enumerate_instance_layer_properties(&mut num, v.as_mut_ptr());
v.set_len(num as usize);
match err_code {
vk::Result::Success => Ok(v),
_ => Err(err_code),
}
}
}
pub fn enumerate_instance_extension_properties(&self)
-> VkResult<Vec<vk::ExtensionProperties>> {
unsafe {
let mut num = 0;
self.entry_fn
.fp_v1_0()
.enumerate_instance_extension_properties(ptr::null(), &mut num, ptr::null_mut());
let mut data = Vec::with_capacity(num as usize);
let err_code = self.entry_fn
.fp_v1_0()
.enumerate_instance_extension_properties(ptr::null(), &mut num, data.as_mut_ptr());
data.set_len(num as usize);
match err_code {
vk::Result::Success => Ok(data),
_ => Err(err_code),
}
}
}
pub fn get_instance_proc_addr(&self,
instance: vk::Instance,
p_name: *const vk::c_char)
-> vk::PFN_vkVoidFunction {
unsafe { self.static_fn.get_instance_proc_addr(instance, p_name) }
}
}

View file

@ -5,7 +5,7 @@ use entry::Entry;
use vk;
use std::ffi::CStr;
use ::RawPtr;
use version::{V1_0, InstanceFpV1_0};
use version::{V1_0, EntryV1_0};
#[derive(Clone)]
pub struct DebugReport {
@ -14,7 +14,9 @@ pub struct DebugReport {
}
impl DebugReport {
pub fn new(entry: &Entry<V1_0>, instance: &Instance<V1_0>) -> Result<DebugReport, Vec<&'static str>> {
pub fn new(entry: &Entry<V1_0>,
instance: &Instance<V1_0>)
-> Result<DebugReport, Vec<&'static str>> {
let debug_report_fn = vk::DebugReportFn::load(|name| {
unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -7,7 +7,7 @@ use entry::Entry;
use vk;
use std::ffi::CStr;
use ::RawPtr;
use version::{V1_0};
use version::{V1_0, EntryV1_0};
#[derive(Clone)]
pub struct Surface {
@ -16,7 +16,9 @@ pub struct Surface {
}
impl Surface {
pub fn new(entry: &Entry<V1_0>, instance: &Instance<V1_0>) -> Result<Surface, Vec<&'static str>> {
pub fn new(entry: &Entry<V1_0>,
instance: &Instance<V1_0>)
-> Result<Surface, Vec<&'static str>> {
let surface_fn = vk::SurfaceFn::load(|name| {
unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))
@ -112,7 +114,10 @@ impl Surface {
}
}
pub unsafe fn destroy_surface_khr(&self, surface: vk::SurfaceKHR, allocation_callbacks: Option<&vk::AllocationCallbacks>) {
self.surface_fn.destroy_surface_khr(self.handle, surface, allocation_callbacks.as_raw_ptr());
pub unsafe fn destroy_surface_khr(&self,
surface: vk::SurfaceKHR,
allocation_callbacks: Option<&vk::AllocationCallbacks>) {
self.surface_fn
.destroy_surface_khr(self.handle, surface, allocation_callbacks.as_raw_ptr());
}
}

View file

@ -7,7 +7,7 @@ use vk;
use std::ffi::CStr;
use ::RawPtr;
use instance::InstanceV1_0;
use version::{V1_0};
use version::{V1_0, EntryV1_0};
#[derive(Clone)]
pub struct Win32Surface {
@ -16,7 +16,9 @@ pub struct Win32Surface {
}
impl Win32Surface {
pub fn new(entry: &Entry<V1_0>, instance: &Instance<V1_0>) -> Result<Win32Surface, Vec<&'static str>> {
pub fn new(entry: &Entry<V1_0>,
instance: &Instance<V1_0>)
-> Result<Win32Surface, Vec<&'static str>> {
let surface_fn = vk::Win32SurfaceFn::load(|name| {
unsafe {
mem::transmute(entry.get_instance_proc_addr(instance.handle(), name.as_ptr()))

View file

@ -7,7 +7,7 @@ use vk;
use std::ffi::CStr;
use ::RawPtr;
use instance::InstanceV1_0;
use version::{V1_0};
use version::{V1_0, EntryV1_0};
#[derive(Clone)]
pub struct XlibSurface {

View file

@ -1,6 +1,7 @@
use vk;
pub use instance::InstanceV1_0;
pub use device::DeviceV1_0;
pub use entry::EntryV1_0;
use std::mem;
pub trait FunctionPointers {
type InstanceFp: InstanceLoader;