Skip to content

Commit

Permalink
Convert wgpu-hal to Use Argument Buffers for Binding Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Dec 16, 2024
1 parent d439c66 commit 7be0e51
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 121 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ wgpu-types = { version = "23.0.0", path = "./wgpu-types" }
winit = { version = "0.29", features = ["android-native-activity"] }

# Metal dependencies
metal = { version = "0.30.0", git = "https://github.com/gfx-rs/metal-rs.git", rev = "ef768ff9d7" }
block = "0.1"
core-graphics-types = "0.1"
metal = { version = "0.30.0" }
objc = "0.2.5"

# Vulkan dependencies
Expand Down
14 changes: 3 additions & 11 deletions wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,18 +905,10 @@ impl super::PrivateCapabilities {
features.set(
F::TEXTURE_BINDING_ARRAY
| F::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING
| F::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING,
self.msl_version >= MTLLanguageVersion::V2_0 && self.supports_arrays_of_textures,
| F::UNIFORM_BUFFER_AND_STORAGE_TEXTURE_ARRAY_NON_UNIFORM_INDEXING
| F::PARTIALLY_BOUND_BINDING_ARRAY,
self.msl_version >= MTLLanguageVersion::V3_0 && self.supports_arrays_of_textures,
);
//// XXX: this is technically not true, as read-only storage images can be used in arrays
//// on precisely the same conditions that sampled textures can. But texel fetch from a
//// sampled texture is a thing; should we bother introducing another feature flag?
if self.msl_version >= MTLLanguageVersion::V2_2
&& self.supports_arrays_of_textures
&& self.supports_arrays_of_textures_write
{
features.insert(F::STORAGE_RESOURCE_BINDING_ARRAY);
}
features.set(
F::SHADER_INT64,
self.int64 && self.msl_version >= MTLLanguageVersion::V2_3,
Expand Down
15 changes: 14 additions & 1 deletion wgpu-hal/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ impl crate::CommandEncoder for super::CommandEncoder {
offset += dynamic_offsets[dyn_index as usize] as wgt::BufferAddress;
}
encoder.set_vertex_buffer(
(bg_info.base_resource_indices.vs.buffers + index) as u64,
dbg!(bg_info.base_resource_indices.vs.buffers + index) as u64,
Some(buf.ptr.as_native()),
offset,
);
Expand Down Expand Up @@ -750,6 +750,11 @@ impl crate::CommandEncoder for super::CommandEncoder {
Some(res.as_native()),
);
}

// Call useResource on all textures and buffers used indirectly so they are alive
for (resource, use_info) in group.resources_to_use.iter() {
encoder.use_resource_at(resource.as_native(), use_info.uses, use_info.stages);
}
}

if let Some(ref encoder) = self.state.compute {
Expand Down Expand Up @@ -807,6 +812,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
Some(res.as_native()),
);
}

// Call useResource on all textures and buffers used indirectly so they are alive
for (resource, use_info) in group.resources_to_use.iter() {
if !use_info.visible_in_compute {
continue;
}
encoder.use_resource(resource.as_native(), use_info.uses);
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions wgpu-hal/src/metal/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,31 @@ pub fn get_blit_option(
metal::MTLBlitOption::None
}
}

pub fn map_render_stages(stage: wgt::ShaderStages) -> metal::MTLRenderStages {
let mut raw_stages = metal::MTLRenderStages::empty();

if stage.contains(wgt::ShaderStages::VERTEX) {
raw_stages |= metal::MTLRenderStages::Vertex;
}
if stage.contains(wgt::ShaderStages::FRAGMENT) {
raw_stages |= metal::MTLRenderStages::Fragment;
}

raw_stages
}

pub fn map_resource_usage(ty: &wgt::BindingType) -> metal::MTLResourceUsage {
match ty {
wgt::BindingType::Texture { .. } => metal::MTLResourceUsage::Sample,
wgt::BindingType::StorageTexture { access, .. } => match access {
wgt::StorageTextureAccess::WriteOnly => metal::MTLResourceUsage::Write,
wgt::StorageTextureAccess::ReadOnly => metal::MTLResourceUsage::Read,
wgt::StorageTextureAccess::ReadWrite => {
metal::MTLResourceUsage::Read | metal::MTLResourceUsage::Write
}
},
wgt::BindingType::Sampler(..) => metal::MTLResourceUsage::empty(),
_ => unreachable!(),
}
}
Loading

0 comments on commit 7be0e51

Please sign in to comment.