blob: c1a2f8ab5689c312ead482963773575337704ade [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
bsalomonedea94c2016-05-16 14:09:56 -070012#include "vk/GrVkInterface.h"
13#include "vk/GrVkUtil.h"
14#include <vulkan/vulkan.h>
15
bsalomon18a2f9d2016-05-11 10:09:18 -070016namespace {
bsalomonedea94c2016-05-16 14:09:56 -070017/**
csmartdalton421a3c12016-10-04 11:08:45 -070018 * Implements sk_gpu_test::FenceSync for Vulkan. It creates a single command
19 * buffer with USAGE_SIMULTANEOUS with no content . On every insertFence request
20 * it submits the command buffer with a new fence.
bsalomonedea94c2016-05-16 14:09:56 -070021 */
csmartdalton421a3c12016-10-04 11:08:45 -070022class VkFenceSync : public sk_gpu_test::FenceSync {
bsalomonedea94c2016-05-16 14:09:56 -070023public:
24 VkFenceSync(sk_sp<const GrVkInterface> vk, VkDevice device, VkQueue queue,
25 uint32_t queueFamilyIndex)
26 : fVk(std::move(vk))
27 , fDevice(device)
28 , fQueue(queue) {
29 SkDEBUGCODE(fUnfinishedSyncs = 0;)
30 VkCommandPoolCreateInfo createInfo;
31 createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
32 createInfo.pNext = nullptr;
33 createInfo.flags = 0;
34 createInfo.queueFamilyIndex = queueFamilyIndex;
35 GR_VK_CALL_ERRCHECK(fVk, CreateCommandPool(fDevice, &createInfo, nullptr, &fCommandPool));
36
37 VkCommandBufferAllocateInfo allocateInfo;
38 allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
39 allocateInfo.pNext = nullptr;
40 allocateInfo.commandBufferCount = 1;
41 allocateInfo.commandPool = fCommandPool;
42 allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
43 GR_VK_CALL_ERRCHECK(fVk, AllocateCommandBuffers(fDevice, &allocateInfo, &fCommandBuffer));
44
45 VkCommandBufferBeginInfo beginInfo;
46 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
47 beginInfo.pNext = nullptr;
48 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
49 beginInfo.pInheritanceInfo = nullptr;
50 GR_VK_CALL_ERRCHECK(fVk, BeginCommandBuffer(fCommandBuffer, &beginInfo));
51 GR_VK_CALL_ERRCHECK(fVk, EndCommandBuffer(fCommandBuffer));
52 }
53
54 ~VkFenceSync() override {
55 SkASSERT(!fUnfinishedSyncs);
56 // If the above assertion is true then the command buffer should not be in flight.
57 GR_VK_CALL(fVk, FreeCommandBuffers(fDevice, fCommandPool, 1, &fCommandBuffer));
58 GR_VK_CALL(fVk, DestroyCommandPool(fDevice, fCommandPool, nullptr));
59 }
60
csmartdalton421a3c12016-10-04 11:08:45 -070061 sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override {
bsalomonedea94c2016-05-16 14:09:56 -070062 VkFence fence;
63 VkFenceCreateInfo info;
64 info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
65 info.pNext = nullptr;
66 info.flags = 0;
67 GR_VK_CALL_ERRCHECK(fVk, CreateFence(fDevice, &info, nullptr, &fence));
68 VkSubmitInfo submitInfo;
69 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
70 submitInfo.pNext = nullptr;
71 submitInfo.waitSemaphoreCount = 0;
72 submitInfo.pWaitSemaphores = nullptr;
73 submitInfo.pWaitDstStageMask = nullptr;
74 submitInfo.commandBufferCount = 1;
75 submitInfo.pCommandBuffers = &fCommandBuffer;
76 submitInfo.signalSemaphoreCount = 0;
77 submitInfo.pSignalSemaphores = nullptr;
78 GR_VK_CALL_ERRCHECK(fVk, QueueSubmit(fQueue, 1, &submitInfo, fence));
79 SkDEBUGCODE(++fUnfinishedSyncs;)
csmartdalton421a3c12016-10-04 11:08:45 -070080 return reinterpret_cast<sk_gpu_test::PlatformFence>(fence);
bsalomonedea94c2016-05-16 14:09:56 -070081 }
82
csmartdalton421a3c12016-10-04 11:08:45 -070083 bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override {
bsalomonedea94c2016-05-16 14:09:56 -070084 VkFence fence = reinterpret_cast<VkFence>(opaqueFence);
85 static constexpr uint64_t kForever = ~((uint64_t)0);
86 auto result = GR_VK_CALL(fVk, WaitForFences(fDevice, 1, &fence, true, kForever));
87 return result != VK_TIMEOUT;
88 }
89
csmartdalton421a3c12016-10-04 11:08:45 -070090 void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override {
bsalomonedea94c2016-05-16 14:09:56 -070091 VkFence fence = reinterpret_cast<VkFence>(opaqueFence);
92 GR_VK_CALL(fVk, DestroyFence(fDevice, fence, nullptr));
93 SkDEBUGCODE(--fUnfinishedSyncs;)
94 }
95
96private:
97 sk_sp<const GrVkInterface> fVk;
98 VkDevice fDevice;
99 VkQueue fQueue;
100 VkCommandPool fCommandPool;
101 VkCommandBuffer fCommandBuffer;
102 SkDEBUGCODE(mutable int fUnfinishedSyncs;)
csmartdalton421a3c12016-10-04 11:08:45 -0700103 typedef sk_gpu_test::FenceSync INHERITED;
bsalomonedea94c2016-05-16 14:09:56 -0700104};
105
106// TODO: Implement swap buffers and finish
bsalomon18a2f9d2016-05-11 10:09:18 -0700107class VkTestContextImpl : public sk_gpu_test::VkTestContext {
108public:
bsalomonedea94c2016-05-16 14:09:56 -0700109 static VkTestContext* Create() {
110 sk_sp<const GrVkBackendContext> backendContext(GrVkBackendContext::Create());
111 if (!backendContext) {
112 return nullptr;
113 }
114 return new VkTestContextImpl(std::move(backendContext));
115 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700116
117 ~VkTestContextImpl() override { this->teardown(); }
118
119 void testAbandon() override {}
120
bsalomonedea94c2016-05-16 14:09:56 -0700121 // There is really nothing to here since we don't own any unqueued command buffers here.
bsalomonc8699322016-05-11 11:55:36 -0700122 void submit() override {}
bsalomonedea94c2016-05-16 14:09:56 -0700123
bsalomonc8699322016-05-11 11:55:36 -0700124 void finish() override {}
125
bsalomon18a2f9d2016-05-11 10:09:18 -0700126protected:
bsalomonedea94c2016-05-16 14:09:56 -0700127 void teardown() override {
128 INHERITED::teardown();
129 fVk.reset(nullptr);
130 }
bsalomon18a2f9d2016-05-11 10:09:18 -0700131
132private:
bsalomonedea94c2016-05-16 14:09:56 -0700133 VkTestContextImpl(sk_sp<const GrVkBackendContext> backendContext)
134 : VkTestContext(std::move(backendContext)) {
135 fFenceSync = new VkFenceSync(sk_ref_sp(fVk->fInterface.get()), fVk->fDevice, fVk->fQueue,
136 fVk->fGraphicsQueueIndex);
137 }
138
bsalomon18a2f9d2016-05-11 10:09:18 -0700139 void onPlatformMakeCurrent() const override {}
140 void onPlatformSwapBuffers() const override {}
141
142 typedef sk_gpu_test::VkTestContext INHERITED;
143};
csmartdalton421a3c12016-10-04 11:08:45 -0700144} // anonymous namespace
bsalomon18a2f9d2016-05-11 10:09:18 -0700145
146namespace sk_gpu_test {
bsalomonedea94c2016-05-16 14:09:56 -0700147VkTestContext* CreatePlatformVkTestContext() { return VkTestContextImpl::Create(); }
bsalomon18a2f9d2016-05-11 10:09:18 -0700148} // namespace sk_gpu_test
149
150#endif