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
use {
    super::{Bind, Binding, RenderGraph, Resolver, Subresource, SwapchainImageNode, Unbind},
    crate::{
        driver::{Image, SwapchainImage},
        ptr::Shared,
    },
    archery::SharedPointerKind,
    std::{fmt::Debug, mem::replace},
    vk_sync::AccessType,
};

#[derive(Debug)]
pub struct SwapchainImageBinding<P>
where
    P: SharedPointerKind,
{
    pub(super) item: SwapchainImage<P>,
    pub(super) previous_access: AccessType,
    pub(super) previous_subresource: Option<Subresource>,
}

impl<P> SwapchainImageBinding<P>
where
    P: SharedPointerKind,
{
    pub(super) fn new(item: SwapchainImage<P>) -> Self {
        Self::new_unbind(item, AccessType::Nothing, None)
    }

    pub(super) fn new_unbind(
        item: SwapchainImage<P>,
        previous_access: AccessType,
        previous_subresource: Option<Subresource>,
    ) -> Self {
        Self {
            item,
            previous_access,
            previous_subresource,
        }
    }

    pub(super) fn next_access(
        &mut self,
        access: AccessType,
        subresource: Option<Subresource>,
    ) -> (AccessType, Option<Subresource>) {
        (
            replace(&mut self.previous_access, access),
            replace(&mut self.previous_subresource, subresource),
        )
    }

    /// Allows for direct access to the item inside this binding, without the Shared
    /// wrapper. Returns the previous access type and subresource access which you
    /// should use to create a barrier for whatever access is actually being done.
    pub fn access_inner(
        &mut self,
        access: AccessType,
    ) -> (&Image<P>, AccessType, Option<Subresource>) {
        self.access_inner_subresource(access, None)
    }

    /// Allows for direct access to the item inside this binding, without the Shared
    /// wrapper. Returns the previous access type and subresource access which you
    /// should use to create a barrier for whatever access is actually being done.
    pub fn access_inner_subresource(
        &mut self,
        access: AccessType,
        subresource: impl Into<Option<Subresource>>,
    ) -> (&Image<P>, AccessType, Option<Subresource>) {
        let subresource = subresource.into();
        let (previous_access, previous_subresource) = self.next_access(access, subresource);

        (&self.item, previous_access, previous_subresource)
    }
}

impl<P> Bind<&mut RenderGraph<P>, SwapchainImageNode<P>, P> for SwapchainImage<P>
where
    P: SharedPointerKind,
{
    fn bind(self, graph: &mut RenderGraph<P>) -> SwapchainImageNode<P> {
        // We will return a new node
        let res = SwapchainImageNode::new(graph.bindings.len());

        //trace!("Node {}: {:?}", res.idx, &self);

        let binding = Binding::SwapchainImage(SwapchainImageBinding::new(self), true);
        graph.bindings.push(binding);

        res
    }
}

impl<P> Bind<&mut RenderGraph<P>, SwapchainImageNode<P>, P> for SwapchainImageBinding<P>
where
    P: SharedPointerKind,
{
    fn bind(self, graph: &mut RenderGraph<P>) -> SwapchainImageNode<P> {
        // We will return a new node
        let res = SwapchainImageNode::new(graph.bindings.len());

        //trace!("Node {}: {:?}", res.idx, &self);

        graph.bindings.push(Binding::SwapchainImage(self, true));

        res
    }
}

impl<P> Binding<P>
where
    P: SharedPointerKind,
{
    pub(super) fn as_swapchain_image(&self) -> &SwapchainImageBinding<P> {
        if let Self::SwapchainImage(binding, true) = self {
            binding
        } else if let Self::SwapchainImage(_, false) = self {
            // User code might try this - but it is a programmer error
            // to access a binding after it has been unbound so dont
            panic!();
        } else {
            // The private code in this module should prevent this branch
            unreachable!("boom");
        }
    }
}

impl<P> Unbind<Resolver<P>, SwapchainImage<P>> for SwapchainImageNode<P>
where
    P: SharedPointerKind,
{
    // We allow the resolver to unbind a swapchain node directly into a shared image
    fn unbind(self, graph: &mut Resolver<P>) -> SwapchainImage<P> {
        graph.graph.bindings[self.idx]
            .as_swapchain_image()
            .item
            .clone()
    }
}