Skip to content
This repository was archived by the owner on Jun 18, 2021. It is now read-only.

Commit ab1bb53

Browse files
committed
Expose more functions on the web backend
1 parent db6ab34 commit ab1bb53

File tree

3 files changed

+326
-158
lines changed

3 files changed

+326
-158
lines changed

src/backend/native.rs

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,10 @@ pub(crate) fn buffer_unmap(buffer: &BufferId) {
351351
wgn::wgpu_buffer_unmap(*buffer);
352352
}
353353

354+
pub(crate) fn buffer_drop(buffer: &BufferId) {
355+
wgn::wgpu_buffer_destroy(*buffer);
356+
}
357+
354358
pub(crate) fn device_create_buffer(device: &DeviceId, desc: &BufferDescriptor) -> crate::Buffer {
355359
let owned_label = OwnedLabel::new(desc.label.as_deref());
356360
crate::Buffer {
@@ -445,6 +449,20 @@ pub(crate) fn command_encoder_copy_texture_to_buffer(
445449
);
446450
}
447451

452+
pub(crate) fn command_encoder_copy_texture_to_texture(
453+
command_encoder: &CommandEncoderId,
454+
source: crate::TextureCopyView,
455+
destination: crate::TextureCopyView,
456+
copy_size: wgt::Extent3d,
457+
) {
458+
wgn::wgpu_command_encoder_copy_texture_to_texture(
459+
*command_encoder,
460+
&map_texture_copy_view(source),
461+
&map_texture_copy_view(destination),
462+
copy_size,
463+
);
464+
}
465+
448466
pub(crate) fn begin_compute_pass(command_encoder: &CommandEncoderId) -> ComputePassId {
449467
unsafe { wgn::wgpu_command_encoder_begin_compute_pass(*command_encoder, None) }
450468
}
@@ -554,10 +572,50 @@ pub(crate) fn buffer_map_read(
554572
future
555573
}
556574

575+
pub(crate) fn buffer_map_write(
576+
buffer: &crate::Buffer,
577+
start: wgt::BufferAddress,
578+
size: wgt::BufferAddress,
579+
) -> impl Future<Output = Result<crate::BufferWriteMapping, crate::BufferAsyncErr>> {
580+
let (future, completion) = native_gpu_future::new_gpu_future(buffer.id, size);
581+
582+
extern "C" fn buffer_map_write_future_wrapper(
583+
status: wgc::resource::BufferMapAsyncStatus,
584+
data: *mut u8,
585+
user_data: *mut u8,
586+
) {
587+
let completion =
588+
unsafe { native_gpu_future::GpuFutureCompletion::from_raw(user_data as _) };
589+
let (buffer_id, size) = completion.get_buffer_info();
590+
591+
if let wgc::resource::BufferMapAsyncStatus::Success = status {
592+
completion.complete(Ok(crate::BufferWriteMapping {
593+
detail: BufferWriteMappingDetail {
594+
data,
595+
size: size as usize,
596+
buffer_id,
597+
},
598+
}));
599+
} else {
600+
completion.complete(Err(crate::BufferAsyncErr));
601+
}
602+
}
603+
604+
wgn::wgpu_buffer_map_write_async(
605+
buffer.id,
606+
start,
607+
size,
608+
buffer_map_write_future_wrapper,
609+
completion.to_raw() as _,
610+
);
611+
612+
future
613+
}
614+
557615
pub(crate) struct BufferReadMappingDetail {
616+
pub(crate) buffer_id: BufferId,
558617
data: *const u8,
559618
size: usize,
560-
pub(crate) buffer_id: BufferId,
561619
}
562620

563621
impl BufferReadMappingDetail {
@@ -566,6 +624,18 @@ impl BufferReadMappingDetail {
566624
}
567625
}
568626

627+
pub(crate) struct BufferWriteMappingDetail {
628+
pub(crate) buffer_id: BufferId,
629+
data: *mut u8,
630+
size: usize,
631+
}
632+
633+
impl BufferWriteMappingDetail {
634+
pub(crate) fn as_slice(&mut self) -> &mut [u8] {
635+
unsafe { slice::from_raw_parts_mut(self.data as *mut u8, self.size) }
636+
}
637+
}
638+
569639
pub(crate) fn device_create_surface<W: raw_window_handle::HasRawWindowHandle>(
570640
window: &W,
571641
) -> SurfaceId {
@@ -633,6 +703,12 @@ pub(crate) fn render_pass_set_pipeline(
633703
}
634704
}
635705

706+
pub(crate) fn render_pass_set_blend_color(render_pass: &RenderPassEncoderId, color: wgt::Color) {
707+
unsafe {
708+
wgn::wgpu_render_pass_set_blend_color(render_pass.as_mut().unwrap(), &color);
709+
}
710+
}
711+
636712
pub(crate) fn render_pass_set_bind_group(
637713
render_pass: &RenderPassEncoderId,
638714
index: u32,
@@ -684,6 +760,46 @@ pub(crate) fn render_pass_set_vertex_buffer<'a>(
684760
};
685761
}
686762

763+
pub(crate) fn render_pass_set_scissor_rect(
764+
render_pass: &RenderPassEncoderId,
765+
x: u32,
766+
y: u32,
767+
width: u32,
768+
height: u32,
769+
) {
770+
unsafe {
771+
wgn::wgpu_render_pass_set_scissor_rect(render_pass.as_mut().unwrap(), x, y, width, height);
772+
}
773+
}
774+
775+
pub(crate) fn render_pass_set_viewport(
776+
render_pass: &RenderPassEncoderId,
777+
x: f32,
778+
y: f32,
779+
width: f32,
780+
height: f32,
781+
min_depth: f32,
782+
max_depth: f32,
783+
) {
784+
unsafe {
785+
wgn::wgpu_render_pass_set_viewport(
786+
render_pass.as_mut().unwrap(),
787+
x,
788+
y,
789+
width,
790+
height,
791+
min_depth,
792+
max_depth,
793+
);
794+
}
795+
}
796+
797+
pub(crate) fn render_pass_set_stencil_reference(render_pass: &RenderPassEncoderId, reference: u32) {
798+
unsafe {
799+
wgn::wgpu_render_pass_set_stencil_reference(render_pass.as_mut().unwrap(), reference);
800+
}
801+
}
802+
687803
pub(crate) fn render_pass_draw(
688804
render_pass: &RenderPassEncoderId,
689805
vertices: Range<u32>,
@@ -718,6 +834,34 @@ pub(crate) fn render_pass_draw_indexed(
718834
}
719835
}
720836

837+
pub(crate) fn render_pass_draw_indirect<'a>(
838+
render_pass: &RenderPassEncoderId,
839+
indirect_buffer: &'a crate::Buffer,
840+
indirect_offset: wgt::BufferAddress,
841+
) {
842+
unsafe {
843+
wgn::wgpu_render_pass_draw_indirect(
844+
render_pass.as_mut().unwrap(),
845+
indirect_buffer.id,
846+
indirect_offset,
847+
);
848+
}
849+
}
850+
851+
pub(crate) fn render_pass_draw_indexed_indirect<'a>(
852+
render_pass: &RenderPassEncoderId,
853+
indirect_buffer: &'a crate::Buffer,
854+
indirect_offset: wgt::BufferAddress,
855+
) {
856+
unsafe {
857+
wgn::wgpu_render_pass_draw_indexed_indirect(
858+
render_pass.as_mut().unwrap(),
859+
indirect_buffer.id,
860+
indirect_offset,
861+
);
862+
}
863+
}
864+
721865
pub(crate) fn render_pass_end_pass(render_pass: &RenderPassEncoderId) {
722866
unsafe {
723867
wgn::wgpu_render_pass_end_pass(*render_pass);
@@ -731,6 +875,14 @@ pub(crate) fn texture_create_view(
731875
wgn::wgpu_texture_create_view(*texture, desc)
732876
}
733877

878+
pub(crate) fn texture_drop(texture: &TextureId) {
879+
wgn::wgpu_texture_destroy(*texture);
880+
}
881+
882+
pub(crate) fn texture_view_drop(texture_view: &TextureViewId) {
883+
wgn::wgpu_texture_view_destroy(*texture_view);
884+
}
885+
734886
pub(crate) fn swap_chain_present(swap_chain: &SwapChainId) {
735887
wgn::wgpu_swap_chain_present(*swap_chain);
736888
}

src/backend/web.rs

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ fn map_address_mode(mode: wgt::AddressMode) -> web_sys::GpuAddressMode {
533533
}
534534
}
535535

536+
fn map_color(color: wgt::Color) -> web_sys::GpuColorDict {
537+
web_sys::GpuColorDict::new(color.a, color.b, color.g, color.r)
538+
}
539+
536540
pub(crate) fn create_render_pipeline(
537541
device: &DeviceId,
538542
desc: &RenderPipelineDescriptor,
@@ -665,6 +669,10 @@ pub(crate) fn buffer_unmap(buffer: &BufferId) {
665669
buffer.unmap();
666670
}
667671

672+
pub(crate) fn buffer_drop(_buffer: &BufferId) {
673+
// Buffer is dropped automatically
674+
}
675+
668676
pub(crate) fn device_create_buffer(device: &DeviceId, desc: &BufferDescriptor) -> crate::Buffer {
669677
let mapped_desc = web_sys::GpuBufferDescriptor::new(desc.size as f64, desc.usage.bits());
670678
crate::Buffer {
@@ -752,6 +760,19 @@ pub(crate) fn command_encoder_copy_texture_to_buffer(
752760
);
753761
}
754762

763+
pub(crate) fn command_encoder_copy_texture_to_texture(
764+
command_encoder: &CommandEncoderId,
765+
source: crate::TextureCopyView,
766+
destination: crate::TextureCopyView,
767+
copy_size: wgt::Extent3d,
768+
) {
769+
command_encoder.copy_texture_to_texture_with_gpu_extent_3d_dict(
770+
&map_texture_copy_view(source),
771+
&map_texture_copy_view(destination),
772+
&map_extent_3d(copy_size),
773+
);
774+
}
775+
755776
pub(crate) fn begin_compute_pass(command_encoder: &CommandEncoderId) -> ComputePassId {
756777
let mapped_desc = web_sys::GpuComputePassDescriptor::new();
757778
command_encoder.begin_compute_pass_with_descriptor(&mapped_desc)
@@ -831,6 +852,26 @@ pub(crate) async fn buffer_map_read(
831852
})
832853
}
833854

855+
pub(crate) async fn buffer_map_write(
856+
buffer: &crate::Buffer,
857+
_start: wgt::BufferAddress,
858+
_size: wgt::BufferAddress,
859+
) -> Result<crate::BufferWriteMapping, crate::BufferAsyncErr> {
860+
let array_buffer_promise = buffer.id.map_write_async();
861+
let array_buffer: js_sys::ArrayBuffer =
862+
wasm_bindgen_futures::JsFuture::from(array_buffer_promise)
863+
.await
864+
.expect("Unable to map buffer")
865+
.into();
866+
let view = js_sys::Uint8Array::new(&array_buffer);
867+
Ok(crate::BufferWriteMapping {
868+
detail: BufferWriteMappingDetail {
869+
buffer_id: buffer.id.clone(),
870+
mapped: view.to_vec(),
871+
},
872+
})
873+
}
874+
834875
pub(crate) struct BufferReadMappingDetail {
835876
pub(crate) buffer_id: BufferId,
836877
mapped: Vec<u8>,
@@ -842,6 +883,17 @@ impl BufferReadMappingDetail {
842883
}
843884
}
844885

886+
pub(crate) struct BufferWriteMappingDetail {
887+
pub(crate) buffer_id: BufferId,
888+
mapped: Vec<u8>,
889+
}
890+
891+
impl BufferWriteMappingDetail {
892+
pub(crate) fn as_slice(&mut self) -> &mut [u8] {
893+
&mut self.mapped[..]
894+
}
895+
}
896+
845897
pub(crate) fn device_create_surface<W: raw_window_handle::HasRawWindowHandle>(
846898
window: &W,
847899
) -> SurfaceId {
@@ -901,12 +953,7 @@ pub(crate) fn command_encoder_begin_render_pass<'a>(
901953
let mut mapped_color_attachment = web_sys::GpuRenderPassColorAttachmentDescriptor::new(
902954
&ca.attachment.id,
903955
&match ca.load_op {
904-
wgt::LoadOp::Clear => {
905-
let color = ca.clear_color;
906-
let mapped_color =
907-
web_sys::GpuColorDict::new(color.a, color.b, color.g, color.r);
908-
wasm_bindgen::JsValue::from(mapped_color)
909-
}
956+
wgt::LoadOp::Clear => wasm_bindgen::JsValue::from(map_color(ca.clear_color)),
910957
wgt::LoadOp::Load => wasm_bindgen::JsValue::from(web_sys::GpuLoadOp::Load),
911958
},
912959
);
@@ -952,6 +999,10 @@ pub(crate) fn render_pass_set_pipeline(
952999
render_pass.set_pipeline(&pipeline);
9531000
}
9541001

1002+
pub(crate) fn render_pass_set_blend_color(render_pass: &RenderPassEncoderId, color: wgt::Color) {
1003+
render_pass.set_blend_color_with_gpu_color_dict(&map_color(color));
1004+
}
1005+
9551006
pub(crate) fn render_pass_set_bind_group(
9561007
render_pass: &RenderPassEncoderId,
9571008
index: u32,
@@ -997,6 +1048,32 @@ pub(crate) fn render_pass_set_vertex_buffer<'a>(
9971048
);
9981049
}
9991050

1051+
pub(crate) fn render_pass_set_scissor_rect(
1052+
render_pass: &RenderPassEncoderId,
1053+
x: u32,
1054+
y: u32,
1055+
width: u32,
1056+
height: u32,
1057+
) {
1058+
render_pass.set_scissor_rect(x, y, width, height);
1059+
}
1060+
1061+
pub(crate) fn render_pass_set_viewport(
1062+
render_pass: &RenderPassEncoderId,
1063+
x: f32,
1064+
y: f32,
1065+
width: f32,
1066+
height: f32,
1067+
min_depth: f32,
1068+
max_depth: f32,
1069+
) {
1070+
render_pass.set_viewport(x, y, width, height, min_depth, max_depth);
1071+
}
1072+
1073+
pub(crate) fn render_pass_set_stencil_reference(render_pass: &RenderPassEncoderId, reference: u32) {
1074+
render_pass.set_stencil_reference(reference);
1075+
}
1076+
10001077
pub(crate) fn render_pass_draw(
10011078
render_pass: &RenderPassEncoderId,
10021079
vertices: Range<u32>,
@@ -1026,6 +1103,22 @@ pub(crate) fn render_pass_draw_indexed(
10261103
);
10271104
}
10281105

1106+
pub(crate) fn render_pass_draw_indirect<'a>(
1107+
render_pass: &RenderPassEncoderId,
1108+
indirect_buffer: &'a crate::Buffer,
1109+
indirect_offset: wgt::BufferAddress,
1110+
) {
1111+
render_pass.draw_indirect_with_f64(&indirect_buffer.id, indirect_offset as f64);
1112+
}
1113+
1114+
pub(crate) fn render_pass_draw_indexed_indirect<'a>(
1115+
render_pass: &RenderPassEncoderId,
1116+
indirect_buffer: &'a crate::Buffer,
1117+
indirect_offset: wgt::BufferAddress,
1118+
) {
1119+
render_pass.draw_indexed_indirect_with_f64(&indirect_buffer.id, indirect_offset as f64);
1120+
}
1121+
10291122
pub(crate) fn render_pass_end_pass(render_pass: &RenderPassEncoderId) {
10301123
render_pass.end_pass();
10311124
}
@@ -1050,6 +1143,14 @@ pub(crate) fn texture_create_view(
10501143
}
10511144
}
10521145

1146+
pub(crate) fn texture_drop(_texture: &TextureId) {
1147+
// Texture is dropped automatically
1148+
}
1149+
1150+
pub(crate) fn texture_view_drop(_texture_view: &TextureViewId) {
1151+
// Texture view is dropped automatically
1152+
}
1153+
10531154
pub(crate) fn swap_chain_present(_swap_chain: &SwapChainId) {
10541155
// Swapchain is presented automatically
10551156
}

0 commit comments

Comments
 (0)