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 { |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 18 | /** |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 19 | * Implements sk_gpu_test::FenceSync for Vulkan. It creates a single command |
| 20 | * buffer with USAGE_SIMULTANEOUS with no content . On every insertFence request |
| 21 | * it submits the command buffer with a new fence. |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 22 | */ |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 23 | class VkFenceSync : public sk_gpu_test::FenceSync { |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 24 | public: |
| 25 | VkFenceSync(sk_sp<const GrVkInterface> vk, VkDevice device, VkQueue queue, |
| 26 | uint32_t queueFamilyIndex) |
| 27 | : fVk(std::move(vk)) |
| 28 | , fDevice(device) |
| 29 | , fQueue(queue) { |
| 30 | SkDEBUGCODE(fUnfinishedSyncs = 0;) |
| 31 | VkCommandPoolCreateInfo createInfo; |
| 32 | createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 33 | createInfo.pNext = nullptr; |
| 34 | createInfo.flags = 0; |
| 35 | createInfo.queueFamilyIndex = queueFamilyIndex; |
| 36 | GR_VK_CALL_ERRCHECK(fVk, CreateCommandPool(fDevice, &createInfo, nullptr, &fCommandPool)); |
| 37 | |
| 38 | VkCommandBufferAllocateInfo allocateInfo; |
| 39 | allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 40 | allocateInfo.pNext = nullptr; |
| 41 | allocateInfo.commandBufferCount = 1; |
| 42 | allocateInfo.commandPool = fCommandPool; |
| 43 | allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 44 | GR_VK_CALL_ERRCHECK(fVk, AllocateCommandBuffers(fDevice, &allocateInfo, &fCommandBuffer)); |
| 45 | |
| 46 | VkCommandBufferBeginInfo beginInfo; |
| 47 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 48 | beginInfo.pNext = nullptr; |
| 49 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; |
| 50 | beginInfo.pInheritanceInfo = nullptr; |
| 51 | GR_VK_CALL_ERRCHECK(fVk, BeginCommandBuffer(fCommandBuffer, &beginInfo)); |
| 52 | GR_VK_CALL_ERRCHECK(fVk, EndCommandBuffer(fCommandBuffer)); |
| 53 | } |
| 54 | |
| 55 | ~VkFenceSync() override { |
| 56 | SkASSERT(!fUnfinishedSyncs); |
| 57 | // If the above assertion is true then the command buffer should not be in flight. |
| 58 | GR_VK_CALL(fVk, FreeCommandBuffers(fDevice, fCommandPool, 1, &fCommandBuffer)); |
| 59 | GR_VK_CALL(fVk, DestroyCommandPool(fDevice, fCommandPool, nullptr)); |
| 60 | } |
| 61 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 62 | sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override { |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 63 | VkFence fence; |
| 64 | VkFenceCreateInfo info; |
| 65 | info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 66 | info.pNext = nullptr; |
| 67 | info.flags = 0; |
| 68 | GR_VK_CALL_ERRCHECK(fVk, CreateFence(fDevice, &info, nullptr, &fence)); |
| 69 | VkSubmitInfo submitInfo; |
| 70 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 71 | submitInfo.pNext = nullptr; |
| 72 | submitInfo.waitSemaphoreCount = 0; |
| 73 | submitInfo.pWaitSemaphores = nullptr; |
| 74 | submitInfo.pWaitDstStageMask = nullptr; |
| 75 | submitInfo.commandBufferCount = 1; |
| 76 | submitInfo.pCommandBuffers = &fCommandBuffer; |
| 77 | submitInfo.signalSemaphoreCount = 0; |
| 78 | submitInfo.pSignalSemaphores = nullptr; |
| 79 | GR_VK_CALL_ERRCHECK(fVk, QueueSubmit(fQueue, 1, &submitInfo, fence)); |
| 80 | SkDEBUGCODE(++fUnfinishedSyncs;) |
Brian Osman | bbdf45e | 2016-10-10 17:04:52 -0400 | [diff] [blame] | 81 | return (sk_gpu_test::PlatformFence)fence; |
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 | bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override { |
Brian Osman | bbdf45e | 2016-10-10 17:04:52 -0400 | [diff] [blame] | 85 | VkFence fence = (VkFence)opaqueFence; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 86 | static constexpr uint64_t kForever = ~((uint64_t)0); |
| 87 | auto result = GR_VK_CALL(fVk, WaitForFences(fDevice, 1, &fence, true, kForever)); |
| 88 | return result != VK_TIMEOUT; |
| 89 | } |
| 90 | |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 91 | void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override { |
Brian Osman | bbdf45e | 2016-10-10 17:04:52 -0400 | [diff] [blame] | 92 | VkFence fence = (VkFence)opaqueFence; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 93 | GR_VK_CALL(fVk, DestroyFence(fDevice, fence, nullptr)); |
| 94 | SkDEBUGCODE(--fUnfinishedSyncs;) |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | sk_sp<const GrVkInterface> fVk; |
| 99 | VkDevice fDevice; |
| 100 | VkQueue fQueue; |
| 101 | VkCommandPool fCommandPool; |
| 102 | VkCommandBuffer fCommandBuffer; |
| 103 | SkDEBUGCODE(mutable int fUnfinishedSyncs;) |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 104 | typedef sk_gpu_test::FenceSync INHERITED; |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
csmartdalton | 024229a | 2016-10-04 14:24:23 -0700 | [diff] [blame] | 107 | GR_STATIC_ASSERT(sizeof(VkFence) <= sizeof(sk_gpu_test::PlatformFence)); |
| 108 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 109 | // TODO: Implement swap buffers and finish |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 110 | class VkTestContextImpl : public sk_gpu_test::VkTestContext { |
| 111 | public: |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 112 | static VkTestContext* Create(VkTestContext* sharedContext) { |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame^] | 113 | GrVkBackendContext backendContext; |
| 114 | bool ownsContext = true; |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 115 | if (sharedContext) { |
| 116 | backendContext = sharedContext->getVkBackendContext(); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame^] | 117 | // We always delete the parent context last so make sure the child does not think they |
| 118 | // own the vulkan context. |
| 119 | ownsContext = false; |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 120 | } else { |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 121 | PFN_vkGetInstanceProcAddr instProc; |
| 122 | PFN_vkGetDeviceProcAddr devProc; |
| 123 | if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) { |
| 124 | return nullptr; |
| 125 | } |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame^] | 126 | if (!sk_gpu_test::CreateVkBackendContext(instProc, devProc, &backendContext)) { |
| 127 | return nullptr; |
| 128 | } |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 129 | } |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame^] | 130 | return new VkTestContextImpl(backendContext, ownsContext); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 131 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 132 | |
| 133 | ~VkTestContextImpl() override { this->teardown(); } |
| 134 | |
| 135 | void testAbandon() override {} |
| 136 | |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 137 | // 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] | 138 | void submit() override {} |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 139 | |
bsalomon | c869932 | 2016-05-11 11:55:36 -0700 | [diff] [blame] | 140 | void finish() override {} |
| 141 | |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 142 | sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override { |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 143 | return GrContext::MakeVulkan(fVk, options); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 144 | } |
| 145 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 146 | protected: |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 147 | void teardown() override { |
| 148 | INHERITED::teardown(); |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame^] | 149 | fVk.fMemoryAllocator.reset(); |
| 150 | if (fOwnsContext) { |
| 151 | GR_VK_CALL(this->vk(), DeviceWaitIdle(fVk.fDevice)); |
| 152 | GR_VK_CALL(this->vk(), DestroyDevice(fVk.fDevice, nullptr)); |
| 153 | GR_VK_CALL(this->vk(), DestroyInstance(fVk.fInstance, nullptr)); |
| 154 | } |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 155 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 156 | |
| 157 | private: |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame^] | 158 | VkTestContextImpl(const GrVkBackendContext& backendContext, bool ownsContext) |
| 159 | : VkTestContext(backendContext, ownsContext) { |
| 160 | fFenceSync.reset(new VkFenceSync(fVk.fInterface, fVk.fDevice, fVk.fQueue, |
| 161 | fVk.fGraphicsQueueIndex)); |
bsalomon | edea94c | 2016-05-16 14:09:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 164 | void onPlatformMakeCurrent() const override {} |
Brian Salomon | 55ad774 | 2017-11-17 09:25:23 -0500 | [diff] [blame] | 165 | std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 166 | void onPlatformSwapBuffers() const override {} |
| 167 | |
| 168 | typedef sk_gpu_test::VkTestContext INHERITED; |
| 169 | }; |
csmartdalton | 421a3c1 | 2016-10-04 11:08:45 -0700 | [diff] [blame] | 170 | } // anonymous namespace |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 171 | |
| 172 | namespace sk_gpu_test { |
Greg Daniel | 604b197 | 2017-05-15 13:50:35 -0400 | [diff] [blame] | 173 | VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) { |
| 174 | return VkTestContextImpl::Create(sharedContext); |
| 175 | } |
bsalomon | 18a2f9d | 2016-05-11 10:09:18 -0700 | [diff] [blame] | 176 | } // namespace sk_gpu_test |
| 177 | |
| 178 | #endif |