2016-05-11 17:09:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "VkTestContext.h"
|
|
|
|
|
|
|
|
#ifdef SK_VULKAN
|
|
|
|
|
2017-07-25 14:05:01 +00:00
|
|
|
#include "GrContext.h"
|
2017-11-10 15:03:05 +00:00
|
|
|
#include "VkTestUtils.h"
|
2016-05-16 21:09:56 +00:00
|
|
|
#include "vk/GrVkInterface.h"
|
|
|
|
#include "vk/GrVkUtil.h"
|
|
|
|
|
2016-05-11 17:09:18 +00:00
|
|
|
namespace {
|
2016-05-16 21:09:56 +00:00
|
|
|
/**
|
2016-10-04 18:08:45 +00:00
|
|
|
* Implements sk_gpu_test::FenceSync for Vulkan. It creates a single command
|
|
|
|
* buffer with USAGE_SIMULTANEOUS with no content . On every insertFence request
|
|
|
|
* it submits the command buffer with a new fence.
|
2016-05-16 21:09:56 +00:00
|
|
|
*/
|
2016-10-04 18:08:45 +00:00
|
|
|
class VkFenceSync : public sk_gpu_test::FenceSync {
|
2016-05-16 21:09:56 +00:00
|
|
|
public:
|
|
|
|
VkFenceSync(sk_sp<const GrVkInterface> vk, VkDevice device, VkQueue queue,
|
|
|
|
uint32_t queueFamilyIndex)
|
|
|
|
: fVk(std::move(vk))
|
|
|
|
, fDevice(device)
|
|
|
|
, fQueue(queue) {
|
|
|
|
SkDEBUGCODE(fUnfinishedSyncs = 0;)
|
|
|
|
VkCommandPoolCreateInfo createInfo;
|
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
|
|
|
createInfo.pNext = nullptr;
|
|
|
|
createInfo.flags = 0;
|
|
|
|
createInfo.queueFamilyIndex = queueFamilyIndex;
|
|
|
|
GR_VK_CALL_ERRCHECK(fVk, CreateCommandPool(fDevice, &createInfo, nullptr, &fCommandPool));
|
|
|
|
|
|
|
|
VkCommandBufferAllocateInfo allocateInfo;
|
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
|
|
|
allocateInfo.pNext = nullptr;
|
|
|
|
allocateInfo.commandBufferCount = 1;
|
|
|
|
allocateInfo.commandPool = fCommandPool;
|
|
|
|
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
|
|
GR_VK_CALL_ERRCHECK(fVk, AllocateCommandBuffers(fDevice, &allocateInfo, &fCommandBuffer));
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo;
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
|
|
beginInfo.pNext = nullptr;
|
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
|
|
|
beginInfo.pInheritanceInfo = nullptr;
|
|
|
|
GR_VK_CALL_ERRCHECK(fVk, BeginCommandBuffer(fCommandBuffer, &beginInfo));
|
|
|
|
GR_VK_CALL_ERRCHECK(fVk, EndCommandBuffer(fCommandBuffer));
|
|
|
|
}
|
|
|
|
|
|
|
|
~VkFenceSync() override {
|
|
|
|
SkASSERT(!fUnfinishedSyncs);
|
|
|
|
// If the above assertion is true then the command buffer should not be in flight.
|
|
|
|
GR_VK_CALL(fVk, FreeCommandBuffers(fDevice, fCommandPool, 1, &fCommandBuffer));
|
|
|
|
GR_VK_CALL(fVk, DestroyCommandPool(fDevice, fCommandPool, nullptr));
|
|
|
|
}
|
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
sk_gpu_test::PlatformFence SK_WARN_UNUSED_RESULT insertFence() const override {
|
2016-05-16 21:09:56 +00:00
|
|
|
VkFence fence;
|
|
|
|
VkFenceCreateInfo info;
|
|
|
|
info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
|
|
|
info.pNext = nullptr;
|
|
|
|
info.flags = 0;
|
|
|
|
GR_VK_CALL_ERRCHECK(fVk, CreateFence(fDevice, &info, nullptr, &fence));
|
|
|
|
VkSubmitInfo submitInfo;
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
submitInfo.pNext = nullptr;
|
|
|
|
submitInfo.waitSemaphoreCount = 0;
|
|
|
|
submitInfo.pWaitSemaphores = nullptr;
|
|
|
|
submitInfo.pWaitDstStageMask = nullptr;
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
submitInfo.pCommandBuffers = &fCommandBuffer;
|
|
|
|
submitInfo.signalSemaphoreCount = 0;
|
|
|
|
submitInfo.pSignalSemaphores = nullptr;
|
|
|
|
GR_VK_CALL_ERRCHECK(fVk, QueueSubmit(fQueue, 1, &submitInfo, fence));
|
|
|
|
SkDEBUGCODE(++fUnfinishedSyncs;)
|
2016-10-10 21:04:52 +00:00
|
|
|
return (sk_gpu_test::PlatformFence)fence;
|
2016-05-16 21:09:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
bool waitFence(sk_gpu_test::PlatformFence opaqueFence) const override {
|
2016-10-10 21:04:52 +00:00
|
|
|
VkFence fence = (VkFence)opaqueFence;
|
2016-05-16 21:09:56 +00:00
|
|
|
static constexpr uint64_t kForever = ~((uint64_t)0);
|
|
|
|
auto result = GR_VK_CALL(fVk, WaitForFences(fDevice, 1, &fence, true, kForever));
|
|
|
|
return result != VK_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
2016-10-04 18:08:45 +00:00
|
|
|
void deleteFence(sk_gpu_test::PlatformFence opaqueFence) const override {
|
2016-10-10 21:04:52 +00:00
|
|
|
VkFence fence = (VkFence)opaqueFence;
|
2016-05-16 21:09:56 +00:00
|
|
|
GR_VK_CALL(fVk, DestroyFence(fDevice, fence, nullptr));
|
|
|
|
SkDEBUGCODE(--fUnfinishedSyncs;)
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
sk_sp<const GrVkInterface> fVk;
|
|
|
|
VkDevice fDevice;
|
|
|
|
VkQueue fQueue;
|
|
|
|
VkCommandPool fCommandPool;
|
|
|
|
VkCommandBuffer fCommandBuffer;
|
|
|
|
SkDEBUGCODE(mutable int fUnfinishedSyncs;)
|
2016-10-04 18:08:45 +00:00
|
|
|
typedef sk_gpu_test::FenceSync INHERITED;
|
2016-05-16 21:09:56 +00:00
|
|
|
};
|
|
|
|
|
2016-10-04 21:24:23 +00:00
|
|
|
GR_STATIC_ASSERT(sizeof(VkFence) <= sizeof(sk_gpu_test::PlatformFence));
|
|
|
|
|
2016-05-16 21:09:56 +00:00
|
|
|
// TODO: Implement swap buffers and finish
|
2016-05-11 17:09:18 +00:00
|
|
|
class VkTestContextImpl : public sk_gpu_test::VkTestContext {
|
|
|
|
public:
|
2017-05-15 17:50:35 +00:00
|
|
|
static VkTestContext* Create(VkTestContext* sharedContext) {
|
2018-06-28 16:47:48 +00:00
|
|
|
GrVkBackendContext backendContext;
|
|
|
|
bool ownsContext = true;
|
2017-05-15 17:50:35 +00:00
|
|
|
if (sharedContext) {
|
|
|
|
backendContext = sharedContext->getVkBackendContext();
|
2018-06-28 16:47:48 +00:00
|
|
|
// We always delete the parent context last so make sure the child does not think they
|
|
|
|
// own the vulkan context.
|
|
|
|
ownsContext = false;
|
2017-05-15 17:50:35 +00:00
|
|
|
} else {
|
2017-11-10 15:03:05 +00:00
|
|
|
PFN_vkGetInstanceProcAddr instProc;
|
|
|
|
PFN_vkGetDeviceProcAddr devProc;
|
|
|
|
if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-28 23:30:23 +00:00
|
|
|
if (!sk_gpu_test::CreateVkBackendContext(instProc, devProc, &backendContext)) {
|
2018-06-28 16:47:48 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-27 15:26:54 +00:00
|
|
|
}
|
2018-06-28 23:30:23 +00:00
|
|
|
return new VkTestContextImpl(backendContext, ownsContext);
|
2016-05-16 21:09:56 +00:00
|
|
|
}
|
2016-05-11 17:09:18 +00:00
|
|
|
|
|
|
|
~VkTestContextImpl() override { this->teardown(); }
|
|
|
|
|
|
|
|
void testAbandon() override {}
|
|
|
|
|
2016-05-16 21:09:56 +00:00
|
|
|
// There is really nothing to here since we don't own any unqueued command buffers here.
|
2016-05-11 18:55:36 +00:00
|
|
|
void submit() override {}
|
2016-05-16 21:09:56 +00:00
|
|
|
|
2016-05-11 18:55:36 +00:00
|
|
|
void finish() override {}
|
|
|
|
|
2017-07-25 14:05:01 +00:00
|
|
|
sk_sp<GrContext> makeGrContext(const GrContextOptions& options) override {
|
2017-12-07 17:33:05 +00:00
|
|
|
return GrContext::MakeVulkan(fVk, options);
|
2017-07-25 14:05:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 17:09:18 +00:00
|
|
|
protected:
|
2016-05-16 21:09:56 +00:00
|
|
|
void teardown() override {
|
|
|
|
INHERITED::teardown();
|
2018-06-28 16:47:48 +00:00
|
|
|
fVk.fMemoryAllocator.reset();
|
|
|
|
if (fOwnsContext) {
|
|
|
|
GR_VK_CALL(this->vk(), DeviceWaitIdle(fVk.fDevice));
|
|
|
|
GR_VK_CALL(this->vk(), DestroyDevice(fVk.fDevice, nullptr));
|
|
|
|
GR_VK_CALL(this->vk(), DestroyInstance(fVk.fInstance, nullptr));
|
|
|
|
}
|
2016-05-16 21:09:56 +00:00
|
|
|
}
|
2016-05-11 17:09:18 +00:00
|
|
|
|
|
|
|
private:
|
2018-06-28 23:30:23 +00:00
|
|
|
VkTestContextImpl(const GrVkBackendContext& backendContext, bool ownsContext)
|
|
|
|
: VkTestContext(backendContext, ownsContext) {
|
2018-06-28 16:47:48 +00:00
|
|
|
fFenceSync.reset(new VkFenceSync(fVk.fInterface, fVk.fDevice, fVk.fQueue,
|
|
|
|
fVk.fGraphicsQueueIndex));
|
2016-05-16 21:09:56 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 17:09:18 +00:00
|
|
|
void onPlatformMakeCurrent() const override {}
|
2017-11-17 14:25:23 +00:00
|
|
|
std::function<void()> onPlatformGetAutoContextRestore() const override { return nullptr; }
|
2016-05-11 17:09:18 +00:00
|
|
|
void onPlatformSwapBuffers() const override {}
|
|
|
|
|
|
|
|
typedef sk_gpu_test::VkTestContext INHERITED;
|
|
|
|
};
|
2016-10-04 18:08:45 +00:00
|
|
|
} // anonymous namespace
|
2016-05-11 17:09:18 +00:00
|
|
|
|
|
|
|
namespace sk_gpu_test {
|
2017-05-15 17:50:35 +00:00
|
|
|
VkTestContext* CreatePlatformVkTestContext(VkTestContext* sharedContext) {
|
|
|
|
return VkTestContextImpl::Create(sharedContext);
|
|
|
|
}
|
2016-05-11 17:09:18 +00:00
|
|
|
} // namespace sk_gpu_test
|
|
|
|
|
|
|
|
#endif
|