blob: 05a3a30f1f5d1a4b9cc371f18c8f8bc18399c7cc [file] [log] [blame]
bsalomon18a2f9d2016-05-11 10:09:18 -07001/*
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 Daniel02611d92017-07-25 10:05:01 -040012#include "GrContext.h"
Greg Daniel35970ec2017-11-10 10:03:05 -050013#include "VkTestUtils.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040014#include "vk/GrVkExtensions.h"
bsalomonedea94c2016-05-16 14:09:56 -070015#include "vk/GrVkInterface.h"
16#include "vk/GrVkUtil.h"
bsalomonedea94c2016-05-16 14:09:56 -070017
bsalomon18a2f9d2016-05-11 10:09:18 -070018namespace {
Greg Danielc8cd45a2018-07-12 10:02:37 -040019
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
bsalomonedea94c2016-05-16 14:09:56 -070024/**
csmartdalton421a3c12016-10-04 11:08:45 -070025 * 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.
bsalomonedea94c2016-05-16 14:09:56 -070028 */
csmartdalton421a3c12016-10-04 11:08:45 -070029class VkFenceSync : public sk_gpu_test::FenceSync {
bsalomonedea94c2016-05-16 14:09:56 -070030public:
Greg Danielc8cd45a2018-07-12 10:02:37 -040031 VkFenceSync(GrVkGetProc getProc, VkDevice device, VkQueue queue,
bsalomonedea94c2016-05-16 14:09:56 -070032 uint32_t queueFamilyIndex)
Greg Danielc8cd45a2018-07-12 10:02:37 -040033 : fDevice(device)
bsalomonedea94c2016-05-16 14:09:56 -070034 , fQueue(queue) {
Greg Danielc8cd45a2018-07-12 10:02:37 -040035 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;
bsalomonedea94c2016-05-16 14:09:56 -070047 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 Danielc8cd45a2018-07-12 10:02:37 -040053 result = fCreateCommandPool(fDevice, &createInfo, nullptr, &fCommandPool);
54 SkASSERT(VK_SUCCESS == result);
bsalomonedea94c2016-05-16 14:09:56 -070055
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 Danielc8cd45a2018-07-12 10:02:37 -040062 result = fAllocateCommandBuffers(fDevice, &allocateInfo, &fCommandBuffer);
63 SkASSERT(VK_SUCCESS == result);
bsalomonedea94c2016-05-16 14:09:56 -070064
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 Danielc8cd45a2018-07-12 10:02:37 -040070 result = fBeginCommandBuffer(fCommandBuffer, &beginInfo);
71 SkASSERT(VK_SUCCESS == result);
72 result = fEndCommandBuffer(fCommandBuffer);
73 SkASSERT(VK_SUCCESS == result);
74
bsalomonedea94c2016-05-16 14:09:56 -070075 }
76
77 ~VkFenceSync() override {
78 SkASSERT(!fUnfinishedSyncs);
79 // If the above assertion is true then the command buffer should not be in flight.
Greg Danielc8cd45a2018-07-12 10:02:37 -040080 fFreeCommandBuffers(fDevice, fCommandPool, 1, &fCommandBuffer);
81 fDestroyCommandPool(fDevice, fCommandPool, nullptr);
bsalomonedea94c2016-05-16 14:09:56 -070082 }
83
csmartdalton421a3c12016-10-04 11:08:45 -070084 sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override {
Greg Danielc8cd45a2018-07-12 10:02:37 -040085 VkResult result;
86
bsalomonedea94c2016-05-16 14:09:56 -070087 VkFence fence;
88 VkFenceCreateInfo info;
89 info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
90 info.pNext = nullptr;
91 info.flags = 0;
Greg Danielc8cd45a2018-07-12 10:02:37 -040092 result = fCreateFence(fDevice, &info, nullptr, &fence);
93 SkASSERT(VK_SUCCESS == result);
94
bsalomonedea94c2016-05-16 14:09:56 -070095 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 Danielc8cd45a2018-07-12 10:02:37 -0400105 result = fQueueSubmit(fQueue, 1, &submitInfo, fence);
106 SkASSERT(VK_SUCCESS == result);
107
bsalomonedea94c2016-05-16 14:09:56 -0700108 SkDEBUGCODE(++fUnfinishedSyncs;)
Brian Osmanbbdf45e2016-10-10 17:04:52 -0400109 return (sk_gpu_test::PlatformFence)fence;
bsalomonedea94c2016-05-16 14:09:56 -0700110 }
111
csmartdalton421a3c12016-10-04 11:08:45 -0700112 bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override {
Brian Osmanbbdf45e2016-10-10 17:04:52 -0400113 VkFence fence = (VkFence)opaqueFence;
bsalomonedea94c2016-05-16 14:09:56 -0700114 static constexpr uint64_t kForever = ~((uint64_t)0);
Greg Danielc8cd45a2018-07-12 10:02:37 -0400115 auto result = fWaitForFences(fDevice, 1, &fence, true, kForever);
bsalomonedea94c2016-05-16 14:09:56 -0700116 return result != VK_TIMEOUT;
117 }
118
csmartdalton421a3c12016-10-04 11:08:45 -0700119 void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override {
Brian Osmanbbdf45e2016-10-10 17:04:52 -0400120 VkFence fence = (VkFence)opaqueFence;
Greg Danielc8cd45a2018-07-12 10:02:37 -0400121 fDestroyFence(fDevice, fence, nullptr);
bsalomonedea94c2016-05-16 14:09:56 -0700122 SkDEBUGCODE(--fUnfinishedSyncs;)
123 }
124
125private:
bsalomonedea94c2016-05-16 14:09:56 -0700126 VkDevice fDevice;
127 VkQueue fQueue;
128 VkCommandPool fCommandPool;
129 VkCommandBuffer fCommandBuffer;
Greg Danielc8cd45a2018-07-12 10:02:37 -0400130
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
bsalomonedea94c2016-05-16 14:09:56 -0700142 SkDEBUGCODE(mutable int fUnfinishedSyncs;)
csmartdalton421a3c12016-10-04 11:08:45 -0700143 typedef sk_gpu_test::FenceSync INHERITED;
bsalomonedea94c2016-05-16 14:09:56 -0700144};
145
csmartdalton024229a2016-10-04 14:24:23 -0700146GR_STATIC_ASSERT(sizeof(VkFence) <= sizeof(sk_gpu_test::PlatformFence));
147
bsalomonedea94c2016-05-16 14:09:56 -0700148// TODO: Implement swap buffers and finish
bsalomon18a2f9d2016-05-11 10:09:18 -0700149class VkTestContextImpl : public sk_gpu_test::VkTestContext {
150public:
Greg Daniel604b1972017-05-15 13:50:35 -0400151 static VkTestContext* Create(VkTestContext* sharedContext) {
Greg Danielf730c182018-07-02 20:15:37 +0000152 GrVkBackendContext backendContext;
Greg Daniel98bffae2018-08-01 13:25:41 -0400153 GrVkExtensions* extensions;
Greg Daniela0651ac2018-08-08 09:23:18 -0400154 VkPhysicalDeviceFeatures2* features;
Greg Danielf730c182018-07-02 20:15:37 +0000155 bool ownsContext = true;
Greg Daniel37329b32018-07-02 20:16:44 +0000156 VkDebugReportCallbackEXT debugCallback = VK_NULL_HANDLE;
Greg Daniela31f4e52018-08-01 16:48:52 -0400157 PFN_vkDestroyDebugReportCallbackEXT destroyCallback = nullptr;
Greg Daniel604b1972017-05-15 13:50:35 -0400158 if (sharedContext) {
159 backendContext = sharedContext->getVkBackendContext();
Greg Daniel98bffae2018-08-01 13:25:41 -0400160 extensions = const_cast<GrVkExtensions*>(sharedContext->getVkExtensions());
Greg Daniela0651ac2018-08-08 09:23:18 -0400161 features = const_cast<VkPhysicalDeviceFeatures2*>(sharedContext->getVkFeatures());
Greg Danielf730c182018-07-02 20:15:37 +0000162 // We always delete the parent context last so make sure the child does not think they
163 // own the vulkan context.
164 ownsContext = false;
Greg Daniel604b1972017-05-15 13:50:35 -0400165 } else {
Greg Daniel35970ec2017-11-10 10:03:05 -0500166 PFN_vkGetInstanceProcAddr instProc;
167 PFN_vkGetDeviceProcAddr devProc;
168 if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
169 return nullptr;
170 }
Greg Danield3e65aa2018-08-01 09:19:45 -0400171 auto getProc = [instProc, devProc](const char* proc_name,
172 VkInstance instance, VkDevice device) {
173 if (device != VK_NULL_HANDLE) {
174 return devProc(device, proc_name);
175 }
176 return instProc(instance, proc_name);
177 };
Greg Daniel98bffae2018-08-01 13:25:41 -0400178 extensions = new GrVkExtensions();
Greg Daniela0651ac2018-08-08 09:23:18 -0400179 features = new VkPhysicalDeviceFeatures2;
Greg Daniel98bffae2018-08-01 13:25:41 -0400180 if (!sk_gpu_test::CreateVkBackendContext(getProc, &backendContext, extensions,
Greg Daniela0651ac2018-08-08 09:23:18 -0400181 features, &debugCallback)) {
182 sk_gpu_test::FreeVulkanFeaturesStructs(features);
183 delete features;
Greg Danielf730c182018-07-02 20:15:37 +0000184 return nullptr;
185 }
Greg Daniela31f4e52018-08-01 16:48:52 -0400186 if (debugCallback != VK_NULL_HANDLE) {
187 destroyCallback = (PFN_vkDestroyDebugReportCallbackEXT) instProc(
188 backendContext.fInstance, "vkDestroyDebugReportCallbackEXT");
189 }
Greg Daniel604b1972017-05-15 13:50:35 -0400190 }
Greg Daniela0651ac2018-08-08 09:23:18 -0400191 return new VkTestContextImpl(backendContext, extensions, features, ownsContext,
192 debugCallback, destroyCallback);
bsalomonedea94c2016-05-16 14:09:56 -0700193 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700194
195 ~VkTestContextImpl() override { this->teardown(); }
196
197 void testAbandon() override {}
198
bsalomonedea94c2016-05-16 14:09:56 -0700199 // There is really nothing to here since we don't own any unqueued command buffers here.
bsalomonc8699322016-05-11 11:55:36 -0700200 void submit() override {}
bsalomonedea94c2016-05-16 14:09:56 -0700201
bsalomonc8699322016-05-11 11:55:36 -0700202 void finish() override {}
203
Greg Daniel02611d92017-07-25 10:05:01 -0400204 sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override {
Brian Salomon384fab42017-12-07 12:33:05 -0500205 return GrContext::MakeVulkan(fVk, options);
Greg Daniel02611d92017-07-25 10:05:01 -0400206 }
207
bsalomon18a2f9d2016-05-11 10:09:18 -0700208protected:
Greg Danielc8cd45a2018-07-12 10:02:37 -0400209#define ACQUIRE_VK_PROC_LOCAL(name, inst) \
210 PFN_vk##name grVk##name = \
211 reinterpret_cast<PFN_vk##name>(fVk.fGetProc("vk" #name, inst, nullptr)); \
212 if (grVk##name == nullptr) { \
213 SkDebugf("Function ptr for vk%s could not be acquired\n", #name); \
214 return; \
215 }
216
bsalomonedea94c2016-05-16 14:09:56 -0700217 void teardown() override {
218 INHERITED::teardown();
Greg Danielf730c182018-07-02 20:15:37 +0000219 fVk.fMemoryAllocator.reset();
220 if (fOwnsContext) {
Greg Danielc8cd45a2018-07-12 10:02:37 -0400221 ACQUIRE_VK_PROC_LOCAL(DeviceWaitIdle, fVk.fInstance);
222 ACQUIRE_VK_PROC_LOCAL(DestroyDevice, fVk.fInstance);
223 ACQUIRE_VK_PROC_LOCAL(DestroyInstance, fVk.fInstance);
224 grVkDeviceWaitIdle(fVk.fDevice);
225 grVkDestroyDevice(fVk.fDevice, nullptr);
Greg Daniel37329b32018-07-02 20:16:44 +0000226#ifdef SK_ENABLE_VK_LAYERS
227 if (fDebugCallback != VK_NULL_HANDLE) {
Greg Danielc8cd45a2018-07-12 10:02:37 -0400228 ACQUIRE_VK_PROC_LOCAL(DestroyDebugReportCallbackEXT, fVk.fInstance);
229 grVkDestroyDebugReportCallbackEXT(fVk.fInstance, fDebugCallback, nullptr);
Greg Daniel37329b32018-07-02 20:16:44 +0000230 }
231#endif
Greg Danielc8cd45a2018-07-12 10:02:37 -0400232 grVkDestroyInstance(fVk.fInstance, nullptr);
Greg Daniel98bffae2018-08-01 13:25:41 -0400233 delete fExtensions;
Greg Daniela0651ac2018-08-08 09:23:18 -0400234
235 sk_gpu_test::FreeVulkanFeaturesStructs(fFeatures);
236 delete fFeatures;
Greg Danielf730c182018-07-02 20:15:37 +0000237 }
bsalomonedea94c2016-05-16 14:09:56 -0700238 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700239
240private:
Greg Daniel98bffae2018-08-01 13:25:41 -0400241 VkTestContextImpl(const GrVkBackendContext& backendContext, const GrVkExtensions* extensions,
Greg Daniela0651ac2018-08-08 09:23:18 -0400242 VkPhysicalDeviceFeatures2* features, bool ownsContext,
243 VkDebugReportCallbackEXT debugCallback,
Greg Daniela31f4e52018-08-01 16:48:52 -0400244 PFN_vkDestroyDebugReportCallbackEXT destroyCallback)
Greg Daniela0651ac2018-08-08 09:23:18 -0400245 : VkTestContext(backendContext, extensions, features, ownsContext, debugCallback,
Greg Daniela31f4e52018-08-01 16:48:52 -0400246 destroyCallback) {
Greg Danielc8cd45a2018-07-12 10:02:37 -0400247 fFenceSync.reset(new VkFenceSync(fVk.fGetProc, fVk.fDevice, fVk.fQueue,
Greg Danielf730c182018-07-02 20:15:37 +0000248 fVk.fGraphicsQueueIndex));
bsalomonedea94c2016-05-16 14:09:56 -0700249 }
250
bsalomon18a2f9d2016-05-11 10:09:18 -0700251 void onPlatformMakeCurrent() const override {}
Brian Salomon55ad7742017-11-17 09:25:23 -0500252 std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
bsalomon18a2f9d2016-05-11 10:09:18 -0700253 void onPlatformSwapBuffers() const override {}
254
255 typedef sk_gpu_test::VkTestContext INHERITED;
256};
csmartdalton421a3c12016-10-04 11:08:45 -0700257} // anonymous namespace
bsalomon18a2f9d2016-05-11 10:09:18 -0700258
259namespace sk_gpu_test {
Greg Daniel604b1972017-05-15 13:50:35 -0400260VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) {
261 return VkTestContextImpl::Create(sharedContext);
262}
bsalomon18a2f9d2016-05-11 10:09:18 -0700263} // namespace sk_gpu_test
264
265#endif