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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use {
    super::{
        AccelerationStructureBinding, AccelerationStructureLeaseBinding, BufferBinding,
        BufferLeaseBinding, ImageBinding, ImageLeaseBinding, Information, NodeIndex, RenderGraph,
        Subresource,
    },
    crate::driver::{
        vk, AccelerationStructureInfo, BufferInfo, BufferSubresource, ImageInfo, ImageSubresource,
        ImageViewInfo,
    },
    archery::{SharedPointer, SharedPointerKind},
    std::{marker::PhantomData, ops::Range},
};

#[derive(Debug)]
pub enum AnyAccelerationStructureNode<P> {
    AccelerationStructure(AccelerationStructureNode<P>),
    AccelerationStructureLease(AccelerationStructureLeaseNode<P>),
}

impl<P> Clone for AnyAccelerationStructureNode<P> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<P> Copy for AnyAccelerationStructureNode<P> {}

impl<P> Information for AnyAccelerationStructureNode<P> {
    type Info = AccelerationStructureInfo;

    fn get(self, graph: &RenderGraph<impl SharedPointerKind + Send>) -> Self::Info {
        match self {
            Self::AccelerationStructure(node) => node.get(graph),
            Self::AccelerationStructureLease(node) => node.get(graph),
        }
    }
}

impl<P> From<AccelerationStructureNode<P>> for AnyAccelerationStructureNode<P> {
    fn from(node: AccelerationStructureNode<P>) -> Self {
        Self::AccelerationStructure(node)
    }
}

impl<P> From<AccelerationStructureLeaseNode<P>> for AnyAccelerationStructureNode<P> {
    fn from(node: AccelerationStructureLeaseNode<P>) -> Self {
        Self::AccelerationStructureLease(node)
    }
}

impl<P> Node<P> for AnyAccelerationStructureNode<P> {
    fn index(self) -> NodeIndex {
        match self {
            Self::AccelerationStructure(node) => node.index(),
            Self::AccelerationStructureLease(node) => node.index(),
        }
    }
}

#[derive(Debug)]
pub enum AnyBufferNode<P> {
    Buffer(BufferNode<P>),
    BufferLease(BufferLeaseNode<P>),
}

impl<P> Clone for AnyBufferNode<P> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<P> Copy for AnyBufferNode<P> {}

impl<P> Information for AnyBufferNode<P> {
    type Info = BufferInfo;

    fn get(self, graph: &RenderGraph<impl SharedPointerKind + Send>) -> Self::Info {
        match self {
            Self::Buffer(node) => node.get(graph),
            Self::BufferLease(node) => node.get(graph),
        }
    }
}

impl<P> From<BufferNode<P>> for AnyBufferNode<P> {
    fn from(node: BufferNode<P>) -> Self {
        Self::Buffer(node)
    }
}

impl<P> From<BufferLeaseNode<P>> for AnyBufferNode<P> {
    fn from(node: BufferLeaseNode<P>) -> Self {
        Self::BufferLease(node)
    }
}

impl<P> Node<P> for AnyBufferNode<P> {
    fn index(self) -> NodeIndex {
        match self {
            Self::Buffer(node) => node.index(),
            Self::BufferLease(node) => node.index(),
        }
    }
}

#[derive(Debug)]
pub enum AnyImageNode<P> {
    Image(ImageNode<P>),
    ImageLease(ImageLeaseNode<P>),
    SwapchainImage(SwapchainImageNode<P>),
}

impl<P> Clone for AnyImageNode<P> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<P> Copy for AnyImageNode<P> {}

impl<P> Information for AnyImageNode<P> {
    type Info = ImageInfo;

    fn get(self, graph: &RenderGraph<impl SharedPointerKind + Send>) -> Self::Info {
        match self {
            Self::Image(node) => node.get(graph),
            Self::ImageLease(node) => node.get(graph),
            Self::SwapchainImage(node) => node.get(graph),
        }
    }
}

impl<P> From<ImageNode<P>> for AnyImageNode<P> {
    fn from(node: ImageNode<P>) -> Self {
        Self::Image(node)
    }
}

impl<P> From<ImageLeaseNode<P>> for AnyImageNode<P> {
    fn from(node: ImageLeaseNode<P>) -> Self {
        Self::ImageLease(node)
    }
}

impl<P> From<SwapchainImageNode<P>> for AnyImageNode<P> {
    fn from(node: SwapchainImageNode<P>) -> Self {
        Self::SwapchainImage(node)
    }
}

impl<P> Node<P> for AnyImageNode<P> {
    fn index(self) -> NodeIndex {
        match self {
            Self::Image(node) => node.index(),
            Self::ImageLease(node) => node.index(),
            Self::SwapchainImage(node) => node.index(),
        }
    }
}

pub trait Node<P>: Copy {
    fn index(self) -> NodeIndex;
}

macro_rules! node {
    ($name:ident) => {
        paste::paste! {
            #[derive(Debug)]
            pub struct [<$name Node>]<P> {
                __: PhantomData<P>,
                pub(super) idx: NodeIndex,
            }

            impl<P> [<$name Node>]<P> {
                pub(super) fn new(idx: NodeIndex) -> Self {
                    Self {
                        __: PhantomData,
                        idx,
                    }
                }
            }

            impl<P> Clone for [<$name Node>]<P> {
                fn clone(&self) -> Self {
                    *self
                }
            }

            impl<P> Copy for [<$name Node>]<P> {}

            impl<P> Node<P> for [<$name Node>]<P>  {
                fn index(self) -> NodeIndex {
                    self.idx
                }
            }
        }
    };
}

node!(AccelerationStructure);
node!(AccelerationStructureLease);
node!(Buffer);
node!(BufferLease);
node!(Image);
node!(ImageLease);
node!(SwapchainImage);

macro_rules! node_unbind {
    ($name:ident) => {
        paste::paste! {
            impl<P> Unbind<RenderGraph<P>, [<$name Binding>]<P>> for [<$name Node>]<P>
            where
                P: SharedPointerKind + Send + 'static,
            {
                fn unbind(self, graph: &mut RenderGraph<P>) -> [<$name Binding>]<P> {
                    let binding = {
                        let binding = graph.bindings[self.idx].[<as_ $name:snake>]().unwrap();
                        let item = SharedPointer::clone(&binding.item);

                        // When unbinding we return a binding that has the last access type set to
                        // whatever the last acccess in the graph was (because it will be valid once
                        // the graph is resolved and you should not use an unbound binding before
                        // the graph is resolved. Resolve it and then use said binding on a
                        // different graph.)
                        let previous_access = graph.last_access(self)
                            .unwrap_or(binding.access).clone();
                        [<$name Binding>]::new_unbind(item, previous_access)
                    };
                    graph.bindings[self.idx].unbind();

                    binding
                }
            }
        }
    };
}

node_unbind!(AccelerationStructure);
node_unbind!(Buffer);
node_unbind!(Image);

macro_rules! node_unbind_lease {
    ($name:ident) => {
        paste::paste! {
            impl<P> Unbind<RenderGraph<P>, [<$name LeaseBinding>]<P>> for [<$name LeaseNode>]<P>
            where
                P: SharedPointerKind + Send + 'static,
            {
                fn unbind(self, graph: &mut RenderGraph<P>) -> [<$name LeaseBinding>]<P> {
                    let binding = {
                        let last_access = graph.last_access(self);
                        let (binding, _) = graph.bindings[self.idx].[<as_ $name:snake _lease_mut>]().unwrap();
                        let item = binding.item.clone();

                        // When unbinding we return a binding that has the last access type set to
                        // whatever the last acccess in the graph was (because it will be valid once
                        // the graph is resolved and you should not use an unbound binding before
                        // the graph is resolved. Resolve it and then use said binding on a
                        // different graph.)
                        let previous_access = last_access.unwrap_or(binding.access);
                        let item_binding = [<$name Binding>]::new_unbind(
                            item,
                            previous_access,
                        );

                        // Move the return-to-pool-on-drop behavior to a new lease
                        let lease = binding.transfer(item_binding);

                        [<$name LeaseBinding>](lease)
                    };
                    graph.bindings[self.idx].unbind();

                    binding
                }
            }
        }
    };
}

node_unbind_lease!(AccelerationStructure);
node_unbind_lease!(Buffer);
node_unbind_lease!(Image);

pub trait Unbind<Graph, Binding> {
    fn unbind(self, graph: &mut Graph) -> Binding;
}

pub trait View<P>: Node<P>
where
    Self::Information: Clone,
    Self::Subresource: Into<Subresource>,
{
    type Information;
    type Subresource;
}

impl<P> View<P> for AccelerationStructureNode<P> {
    type Information = ();
    type Subresource = ();
}

impl<P> View<P> for AccelerationStructureLeaseNode<P> {
    type Information = ();
    type Subresource = ();
}

impl<P> View<P> for AnyAccelerationStructureNode<P> {
    type Information = ();
    type Subresource = ();
}

impl<P> View<P> for AnyBufferNode<P> {
    type Information = BufferSubresource;
    type Subresource = BufferSubresource;
}

impl<P> View<P> for AnyImageNode<P> {
    type Information = ImageViewInfo;
    type Subresource = ImageSubresource;
}

impl<P> View<P> for BufferLeaseNode<P> {
    type Information = BufferSubresource;
    type Subresource = BufferSubresource;
}

impl<P> View<P> for BufferNode<P> {
    type Information = BufferSubresource;
    type Subresource = BufferSubresource;
}

impl<P> View<P> for ImageLeaseNode<P> {
    type Information = ImageViewInfo;
    type Subresource = ImageSubresource;
}

impl<P> View<P> for ImageNode<P> {
    type Information = ImageViewInfo;
    type Subresource = ImageSubresource;
}

impl<P> View<P> for SwapchainImageNode<P> {
    type Information = ImageViewInfo;
    type Subresource = ImageSubresource;
}

pub enum ViewType {
    AccelerationStructure,
    Image(ImageViewInfo),
    Buffer(Range<vk::DeviceSize>),
}

impl ViewType {
    pub(super) fn as_buffer(&self) -> Option<&Range<vk::DeviceSize>> {
        match self {
            Self::Buffer(view_info) => Some(view_info),
            _ => None,
        }
    }

    pub(super) fn as_image(&self) -> Option<&ImageViewInfo> {
        match self {
            Self::Image(view_info) => Some(view_info),
            _ => None,
        }
    }
}

// TODO: Remove this
impl From<()> for ViewType {
    fn from(_: ()) -> Self {
        Self::AccelerationStructure
    }
}

impl From<BufferSubresource> for ViewType {
    fn from(subresource: BufferSubresource) -> Self {
        Self::Buffer(subresource.start..subresource.end)
    }
}

impl From<ImageViewInfo> for ViewType {
    fn from(info: ImageViewInfo) -> Self {
        Self::Image(info)
    }
}

impl From<Range<vk::DeviceSize>> for ViewType {
    fn from(range: Range<vk::DeviceSize>) -> Self {
        Self::Buffer(range)
    }
}