Skip to content

Commit d602cf4

Browse files
committed
[metal] Implement support for external textures
This contains the Metal HAL changes required to support external textures. When creating a bind group we create resource bindings for each of the 3 textures and parameters buffer that the external texture has been lowered to. When creating the pipeline layout we fill the `BindTarget` accordingly, so that the Naga MSL backend can bind each of the global variables to which the the external texture has been lowered to each of these resources. We must also ensure the size of the buffer bound to the parameters global matches the size of the MSL type, else metal validation complains. We do this by adding a padding field to the rust-side ExternalTextureParams struct, the size of which is used as the size of the buffer to allocate. Lastly we enable `Features::EXTERNAL_TEXTURE` on the Metal backend.
1 parent e8619a7 commit d602cf4

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

wgpu-core/src/device/resource.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ pub struct ExternalTextureParams {
163163
/// plane and an interleaved CbCr plane. 3 indicates separate Y, Cb, and Cr
164164
/// planes.
165165
pub num_planes: u32,
166+
// Ensure the size of this struct matches the type generated by Naga.
167+
pub _padding: [u8; 4],
166168
}
167169

168170
impl ExternalTextureParams {
@@ -191,6 +193,7 @@ impl ExternalTextureParams {
191193
sample_transform: desc.sample_transform,
192194
load_transform: desc.load_transform,
193195
num_planes: desc.num_planes() as u32,
196+
_padding: Default::default(),
194197
}
195198
}
196199
}
@@ -512,6 +515,7 @@ impl Device {
512515
0.0, 0.0
513516
],
514517
num_planes: 1,
518+
_padding: Default::default(),
515519
};
516520
let mut staging_buffer =
517521
StagingBuffer::new(self, wgt::BufferSize::new(size_of_val(&data) as _).unwrap())?;

wgpu-hal/src/metal/adapter.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,8 @@ impl super::PrivateCapabilities {
927927
| F::TEXTURE_FORMAT_16BIT_NORM
928928
| F::SHADER_F16
929929
| F::DEPTH32FLOAT_STENCIL8
930-
| F::BGRA8UNORM_STORAGE;
930+
| F::BGRA8UNORM_STORAGE
931+
| F::EXTERNAL_TEXTURE;
931932

932933
features.set(F::FLOAT32_FILTERABLE, self.supports_float_filtering);
933934
features.set(

wgpu-hal/src/metal/device.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,19 @@ impl crate::Device for super::Device {
747747
};
748748
}
749749
wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),
750-
wgt::BindingType::ExternalTexture => unimplemented!(),
750+
wgt::BindingType::ExternalTexture => {
751+
target.external_texture =
752+
Some(naga::back::msl::BindExternalTextureTarget {
753+
planes: [
754+
info.counters.textures as _,
755+
(info.counters.textures + 1) as _,
756+
(info.counters.textures + 2) as _,
757+
],
758+
params: info.counters.buffers as _,
759+
});
760+
info.counters.textures += 3;
761+
info.counters.buffers += 1;
762+
}
751763
}
752764
}
753765

@@ -980,7 +992,28 @@ impl crate::Device for super::Device {
980992
counter.textures += 1;
981993
}
982994
wgt::BindingType::AccelerationStructure { .. } => unimplemented!(),
983-
wgt::BindingType::ExternalTexture => unimplemented!(),
995+
wgt::BindingType::ExternalTexture => {
996+
// We don't yet support binding arrays of external textures.
997+
// https://github.com/gfx-rs/wgpu/issues/8027
998+
assert_eq!(entry.count, 1);
999+
let external_texture =
1000+
&desc.external_textures[entry.resource_index as usize];
1001+
bg.textures.extend(
1002+
external_texture
1003+
.planes
1004+
.iter()
1005+
.map(|plane| plane.view.as_raw()),
1006+
);
1007+
bg.buffers.push(super::BufferResource {
1008+
ptr: external_texture.params.buffer.as_raw(),
1009+
offset: external_texture.params.offset,
1010+
dynamic_index: None,
1011+
binding_size: None,
1012+
binding_location: layout.binding,
1013+
});
1014+
counter.textures += 3;
1015+
counter.buffers += 1;
1016+
}
9841017
}
9851018
}
9861019
}

wgpu-types/src/features.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ bitflags_array! {
10081008
///
10091009
/// Supported platforms:
10101010
/// - DX12
1011+
/// - Metal
10111012
const EXTERNAL_TEXTURE = 1 << 31;
10121013

10131014
// Shader:

0 commit comments

Comments
 (0)