use std::fmt;
use std::ops;
use std::collections::BTreeSet;
use std::borrow::Cow;
use std::os::raw;
use std::ffi::CStr;
use types;
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash)]
pub struct VkVersion(pub u32);
impl VkVersion {
#[inline]
pub fn new(major: u32, minor: u32, patch: u32) -> VkVersion {
VkVersion((major << 22) | (minor << 12) | patch)
}
#[inline]
pub fn major(self) -> u32 {
self.0 >> 22
}
#[inline]
pub fn minor(self) -> u32 {
(self.0 >> 12) & 0x3ff
}
#[inline]
pub fn patch(self) -> u32 {
self.0 & 0xfff
}
}
impl fmt::Display for VkVersion {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}.{}", self.major(), self.minor(), self.patch())
}
}
impl fmt::Debug for VkVersion {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "VkVersion({}.{}.{})", self.major(), self.minor(), self.patch())
}
}
impl fmt::LowerHex for VkVersion {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}
impl fmt::UpperHex for VkVersion {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:X}", self.0)
}
}
impl Into<u32> for VkVersion {
#[inline]
fn into(self) -> u32 {
self.0
}
}
impl From<u32> for VkVersion {
#[inline]
fn from(value: u32) -> VkVersion {
VkVersion(value)
}
}
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq)]
pub struct VkDispatchableHandle{
value: usize,
}
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq)]
pub struct VkNonDispatchableHandle {
value: u64,
}
impl VkDispatchableHandle {
pub const NULL : VkDispatchableHandle = VkDispatchableHandle { value: 0 };
}
impl VkNonDispatchableHandle {
pub const NULL : VkNonDispatchableHandle = VkNonDispatchableHandle { value: 0 };
}
impl fmt::Debug for VkDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "`H:{:#x}`", self.value)
}
}
impl fmt::Display for VkDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "`H:{:#x}`", self.value)
}
}
impl fmt::Pointer for VkDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:#x}", self.value)
}
}
impl fmt::LowerHex for VkDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:x}", self.value)
}
}
impl fmt::UpperHex for VkDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:X}", self.value)
}
}
impl fmt::Debug for VkNonDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "`N:{:#x}`", self.value)
}
}
impl fmt::Display for VkNonDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "`N:{:#x}`", self.value)
}
}
impl fmt::Pointer for VkNonDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:#x}", self.value)
}
}
impl fmt::LowerHex for VkNonDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:x}", self.value)
}
}
impl fmt::UpperHex for VkNonDispatchableHandle {
#[inline]
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "{:X}", self.value)
}
}
impl VkNullHandle for VkDispatchableHandle {
const NULL : VkDispatchableHandle = VkDispatchableHandle::NULL;
#[inline]
fn null() -> VkDispatchableHandle {
VkDispatchableHandle::NULL
}
}
impl VkNullHandle for VkNonDispatchableHandle {
const NULL : VkNonDispatchableHandle = VkNonDispatchableHandle::NULL;
#[inline]
fn null() -> VkNonDispatchableHandle {
VkNonDispatchableHandle::NULL
}
}
impl Default for VkDispatchableHandle {
#[inline]
fn default() -> VkDispatchableHandle {
VkDispatchableHandle::NULL
}
}
impl Default for VkNonDispatchableHandle {
#[inline]
fn default() -> VkNonDispatchableHandle {
VkNonDispatchableHandle::NULL
}
}
pub trait VkDestroyableHandle: VkNullHandle {
type Owner: Default+Copy+Clone;
fn destroy(self, owner: Self::Owner, p_allocator: Option<&types::VkAllocationCallbacks>);
}
pub type VkError = ::types::VkResult;
impl VkError {
#[inline]
pub fn is_success(self) -> bool{
return (self as i32) >= 0;
}
#[inline]
pub fn is_error(self) -> bool {
return (self as i32) < 0;
}
#[inline]
pub fn into_result(self) -> VkResultObj {
if self.is_error() {
Err(self)
} else {
Ok(self)
}
}
#[inline]
pub fn from_result(result: VkResultObj) -> Self {
match result {
Ok(r) | Err(r) => r,
}
}
}
impl fmt::Display for VkError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", *self)
}
}
impl fmt::LowerHex for VkError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", *self as u32)
}
}
impl fmt::UpperHex for VkError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:X}", *self as u32)
}
}
impl Into<String> for VkError {
#[inline]
fn into(self) -> String {
format!("{:?}", self)
}
}
impl Into<::std::io::Error> for VkError {
#[inline]
fn into(self) -> ::std::io::Error {
::std::io::Error::new(::std::io::ErrorKind::Other, format!("{:?}", self))
}
}
impl Into<VkResultObj> for VkError {
#[inline]
fn into(self) -> VkResultObj {
if self.is_error() {
Err(self)
} else {
Ok(self)
}
}
}
impl Into<VkResultObj<()>> for VkError {
#[inline]
fn into(self) -> VkResultObj<()> {
if self != ::types::VK_SUCCESS {
Err(self)
} else {
Ok(())
}
}
}
impl From<VkResultObj> for VkError {
#[inline]
fn from(result: VkResultObj) -> VkError {
match result {
Ok(r) | Err(r) => r,
}
}
}
impl From<VkResultObj<()>> for VkError {
#[inline]
fn from(result: VkResultObj<()>) -> VkError {
match result {
Ok(()) => ::types::VK_SUCCESS,
Err(r) => r,
}
}
}
#[cfg_attr(feature="nightly", feature(try_trait))]
#[cfg(feature="nightly")]
impl ops::Try for VkError {
type Ok = VkError;
type Error = VkError;
#[inline]
fn into_result(self) -> Result<VkError,VkError> {
if self.is_error() {
Err(self)
} else {
Ok(self)
}
}
#[inline]
fn from_ok(v: VkError) -> VkError {
v
}
#[inline]
fn from_error(v: VkError) -> VkError {
v
}
}
pub type VkResultObj<T=::types::VkResult> = Result<T, VkError>;
pub use std::ptr::null_mut as vk_null;
pub trait VkNullHandle: Sized+PartialEq+Eq+Copy+Clone+Sized {
const NULL : Self;
#[inline]
fn null() -> Self {
Self::NULL
}
#[inline]
fn is_null(self) -> bool {
self == Self::NULL
}
}
#[inline]
pub fn vk_null_handle<T>() -> T where T: VkNullHandle {
T::NULL
}
pub unsafe fn extensions_list_to_set<'l>(p: *const *const raw::c_char, len: u32) -> BTreeSet<Cow<'l, str>> {
let mut extensions : BTreeSet<Cow<str>> = BTreeSet::new();
if !p.is_null() {
for ext in ::std::slice::from_raw_parts(p, len as usize) {
if ext.is_null() {
break;
}
extensions.insert(CStr::from_ptr(*ext).to_string_lossy());
}
}
extensions
}
pub trait VkFlagBits: Copy + Clone + 'static {
const NONE_VALUE : u32 = 0;
const ALL_VALUE : u32;
const NONE : VkFlags<Self> = VkFlags::NONE;
const ALL : VkFlags<Self> = VkFlags::ALL;
fn value(self) -> u32;
fn from_value(value: u32) -> Option<Self>;
#[inline]
fn flags(self) -> VkFlags<Self> {
VkFlags::one(self)
}
}
#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash,Debug)]
pub enum VkVoid{}
impl VkFlagBits for VkVoid {
const ALL_VALUE : u32 = 0;
#[inline]
fn value(self) -> u32 {
unreachable!()
}
#[inline]
fn from_value(_: u32) -> Option<VkVoid> {
None
}
}
#[repr(C)]
#[derive(Copy,Clone,PartialEq,Eq,PartialOrd,Ord,Hash)]
pub struct VkFlags<E:VkFlagBits=VkVoid>(u32, ::std::marker::PhantomData<E>);
impl<E:VkFlagBits> VkFlags<E> {
pub const NONE : VkFlags<E> = VkFlags(E::NONE_VALUE, ::std::marker::PhantomData);
pub const ALL : VkFlags<E> = VkFlags(E::ALL_VALUE, ::std::marker::PhantomData);
#[inline]
pub fn none() -> VkFlags<E> {
Self::NONE
}
#[inline]
pub fn one(e: E) -> VkFlags<E> {
VkFlags(e.value(), ::std::marker::PhantomData)
}
#[inline]
pub fn all() -> VkFlags<E> {
Self::ALL
}
#[inline]
pub fn is_empty(self) -> bool {
self.0 == 0
}
#[inline]
pub fn is_not_empty(self) -> bool {
self.0 != 0
}
#[inline]
pub fn contains(self, other: E) -> bool {
self.0 & other.value() != 0
}
#[inline]
pub fn contains_one_of(self, other: Self) -> bool {
self.0 & other.0 != 0
}
#[inline]
pub fn contains_all_of(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
}
impl<E:VkFlagBits> Default for VkFlags<E> {
#[inline]
fn default() -> VkFlags<E> {
VkFlags::NONE
}
}
impl<E:VkFlagBits> From<E> for VkFlags<E> {
#[inline]
fn from(e: E) -> VkFlags<E> {
VkFlags::one(e)
}
}
impl<E:VkFlagBits> ops::BitAnd<E> for VkFlags<E> {
type Output = VkFlags<E>;
#[inline]
fn bitand(self, rhs: E) -> VkFlags<E> {
VkFlags(self.0 & rhs.value(), ::std::marker::PhantomData)
}
}
impl<E:VkFlagBits> ops::BitOr<E> for VkFlags<E> {
type Output = VkFlags<E>;
#[inline]
fn bitor(self, rhs: E) -> VkFlags<E> {
VkFlags(self.0 | rhs.value(), ::std::marker::PhantomData)
}
}
impl<E:VkFlagBits> ops::BitAnd<VkFlags<E>> for VkFlags<E> {
type Output = VkFlags<E>;
#[inline]
fn bitand(self, rhs: VkFlags<E>) -> VkFlags<E> {
VkFlags(self.0 & rhs.0, ::std::marker::PhantomData)
}
}
impl<E:VkFlagBits> ops::BitOr<VkFlags<E>> for VkFlags<E> {
type Output = VkFlags<E>;
#[inline]
fn bitor(self, rhs: VkFlags<E>) -> VkFlags<E> {
VkFlags(self.0 | rhs.0, ::std::marker::PhantomData)
}
}
impl<E:VkFlagBits+fmt::Debug> fmt::Debug for VkFlags<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_empty() {
write!(f, "VkFlags[]")
} else {
write!(f, "VkFlags[{}]", self)
}
}
}
impl<E:VkFlagBits+fmt::Debug> fmt::Display for VkFlags<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_empty() {
write!(f, "(None)")
} else {
let value = self.0 & E::ALL_VALUE;
let mut i = 1;
let mut n = 0;
loop {
if (value & i) != 0 {
if let Some(e) = E::from_value(i) {
if n > 0 {
write!(f, "|")?;
}
write!(f, "{:?}", e)?;
n += 1;
}
}
i = i << 1;
if i==0 || i>E::ALL_VALUE {
break;
}
}
Ok(())
}
}
}
impl<E:VkFlagBits> fmt::LowerHex for VkFlags<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:x}", self.0)
}
}
impl<E:VkFlagBits> fmt::UpperHex for VkFlags<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:X}", self.0)
}
}
impl<E:VkFlagBits> fmt::Octal for VkFlags<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:o}", self.0)
}
}
impl<E:VkFlagBits> fmt::Binary for VkFlags<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:b}", self.0)
}
}