Allow pipeline specialization with extra mutable data
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 161
Description
## What problem does this solve or what need does it fill?
Both `SpecializedRenderPipeline` and `SpecializedComputePipeline` have a `specialize()` function taking a single `&Key`, which both determines how the pipeline is specialized and forms the hash map key where to store that specialized pipeline.
Sometimes though, the specialization influences things like the bind group layout(s) used in the pipeline. To that effect, currently existing pipelines like `DepthOfFieldPipeline` create multiple variants of the layouts, and pick the correct one based on the key. This is fine if you have only a low number of variants, and the layout(s) do not depend on anything except for picking one or the other. This doesn't work however when extra data is needed to form those layouts, and/or create them lazily on the fly during specialization.
## What solution would you like?
Change the function of those traits to take an extra mutable argument referencing any external data:
```rust
pub trait SpecializedRenderPipeline {
type Key: Clone + Hash + PartialEq + Eq;
fn specialize(&self, key: Self::Key, extra: &mut E) -> RenderPipelineDescriptor;
}
```
That way the caller can pass extra data, typically a storage for the bind group layouts created based on the key:
```rust
let mut extra = HashMap::new();
pipelines.specialize(&my_key, &mut extra);
impl SpecializedRenderPipeline> for MyPipeline {
fn specialize(&self, key: Self::Key, extra: &mut HashMap) -> RenderPipelineDescriptor {
let layout = extra.entry(key.index).or_insert_with(device.create_bind_group_layout(...));
// [...]
}
}
```
## What alternative(s) have you considered?
1. If the number of layout variants is small, create all of them in `FromWorld` ahead of time. Limited wasted of GPU memory, but still.
2. If not, really no good solution. Have to resort to ugly interior mutability and store some kind of `Arc` or whatnot inside the pipeline to reference the external bind group layout storage.
Contributor guide
Assessment
This issue has not been assessed yet.