-
Notifications
You must be signed in to change notification settings - Fork 14
/
swapchain.cpp
405 lines (353 loc) · 16.9 KB
/
swapchain.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (C) 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "swapchain.h"
#include <cassert>
#include <vector>
#include "callback_swapchain.h"
namespace swapchain {
void RegisterInstance(VkInstance instance, InstanceData& data) {
uint32_t num_devices = 0;
data.vkEnumeratePhysicalDevices(instance, &num_devices, nullptr);
data.physical_devices_.resize(num_devices);
data.vkEnumeratePhysicalDevices(instance, &num_devices,
data.physical_devices_.data());
auto physical_device_map = GetGlobalContext().GetPhysicalDeviceMap();
for (VkPhysicalDevice physical_device : data.physical_devices_) {
PhysicalDeviceData dat{instance};
data.vkGetPhysicalDeviceMemoryProperties(physical_device,
&dat.memory_properties_);
data.vkGetPhysicalDeviceProperties(physical_device,
&dat.physical_device_properties_);
(*physical_device_map)[physical_device] = dat;
}
}
// For now CallbackSurface is empty. Once we start tracking more
// information from the host, then we can start expanding
// what we are able to expose here.
struct CallbackSurface {};
VKAPI_ATTR VkResult VKAPI_CALL vkCreateCallbackSurface(
VkInstance instance, const void* /*pCreateInfo*/,
const VkAllocationCallbacks* pAllocator, VkSurfaceKHR* pSurface) {
*pSurface = reinterpret_cast<VkSurfaceKHR>(new CallbackSurface());
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfaceSupportKHR(
VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex,
VkSurfaceKHR surface, VkBool32* pSupported) {
const auto instance_dat = *GetGlobalContext().GetInstanceData(
GetGlobalContext().GetPhysicalDeviceData(physicalDevice)->instance_);
for (uint32_t i = 0; i <= queueFamilyIndex; ++i) {
uint32_t property_count = 0;
instance_dat.vkGetPhysicalDeviceQueueFamilyProperties(
physicalDevice, &property_count, nullptr);
assert(property_count > queueFamilyIndex);
std::vector<VkQueueFamilyProperties> properties(property_count);
instance_dat.vkGetPhysicalDeviceQueueFamilyProperties(
physicalDevice, &property_count, properties.data());
if (properties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
*pSupported = (i == queueFamilyIndex);
return VK_SUCCESS;
}
}
// For now only support the FIRST graphics queue. It looks like all of
// the commands we will have to run are transfer commands, so
// we can probably get away with ANY queue (other than
// SPARSE_BINDING).
*pSupported = false;
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
VkSurfaceCapabilitiesKHR* pSurfaceCapabilities) {
// It would be illegal for the program to call VkDestroyInstance here.
// We do not need to lock the map for the whole time, just
// long enough to get the data out. unordered_map guarantees that
// even if re-hashing occurs, references remain valid.
VkPhysicalDeviceProperties& properties =
GetGlobalContext()
.GetPhysicalDeviceData(physicalDevice)
->physical_device_properties_;
pSurfaceCapabilities->minImageCount = 1;
pSurfaceCapabilities->maxImageCount = 0;
pSurfaceCapabilities->currentExtent = {0xFFFFFFFF, 0xFFFFFFFF};
pSurfaceCapabilities->minImageExtent = {1, 1};
pSurfaceCapabilities->maxImageExtent = {
properties.limits.maxImageDimension2D,
properties.limits.maxImageDimension2D};
pSurfaceCapabilities->maxImageArrayLayers =
properties.limits.maxImageArrayLayers;
pSurfaceCapabilities->supportedTransforms =
VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
// TODO(awoloszyn): Handle all of the transforms eventually
pSurfaceCapabilities->currentTransform =
VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
pSurfaceCapabilities->supportedCompositeAlpha =
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
// TODO(awoloszyn): Handle all of the composite types.
pSurfaceCapabilities->supportedUsageFlags =
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
// TODO(awoloszyn): Find a good set of formats that we can use
// for rendering.
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfaceFormatsKHR(
VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
uint32_t* pSurfaceFormatCount, VkSurfaceFormatKHR* pSurfaceFormats) {
if (!pSurfaceFormats) {
*pSurfaceFormatCount = 1;
return VK_SUCCESS;
}
if (*pSurfaceFormatCount < 1) {
return VK_INCOMPLETE;
}
*pSurfaceFormatCount = 1;
// TODO(awoloszyn): Handle more different formats.
pSurfaceFormats->format = VK_FORMAT_R8G8B8A8_UNORM;
pSurfaceFormats->colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL vkGetPhysicalDeviceSurfacePresentModesKHR(
VkPhysicalDevice physicalDevice, VkSurfaceKHR surface,
uint32_t* pPresentModeCount, VkPresentModeKHR* pPresentModes) {
if (!pPresentModes) {
*pPresentModeCount = 1;
return VK_SUCCESS;
}
if (*pPresentModeCount < 1) {
return VK_INCOMPLETE;
}
// TODO(awoloszyn): Add more present modes. we MUST support
// VK_PRESENT_MODE_FIFO_KHR.
*pPresentModes = VK_PRESENT_MODE_FIFO_KHR;
return VK_SUCCESS;
}
VKAPI_ATTR VkResult VKAPI_CALL vkCreateSwapchainKHR(
VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) {
DeviceData& dev_dat = *GetGlobalContext().GetDeviceData(device);
PhysicalDeviceData& pdd =
*GetGlobalContext().GetPhysicalDeviceData(dev_dat.physicalDevice);
InstanceData& inst_dat = *GetGlobalContext().GetInstanceData(pdd.instance_);
uint32_t property_count = 0;
inst_dat.vkGetPhysicalDeviceQueueFamilyProperties(dev_dat.physicalDevice,
&property_count, nullptr);
std::vector<VkQueueFamilyProperties> queue_properties(property_count);
inst_dat.vkGetPhysicalDeviceQueueFamilyProperties(
dev_dat.physicalDevice, &property_count, queue_properties.data());
uint32_t queue = 0;
for (; queue < static_cast<uint32_t>(queue_properties.size()); ++queue) {
if (queue_properties[queue].queueFlags & VK_QUEUE_GRAPHICS_BIT) break;
}
assert(queue < queue_properties.size());
*pSwapchain = reinterpret_cast<VkSwapchainKHR>(new CallbackSwapchain(
device, queue, &pdd.physical_device_properties_, &pdd.memory_properties_,
&dev_dat, pCreateInfo, pAllocator));
return VK_SUCCESS;
}
VKAPI_ATTR void VKAPI_CALL
vkDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain,
const VkAllocationCallbacks* pAllocator) {
CallbackSwapchain* swp = reinterpret_cast<CallbackSwapchain*>(swapchain);
swp->Destroy(pAllocator);
delete swp;
}
VKAPI_ATTR void VKAPI_CALL
vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface,
const VkAllocationCallbacks* pAllocator) {}
VKAPI_ATTR VkResult VKAPI_CALL vkGetSwapchainImagesKHR(
VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount,
VkImage* pSwapchainImages) {
CallbackSwapchain* swp = reinterpret_cast<CallbackSwapchain*>(swapchain);
const auto images =
swp->GetImages(*pSwapchainImageCount, pSwapchainImages != nullptr);
if (!pSwapchainImages) {
*pSwapchainImageCount = static_cast<uint32_t>(images.size());
return VK_SUCCESS;
}
VkResult res = VK_INCOMPLETE;
if (*pSwapchainImageCount >= images.size()) {
*pSwapchainImageCount = static_cast<uint32_t>(images.size());
res = VK_SUCCESS;
}
for (size_t i = 0; i < *pSwapchainImageCount; ++i) {
pSwapchainImages[i] = images[i];
}
return res;
}
VKAPI_ATTR void VKAPI_CALL vkSetSwapchainCallback(VkSwapchainKHR swapchain,
void callback(void*, uint8_t*,
size_t),
void* user_data) {
// Force unwarping from higher layer when the env is set.
if (getenv("FORCE_UNWRAP_SWAPCHAIN_HANDLE")) {
swapchain = *reinterpret_cast<VkSwapchainKHR*>(swapchain);
}
CallbackSwapchain* swp = reinterpret_cast<CallbackSwapchain*>(swapchain);
swp->SetCallback(callback, user_data);
}
// We actually have to be able to submit data to the Queue right now.
// The user can supply either a semaphore, or a fence or both to this function.
// Because of this, once the image is available we have to submit
// a command to the queue to signal these.
VKAPI_ATTR VkResult VKAPI_CALL vkAcquireNextImageKHR(
VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout,
VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) {
CallbackSwapchain* swp = reinterpret_cast<CallbackSwapchain*>(swapchain);
if (!swp->GetImage(timeout, pImageIndex)) {
return timeout == 0 ? VK_NOT_READY : VK_TIMEOUT;
}
// It is important that we do not keep the lock here.
// *GetGlobalContext().GetDeviceData() only holds the lock
// for the duration of the call, if we instead do something like
// auto dat = GetGlobalContext().GetDeviceData(device),
// then the lock will be let go when dat is destroyed, which is
// AFTER swapchain::vkQueueSubmit, this would be a priority
// inversion on the locks.
DeviceData& dat = *GetGlobalContext().GetDeviceData(device);
VkQueue q;
dat.vkGetDeviceQueue(device, swp->DeviceQueue(), 0, &q);
bool has_semaphore = semaphore != VK_NULL_HANDLE;
VkSubmitInfo info{VK_STRUCTURE_TYPE_SUBMIT_INFO, // sType
nullptr, // pNext
0, // waitSemaphoreCount
nullptr, // waitSemaphores
nullptr, // waitDstStageMask
0, // commandBufferCount
nullptr, // pCommandBuffers
(has_semaphore ? 1u : 0u), // waitSemaphoreCount
(has_semaphore ? &semaphore : nullptr)};
return swapchain::vkQueueSubmit(q, 1, &info, fence);
}
VKAPI_ATTR VkResult VKAPI_CALL
vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) {
// We submit to the queue the commands set up by the callback swapchain.
// This will start a copy operation from the image to the swapchain
// buffers.
uint32_t res = VK_SUCCESS;
std::vector<VkPipelineStageFlags> pipeline_stages(
pPresentInfo->waitSemaphoreCount, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
for (size_t i = 0; i < pPresentInfo->swapchainCount; ++i) {
uint32_t image_index = pPresentInfo->pImageIndices[i];
CallbackSwapchain* swp =
reinterpret_cast<CallbackSwapchain*>(pPresentInfo->pSwapchains[i]);
VkSubmitInfo submitInfo{
VK_STRUCTURE_TYPE_SUBMIT_INFO, // sType
nullptr, // nullptr
i == 0 ? pPresentInfo->waitSemaphoreCount : 0, // waitSemaphoreCount
i == 0 ? pPresentInfo->pWaitSemaphores : nullptr, // pWaitSemaphores
i == 0 ? pipeline_stages.data() : nullptr, // pWaitDstStageMask
1, // commandBufferCount
&swp->GetCommandBuffer(image_index), // pCommandBuffers
0, // semaphoreCount
nullptr // pSemaphores
};
res |= GetGlobalContext().GetQueueData(queue)->vkQueueSubmit(
queue, 1, &submitInfo, swp->GetFence(image_index));
swp->NotifySubmitted(image_index);
}
return VkResult(res);
}
VKAPI_ATTR VkResult VKAPI_CALL vkQueueSubmit(VkQueue queue,
uint32_t submitCount,
const VkSubmitInfo* pSubmits,
VkFence fence) {
// We actually DO have to lock here, we may share this queue with
// vkAcquireNextImageKHR, which is not externally synchronized on Queue.
return GetGlobalContext().GetQueueData(queue)->vkQueueSubmit(
queue, submitCount, pSubmits, fence);
}
// The following 3 functions are special. We would normally not have to
// handle them, but since we cannot rely on there being an internal swapchain
// mechanism, we cannot allow VK_IMAGE_LAYOUT_PRESENT_SRC_KHR to be passed
// to the driver. In this case any time a user uses a layout that is
// VK_IMAGE_LAYOUT_PRESENT_SRC_KHR we replace that with
// VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, which is what we need an image to be
// set up as when we have to copy anyway.
VKAPI_ATTR void VKAPI_CALL vkCmdPipelineBarrier(
VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier* pBufferMemoryBarriers,
uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier* pImageMemoryBarriers) {
std::vector<VkImageMemoryBarrier> imageBarriers(imageMemoryBarrierCount);
for (size_t i = 0; i < imageMemoryBarrierCount; ++i) {
imageBarriers[i] = pImageMemoryBarriers[i];
if (imageBarriers[i].oldLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
imageBarriers[i].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
imageBarriers[i].srcAccessMask |= VK_ACCESS_TRANSFER_READ_BIT;
}
if (imageBarriers[i].newLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
imageBarriers[i].newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
imageBarriers[i].dstAccessMask |= VK_ACCESS_TRANSFER_READ_BIT;
}
}
PFN_vkCmdPipelineBarrier func = GetGlobalContext()
.GetCommandBufferData(commandBuffer)
->vkCmdPipelineBarrier;
return func(commandBuffer, srcStageMask, dstStageMask, dependencyFlags,
memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
pBufferMemoryBarriers, imageMemoryBarrierCount,
imageBarriers.data());
}
VKAPI_ATTR void VKAPI_CALL vkCmdWaitEvents(
VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent* pEvents,
VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
uint32_t memoryBarrierCount, const VkMemoryBarrier* pMemoryBarriers,
uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier* pBufferMemoryBarriers,
uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier* pImageMemoryBarriers) {
std::vector<VkImageMemoryBarrier> imageBarriers(imageMemoryBarrierCount);
for (size_t i = 0; i < imageMemoryBarrierCount; ++i) {
imageBarriers[i] = pImageMemoryBarriers[i];
if (imageBarriers[i].oldLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
imageBarriers[i].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
imageBarriers[i].srcAccessMask |= VK_ACCESS_TRANSFER_READ_BIT;
}
if (imageBarriers[i].newLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
imageBarriers[i].newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
imageBarriers[i].dstAccessMask |= VK_ACCESS_TRANSFER_READ_BIT;
}
}
PFN_vkCmdWaitEvents func =
GetGlobalContext().GetCommandBufferData(commandBuffer)->vkCmdWaitEvents;
func(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask,
memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
pBufferMemoryBarriers, imageMemoryBarrierCount, imageBarriers.data());
}
VKAPI_ATTR VkResult VKAPI_CALL vkCreateRenderPass(
VkDevice device, const VkRenderPassCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, VkRenderPass* pRenderPass) {
VkRenderPassCreateInfo intercepted = *pCreateInfo;
std::vector<VkAttachmentDescription> attachments(
pCreateInfo->attachmentCount);
intercepted.pAttachments = attachments.data();
for (size_t i = 0; i < pCreateInfo->attachmentCount; ++i) {
attachments[i] = pCreateInfo->pAttachments[i];
if (attachments[i].initialLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
attachments[i].initialLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
}
if (attachments[i].finalLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR) {
attachments[i].finalLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
}
}
PFN_vkCreateRenderPass func =
GetGlobalContext().GetDeviceData(device)->vkCreateRenderPass;
return func(device, &intercepted, pAllocator, pRenderPass);
}
} // namespace swapchain