-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVkBarrierHelper.cs
197 lines (155 loc) · 5.23 KB
/
VkBarrierHelper.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using static JLeb.Estragonia.VkInterop;
namespace JLeb.Estragonia;
/// <summary>
/// An helper to create Vulkan image barriers.
/// </summary>
internal sealed class VkBarrierHelper : IDisposable {
private readonly VkDevice _device;
private readonly VkQueue _queue;
private readonly VkDeviceApi _deviceApi;
private readonly uint _queueFamilyIndex;
private readonly List<ReusableBuffer> _reusableBuffers = new();
private bool _isDisposed;
public VkBarrierHelper(VkDevice device, VkQueue queue, VkDeviceApi deviceApi, uint queueFamilyIndex) {
_device = device;
_queue = queue;
_deviceApi = deviceApi;
_queueFamilyIndex = queueFamilyIndex;
}
public unsafe void TransitionImageLayout(
VkImage image,
VkImageLayout sourceLayout,
VkAccessFlags sourceAccessMask,
VkImageLayout destinationLayout,
VkAccessFlags destinationAccessMask
) {
if (_isDisposed)
ThrowDisposed();
var reusableBuffer = GetOrCreateReusableBuffer();
var fence = reusableBuffer.Fence;
_deviceApi.ResetFences(_device, 1, &fence);
var commandBuffer = reusableBuffer.CommandBuffer;
var beginInfo = new VkCommandBufferBeginInfo {
sType = VkStructureType.COMMAND_BUFFER_BEGIN_INFO,
flags = VkCommandBufferUsageFlags.ONE_TIME_SUBMIT_BIT
};
_deviceApi.BeginCommandBuffer(commandBuffer, ref beginInfo);
var barrier = new VkImageMemoryBarrier {
sType = VkStructureType.IMAGE_MEMORY_BARRIER,
srcAccessMask = sourceAccessMask,
dstAccessMask = destinationAccessMask,
oldLayout = sourceLayout,
newLayout = destinationLayout,
srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
image = image,
subresourceRange = new VkImageSubresourceRange {
aspectMask = VkImageAspectFlags.COLOR_BIT,
baseMipLevel = 0,
levelCount = 1,
baseArrayLayer = 0,
layerCount = 1
}
};
_deviceApi.CmdPipelineBarrier(
reusableBuffer.CommandBuffer,
VkPipelineStageFlags.ALL_COMMANDS_BIT,
VkPipelineStageFlags.ALL_COMMANDS_BIT,
0,
0,
IntPtr.Zero,
0,
IntPtr.Zero,
1,
&barrier
);
_deviceApi.EndCommandBuffer(commandBuffer);
var submitInfo = new VkSubmitInfo {
sType = VkStructureType.SUBMIT_INFO,
waitSemaphoreCount = 0,
pWaitSemaphores = null,
pWaitDstStageMask = null,
commandBufferCount = 1,
pCommandBuffers = &commandBuffer,
signalSemaphoreCount = 0,
pSignalSemaphores = null
};
_deviceApi.QueueSubmit(_queue, 1, &submitInfo, fence);
}
private ReusableBuffer GetOrCreateReusableBuffer() {
for (int i = 0; i < _reusableBuffers.Count; ++i) {
var existingBuffer = _reusableBuffers[i];
if (existingBuffer.IsAvailable())
return existingBuffer;
}
var newBuffer = new ReusableBuffer(_device, _deviceApi, _queueFamilyIndex);
_reusableBuffers.Add(newBuffer);
return newBuffer;
}
[DoesNotReturn]
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowDisposed()
=> throw new ObjectDisposedException(nameof(VkBarrierHelper));
public void Dispose() {
if (_isDisposed)
return;
_isDisposed = true;
for (var i = _reusableBuffers.Count - 1; i >= 0; --i)
_reusableBuffers[i].Dispose();
_reusableBuffers.Clear();
}
/// <summary>
/// Contains a reusable command pool, command buffer and an associated fence.
/// </summary>
private sealed class ReusableBuffer {
private readonly VkDevice _device;
private readonly VkDeviceApi _deviceApi;
private readonly VkCommandPool _commandPool;
private bool _isDisposed;
public VkCommandBuffer CommandBuffer { get; }
public VkFence Fence { get; }
public bool IsAvailable()
=> _deviceApi.GetFenceStatus(_device, Fence) == VkResult.VK_SUCCESS;
public unsafe ReusableBuffer(VkDevice device, VkDeviceApi deviceApi, uint queueFamilyIndex) {
_device = device;
_deviceApi = deviceApi;
var poolCreateInfo = new VkCommandPoolCreateInfo {
sType = VkStructureType.COMMAND_POOL_CREATE_INFO,
flags = VkCommandPoolCreateFlags.RESET_COMMAND_BUFFER_BIT,
queueFamilyIndex = queueFamilyIndex
};
deviceApi.CreateCommandPool(device, ref poolCreateInfo, IntPtr.Zero, out _commandPool);
var bufferAllocateInfo = new VkCommandBufferAllocateInfo {
sType = VkStructureType.COMMAND_BUFFER_ALLOCATE_INFO,
commandPool = _commandPool,
level = VkCommandBufferLevel.PRIMARY,
commandBufferCount = 1
};
VkCommandBuffer commandBuffer;
deviceApi.AllocateCommandBuffers(_device, ref bufferAllocateInfo, &commandBuffer);
CommandBuffer = commandBuffer;
var fenceCreateInfo = new VkFenceCreateInfo
{
sType = VkStructureType.FENCE_CREATE_INFO,
flags = VkFenceCreateFlags.SIGNALED_BIT
};
deviceApi.CreateFence(device, ref fenceCreateInfo, IntPtr.Zero, out var fence);
Fence = fence;
}
public unsafe void Dispose() {
if (_isDisposed)
return;
_isDisposed = true;
var fence = Fence;
_deviceApi.WaitForFences(_device, 1, &fence, 1, UInt64.MaxValue);
_deviceApi.DestroyFence(_device, fence, IntPtr.Zero);
var commandBuffer = CommandBuffer;
_deviceApi.FreeCommandBuffers(_device, _commandPool, 1, &commandBuffer);
_deviceApi.DestroyCommandPool(_device, _commandPool, IntPtr.Zero);
}
}
}