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