Skip to content

Commit 7087f0c

Browse files
jamienicoljimblandy
authored andcommitted
[wgpu-core] Allow creation of bind groups containing external textures
Adds a `BindingResource` variant for external textures. In core's create_bind_group() implementation, allow binding either external textures or texture views to `BindingType::ExternalTexture` layout entries. In either case, provide HAL with a `hal::ExternalTextureBinding`, consisting of 3 `hal::TextureBinding`s and a `hal::BufferBinding`. In the texture view case we use the device's default params buffer for the buffer. When there are fewer than 3 planes we can simply repeat an existing plane multiple times - the contents of the params buffer will ensure the shader only accesses the correct number of planes anyway. Track the view or external texture in `BindGroupStates` to ensure they remain alive whilst required. And finally, add the corresponding API to wgpu, with an implementation for the wgpu-core backend.
1 parent d263b18 commit 7087f0c

File tree

16 files changed

+274
-36
lines changed

16 files changed

+274
-36
lines changed

wgpu-core/src/binding_model.rs

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use crate::{
2121
device::{
2222
bgl, Device, DeviceError, MissingDownlevelFlags, MissingFeatures, SHADER_STAGE_COUNT,
2323
},
24-
id::{BindGroupLayoutId, BufferId, SamplerId, TextureViewId, TlasId},
24+
id::{BindGroupLayoutId, BufferId, ExternalTextureId, SamplerId, TextureViewId, TlasId},
2525
init_tracker::{BufferInitTrackerAction, TextureInitTrackerAction},
2626
pipeline::{ComputePipeline, RenderPipeline},
2727
resource::{
28-
Buffer, DestroyedResourceError, InvalidResourceError, Labeled, MissingBufferUsageError,
29-
MissingTextureUsageError, RawResourceAccess, ResourceErrorIdent, Sampler, TextureView,
30-
Tlas, TrackingData,
28+
Buffer, DestroyedResourceError, ExternalTexture, InvalidResourceError, Labeled,
29+
MissingBufferUsageError, MissingTextureUsageError, RawResourceAccess, ResourceErrorIdent,
30+
Sampler, TextureView, Tlas, TrackingData,
3131
},
3232
resource_log,
3333
snatch::{SnatchGuard, Snatchable},
@@ -594,8 +594,14 @@ impl BindingTypeMaxCountValidator {
594594
/// cbindgen:ignore
595595
#[derive(Clone, Debug)]
596596
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
597-
pub struct BindGroupEntry<'a, B = BufferId, S = SamplerId, TV = TextureViewId, TLAS = TlasId>
598-
where
597+
pub struct BindGroupEntry<
598+
'a,
599+
B = BufferId,
600+
S = SamplerId,
601+
TV = TextureViewId,
602+
TLAS = TlasId,
603+
ET = ExternalTextureId,
604+
> where
599605
[BufferBinding<B>]: ToOwned,
600606
[S]: ToOwned,
601607
[TV]: ToOwned,
@@ -608,15 +614,21 @@ where
608614
pub binding: u32,
609615
#[cfg_attr(
610616
feature = "serde",
611-
serde(bound(deserialize = "BindingResource<'a, B, S, TV, TLAS>: Deserialize<'de>"))
617+
serde(bound(deserialize = "BindingResource<'a, B, S, TV, TLAS, ET>: Deserialize<'de>"))
612618
)]
613619
/// Resource to attach to the binding
614-
pub resource: BindingResource<'a, B, S, TV, TLAS>,
620+
pub resource: BindingResource<'a, B, S, TV, TLAS, ET>,
615621
}
616622

617623
/// cbindgen:ignore
618-
pub type ResolvedBindGroupEntry<'a> =
619-
BindGroupEntry<'a, Arc<Buffer>, Arc<Sampler>, Arc<TextureView>, Arc<Tlas>>;
624+
pub type ResolvedBindGroupEntry<'a> = BindGroupEntry<
625+
'a,
626+
Arc<Buffer>,
627+
Arc<Sampler>,
628+
Arc<TextureView>,
629+
Arc<Tlas>,
630+
Arc<ExternalTexture>,
631+
>;
620632

621633
/// Describes a group of bindings and the resources to be bound.
622634
#[derive(Clone, Debug)]
@@ -628,15 +640,16 @@ pub struct BindGroupDescriptor<
628640
S = SamplerId,
629641
TV = TextureViewId,
630642
TLAS = TlasId,
643+
ET = ExternalTextureId,
631644
> where
632645
[BufferBinding<B>]: ToOwned,
633646
[S]: ToOwned,
634647
[TV]: ToOwned,
635648
<[BufferBinding<B>] as ToOwned>::Owned: fmt::Debug,
636649
<[S] as ToOwned>::Owned: fmt::Debug,
637650
<[TV] as ToOwned>::Owned: fmt::Debug,
638-
[BindGroupEntry<'a, B, S, TV, TLAS>]: ToOwned,
639-
<[BindGroupEntry<'a, B, S, TV, TLAS>] as ToOwned>::Owned: fmt::Debug,
651+
[BindGroupEntry<'a, B, S, TV, TLAS, ET>]: ToOwned,
652+
<[BindGroupEntry<'a, B, S, TV, TLAS, ET>] as ToOwned>::Owned: fmt::Debug,
640653
{
641654
/// Debug label of the bind group.
642655
///
@@ -647,11 +660,12 @@ pub struct BindGroupDescriptor<
647660
#[cfg_attr(
648661
feature = "serde",
649662
serde(bound(
650-
deserialize = "<[BindGroupEntry<'a, B, S, TV, TLAS>] as ToOwned>::Owned: Deserialize<'de>"
663+
deserialize = "<[BindGroupEntry<'a, B, S, TV, TLAS, ET>] as ToOwned>::Owned: Deserialize<'de>"
651664
))
652665
)]
653666
/// The resources to bind to this bind group.
654-
pub entries: Cow<'a, [BindGroupEntry<'a, B, S, TV, TLAS>]>,
667+
#[allow(clippy::type_complexity)]
668+
pub entries: Cow<'a, [BindGroupEntry<'a, B, S, TV, TLAS, ET>]>,
655669
}
656670

657671
/// cbindgen:ignore
@@ -662,6 +676,7 @@ pub type ResolvedBindGroupDescriptor<'a> = BindGroupDescriptor<
662676
Arc<Sampler>,
663677
Arc<TextureView>,
664678
Arc<Tlas>,
679+
Arc<ExternalTexture>,
665680
>;
666681

667682
/// Describes a [`BindGroupLayout`].
@@ -1005,8 +1020,14 @@ pub type ResolvedBufferBinding = BufferBinding<Arc<Buffer>>;
10051020
// They're different enough that it doesn't make sense to share a common type
10061021
#[derive(Debug, Clone)]
10071022
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1008-
pub enum BindingResource<'a, B = BufferId, S = SamplerId, TV = TextureViewId, TLAS = TlasId>
1009-
where
1023+
pub enum BindingResource<
1024+
'a,
1025+
B = BufferId,
1026+
S = SamplerId,
1027+
TV = TextureViewId,
1028+
TLAS = TlasId,
1029+
ET = ExternalTextureId,
1030+
> where
10101031
[BufferBinding<B>]: ToOwned,
10111032
[S]: ToOwned,
10121033
[TV]: ToOwned,
@@ -1033,10 +1054,17 @@ where
10331054
)]
10341055
TextureViewArray(Cow<'a, [TV]>),
10351056
AccelerationStructure(TLAS),
1057+
ExternalTexture(ET),
10361058
}
10371059

1038-
pub type ResolvedBindingResource<'a> =
1039-
BindingResource<'a, Arc<Buffer>, Arc<Sampler>, Arc<TextureView>, Arc<Tlas>>;
1060+
pub type ResolvedBindingResource<'a> = BindingResource<
1061+
'a,
1062+
Arc<Buffer>,
1063+
Arc<Sampler>,
1064+
Arc<TextureView>,
1065+
Arc<Tlas>,
1066+
Arc<ExternalTexture>,
1067+
>;
10401068

10411069
#[derive(Clone, Debug, Error)]
10421070
#[non_exhaustive]

wgpu-core/src/device/global.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ impl Global {
846846
sampler_storage: &Storage<Fallible<resource::Sampler>>,
847847
texture_view_storage: &Storage<Fallible<resource::TextureView>>,
848848
tlas_storage: &Storage<Fallible<resource::Tlas>>,
849+
external_texture_storage: &Storage<Fallible<resource::ExternalTexture>>,
849850
) -> Result<ResolvedBindGroupEntry<'a>, binding_model::CreateBindGroupError>
850851
{
851852
let resolve_buffer = |bb: &BufferBinding| {
@@ -877,6 +878,12 @@ impl Global {
877878
.get()
878879
.map_err(binding_model::CreateBindGroupError::from)
879880
};
881+
let resolve_external_texture = |id: &id::ExternalTextureId| {
882+
external_texture_storage
883+
.get(*id)
884+
.get()
885+
.map_err(binding_model::CreateBindGroupError::from)
886+
};
880887
let resource = match e.resource {
881888
BindingResource::Buffer(ref buffer) => {
882889
ResolvedBindingResource::Buffer(resolve_buffer(buffer)?)
@@ -911,6 +918,9 @@ impl Global {
911918
BindingResource::AccelerationStructure(ref tlas) => {
912919
ResolvedBindingResource::AccelerationStructure(resolve_tlas(tlas)?)
913920
}
921+
BindingResource::ExternalTexture(ref et) => {
922+
ResolvedBindingResource::ExternalTexture(resolve_external_texture(et)?)
923+
}
914924
};
915925
Ok(ResolvedBindGroupEntry {
916926
binding: e.binding,
@@ -923,6 +933,7 @@ impl Global {
923933
let texture_view_guard = hub.texture_views.read();
924934
let sampler_guard = hub.samplers.read();
925935
let tlas_guard = hub.tlas_s.read();
936+
let external_texture_guard = hub.external_textures.read();
926937
desc.entries
927938
.iter()
928939
.map(|e| {
@@ -932,6 +943,7 @@ impl Global {
932943
&sampler_guard,
933944
&texture_view_guard,
934945
&tlas_guard,
946+
&external_texture_guard,
935947
)
936948
})
937949
.collect::<Result<Vec<_>, _>>()

0 commit comments

Comments
 (0)