Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport device limits fix to 0.17 #4964

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Bottom level categories:
#### Vulkan

- Fix x11 hang while resizing on vulkan. @Azorlogh in [#4184](https://github.com/gfx-rs/wgpu/pull/4184).
- Backport fix to ensure that limit requests and reporting is done correctly. (By @OptimisticPeach in [#4107](https://github.com/gfx-rs/wgpu/pull/4107)), @cshenton in [#4964](https://github.com/gfx-rs/wgpu/pull/4964)

## v0.17.1 (2023-09-27)

Expand Down
30 changes: 25 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ path = "./wgpu-hal"
version = "0.17"

[workspace.dependencies.naga]
git = "https://github.com/gfx-rs/naga"
rev = "bac2d82a430fbfcf100ee22b7c3bc12f3d593079"
# git = "https://github.com/gfx-rs/naga"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got accidentally committed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually not an accident. Without removing this, there are naga version conflicts when building against other packages. Is there a reason this particular commit is targeted rather than the version on crates.io?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit here is just the commit for the release of 0.17 - the sources are the same. When locally patching, you should be able to patch both wgpu and naga to the same place and it work fine. Something like:

[patch.crates-io]
wgpu = { git = "https://github.com/gfx-rs/wgpu.git", rev = "7b7dfa6" }
wgpu-core = { git = "https://github.com/gfx-rs/wgpu.git", rev = "7b7dfa6" }
wgpu-hal = { git = "https://github.com/gfx-rs/wgpu.git", rev = "7b7dfa6" }
wgpu-types = { git = "https://github.com/gfx-rs/wgpu.git", rev = "7b7dfa6" }
naga = { git = "https://github.com/gfx-rs/naga.git", rev = "bac2d82a430fbfcf100ee22b7c3bc12f3d593079" }

# rev = "bac2d82a430fbfcf100ee22b7c3bc12f3d593079"
version = "0.13.0"

[workspace.dependencies]
Expand Down
135 changes: 107 additions & 28 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,99 @@ fn map_wgt_features(supported_features: web_sys::GpuSupportedFeatures) -> wgt::F
features
}

fn map_wgt_limits(limits: web_sys::GpuSupportedLimits) -> wgt::Limits {
wgt::Limits {
max_texture_dimension_1d: limits.max_texture_dimension_1d(),
max_texture_dimension_2d: limits.max_texture_dimension_2d(),
max_texture_dimension_3d: limits.max_texture_dimension_3d(),
max_texture_array_layers: limits.max_texture_array_layers(),
max_bind_groups: limits.max_bind_groups(),
max_bindings_per_bind_group: limits.max_bindings_per_bind_group(),
max_dynamic_uniform_buffers_per_pipeline_layout: limits
.max_dynamic_uniform_buffers_per_pipeline_layout(),
max_dynamic_storage_buffers_per_pipeline_layout: limits
.max_dynamic_storage_buffers_per_pipeline_layout(),
max_sampled_textures_per_shader_stage: limits.max_sampled_textures_per_shader_stage(),
max_samplers_per_shader_stage: limits.max_samplers_per_shader_stage(),
max_storage_buffers_per_shader_stage: limits.max_storage_buffers_per_shader_stage(),
max_storage_textures_per_shader_stage: limits.max_storage_textures_per_shader_stage(),
max_uniform_buffers_per_shader_stage: limits.max_uniform_buffers_per_shader_stage(),
max_uniform_buffer_binding_size: limits.max_uniform_buffer_binding_size() as u32,
max_storage_buffer_binding_size: limits.max_storage_buffer_binding_size() as u32,
max_vertex_buffers: limits.max_vertex_buffers(),
max_buffer_size: limits.max_buffer_size() as u64,
max_vertex_attributes: limits.max_vertex_attributes(),
max_vertex_buffer_array_stride: limits.max_vertex_buffer_array_stride(),
min_uniform_buffer_offset_alignment: limits.min_uniform_buffer_offset_alignment(),
min_storage_buffer_offset_alignment: limits.min_storage_buffer_offset_alignment(),
max_inter_stage_shader_components: limits.max_inter_stage_shader_components(),
max_compute_workgroup_storage_size: limits.max_compute_workgroup_storage_size(),
max_compute_invocations_per_workgroup: limits.max_compute_invocations_per_workgroup(),
max_compute_workgroup_size_x: limits.max_compute_workgroup_size_x(),
max_compute_workgroup_size_y: limits.max_compute_workgroup_size_y(),
max_compute_workgroup_size_z: limits.max_compute_workgroup_size_z(),
max_compute_workgroups_per_dimension: limits.max_compute_workgroups_per_dimension(),
// The following are not part of WebGPU
max_push_constant_size: wgt::Limits::default().max_push_constant_size,
// max_non_sampler_bindings: wgt::Limits::default().max_non_sampler_bindings,
}
}

fn map_js_sys_limits(limits: &wgt::Limits) -> js_sys::Object {
let object = js_sys::Object::new();

macro_rules! set_properties {
(($from:expr) => ($on:expr) : $(($js_ident:ident, $rs_ident:ident)),* $(,)?) => {
$(
::js_sys::Reflect::set(
&$on,
&::wasm_bindgen::JsValue::from(stringify!($js_ident)),
// Numbers may be u64, however using `from` on a u64 yields
// errors on the wasm side, since it uses an unsupported api.
// Wasm sends us things that need to fit into u64s by sending
// us f64s instead. So we just send them f64s back.
&::wasm_bindgen::JsValue::from($from.$rs_ident as f64)
)
.expect("Setting Object properties should never fail.");
)*
}
}

set_properties![
(limits) => (object):
(maxTextureDimension1D, max_texture_dimension_1d),
(maxTextureDimension2D, max_texture_dimension_2d),
(maxTextureDimension3D, max_texture_dimension_3d),
(maxTextureArrayLayers, max_texture_array_layers),
(maxBindGroups, max_bind_groups),
(maxBindingsPerBindGroup, max_bindings_per_bind_group),
(maxDynamicUniformBuffersPerPipelineLayout, max_dynamic_uniform_buffers_per_pipeline_layout),
(maxDynamicStorageBuffersPerPipelineLayout, max_dynamic_storage_buffers_per_pipeline_layout),
(maxSampledTexturesPerShaderStage, max_sampled_textures_per_shader_stage),
(maxSamplersPerShaderStage, max_samplers_per_shader_stage),
(maxStorageBuffersPerShaderStage, max_storage_buffers_per_shader_stage),
(maxStorageTexturesPerShaderStage, max_storage_textures_per_shader_stage),
(maxUniformBuffersPerShaderStage, max_uniform_buffers_per_shader_stage),
(maxUniformBufferBindingSize, max_uniform_buffer_binding_size),
(maxStorageBufferBindingSize, max_storage_buffer_binding_size),
(minUniformBufferOffsetAlignment, min_uniform_buffer_offset_alignment),
(minStorageBufferOffsetAlignment, min_storage_buffer_offset_alignment),
(maxVertexBuffers, max_vertex_buffers),
(maxBufferSize, max_buffer_size),
(maxVertexAttributes, max_vertex_attributes),
(maxVertexBufferArrayStride, max_vertex_buffer_array_stride),
(maxInterStageShaderComponents, max_inter_stage_shader_components),
(maxComputeWorkgroupStorageSize, max_compute_workgroup_storage_size),
(maxComputeInvocationsPerWorkgroup, max_compute_invocations_per_workgroup),
(maxComputeWorkgroupSizeX, max_compute_workgroup_size_x),
(maxComputeWorkgroupSizeY, max_compute_workgroup_size_y),
(maxComputeWorkgroupSizeZ, max_compute_workgroup_size_z),
(maxComputeWorkgroupsPerDimension, max_compute_workgroups_per_dimension),
];

object
}

type JsFutureResult = Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>;

fn future_request_adapter(
Expand Down Expand Up @@ -1016,9 +1109,19 @@ impl crate::context::Context for Context {
//Error: Tracing isn't supported on the Web target
}

// TODO: non-guaranteed limits
let mut mapped_desc = web_sys::GpuDeviceDescriptor::new();

// TODO: Migrate to a web_sys api.
// See https://github.com/rustwasm/wasm-bindgen/issues/3587
let limits_object = map_js_sys_limits(&desc.limits);

js_sys::Reflect::set(
&mapped_desc,
&JsValue::from("requiredLimits"),
&limits_object,
)
.expect("Setting Object properties should never fail.");

let required_features = FEATURES_MAPPING
.iter()
.copied()
Expand Down Expand Up @@ -1072,30 +1175,7 @@ impl crate::context::Context for Context {
_adapter: &Self::AdapterId,
adapter_data: &Self::AdapterData,
) -> wgt::Limits {
let limits = adapter_data.0.limits();
wgt::Limits {
max_texture_dimension_1d: limits.max_texture_dimension_1d(),
max_texture_dimension_2d: limits.max_texture_dimension_2d(),
max_texture_dimension_3d: limits.max_texture_dimension_3d(),
max_texture_array_layers: limits.max_texture_array_layers(),
max_bind_groups: limits.max_bind_groups(),
max_bindings_per_bind_group: limits.max_bindings_per_bind_group(),
max_dynamic_uniform_buffers_per_pipeline_layout: limits
.max_dynamic_uniform_buffers_per_pipeline_layout(),
max_dynamic_storage_buffers_per_pipeline_layout: limits
.max_dynamic_storage_buffers_per_pipeline_layout(),
max_sampled_textures_per_shader_stage: limits.max_sampled_textures_per_shader_stage(),
max_samplers_per_shader_stage: limits.max_samplers_per_shader_stage(),
max_storage_buffers_per_shader_stage: limits.max_storage_buffers_per_shader_stage(),
max_storage_textures_per_shader_stage: limits.max_storage_textures_per_shader_stage(),
max_uniform_buffers_per_shader_stage: limits.max_uniform_buffers_per_shader_stage(),
max_uniform_buffer_binding_size: limits.max_uniform_buffer_binding_size() as u32,
max_storage_buffer_binding_size: limits.max_storage_buffer_binding_size() as u32,
max_vertex_buffers: limits.max_vertex_buffers(),
max_vertex_attributes: limits.max_vertex_attributes(),
max_vertex_buffer_array_stride: limits.max_vertex_buffer_array_stride(),
..wgt::Limits::default()
}
map_wgt_limits(adapter_data.0.limits())
}

fn adapter_downlevel_capabilities(
Expand Down Expand Up @@ -1258,10 +1338,9 @@ impl crate::context::Context for Context {
fn device_limits(
&self,
_device: &Self::DeviceId,
_device_data: &Self::DeviceData,
device_data: &Self::DeviceData,
) -> wgt::Limits {
// TODO
wgt::Limits::default()
map_wgt_limits(device_data.0.limits())
}

fn device_downlevel_properties(
Expand Down
Loading