bedrock/
dependency.rs

1use crate::*;
2#[cfg(feature = "VK_KHR_synchronization2")]
3use ffi_helper::slice_as_ptr_empty_null;
4
5#[cfg(feature = "VK_KHR_synchronization2")]
6#[repr(transparent)]
7#[derive(Clone, Debug)]
8pub struct MemoryBarrier2(VkMemoryBarrier2KHR);
9#[cfg(feature = "VK_KHR_synchronization2")]
10impl MemoryBarrier2 {
11    pub const fn new() -> Self {
12        Self(VkMemoryBarrier2KHR {
13            sType: VkMemoryBarrier2KHR::TYPE,
14            pNext: core::ptr::null(),
15            srcStageMask: VK_PIPELINE_STAGE_2_NONE_KHR,
16            srcAccessMask: VK_ACCESS_2_NONE_KHR,
17            dstStageMask: VK_PIPELINE_STAGE_2_NONE_KHR,
18            dstAccessMask: VK_ACCESS_2_NONE_KHR,
19        })
20    }
21
22    pub const fn from(mut self, stage: PipelineStageFlags2, access: AccessFlags2) -> Self {
23        self.0.srcStageMask = stage.0;
24        self.0.srcAccessMask = access.0;
25        self
26    }
27
28    pub const fn to(mut self, stage: PipelineStageFlags2, access: AccessFlags2) -> Self {
29        self.0.dstStageMask = stage.0;
30        self.0.dstAccessMask = access.0;
31        self
32    }
33
34    pub const fn of_execution(mut self, src: PipelineStageFlags2, dst: PipelineStageFlags2) -> Self {
35        self.0.srcStageMask = src.0;
36        self.0.dstStageMask = dst.0;
37        self
38    }
39
40    pub const fn of_memory(mut self, src: AccessFlags2, dst: AccessFlags2) -> Self {
41        self.0.srcAccessMask = src.0;
42        self.0.dstAccessMask = dst.0;
43        self
44    }
45}
46
47#[cfg(feature = "VK_KHR_synchronization2")]
48#[repr(transparent)]
49#[derive(Clone, Debug)]
50pub struct BufferMemoryBarrier2<'b>(
51    VkBufferMemoryBarrier2KHR,
52    core::marker::PhantomData<&'b dyn VkHandle<Handle = VkBuffer>>,
53);
54#[cfg(feature = "VK_KHR_synchronization2")]
55impl<'b> BufferMemoryBarrier2<'b> {
56    pub fn new(
57        buffer: &'b (impl VkHandle<Handle = VkBuffer> + ?Sized),
58        offset: VkDeviceSize,
59        size: VkDeviceSize,
60    ) -> Self {
61        Self(
62            VkBufferMemoryBarrier2KHR {
63                sType: VkBufferMemoryBarrier2KHR::TYPE,
64                pNext: core::ptr::null(),
65                srcStageMask: VK_PIPELINE_STAGE_2_NONE_KHR,
66                srcAccessMask: VK_ACCESS_2_NONE_KHR,
67                dstStageMask: VK_PIPELINE_STAGE_2_NONE_KHR,
68                dstAccessMask: VK_ACCESS_2_NONE_KHR,
69                srcQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
70                dstQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
71                buffer: buffer.native_ptr(),
72                offset,
73                size,
74            },
75            core::marker::PhantomData,
76        )
77    }
78
79    pub const fn from(mut self, stage: PipelineStageFlags2, access: AccessFlags2) -> Self {
80        self.0.srcStageMask = stage.0;
81        self.0.srcAccessMask = access.0;
82        self
83    }
84
85    pub const fn to(mut self, stage: PipelineStageFlags2, access: AccessFlags2) -> Self {
86        self.0.dstStageMask = stage.0;
87        self.0.dstAccessMask = access.0;
88        self
89    }
90
91    pub const fn of_execution(mut self, src: PipelineStageFlags2, dst: PipelineStageFlags2) -> Self {
92        self.0.srcStageMask = src.0;
93        self.0.dstStageMask = dst.0;
94        self
95    }
96
97    pub const fn of_memory(mut self, src: AccessFlags2, dst: AccessFlags2) -> Self {
98        self.0.srcAccessMask = src.0;
99        self.0.dstAccessMask = dst.0;
100        self
101    }
102
103    pub const fn transferring_queue_family(mut self, from: u32, to: u32) -> Self {
104        self.0.srcQueueFamilyIndex = from;
105        self.0.dstQueueFamilyIndex = to;
106        self
107    }
108}
109
110#[cfg(feature = "VK_KHR_synchronization2")]
111#[repr(transparent)]
112#[derive(Clone)]
113pub struct ImageMemoryBarrier2<'r>(
114    VkImageMemoryBarrier2KHR,
115    core::marker::PhantomData<&'r dyn VkHandle<Handle = VkImage>>,
116);
117#[cfg(feature = "VK_KHR_synchronization2")]
118impl<'r> ImageMemoryBarrier2<'r> {
119    pub fn new(
120        image: &'r (impl VkHandle<Handle = VkImage> + ?Sized),
121        subresource_range: VkImageSubresourceRange,
122    ) -> Self {
123        Self(
124            VkImageMemoryBarrier2KHR {
125                sType: VkImageMemoryBarrier2KHR::TYPE,
126                pNext: core::ptr::null(),
127                srcStageMask: VK_PIPELINE_STAGE_2_NONE_KHR,
128                srcAccessMask: VK_ACCESS_2_NONE_KHR,
129                dstStageMask: VK_PIPELINE_STAGE_2_NONE_KHR,
130                dstAccessMask: VK_ACCESS_2_NONE_KHR,
131                oldLayout: VK_IMAGE_LAYOUT_UNDEFINED,
132                newLayout: VK_IMAGE_LAYOUT_UNDEFINED,
133                srcQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
134                dstQueueFamilyIndex: VK_QUEUE_FAMILY_IGNORED,
135                image: image.native_ptr(),
136                subresourceRange: subresource_range,
137            },
138            core::marker::PhantomData,
139        )
140    }
141
142    pub const fn from(mut self, stage: PipelineStageFlags2, access: AccessFlags2) -> Self {
143        self.0.srcStageMask = stage.0;
144        self.0.srcAccessMask = access.0;
145        self
146    }
147
148    pub const fn to(mut self, stage: PipelineStageFlags2, access: AccessFlags2) -> Self {
149        self.0.dstStageMask = stage.0;
150        self.0.dstAccessMask = access.0;
151        self
152    }
153
154    pub const fn of_execution(mut self, src: PipelineStageFlags2, dst: PipelineStageFlags2) -> Self {
155        self.0.srcStageMask = src.0;
156        self.0.dstStageMask = dst.0;
157        self
158    }
159
160    pub const fn of_memory(mut self, src: AccessFlags2, dst: AccessFlags2) -> Self {
161        self.0.srcAccessMask = src.0;
162        self.0.dstAccessMask = dst.0;
163        self
164    }
165
166    pub const fn transferring_layout(mut self, from: ImageLayout, to: ImageLayout) -> Self {
167        self.0.oldLayout = from as _;
168        self.0.newLayout = to as _;
169        self
170    }
171    pub const fn transit_from(self, trans: LayoutTransition) -> Self {
172        self.transferring_layout(trans.from, trans.to)
173    }
174    pub const fn transit_to(self, trans: LayoutTransition) -> Self {
175        self.transferring_layout(trans.from, trans.to)
176    }
177
178    pub const fn transferring_queue_family(mut self, from: u32, to: u32) -> Self {
179        self.0.srcQueueFamilyIndex = from;
180        self.0.dstQueueFamilyIndex = to;
181        self
182    }
183}
184
185#[cfg(feature = "VK_KHR_synchronization2")]
186#[repr(transparent)]
187#[derive(Debug, Clone)]
188pub struct DependencyInfo<'b, 'r>(
189    VkDependencyInfoKHR,
190    core::marker::PhantomData<(
191        &'b [MemoryBarrier2],
192        &'b [BufferMemoryBarrier2<'r>],
193        &'b [ImageMemoryBarrier2<'r>],
194    )>,
195);
196#[cfg(feature = "VK_KHR_synchronization2")]
197impl<'b, 'r> DependencyInfo<'b, 'r> {
198    pub const fn new(
199        memory_barriers: &'b [MemoryBarrier2],
200        buffer_memory_barriers: &'b [BufferMemoryBarrier2<'r>],
201        image_memory_barriers: &'b [ImageMemoryBarrier2<'r>],
202    ) -> Self {
203        Self(
204            VkDependencyInfoKHR {
205                sType: VkDependencyInfoKHR::TYPE,
206                pNext: core::ptr::null(),
207                dependencyFlags: 0,
208                memoryBarrierCount: memory_barriers.len() as _,
209                pMemoryBarriers: slice_as_ptr_empty_null(memory_barriers) as _,
210                bufferMemoryBarrierCount: buffer_memory_barriers.len() as _,
211                pBufferMemoryBarriers: slice_as_ptr_empty_null(buffer_memory_barriers) as _,
212                imageMemoryBarrierCount: image_memory_barriers.len() as _,
213                pImageMemoryBarriers: slice_as_ptr_empty_null(image_memory_barriers) as _,
214            },
215            core::marker::PhantomData,
216        )
217    }
218
219    pub const fn by_region(mut self) -> Self {
220        self.0.dependencyFlags |= VK_DEPENDENCY_BY_REGION_BIT;
221        self
222    }
223}