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

Commit ab86d7c

Browse files
committed
Fix some remaining TODOs in the web backend
1 parent ab1bb53 commit ab86d7c

File tree

4 files changed

+104
-68
lines changed

4 files changed

+104
-68
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ parking_lot = "0.10"
4949

5050
[dev-dependencies]
5151
cgmath = "0.17"
52-
#glsl-to-spirv = "0.1"
5352
log = "0.4"
5453
png = "0.15"
5554
winit = { version = "0.22.1", features = ["web-sys"] }
@@ -154,6 +153,7 @@ web-sys = { version = "0.3.37", features = [
154153
"GpuSwapChainDescriptor",
155154
"GpuTexture",
156155
"GpuTextureAspect",
156+
"GpuTextureComponentType",
157157
"GpuTextureCopyView",
158158
"GpuTextureDescriptor",
159159
"GpuTextureDimension",

src/backend/native.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,32 @@ pub(crate) fn device_create_swap_chain(
650650
wgn::wgpu_device_create_swap_chain(*device, *surface, desc)
651651
}
652652

653-
pub(crate) fn swap_chain_get_next_texture(swap_chain: &SwapChainId) -> Option<TextureViewId> {
654-
wgn::wgpu_swap_chain_get_next_texture(*swap_chain).view_id
653+
pub(crate) fn device_drop(device: &DeviceId) {
654+
#[cfg(not(target_arch = "wasm32"))]
655+
wgn::wgpu_device_poll(*device, true);
656+
//TODO: make this work in general
657+
#[cfg(not(target_arch = "wasm32"))]
658+
#[cfg(feature = "metal-auto-capture")]
659+
wgn::wgpu_device_destroy(*device);
660+
}
661+
662+
pub(crate) fn swap_chain_get_next_texture(
663+
swap_chain: &SwapChainId,
664+
) -> Result<crate::SwapChainOutput, crate::TimeOut> {
665+
match wgn::wgpu_swap_chain_get_next_texture(*swap_chain).view_id {
666+
Some(id) => Ok(crate::SwapChainOutput {
667+
view: crate::TextureView { id, owned: false },
668+
detail: SwapChainOutputDetail {
669+
swap_chain_id: *swap_chain,
670+
},
671+
}),
672+
None => Err(crate::TimeOut),
673+
}
674+
}
675+
676+
#[derive(Debug)]
677+
pub(crate) struct SwapChainOutputDetail {
678+
swap_chain_id: SwapChainId,
655679
}
656680

657681
pub(crate) fn command_encoder_begin_render_pass<'a>(
@@ -883,8 +907,12 @@ pub(crate) fn texture_view_drop(texture_view: &TextureViewId) {
883907
wgn::wgpu_texture_view_destroy(*texture_view);
884908
}
885909

886-
pub(crate) fn swap_chain_present(swap_chain: &SwapChainId) {
887-
wgn::wgpu_swap_chain_present(*swap_chain);
910+
pub(crate) fn bind_group_drop(bind_group: &BindGroupId) {
911+
wgn::wgpu_bind_group_destroy(*bind_group);
912+
}
913+
914+
pub(crate) fn swap_chain_present(swap_chain_output: &crate::SwapChainOutput) {
915+
wgn::wgpu_swap_chain_present(swap_chain_output.detail.swap_chain_id);
888916
}
889917

890918
pub(crate) fn device_poll(device: &DeviceId, maintain: crate::Maintain) {

src/backend/web.rs

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ pub(crate) async fn request_device_and_queue(
6767
// TODO: Extensions
6868
let mut mapped_limits = web_sys::GpuLimits::new();
6969
mapped_limits.max_bind_groups(d.limits.max_bind_groups);
70-
// TODO: Other fields
7170
mapped_descriptor.limits(&mapped_limits);
7271
adapter.request_device_with_descriptor(&mapped_descriptor)
7372
}
@@ -109,35 +108,43 @@ pub(crate) fn create_bind_group_layout(
109108
BindingType::StorageTexture { .. } => bt::WriteonlyStorageTexture,
110109
};
111110

112-
let mapped_dynamic = match bind.ty {
111+
let mut mapped_entry = web_sys::GpuBindGroupLayoutEntry::new(
112+
bind.binding,
113+
mapped_type,
114+
bind.visibility.bits(),
115+
);
116+
117+
match bind.ty {
113118
BindingType::UniformBuffer { dynamic }
114-
| BindingType::StorageBuffer { dynamic, .. } => dynamic,
115-
_ => false,
116-
};
119+
| BindingType::StorageBuffer { dynamic, .. } => {
120+
mapped_entry.has_dynamic_offset(dynamic);
121+
}
122+
_ => {}
123+
}
117124

118-
let mapped_multisampled = match bind.ty {
119-
BindingType::SampledTexture { multisampled, .. } => multisampled,
120-
_ => false,
121-
};
125+
if let BindingType::SampledTexture { multisampled, .. } = bind.ty {
126+
mapped_entry.multisampled(multisampled);
127+
}
122128

123-
let mapped_view_dimension = match bind.ty {
129+
match bind.ty {
124130
BindingType::SampledTexture { dimension, .. }
125131
| BindingType::StorageTexture { dimension, .. } => {
126-
map_texture_view_dimension(dimension)
132+
mapped_entry.view_dimension(map_texture_view_dimension(dimension));
127133
}
128-
_ => web_sys::GpuTextureViewDimension::N2d,
129-
};
134+
_ => {}
135+
}
130136

131-
let mut mapped_entry = web_sys::GpuBindGroupLayoutEntry::new(
132-
bind.binding,
133-
mapped_type,
134-
bind.visibility.bits(),
135-
);
136-
mapped_entry.has_dynamic_offset(mapped_dynamic);
137-
mapped_entry.multisampled(mapped_multisampled);
138-
mapped_entry.view_dimension(mapped_view_dimension);
137+
if let BindingType::StorageTexture { format, .. } = bind.ty {
138+
mapped_entry.storage_texture_format(map_texture_format(format));
139+
}
139140

140-
// TODO: Texture component type, storage texture format
141+
match bind.ty {
142+
BindingType::SampledTexture { component_type, .. }
143+
| BindingType::StorageTexture { component_type, .. } => {
144+
mapped_entry.texture_component_type(map_texture_component_type(component_type));
145+
}
146+
_ => {}
147+
}
141148

142149
mapped_entry
143150
})
@@ -180,7 +187,6 @@ pub(crate) fn create_pipeline_layout(
180187
device: &DeviceId,
181188
desc: &PipelineLayoutDescriptor,
182189
) -> PipelineLayoutId {
183-
//TODO: avoid allocation here
184190
let temp_layouts = desc
185191
.bind_group_layouts
186192
.iter()
@@ -235,6 +241,16 @@ fn map_texture_format(texture_format: wgt::TextureFormat) -> web_sys::GpuTexture
235241
}
236242
}
237243

244+
fn map_texture_component_type(
245+
texture_component_type: wgt::TextureComponentType,
246+
) -> web_sys::GpuTextureComponentType {
247+
match texture_component_type {
248+
wgt::TextureComponentType::Float => web_sys::GpuTextureComponentType::Float,
249+
wgt::TextureComponentType::Sint => web_sys::GpuTextureComponentType::Sint,
250+
wgt::TextureComponentType::Uint => web_sys::GpuTextureComponentType::Uint,
251+
}
252+
}
253+
238254
fn map_stage_descriptor(
239255
desc: &ProgrammableStageDescriptor,
240256
) -> web_sys::GpuProgrammableStageDescriptor {
@@ -794,9 +810,7 @@ pub(crate) fn compute_pass_set_bind_group<'a>(
794810
compute_pass.set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
795811
index,
796812
bind_group,
797-
// TODO: `offsets` currently requires `&mut` so we have to clone it
798-
// here, but this should be fixed upstream in web-sys in the future
799-
&mut offsets.to_vec(),
813+
offsets,
800814
0f64,
801815
offsets.len() as u32,
802816
);
@@ -929,12 +943,26 @@ pub(crate) fn device_create_swap_chain(
929943
surface.configure_swap_chain(&mapped)
930944
}
931945

932-
pub(crate) fn swap_chain_get_next_texture(swap_chain: &SwapChainId) -> Option<TextureViewId> {
946+
pub(crate) fn device_drop(_device: &DeviceId) {
947+
// Device is dropped automatically
948+
}
949+
950+
pub(crate) fn swap_chain_get_next_texture(
951+
swap_chain: &SwapChainId,
952+
) -> Result<crate::SwapChainOutput, crate::TimeOut> {
933953
// TODO: Should we pass a descriptor here?
934954
// Or is the default view always correct?
935-
Some(swap_chain.get_current_texture().create_view())
955+
Ok(crate::SwapChainOutput {
956+
view: crate::TextureView {
957+
id: swap_chain.get_current_texture().create_view(),
958+
owned: false,
959+
},
960+
detail: (),
961+
})
936962
}
937963

964+
pub(crate) type SwapChainOutputDetail = ();
965+
938966
fn map_store_op(op: wgt::StoreOp) -> web_sys::GpuStoreOp {
939967
match op {
940968
wgt::StoreOp::Clear => web_sys::GpuStoreOp::Clear,
@@ -1012,9 +1040,7 @@ pub(crate) fn render_pass_set_bind_group(
10121040
render_pass.set_bind_group_with_u32_array_and_f64_and_dynamic_offsets_data_length(
10131041
index,
10141042
bind_group,
1015-
// TODO: `offsets` currently requires `&mut` so we have to clone it
1016-
// here, but this should be fixed upstream in web-sys in the future
1017-
&mut offsets.to_vec(),
1043+
offsets,
10181044
0f64,
10191045
offsets.len() as u32,
10201046
);
@@ -1024,28 +1050,19 @@ pub(crate) fn render_pass_set_index_buffer<'a>(
10241050
render_pass: &RenderPassEncoderId,
10251051
buffer: &'a crate::Buffer,
10261052
offset: wgt::BufferAddress,
1027-
_size: wgt::BufferAddress,
1053+
size: wgt::BufferAddress,
10281054
) {
1029-
render_pass.set_index_buffer_with_f64(
1030-
&buffer.id,
1031-
offset as f64,
1032-
// TODO: size,
1033-
);
1055+
render_pass.set_index_buffer_with_f64_and_f64(&buffer.id, offset as f64, size as f64);
10341056
}
10351057

10361058
pub(crate) fn render_pass_set_vertex_buffer<'a>(
10371059
render_pass: &RenderPassEncoderId,
10381060
slot: u32,
10391061
buffer: &'a crate::Buffer,
10401062
offset: wgt::BufferAddress,
1041-
_size: wgt::BufferAddress,
1063+
size: wgt::BufferAddress,
10421064
) {
1043-
render_pass.set_vertex_buffer_with_f64(
1044-
slot,
1045-
&buffer.id,
1046-
offset as f64,
1047-
// TODO: size,
1048-
);
1065+
render_pass.set_vertex_buffer_with_f64_and_f64(slot, &buffer.id, offset as f64, size as f64);
10491066
}
10501067

10511068
pub(crate) fn render_pass_set_scissor_rect(
@@ -1151,7 +1168,11 @@ pub(crate) fn texture_view_drop(_texture_view: &TextureViewId) {
11511168
// Texture view is dropped automatically
11521169
}
11531170

1154-
pub(crate) fn swap_chain_present(_swap_chain: &SwapChainId) {
1171+
pub(crate) fn bind_group_drop(_bind_group: &BindGroupId) {
1172+
// Bind group is dropped automatically
1173+
}
1174+
1175+
pub(crate) fn swap_chain_present(_swap_chain_output: &crate::SwapChainOutput) {
11551176
// Swapchain is presented automatically
11561177
}
11571178

src/lib.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ pub struct BindGroup {
140140

141141
impl Drop for BindGroup {
142142
fn drop(&mut self) {
143-
#[cfg(not(target_arch = "wasm32"))]
144-
wgn::wgpu_bind_group_destroy(self.id);
143+
backend::bind_group_drop(&self.id);
145144
}
146145
}
147146

@@ -460,7 +459,7 @@ pub struct TextureDescriptor<'a> {
460459
#[derive(Debug)]
461460
pub struct SwapChainOutput {
462461
pub view: TextureView,
463-
swap_chain_id: backend::SwapChainId,
462+
detail: backend::SwapChainOutputDetail,
464463
}
465464

466465
/// A view of a buffer which can be used to copy to or from a texture.
@@ -689,12 +688,7 @@ impl Device {
689688
// TODO
690689
impl Drop for Device {
691690
fn drop(&mut self) {
692-
#[cfg(not(target_arch = "wasm32"))]
693-
wgn::wgpu_device_poll(self.id, true);
694-
//TODO: make this work in general
695-
#[cfg(not(target_arch = "wasm32"))]
696-
#[cfg(feature = "metal-auto-capture")]
697-
wgn::wgpu_device_destroy(self.id);
691+
backend::device_drop(&self.id);
698692
}
699693
}
700694

@@ -1097,7 +1091,7 @@ impl Queue {
10971091
impl Drop for SwapChainOutput {
10981092
fn drop(&mut self) {
10991093
if !thread::panicking() {
1100-
backend::swap_chain_present(&self.swap_chain_id);
1094+
backend::swap_chain_present(&self);
11011095
}
11021096
}
11031097
}
@@ -1113,13 +1107,6 @@ impl SwapChain {
11131107
/// When the [`SwapChainOutput`] returned by this method is dropped, the swapchain will present
11141108
/// the texture to the associated [`Surface`].
11151109
pub fn get_next_texture(&mut self) -> Result<SwapChainOutput, TimeOut> {
1116-
match backend::swap_chain_get_next_texture(&self.id) {
1117-
Some(id) => Ok(SwapChainOutput {
1118-
view: TextureView { id, owned: false },
1119-
// TODO: Remove from web backend
1120-
swap_chain_id: self.id.clone(),
1121-
}),
1122-
None => Err(TimeOut),
1123-
}
1110+
backend::swap_chain_get_next_texture(&self.id)
11241111
}
11251112
}

0 commit comments

Comments
 (0)