bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "VkTestContext.h" |
| 9 | |
| 10 | #ifdef SK_VULKAN |
| 11 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 12 | #include "GrContext.h" |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 13 | #include "VkTestUtils.h" |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 14 | #include "vk/GrVkInterface.h" |
| 15 | #include "vk/GrVkUtil.h" |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 16 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 17 | namespace { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 18 | |
| 19 | #define ACQUIRE_VK_PROC(name, device) \ |
| 20 | f##name = reinterpret_cast<PFN_vk##name>(getProc("vk" #name, nullptr, device)); \ |
| 21 | SkASSERT(f##name); |
| 22 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 23 | /** |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 24 | * Implements sk_gpu_test::FenceSync for Vulkan. It creates a single command |
| 25 | * buffer with USAGE_SIMULTANEOUS with no content . On every insertFence request |
| 26 | * it submits the command buffer with a new fence. |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 27 | */ |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 28 | class VkFenceSync : public sk_gpu_test::FenceSync { |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 29 | public: |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 30 | VkFenceSync(GrVkGetProc getProc, VkDevice device, VkQueue queue, |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 31 | uint32_t queueFamilyIndex) |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 32 | : fDevice(device) |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 33 | , fQueue(queue) { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 34 | ACQUIRE_VK_PROC(CreateCommandPool, device); |
| 35 | ACQUIRE_VK_PROC(DestroyCommandPool, device); |
| 36 | ACQUIRE_VK_PROC(AllocateCommandBuffers, device); |
| 37 | ACQUIRE_VK_PROC(FreeCommandBuffers, device); |
| 38 | ACQUIRE_VK_PROC(BeginCommandBuffer, device); |
| 39 | ACQUIRE_VK_PROC(EndCommandBuffer, device); |
| 40 | ACQUIRE_VK_PROC(CreateFence, device); |
| 41 | ACQUIRE_VK_PROC(DestroyFence, device); |
| 42 | ACQUIRE_VK_PROC(WaitForFences, device); |
| 43 | ACQUIRE_VK_PROC(QueueSubmit, device); |
| 44 | |
| 45 | VkResult result; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 46 | SkDEBUGCODE(fUnfinishedSyncs = 0;) |
| 47 | VkCommandPoolCreateInfo createInfo; |
| 48 | createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 49 | createInfo.pNext = nullptr; |
| 50 | createInfo.flags = 0; |
| 51 | createInfo.queueFamilyIndex = queueFamilyIndex; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 52 | result = fCreateCommandPool(fDevice, &createInfo, nullptr, &fCommandPool); |
| 53 | SkASSERT(VK_SUCCESS == result); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 54 | |
| 55 | VkCommandBufferAllocateInfo allocateInfo; |
| 56 | allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 57 | allocateInfo.pNext = nullptr; |
| 58 | allocateInfo.commandBufferCount = 1; |
| 59 | allocateInfo.commandPool = fCommandPool; |
| 60 | allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 61 | result = fAllocateCommandBuffers(fDevice, &allocateInfo, &fCommandBuffer); |
| 62 | SkASSERT(VK_SUCCESS == result); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 63 | |
| 64 | VkCommandBufferBeginInfo beginInfo; |
| 65 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 66 | beginInfo.pNext = nullptr; |
| 67 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; |
| 68 | beginInfo.pInheritanceInfo = nullptr; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 69 | result = fBeginCommandBuffer(fCommandBuffer, &beginInfo); |
| 70 | SkASSERT(VK_SUCCESS == result); |
| 71 | result = fEndCommandBuffer(fCommandBuffer); |
| 72 | SkASSERT(VK_SUCCESS == result); |
| 73 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ~VkFenceSync() override { |
| 77 | SkASSERT(!fUnfinishedSyncs); |
| 78 | // If the above assertion is true then the command buffer should not be in flight. |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 79 | fFreeCommandBuffers(fDevice, fCommandPool, 1, &fCommandBuffer); |
| 80 | fDestroyCommandPool(fDevice, fCommandPool, nullptr); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 81 | } |
| 82 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 83 | sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 84 | VkResult result; |
| 85 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 86 | VkFence fence; |
| 87 | VkFenceCreateInfo info; |
| 88 | info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 89 | info.pNext = nullptr; |
| 90 | info.flags = 0; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 91 | result = fCreateFence(fDevice, &info, nullptr, &fence); |
| 92 | SkASSERT(VK_SUCCESS == result); |
| 93 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 94 | VkSubmitInfo submitInfo; |
| 95 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 96 | submitInfo.pNext = nullptr; |
| 97 | submitInfo.waitSemaphoreCount = 0; |
| 98 | submitInfo.pWaitSemaphores = nullptr; |
| 99 | submitInfo.pWaitDstStageMask = nullptr; |
| 100 | submitInfo.commandBufferCount = 1; |
| 101 | submitInfo.pCommandBuffers = &fCommandBuffer; |
| 102 | submitInfo.signalSemaphoreCount = 0; |
| 103 | submitInfo.pSignalSemaphores = nullptr; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 104 | result = fQueueSubmit(fQueue, 1, &submitInfo, fence); |
| 105 | SkASSERT(VK_SUCCESS == result); |
| 106 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 107 | SkDEBUGCODE(++fUnfinishedSyncs;) |
Brian Osman | bbdf45e | 2016-10-10 17:04:52 -0400 | [diff] [blame] | 108 | return (sk_gpu_test::PlatformFence)fence; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 109 | } |
| 110 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 111 | bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override { |
Brian Osman | bbdf45e | 2016-10-10 17:04:52 -0400 | [diff] [blame] | 112 | VkFence fence = (VkFence)opaqueFence; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 113 | static constexpr uint64_t kForever = ~((uint64_t)0); |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 114 | auto result = fWaitForFences(fDevice, 1, &fence, true, kForever); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 115 | return result != VK_TIMEOUT; |
| 116 | } |
| 117 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 118 | void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override { |
Brian Osman | bbdf45e | 2016-10-10 17:04:52 -0400 | [diff] [blame] | 119 | VkFence fence = (VkFence)opaqueFence; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 120 | fDestroyFence(fDevice, fence, nullptr); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 121 | SkDEBUGCODE(--fUnfinishedSyncs;) |
| 122 | } |
| 123 | |
| 124 | private: |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 125 | VkDevice fDevice; |
| 126 | VkQueue fQueue; |
| 127 | VkCommandPool fCommandPool; |
| 128 | VkCommandBuffer fCommandBuffer; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 129 | |
| 130 | PFN_vkCreateCommandPool fCreateCommandPool = nullptr; |
| 131 | PFN_vkDestroyCommandPool fDestroyCommandPool = nullptr; |
| 132 | PFN_vkAllocateCommandBuffers fAllocateCommandBuffers = nullptr; |
| 133 | PFN_vkFreeCommandBuffers fFreeCommandBuffers = nullptr; |
| 134 | PFN_vkBeginCommandBuffer fBeginCommandBuffer = nullptr; |
| 135 | PFN_vkEndCommandBuffer fEndCommandBuffer = nullptr; |
| 136 | PFN_vkCreateFence fCreateFence = nullptr; |
| 137 | PFN_vkDestroyFence fDestroyFence = nullptr; |
| 138 | PFN_vkWaitForFences fWaitForFences = nullptr; |
| 139 | PFN_vkQueueSubmit fQueueSubmit = nullptr; |
| 140 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 141 | SkDEBUGCODE(mutable int fUnfinishedSyncs;) |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 142 | typedef sk_gpu_test::FenceSync INHERITED; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
csmartdalton | 024229a | 2016-10-04 14:24:23 -0700 | [diff] [blame] | 145 | GR_STATIC_ASSERT(sizeof(VkFence) <= sizeof(sk_gpu_test::PlatformFence)); |
| 146 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 147 | // TODO: Implement swap buffers and finish |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 148 | class VkTestContextImpl : public sk_gpu_test::VkTestContext { |
| 149 | public: |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 150 | static VkTestContext* Create(VkTestContext* sharedContext) { |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 151 | GrVkBackendContext backendContext; |
| 152 | bool ownsContext = true; |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 153 | VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE; |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 154 | if (sharedContext) { |
| 155 | backendContext = sharedContext->getVkBackendContext(); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 156 | // We always delete the parent context last so make sure the child does not think they |
| 157 | // own the vulkan context. |
| 158 | ownsContext = false; |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 159 | } else { |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 160 | PFN_vkGetInstanceProcAddr instProc; |
| 161 | PFN_vkGetDeviceProcAddr devProc; |
| 162 | if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) { |
| 163 | return nullptr; |
| 164 | } |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 165 | if (!sk_gpu_test::CreateVkBackendContext(instProc, devProc, &backendContext, |
| 166 | &debugCallback)) { |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 167 | return nullptr; |
| 168 | } |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 169 | } |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 170 | return new VkTestContextImpl(backendContext, ownsContext, debugCallback); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 171 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 172 | |
| 173 | ~VkTestContextImpl() override { this->teardown(); } |
| 174 | |
| 175 | void testAbandon() override {} |
| 176 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 177 | // There is really nothing to here since we don't own any unqueued command buffers here. |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 178 | void submit() override {} |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 179 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 180 | void finish() override {} |
| 181 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 182 | sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override { |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 183 | return GrContext::MakeVulkan(fVk, options); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 184 | } |
| 185 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 186 | protected: |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 187 | #define ACQUIRE_VK_PROC_LOCAL(name, inst) \ |
| 188 | PFN_vk##name grVk##name = \ |
| 189 | reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \ |
| 190 | if (grVk##name == nullptr) { \ |
| 191 | SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \ |
| 192 | return; \ |
| 193 | } |
| 194 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 195 | void teardown() override { |
| 196 | INHERITED::teardown(); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 197 | fVk.fMemoryAllocator.reset(); |
| 198 | if (fOwnsContext) { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 199 | ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance); |
| 200 | ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance); |
| 201 | ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance); |
| 202 | grVkDeviceWaitIdle(fVk.fDevice); |
| 203 | grVkDestroyDevice(fVk.fDevice, nullptr); |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 204 | #ifdef SK_ENABLE_VK_LAYERS |
| 205 | if (fDebugCallback != VK_NULL_HANDLE) { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 206 | ACQUIRE_VK_PROC_LOCAL(DestroyDebugReportCallbackEXT, fVk.fInstance); |
| 207 | grVkDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr); |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 208 | } |
| 209 | #endif |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 210 | grVkDestroyInstance(fVk.fInstance, nullptr); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 211 | } |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 212 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 213 | |
| 214 | private: |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 215 | VkTestContextImpl(const GrVkBackendContext& backendContext, bool ownsContext, |
| 216 | VkDebugReportCallbackEXT debugCallback) |
| 217 | : VkTestContext(backendContext, ownsContext, debugCallback) { |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame^] | 218 | fFenceSync.reset(new VkFenceSync(fVk.fGetProc, fVk.fDevice, fVk.fQueue, |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 219 | fVk.fGraphicsQueueIndex)); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 220 | } |
| 221 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 222 | void onPlatformMakeCurrent() const override {} |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 223 | std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 224 | void onPlatformSwapBuffers() const override {} |
| 225 | |
| 226 | typedef sk_gpu_test::VkTestContext INHERITED; |
| 227 | }; |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 228 | } // anonymous namespace |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 229 | |
| 230 | namespace sk_gpu_test { |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 231 | VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) { |
| 232 | return VkTestContextImpl::Create(sharedContext); |
| 233 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 234 | } // namespace sk_gpu_test |
| 235 | |
| 236 | #endif |