1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! What a pass constructor has created so far, released if it does not finish.
//!
//! A pass's `new` creates its layouts, pipelines, samplers and buffers one seam
//! call at a time with `?` between them, and the seam's `destroy_*` is
//! explicit — so a failure half way through would otherwise leave everything
//! created before it alive until the device goes. Most of those passes are
//! built lazily between frames, the first time a frame asks for them, so a
//! failure there is retried and would leak again on every retry.
//!
//! [`Partial`] is the one guard they share: each handle goes through
//! [`Partial::add`] as it is created, and the guard destroys every one of them
//! when it drops — **in the reverse order they were added**, which is
//! dependency order, since a pipeline is created after its layout and a group
//! after its layout and buffers — unless [`Partial::keep`] handed them to the
//! finished pass first. Dropping rather than an explicit call is what makes
//! every `?` after the guard exists a release, without a cleanup arm per call.
//!
//! The objects it destroys were created by the constructor that is failing and
//! never reached a frame, so no submitted work can be reading them. A
//! constructor that also records into an
//! [`UploadBatch`](crate::texture::UploadBatch) still has to
//! [abandon](crate::texture::UploadBatch::abandon) it on failure, since the
//! batch would otherwise copy into what this destroyed — `Ssr::new` and
//! `Exposure::new` do.
use crcbl_hal::{
BindGroupHandle, BindGroupLayoutHandle, BufferHandle, ComputePipelineHandle, Device,
GraphicsPipelineHandle, ImageHandle, ImageViewHandle, PipelineLayoutHandle, SamplerHandle,
};
use crate::texture::UploadedTexture;
/// One handle a [`Partial`] holds, tagged with the `destroy_*` it needs.
#[derive(Clone, Copy, Debug)]
pub(crate) enum Created {
Buffer(BufferHandle),
Image(ImageHandle),
ImageView(ImageViewHandle),
Sampler(SamplerHandle),
BindGroupLayout(BindGroupLayoutHandle),
BindGroup(BindGroupHandle),
PipelineLayout(PipelineLayoutHandle),
GraphicsPipeline(GraphicsPipelineHandle),
ComputePipeline(ComputePipelineHandle),
}
macro_rules! created_from {
($($handle:ty => $variant:ident),+ $(,)?) => {
$(
impl From<$handle> for Created {
fn from(handle: $handle) -> Self {
Self::$variant(handle)
}
}
)+
};
}
created_from! {
BufferHandle => Buffer,
ImageHandle => Image,
ImageViewHandle => ImageView,
SamplerHandle => Sampler,
BindGroupLayoutHandle => BindGroupLayout,
BindGroupHandle => BindGroup,
PipelineLayoutHandle => PipelineLayout,
GraphicsPipelineHandle => GraphicsPipeline,
ComputePipelineHandle => ComputePipeline,
}
impl Created {
fn destroy(self, device: &dyn Device) {
match self {
Self::Buffer(handle) => device.destroy_buffer(handle),
Self::Image(handle) => device.destroy_image(handle),
Self::ImageView(handle) => device.destroy_image_view(handle),
Self::Sampler(handle) => device.destroy_sampler(handle),
Self::BindGroupLayout(handle) => device.destroy_bind_group_layout(handle),
Self::BindGroup(handle) => device.destroy_bind_group(handle),
Self::PipelineLayout(handle) => device.destroy_pipeline_layout(handle),
Self::GraphicsPipeline(handle) => device.destroy_graphics_pipeline(handle),
Self::ComputePipeline(handle) => device.destroy_compute_pipeline(handle),
}
}
}
/// Every handle a constructor has created so far, destroyed when this drops
/// unless [`keep`](Self::keep) was called — see the module docs.
#[derive(Debug)]
#[must_use = "a guard dropped at once releases nothing it was later meant to hold"]
pub(crate) struct Partial<'d> {
device: &'d dyn Device,
created: Vec<Created>,
}
impl<'d> Partial<'d> {
/// An empty guard over objects `device` creates.
pub(crate) fn new(device: &'d dyn Device) -> Self {
Self {
device,
created: Vec::new(),
}
}
/// Holds `handle` until the guard is kept or dropped, and hands it back.
pub(crate) fn add<H: Copy + Into<Created>>(&mut self, handle: H) -> H {
self.created.push(handle.into());
handle
}
/// Holds an uploaded texture's image and view, and hands it back — the
/// view released first, as [`UploadedTexture::destroy`] does.
pub(crate) fn add_texture(&mut self, texture: UploadedTexture) -> UploadedTexture {
self.add(texture.image);
self.add(texture.view);
texture
}
/// Lets go of everything without destroying it, once the finished pass owns
/// every handle and its own `destroy` will release them.
pub(crate) fn keep(mut self) {
self.created.clear();
}
}
impl Drop for Partial<'_> {
fn drop(&mut self) {
for created in self.created.drain(..).rev() {
created.destroy(self.device);
}
}
}