1#![allow(dead_code, unused_imports)]
2use crate::ffi;
3use super::*;
4use std::cell::RefCell;
5fn string_view_to_string(view: ffi::WGPUStringView) -> String {
6 if view.data.is_null() || view.length == 0 {
7 return String::new();
8 }
9 let data = view.data.cast::<u8>();
10 let slice = unsafe { std::slice::from_raw_parts(data, view.length) };
11 String::from_utf8_lossy(slice).into_owned()
12}
13pub type Callback = Option<unsafe extern "C" fn(*mut std::ffi::c_void) -> ()>;
14pub type DawnLoadCacheDataFunction = Option<
15 unsafe extern "C" fn(
16 *const std::ffi::c_void,
17 usize,
18 *mut std::ffi::c_void,
19 usize,
20 *mut std::ffi::c_void,
21 ) -> usize,
22>;
23pub type DawnStoreCacheDataFunction = Option<
24 unsafe extern "C" fn(
25 *const std::ffi::c_void,
26 usize,
27 *const std::ffi::c_void,
28 usize,
29 *mut std::ffi::c_void,
30 ) -> (),
31>;
32pub type Proc = Option<unsafe extern "C" fn() -> ()>;
33pub type BufferMapCallback = Box<dyn FnMut(MapAsyncStatus, String) + Send + 'static>;
34pub(crate) unsafe extern "C" fn buffer_map_callback_trampoline(
35 status: ffi::WGPUMapAsyncStatus,
36 message: ffi::WGPUStringView,
37 userdata1: *mut std::ffi::c_void,
38 userdata2: *mut std::ffi::c_void,
39) {
40 let _ = userdata2;
41 let status = status.into();
42 let message = string_view_to_string(message);
43 let mut callback = unsafe {
44 Box::from_raw(userdata1.cast::<Option<BufferMapCallback>>())
45 };
46 if let Some(mut callback) = callback.take() {
47 callback(status, message);
48 }
49}
50pub type CompilationInfoCallback = Box<
51 dyn FnMut(CompilationInfoRequestStatus, &CompilationInfo) + Send + 'static,
52>;
53pub(crate) unsafe extern "C" fn compilation_info_callback_trampoline(
54 status: ffi::WGPUCompilationInfoRequestStatus,
55 compilation_info: *const ffi::WGPUCompilationInfo,
56 userdata1: *mut std::ffi::c_void,
57 userdata2: *mut std::ffi::c_void,
58) {
59 let _ = userdata2;
60 let status = status.into();
61 let compilation_info = if compilation_info.is_null() {
62 CompilationInfo::new()
63 } else {
64 CompilationInfo::new()
65 };
66 let mut callback = unsafe {
67 Box::from_raw(userdata1.cast::<Option<CompilationInfoCallback>>())
68 };
69 if let Some(mut callback) = callback.take() {
70 callback(status, &compilation_info);
71 }
72}
73pub type CreateComputePipelineAsyncCallback = Box<
74 dyn FnMut(
75 CreatePipelineAsyncStatus,
76 Option<ComputePipeline>,
77 String,
78 ) + Send + 'static,
79>;
80pub(crate) unsafe extern "C" fn create_compute_pipeline_async_callback_trampoline(
81 status: ffi::WGPUCreatePipelineAsyncStatus,
82 pipeline: ffi::WGPUComputePipeline,
83 message: ffi::WGPUStringView,
84 userdata1: *mut std::ffi::c_void,
85 userdata2: *mut std::ffi::c_void,
86) {
87 let _ = userdata2;
88 let status = status.into();
89 let pipeline = if pipeline.is_null() {
90 None
91 } else {
92 Some(unsafe { ComputePipeline::from_raw(pipeline) })
93 };
94 let message = string_view_to_string(message);
95 let mut callback = unsafe {
96 Box::from_raw(userdata1.cast::<Option<CreateComputePipelineAsyncCallback>>())
97 };
98 if let Some(mut callback) = callback.take() {
99 callback(status, pipeline, message);
100 }
101}
102pub type CreateRenderPipelineAsyncCallback = Box<
103 dyn FnMut(CreatePipelineAsyncStatus, Option<RenderPipeline>, String) + Send + 'static,
104>;
105pub(crate) unsafe extern "C" fn create_render_pipeline_async_callback_trampoline(
106 status: ffi::WGPUCreatePipelineAsyncStatus,
107 pipeline: ffi::WGPURenderPipeline,
108 message: ffi::WGPUStringView,
109 userdata1: *mut std::ffi::c_void,
110 userdata2: *mut std::ffi::c_void,
111) {
112 let _ = userdata2;
113 let status = status.into();
114 let pipeline = if pipeline.is_null() {
115 None
116 } else {
117 Some(unsafe { RenderPipeline::from_raw(pipeline) })
118 };
119 let message = string_view_to_string(message);
120 let mut callback = unsafe {
121 Box::from_raw(userdata1.cast::<Option<CreateRenderPipelineAsyncCallback>>())
122 };
123 if let Some(mut callback) = callback.take() {
124 callback(status, pipeline, message);
125 }
126}
127pub type DeviceLostCallback = Box<
128 dyn FnMut(Vec<Device>, DeviceLostReason, String) + Send + 'static,
129>;
130pub(crate) unsafe extern "C" fn device_lost_callback_trampoline(
131 device: *const ffi::WGPUDevice,
132 reason: ffi::WGPUDeviceLostReason,
133 message: ffi::WGPUStringView,
134 userdata1: *mut std::ffi::c_void,
135 userdata2: *mut std::ffi::c_void,
136) {
137 let _ = userdata2;
138 let device = if device.is_null() {
139 Vec::new()
140 } else {
141 unsafe { std::slice::from_raw_parts(device, 1) }
142 .iter()
143 .map(|raw| unsafe { Device::from_raw(*raw) })
144 .collect()
145 };
146 let reason = reason.into();
147 let message = string_view_to_string(message);
148 let mut callback = unsafe {
149 Box::from_raw(userdata1.cast::<Option<DeviceLostCallback>>())
150 };
151 if let Some(mut callback) = callback.take() {
152 callback(device, reason, message);
153 }
154}
155pub type LoggingCallback = Box<dyn FnMut(LoggingType, String) + Send + 'static>;
156pub(crate) unsafe extern "C" fn logging_callback_trampoline(
157 r#type: ffi::WGPULoggingType,
158 message: ffi::WGPUStringView,
159 userdata1: *mut std::ffi::c_void,
160 userdata2: *mut std::ffi::c_void,
161) {
162 let _ = userdata2;
163 let r#type = r#type.into();
164 let message = string_view_to_string(message);
165 let mut callback = unsafe {
166 Box::from_raw(userdata1.cast::<Option<LoggingCallback>>())
167 };
168 if let Some(mut callback) = callback.take() {
169 callback(r#type, message);
170 }
171}
172pub type PopErrorScopeCallback = Box<
173 dyn FnMut(PopErrorScopeStatus, ErrorType, String) + Send + 'static,
174>;
175pub(crate) unsafe extern "C" fn pop_error_scope_callback_trampoline(
176 status: ffi::WGPUPopErrorScopeStatus,
177 r#type: ffi::WGPUErrorType,
178 message: ffi::WGPUStringView,
179 userdata1: *mut std::ffi::c_void,
180 userdata2: *mut std::ffi::c_void,
181) {
182 let _ = userdata2;
183 let status = status.into();
184 let r#type = r#type.into();
185 let message = string_view_to_string(message);
186 let mut callback = unsafe {
187 Box::from_raw(userdata1.cast::<Option<PopErrorScopeCallback>>())
188 };
189 if let Some(mut callback) = callback.take() {
190 callback(status, r#type, message);
191 }
192}
193pub type QueueWorkDoneCallback = Box<
194 dyn FnMut(QueueWorkDoneStatus, String) + Send + 'static,
195>;
196pub(crate) unsafe extern "C" fn queue_work_done_callback_trampoline(
197 status: ffi::WGPUQueueWorkDoneStatus,
198 message: ffi::WGPUStringView,
199 userdata1: *mut std::ffi::c_void,
200 userdata2: *mut std::ffi::c_void,
201) {
202 let _ = userdata2;
203 let status = status.into();
204 let message = string_view_to_string(message);
205 let mut callback = unsafe {
206 Box::from_raw(userdata1.cast::<Option<QueueWorkDoneCallback>>())
207 };
208 if let Some(mut callback) = callback.take() {
209 callback(status, message);
210 }
211}
212pub type RequestAdapterCallback = Box<
213 dyn FnMut(RequestAdapterStatus, Option<Adapter>, String) + Send + 'static,
214>;
215pub(crate) unsafe extern "C" fn request_adapter_callback_trampoline(
216 status: ffi::WGPURequestAdapterStatus,
217 adapter: ffi::WGPUAdapter,
218 message: ffi::WGPUStringView,
219 userdata1: *mut std::ffi::c_void,
220 userdata2: *mut std::ffi::c_void,
221) {
222 let _ = userdata2;
223 let status = status.into();
224 let adapter = if adapter.is_null() {
225 None
226 } else {
227 Some(unsafe { Adapter::from_raw(adapter) })
228 };
229 let message = string_view_to_string(message);
230 let mut callback = unsafe {
231 Box::from_raw(userdata1.cast::<Option<RequestAdapterCallback>>())
232 };
233 if let Some(mut callback) = callback.take() {
234 callback(status, adapter, message);
235 }
236}
237pub type RequestDeviceCallback = Box<
238 dyn FnMut(RequestDeviceStatus, Option<Device>, String) + Send + 'static,
239>;
240pub(crate) unsafe extern "C" fn request_device_callback_trampoline(
241 status: ffi::WGPURequestDeviceStatus,
242 device: ffi::WGPUDevice,
243 message: ffi::WGPUStringView,
244 userdata1: *mut std::ffi::c_void,
245 userdata2: *mut std::ffi::c_void,
246) {
247 let _ = userdata2;
248 let status = status.into();
249 let device = if device.is_null() {
250 None
251 } else {
252 Some(unsafe { Device::from_raw(device) })
253 };
254 let message = string_view_to_string(message);
255 let mut callback = unsafe {
256 Box::from_raw(userdata1.cast::<Option<RequestDeviceCallback>>())
257 };
258 if let Some(mut callback) = callback.take() {
259 callback(status, device, message);
260 }
261}
262pub type UncapturedErrorCallback = Box<
263 dyn FnMut(Vec<Device>, ErrorType, String) + Send + 'static,
264>;
265pub(crate) unsafe extern "C" fn uncaptured_error_callback_trampoline(
266 device: *const ffi::WGPUDevice,
267 r#type: ffi::WGPUErrorType,
268 message: ffi::WGPUStringView,
269 userdata1: *mut std::ffi::c_void,
270 userdata2: *mut std::ffi::c_void,
271) {
272 let _ = userdata2;
273 let device = if device.is_null() {
274 Vec::new()
275 } else {
276 unsafe { std::slice::from_raw_parts(device, 1) }
277 .iter()
278 .map(|raw| unsafe { Device::from_raw(*raw) })
279 .collect()
280 };
281 let r#type = r#type.into();
282 let message = string_view_to_string(message);
283 let mut callback = unsafe {
284 Box::from_raw(userdata1.cast::<Option<UncapturedErrorCallback>>())
285 };
286 if let Some(mut callback) = callback.take() {
287 callback(device, r#type, message);
288 }
289}
290pub struct BufferMapCallbackInfo {
291 pub mode: Option<CallbackMode>,
292 pub callback: std::cell::RefCell<Option<BufferMapCallback>>,
293}
294impl Default for BufferMapCallbackInfo {
295 fn default() -> Self {
296 Self {
297 mode: None,
298 callback: std::cell::RefCell::new(None),
299 }
300 }
301}
302impl BufferMapCallbackInfo {
303 pub fn new() -> Self {
304 Self::default()
305 }
306}
307pub struct CompilationInfoCallbackInfo {
308 pub mode: Option<CallbackMode>,
309 pub callback: std::cell::RefCell<Option<CompilationInfoCallback>>,
310}
311impl Default for CompilationInfoCallbackInfo {
312 fn default() -> Self {
313 Self {
314 mode: None,
315 callback: std::cell::RefCell::new(None),
316 }
317 }
318}
319impl CompilationInfoCallbackInfo {
320 pub fn new() -> Self {
321 Self::default()
322 }
323}
324pub struct CreateComputePipelineAsyncCallbackInfo {
325 pub mode: Option<CallbackMode>,
326 pub callback: std::cell::RefCell<Option<CreateComputePipelineAsyncCallback>>,
327}
328impl Default for CreateComputePipelineAsyncCallbackInfo {
329 fn default() -> Self {
330 Self {
331 mode: None,
332 callback: std::cell::RefCell::new(None),
333 }
334 }
335}
336impl CreateComputePipelineAsyncCallbackInfo {
337 pub fn new() -> Self {
338 Self::default()
339 }
340}
341pub struct CreateRenderPipelineAsyncCallbackInfo {
342 pub mode: Option<CallbackMode>,
343 pub callback: std::cell::RefCell<Option<CreateRenderPipelineAsyncCallback>>,
344}
345impl Default for CreateRenderPipelineAsyncCallbackInfo {
346 fn default() -> Self {
347 Self {
348 mode: None,
349 callback: std::cell::RefCell::new(None),
350 }
351 }
352}
353impl CreateRenderPipelineAsyncCallbackInfo {
354 pub fn new() -> Self {
355 Self::default()
356 }
357}
358pub struct DeviceLostCallbackInfo {
359 pub mode: Option<CallbackMode>,
360 pub callback: std::cell::RefCell<Option<DeviceLostCallback>>,
361}
362impl Default for DeviceLostCallbackInfo {
363 fn default() -> Self {
364 Self {
365 mode: None,
366 callback: std::cell::RefCell::new(None),
367 }
368 }
369}
370impl DeviceLostCallbackInfo {
371 pub fn new() -> Self {
372 Self::default()
373 }
374}
375pub struct LoggingCallbackInfo {
376 pub callback: std::cell::RefCell<Option<LoggingCallback>>,
377}
378impl Default for LoggingCallbackInfo {
379 fn default() -> Self {
380 Self {
381 callback: std::cell::RefCell::new(None),
382 }
383 }
384}
385impl LoggingCallbackInfo {
386 pub fn new() -> Self {
387 Self::default()
388 }
389}
390pub struct PopErrorScopeCallbackInfo {
391 pub mode: Option<CallbackMode>,
392 pub callback: std::cell::RefCell<Option<PopErrorScopeCallback>>,
393}
394impl Default for PopErrorScopeCallbackInfo {
395 fn default() -> Self {
396 Self {
397 mode: None,
398 callback: std::cell::RefCell::new(None),
399 }
400 }
401}
402impl PopErrorScopeCallbackInfo {
403 pub fn new() -> Self {
404 Self::default()
405 }
406}
407pub struct QueueWorkDoneCallbackInfo {
408 pub mode: Option<CallbackMode>,
409 pub callback: std::cell::RefCell<Option<QueueWorkDoneCallback>>,
410}
411impl Default for QueueWorkDoneCallbackInfo {
412 fn default() -> Self {
413 Self {
414 mode: None,
415 callback: std::cell::RefCell::new(None),
416 }
417 }
418}
419impl QueueWorkDoneCallbackInfo {
420 pub fn new() -> Self {
421 Self::default()
422 }
423}
424pub struct RequestAdapterCallbackInfo {
425 pub mode: Option<CallbackMode>,
426 pub callback: std::cell::RefCell<Option<RequestAdapterCallback>>,
427}
428impl Default for RequestAdapterCallbackInfo {
429 fn default() -> Self {
430 Self {
431 mode: None,
432 callback: std::cell::RefCell::new(None),
433 }
434 }
435}
436impl RequestAdapterCallbackInfo {
437 pub fn new() -> Self {
438 Self::default()
439 }
440}
441pub struct RequestDeviceCallbackInfo {
442 pub mode: Option<CallbackMode>,
443 pub callback: std::cell::RefCell<Option<RequestDeviceCallback>>,
444}
445impl Default for RequestDeviceCallbackInfo {
446 fn default() -> Self {
447 Self {
448 mode: None,
449 callback: std::cell::RefCell::new(None),
450 }
451 }
452}
453impl RequestDeviceCallbackInfo {
454 pub fn new() -> Self {
455 Self::default()
456 }
457}
458pub struct UncapturedErrorCallbackInfo {
459 pub callback: std::cell::RefCell<Option<UncapturedErrorCallback>>,
460}
461impl Default for UncapturedErrorCallbackInfo {
462 fn default() -> Self {
463 Self {
464 callback: std::cell::RefCell::new(None),
465 }
466 }
467}
468impl UncapturedErrorCallbackInfo {
469 pub fn new() -> Self {
470 Self::default()
471 }
472}