@@ -6,8 +6,9 @@ use crate::{
6
6
} ;
7
7
8
8
use arrayvec:: ArrayVec ;
9
+ use futures:: future:: { ready, Ready } ;
9
10
use smallvec:: SmallVec ;
10
- use std:: { ffi:: CString , future :: Future , marker:: PhantomData , ptr, slice} ;
11
+ use std:: { ffi:: CString , marker:: PhantomData , ptr, slice} ;
11
12
12
13
macro_rules! gfx_select {
13
14
( $id: expr => $global: ident. $method: ident( $( $param: expr) ,+ ) ) => {
@@ -25,31 +26,6 @@ macro_rules! gfx_select {
25
26
} ;
26
27
}
27
28
28
- pub ( crate ) async fn request_adapter (
29
- context : & Context ,
30
- options : & crate :: RequestAdapterOptions < ' _ > ,
31
- backends : wgt:: BackendBit ,
32
- ) -> Option < wgc:: id:: AdapterId > {
33
- context. pick_adapter (
34
- & wgc:: instance:: RequestAdapterOptions {
35
- power_preference : options. power_preference ,
36
- compatible_surface : options. compatible_surface . map ( |surface| surface. id ) ,
37
- } ,
38
- wgc:: instance:: AdapterInputs :: Mask ( backends, || PhantomData ) ,
39
- )
40
- }
41
-
42
- pub ( crate ) async fn request_device_and_queue (
43
- context : & Context ,
44
- adapter : & wgc:: id:: AdapterId ,
45
- desc : Option < & wgt:: DeviceDescriptor > ,
46
- ) -> ( wgc:: id:: DeviceId , wgc:: id:: QueueId ) {
47
- let desc = & desc. cloned ( ) . unwrap_or_default ( ) ;
48
- let device_id =
49
- gfx_select ! ( * adapter => context. adapter_request_device( * adapter, desc, PhantomData ) ) ;
50
- ( device_id, device_id)
51
- }
52
-
53
29
fn map_buffer_copy_view ( view : crate :: BufferCopyView < ' _ > ) -> wgc:: command:: BufferCopyView {
54
30
wgc:: command:: BufferCopyView {
55
31
buffer : view. buffer . id ,
@@ -234,11 +210,18 @@ impl crate::Context for Context {
234
210
type BufferWriteMappingDetail = BufferWriteMappingDetail ;
235
211
type SwapChainOutputDetail = SwapChainOutputDetail ;
236
212
213
+ type RequestAdapterFuture = Ready < Option < Self :: AdapterId > > ;
214
+ type RequestDeviceFuture = Ready < ( Self :: DeviceId , Self :: QueueId ) > ;
215
+ type MapReadFuture =
216
+ native_gpu_future:: GpuFuture < Result < BufferReadMappingDetail , crate :: BufferAsyncErr > > ;
217
+ type MapWriteFuture =
218
+ native_gpu_future:: GpuFuture < Result < BufferWriteMappingDetail , crate :: BufferAsyncErr > > ;
219
+
237
220
fn init ( ) -> Self {
238
221
wgc:: hub:: Global :: new ( "wgpu" , wgc:: hub:: IdentityManagerFactory )
239
222
}
240
223
241
- fn create_surface < W : raw_window_handle:: HasRawWindowHandle > (
224
+ fn instance_create_surface < W : raw_window_handle:: HasRawWindowHandle > (
242
225
& self ,
243
226
window : & W ,
244
227
) -> Self :: SurfaceId {
@@ -314,6 +297,31 @@ impl crate::Context for Context {
314
297
. register_identity ( PhantomData , surface, & mut token)
315
298
}
316
299
300
+ fn instance_request_adapter (
301
+ & self ,
302
+ options : & crate :: RequestAdapterOptions < ' _ > ,
303
+ backends : wgt:: BackendBit ,
304
+ ) -> Self :: RequestAdapterFuture {
305
+ let id = self . pick_adapter (
306
+ & wgc:: instance:: RequestAdapterOptions {
307
+ power_preference : options. power_preference ,
308
+ compatible_surface : options. compatible_surface . map ( |surface| surface. id ) ,
309
+ } ,
310
+ wgc:: instance:: AdapterInputs :: Mask ( backends, || PhantomData ) ,
311
+ ) ;
312
+ ready ( id)
313
+ }
314
+
315
+ fn adapter_request_device (
316
+ & self ,
317
+ adapter : & Self :: AdapterId ,
318
+ desc : & crate :: DeviceDescriptor ,
319
+ ) -> Self :: RequestDeviceFuture {
320
+ let device_id =
321
+ gfx_select ! ( * adapter => self . adapter_request_device( * adapter, desc, PhantomData ) ) ;
322
+ ready ( ( device_id, device_id) )
323
+ }
324
+
317
325
fn device_create_swap_chain (
318
326
& self ,
319
327
device : & Self :: DeviceId ,
@@ -662,6 +670,80 @@ impl crate::Context for Context {
662
670
) ) ;
663
671
}
664
672
673
+ fn buffer_map_read (
674
+ & self ,
675
+ buffer : & Self :: BufferId ,
676
+ start : wgt:: BufferAddress ,
677
+ size : wgt:: BufferAddress ,
678
+ ) -> Self :: MapReadFuture {
679
+ let ( future, completion) = native_gpu_future:: new_gpu_future ( * buffer, size) ;
680
+
681
+ extern "C" fn buffer_map_read_future_wrapper (
682
+ status : wgc:: resource:: BufferMapAsyncStatus ,
683
+ data : * const u8 ,
684
+ user_data : * mut u8 ,
685
+ ) {
686
+ let completion =
687
+ unsafe { native_gpu_future:: GpuFutureCompletion :: from_raw ( user_data as _ ) } ;
688
+ let ( buffer_id, size) = completion. get_buffer_info ( ) ;
689
+
690
+ if let wgc:: resource:: BufferMapAsyncStatus :: Success = status {
691
+ completion. complete ( Ok ( BufferReadMappingDetail {
692
+ data,
693
+ size : size as usize ,
694
+ buffer_id,
695
+ } ) ) ;
696
+ } else {
697
+ completion. complete ( Err ( crate :: BufferAsyncErr ) ) ;
698
+ }
699
+ }
700
+
701
+ let operation = wgc:: resource:: BufferMapOperation :: Read {
702
+ callback : buffer_map_read_future_wrapper,
703
+ userdata : completion. to_raw ( ) as _ ,
704
+ } ;
705
+ gfx_select ! ( * buffer => self . buffer_map_async( * buffer, start .. start + size, operation) ) ;
706
+
707
+ future
708
+ }
709
+
710
+ fn buffer_map_write (
711
+ & self ,
712
+ buffer : & Self :: BufferId ,
713
+ start : wgt:: BufferAddress ,
714
+ size : wgt:: BufferAddress ,
715
+ ) -> Self :: MapWriteFuture {
716
+ let ( future, completion) = native_gpu_future:: new_gpu_future ( * buffer, size) ;
717
+
718
+ extern "C" fn buffer_map_write_future_wrapper (
719
+ status : wgc:: resource:: BufferMapAsyncStatus ,
720
+ data : * mut u8 ,
721
+ user_data : * mut u8 ,
722
+ ) {
723
+ let completion =
724
+ unsafe { native_gpu_future:: GpuFutureCompletion :: from_raw ( user_data as _ ) } ;
725
+ let ( buffer_id, size) = completion. get_buffer_info ( ) ;
726
+
727
+ if let wgc:: resource:: BufferMapAsyncStatus :: Success = status {
728
+ completion. complete ( Ok ( BufferWriteMappingDetail {
729
+ data,
730
+ size : size as usize ,
731
+ buffer_id,
732
+ } ) ) ;
733
+ } else {
734
+ completion. complete ( Err ( crate :: BufferAsyncErr ) ) ;
735
+ }
736
+ }
737
+
738
+ let operation = wgc:: resource:: BufferMapOperation :: Write {
739
+ callback : buffer_map_write_future_wrapper,
740
+ userdata : completion. to_raw ( ) as _ ,
741
+ } ;
742
+ gfx_select ! ( * buffer => self . buffer_map_async( * buffer, start .. start + size, operation) ) ;
743
+
744
+ future
745
+ }
746
+
665
747
fn buffer_unmap ( & self , buffer : & Self :: BufferId ) {
666
748
gfx_select ! ( * buffer => self . buffer_unmap( * buffer) )
667
749
}
@@ -883,80 +965,6 @@ impl crate::Context for Context {
883
965
884
966
pub ( crate ) struct CreateBufferMappedDetail ;
885
967
886
- pub ( crate ) fn buffer_map_read (
887
- context : & Context ,
888
- buffer : & wgc:: id:: BufferId ,
889
- start : wgt:: BufferAddress ,
890
- size : wgt:: BufferAddress ,
891
- ) -> impl Future < Output = Result < BufferReadMappingDetail , crate :: BufferAsyncErr > > {
892
- let ( future, completion) = native_gpu_future:: new_gpu_future ( * buffer, size) ;
893
-
894
- extern "C" fn buffer_map_read_future_wrapper (
895
- status : wgc:: resource:: BufferMapAsyncStatus ,
896
- data : * const u8 ,
897
- user_data : * mut u8 ,
898
- ) {
899
- let completion =
900
- unsafe { native_gpu_future:: GpuFutureCompletion :: from_raw ( user_data as _ ) } ;
901
- let ( buffer_id, size) = completion. get_buffer_info ( ) ;
902
-
903
- if let wgc:: resource:: BufferMapAsyncStatus :: Success = status {
904
- completion. complete ( Ok ( BufferReadMappingDetail {
905
- data,
906
- size : size as usize ,
907
- buffer_id,
908
- } ) ) ;
909
- } else {
910
- completion. complete ( Err ( crate :: BufferAsyncErr ) ) ;
911
- }
912
- }
913
-
914
- let operation = wgc:: resource:: BufferMapOperation :: Read {
915
- callback : buffer_map_read_future_wrapper,
916
- userdata : completion. to_raw ( ) as _ ,
917
- } ;
918
- gfx_select ! ( * buffer => context. buffer_map_async( * buffer, start .. start + size, operation) ) ;
919
-
920
- future
921
- }
922
-
923
- pub ( crate ) fn buffer_map_write (
924
- context : & Context ,
925
- buffer : & wgc:: id:: BufferId ,
926
- start : wgt:: BufferAddress ,
927
- size : wgt:: BufferAddress ,
928
- ) -> impl Future < Output = Result < BufferWriteMappingDetail , crate :: BufferAsyncErr > > {
929
- let ( future, completion) = native_gpu_future:: new_gpu_future ( * buffer, size) ;
930
-
931
- extern "C" fn buffer_map_write_future_wrapper (
932
- status : wgc:: resource:: BufferMapAsyncStatus ,
933
- data : * mut u8 ,
934
- user_data : * mut u8 ,
935
- ) {
936
- let completion =
937
- unsafe { native_gpu_future:: GpuFutureCompletion :: from_raw ( user_data as _ ) } ;
938
- let ( buffer_id, size) = completion. get_buffer_info ( ) ;
939
-
940
- if let wgc:: resource:: BufferMapAsyncStatus :: Success = status {
941
- completion. complete ( Ok ( BufferWriteMappingDetail {
942
- data,
943
- size : size as usize ,
944
- buffer_id,
945
- } ) ) ;
946
- } else {
947
- completion. complete ( Err ( crate :: BufferAsyncErr ) ) ;
948
- }
949
- }
950
-
951
- let operation = wgc:: resource:: BufferMapOperation :: Write {
952
- callback : buffer_map_write_future_wrapper,
953
- userdata : completion. to_raw ( ) as _ ,
954
- } ;
955
- gfx_select ! ( * buffer => context. buffer_map_async( * buffer, start .. start + size, operation) ) ;
956
-
957
- future
958
- }
959
-
960
968
pub ( crate ) struct BufferReadMappingDetail {
961
969
pub ( crate ) buffer_id : wgc:: id:: BufferId ,
962
970
data : * const u8 ,
0 commit comments