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

hal: limit binding sizes to i32 #2363

Merged
merged 1 commit into from
Jan 6, 2022
Merged
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
6 changes: 6 additions & 0 deletions wgpu-hal/src/auxil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub mod db {
}
}

/// Maximum binding size for the shaders that only support `i32` indexing.
/// Interestingly, the index itself can't reach that high, because the minimum
/// element size is 4 bytes, but the compiler toolchain still computes the
/// offset at some intermediate point, internally, as i32.
pub const MAX_I32_BINDING_SIZE: u32 = 1 << 31;
Copy link
Contributor

Choose a reason for hiding this comment

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

I may miss something, but 1 << 31 does not fit in an i32, as the maximum value for it is (1 << 31) - 1.

I think that using i32::MAX as u32 instead could be less error-prone.

Copy link
Member Author

Choose a reason for hiding this comment

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

The size doesn't need to fit into i32. It's the byte offsets that are internally converted into i32, and they are strictly less than the full binding size.


pub fn map_naga_stage(stage: naga::ShaderStage) -> wgt::ShaderStages {
match stage {
naga::ShaderStage::Vertex => wgt::ShaderStages::VERTEX,
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/dx12/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl super::Adapter {
max_uniform_buffers_per_shader_stage: full_heap_count,
max_uniform_buffer_binding_size: d3d12::D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT
* 16,
max_storage_buffer_binding_size: !0,
max_storage_buffer_binding_size: crate::auxil::MAX_I32_BINDING_SIZE,
max_vertex_buffers: d3d12::D3D12_VS_INPUT_REGISTER_COUNT
.min(crate::MAX_VERTEX_BUFFERS as u32),
max_vertex_attributes: d3d12::D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT,
Expand Down
8 changes: 6 additions & 2 deletions wgpu-hal/src/vulkan/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,12 @@ impl PhysicalDeviceCapabilities {
max_storage_buffers_per_shader_stage: max_storage_buffers,
max_storage_textures_per_shader_stage: max_storage_textures,
max_uniform_buffers_per_shader_stage: max_uniform_buffers,
max_uniform_buffer_binding_size: limits.max_uniform_buffer_range,
max_storage_buffer_binding_size: limits.max_storage_buffer_range,
max_uniform_buffer_binding_size: limits
.max_uniform_buffer_range
.min(crate::auxil::MAX_I32_BINDING_SIZE),
max_storage_buffer_binding_size: limits
.max_storage_buffer_range
.min(crate::auxil::MAX_I32_BINDING_SIZE),
max_vertex_buffers: limits
.max_vertex_input_bindings
.min(crate::MAX_VERTEX_BUFFERS as u32),
Expand Down