1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::ffi::CString;
use std::ffi::CStr;
use std::cmp;
use std::mem;
use std::os::raw::c_char;
use utils::c_bindings::*;
use utils::vk_traits::*;
use utils::vk_ptr::*;
use vulkan::vk::*;
pub fn vk_to_raw_value<W : VkWrappedType<R>, R>(value: &W) -> R {
unsafe {
let mut dst = mem::uninitialized();
W::vk_to_raw(value, &mut dst);
dst
}
}
pub fn vk_to_raw_value_checked<W : VkWrappedType<R>, R : Default>(value: &Option<W>) -> R {
match value {
Some(v) => vk_to_raw_value(v),
None => Default::default()
}
}
pub fn vk_to_raw_array<W : VkWrappedType<R>, R>(array: &[W], dst: &mut[R]) {
let len = cmp::min(array.len(), dst.len());
for i in 0..len {
W::vk_to_raw(&array[i], &mut dst[i])
}
}
pub fn vk_to_wrapped_array<W, R : VkRawType<W>>(array: &[R], dst: &mut[W]) {
let len = cmp::min(array.len(), dst.len());
for i in 0..len {
dst[i] = R::vk_to_wrapped(&array[i])
}
}
pub fn fill_vk_array<T : Default>(dst: &mut[T]) {
for i in 0..dst.len() {
dst[i] = T::default();
}
}
pub fn to_array<T : Copy>(src: &[T], dst: &mut[T]) {
let len = cmp::min(src.len(), dst.len());
for i in 0..len {
dst[i] = src[i];
}
}
pub fn string_to_byte_array(string: &str, dst: &mut[c_char]) {
let bytes = string.as_bytes();
let len = cmp::min(bytes.len(), dst.len() - 1);
for i in 0..len {
dst[i] = bytes[i] as c_char;
}
dst[len] = 0;
}
pub fn new_vk_array<R : VkRawType<W>, W>(length: u32, ptr: *const R) -> Vec<W> {
unsafe {
let len = length as usize;
let mut vector : Vec<W> = Vec::with_capacity(len);
for i in 0..len {
vector.push(R::vk_to_wrapped(&*ptr.add(i)));
}
vector
}
}
pub fn new_vk_array_checked<R : VkRawType<W>, W>(length: u32, ptr: *const R) -> Option<Vec<W>> {
if ptr.is_null() {
None
} else {
Some(new_vk_array(length, ptr))
}
}
pub fn new_vk_value<R : VkRawType<W>, W>(ptr: *const R) -> W {
unsafe {
R::vk_to_wrapped(&*ptr)
}
}
pub fn new_vk_value_checked<R : VkRawType<W>, W>(ptr: *const R) -> Option<W> {
unsafe {
if ptr.is_null() {
None
} else {
Some(R::vk_to_wrapped(&*ptr))
}
}
}
pub fn new_array<T : Copy>(length: u32, ptr: *const T) -> Vec<T> {
unsafe {
let len = length as usize;
let mut vector : Vec<T> = Vec::with_capacity(len);
for i in 0..len {
vector.push(*ptr.add(i));
}
vector
}
}
pub fn new_string(ptr: *const c_char) -> String {
unsafe {
if ptr.is_null() {
String::from("")
} else {
String::from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes().to_vec())
}
}
}
pub fn new_string_checked(ptr: *const c_char) -> Option<String> {
if ptr.is_null() {
Some(new_string(ptr))
} else {
None
}
}
pub fn new_string_vec(length: u32, ptr: *const *const c_char) -> Vec<String> {
let len = length as usize;
let mut result : Vec<String> = Vec::with_capacity(len);
for i in 0..len {
result.push(new_string(unsafe { *ptr.add(i) }));
}
result
}
pub fn new_string_ref<'a>(ptr: *const c_char) -> &'a str {
unsafe {
if ptr.is_null() {
""
} else {
let len = std::ffi::CStr::from_ptr(ptr).to_bytes().len();
let slice = std::slice::from_raw_parts(ptr as *const u8, len);
std::str::from_utf8_unchecked(slice)
}
}
}
pub fn new_string_ref_checked<'a>(ptr: *const c_char) -> Option<&'a str> {
if ptr.is_null() {
None
} else {
Some(new_string_ref(ptr))
}
}
pub fn new_string_ref_vec<'a>(length: u32, ptr: *const *const c_char) -> Vec<&'a str> {
let len = length as usize;
let mut result : Vec<&'a str> = Vec::with_capacity(len);
for i in 0..len {
result.push(new_string_ref(unsafe { *ptr.add(i) }));
}
result
}
pub fn vec_from_ptr<T>(length: usize, ptr: *const T) -> Vec<T> {
unsafe {
Vec::from_raw_parts(mem::transmute(ptr), length, length)
}
}
pub fn vec_from_ptr_checked<T>(length: usize, ptr: *const T) -> Option<Vec<T>> {
if ptr.is_null() {
None
} else {
Some(vec_from_ptr(length, ptr))
}
}
pub fn slice_from_ptr<'a, T>(length: usize, ptr: *const T) -> &'a [T] {
unsafe {
std::slice::from_raw_parts(ptr, length)
}
}
pub unsafe fn get_vk_instance_function_pointer(instance: RawVkInstance, name: &str) -> *const c_void {
let c_string = CString::new(name).unwrap();
vkGetInstanceProcAddr(instance, c_string.as_c_str().as_ptr())
}
pub unsafe fn get_vk_device_function_pointer(device: RawVkDevice, name: &str) -> *const c_void {
let c_string = CString::new(name).unwrap();
vkGetDeviceProcAddr(device, c_string.as_c_str().as_ptr())
}
extern {
fn vkGetInstanceProcAddr(instance: RawVkInstance, name: *const c_char) -> *const c_void;
fn vkGetDeviceProcAddr(device: RawVkDevice, name: *const c_char) -> *const c_void;
}