blob: 098191723c2a59e61667fe08df09a9dd598628d7 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 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 "GrVkGpu.h"
9
Greg Daniela5cb7812017-06-16 09:45:32 -040010#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrContextOptions.h"
13#include "GrGeometryProcessor.h"
14#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070015#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrPipeline.h"
17#include "GrRenderTargetPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018#include "GrTexturePriv.h"
Greg Daniel81df0412018-05-31 13:13:33 -040019#include "GrVkAMDMemoryAllocator.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050020#include "GrVkCommandBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070021#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050022#include "GrVkImage.h"
23#include "GrVkIndexBuffer.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040024#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050025#include "GrVkMemory.h"
26#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070027#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050028#include "GrVkRenderPass.h"
29#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050030#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050031#include "GrVkTexture.h"
32#include "GrVkTextureRenderTarget.h"
33#include "GrVkTransferBuffer.h"
34#include "GrVkVertexBuffer.h"
Matt Sarett485c4992017-02-14 14:18:27 -050035#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070036#include "SkMipMap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040037#include "SkSLCompiler.h"
38#include "SkTo.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040039
Greg Daniela31f4e52018-08-01 16:48:52 -040040#include "vk/GrVkExtensions.h"
jvanverthfd359ca2016-03-18 11:57:24 -070041#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050042
Ben Wagnerf08d1d02018-06-18 15:11:00 -040043#include <utility>
44
Forrest Reiling44f85712017-03-27 23:22:20 -070045#if !defined(SK_BUILD_FOR_WIN)
46#include <unistd.h>
47#endif // !defined(SK_BUILD_FOR_WIN)
48
Greg Danieldef55462018-08-01 13:40:14 -040049#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
50#include "SkLeanWindows.h"
51#endif
52
Greg Daniel164a9f02016-02-22 09:56:40 -050053#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
54#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
55#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
56
Greg Danielf730c182018-07-02 20:15:37 +000057sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050058 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000059 if (backendContext.fInstance == VK_NULL_HANDLE ||
60 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
61 backendContext.fDevice == VK_NULL_HANDLE ||
62 backendContext.fQueue == VK_NULL_HANDLE) {
63 return nullptr;
64 }
Greg Danield3e65aa2018-08-01 09:19:45 -040065 if (!backendContext.fGetProc) {
66 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040067 }
Greg Danield3e65aa2018-08-01 09:19:45 -040068
Greg Danielc0b03d82018-08-03 14:41:15 -040069 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
70 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
71 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
72 backendContext.fInstance,
73 VK_NULL_HANDLE));
74
75 if (!localGetPhysicalDeviceProperties) {
76 return nullptr;
77 }
78 VkPhysicalDeviceProperties physDeviceProperties;
79 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
80 uint32_t physDevVersion = physDeviceProperties.apiVersion;
81
Greg Daniel98bffae2018-08-01 13:25:41 -040082 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -040083
Greg Daniel98bffae2018-08-01 13:25:41 -040084 if (backendContext.fVkExtensions) {
85 interface.reset(new GrVkInterface(backendContext.fGetProc,
86 backendContext.fInstance,
87 backendContext.fDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -040088 backendContext.fInstanceVersion,
89 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -040090 backendContext.fVkExtensions));
Greg Danielc0b03d82018-08-03 14:41:15 -040091 if (!interface->validate(backendContext.fInstanceVersion, physDevVersion,
92 backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -040093 return nullptr;
94 }
95 } else {
96 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
97 // make an empty GrVkExtensions and pass that to the GrVkInterface.
98 GrVkExtensions extensions;
99 interface.reset(new GrVkInterface(backendContext.fGetProc,
100 backendContext.fInstance,
101 backendContext.fDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -0400102 backendContext.fInstanceVersion,
103 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400104 &extensions));
Greg Danielc0b03d82018-08-03 14:41:15 -0400105 if (!interface->validate(backendContext.fInstanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400106 return nullptr;
107 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500108 }
109
Greg Danielc8cd45a2018-07-12 10:02:37 -0400110 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface));
Greg Daniel164a9f02016-02-22 09:56:40 -0500111}
112
113////////////////////////////////////////////////////////////////////////////////
114
halcanary9d524f22016-03-29 09:03:52 -0700115GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400116 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface)
Brian Salomon384fab42017-12-07 12:33:05 -0500117 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400118 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000119 , fMemoryAllocator(backendContext.fMemoryAllocator)
120 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400121 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000122 , fDevice(backendContext.fDevice)
123 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400124 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500125 , fResourceProvider(this)
126 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000127 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700128
Greg Daniel81df0412018-05-31 13:13:33 -0400129 if (!fMemoryAllocator) {
130 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000131 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400132 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400133 }
134
ethannicholasb3058bd2016-07-01 08:22:01 -0700135 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700136
Greg Daniel92aef4b2018-08-02 13:55:49 -0400137 uint32_t instanceVersion = backendContext.fInstanceVersion ? backendContext.fInstanceVersion
138 : backendContext.fMinAPIVersion;
139
Greg Daniela0651ac2018-08-08 09:23:18 -0400140 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400141 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400142 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400143 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400144 } else if (backendContext.fDeviceFeatures) {
145 VkPhysicalDeviceFeatures2 features2;
146 features2.pNext = nullptr;
147 features2.features = *backendContext.fDeviceFeatures;
148 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
149 features2, instanceVersion, *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400150 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400151 VkPhysicalDeviceFeatures2 features;
152 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
153 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400154 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400155 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400156 }
157 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400158 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400159 }
160 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400161 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400162 }
163 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -0400164 features, instanceVersion, GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400165 }
jvanverth633b3562016-03-23 11:01:22 -0700166 fCaps.reset(SkRef(fVkCaps.get()));
167
Greg Danielf730c182018-07-02 20:15:37 +0000168 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
169 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700170
171 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700172 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
173 nullptr, // pNext
174 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
175 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
Greg Danielf730c182018-07-02 20:15:37 +0000176 backendContext.fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700177 };
halcanary9d524f22016-03-29 09:03:52 -0700178 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700179 &fCmdPool));
180
181 // must call this after creating the CommandPool
182 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700183 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700184 SkASSERT(fCurrentCmdBuffer);
185 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500186}
187
Greg Daniel8606cf82017-05-08 16:17:53 -0400188void GrVkGpu::destroyResources() {
189 if (fCurrentCmdBuffer) {
190 fCurrentCmdBuffer->end(this);
191 fCurrentCmdBuffer->unref(this);
192 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500193
194 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700195 fResourceProvider.checkCommandBuffers();
Jim Van Verth09557d72016-11-07 11:10:21 -0500196 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700197
198 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
199 // on the command buffers even though they have completed. This causes an assert to fire when
200 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500201 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700202#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500203 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700204#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500205 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700206#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500207 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700208#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500209 }
egdanielf8c2be32016-06-24 13:18:27 -0700210#endif
211
egdanielbe9d8212016-09-20 08:54:23 -0700212#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400213 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700214#endif
halcanary9d524f22016-03-29 09:03:52 -0700215
Greg Daniel6be35232017-03-01 17:01:09 -0500216 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
217 fSemaphoresToWaitOn[i]->unref(this);
218 }
219 fSemaphoresToWaitOn.reset();
220
Greg Daniela5cb7812017-06-16 09:45:32 -0400221 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
222 fSemaphoresToSignal[i]->unref(this);
223 }
224 fSemaphoresToSignal.reset();
225
226
egdanielbc9b2962016-09-27 08:00:53 -0700227 fCopyManager.destroyResources(this);
228
Jim Van Verth09557d72016-11-07 11:10:21 -0500229 // must call this just before we destroy the command pool and VkDevice
230 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500231
Greg Daniel8606cf82017-05-08 16:17:53 -0400232 if (fCmdPool != VK_NULL_HANDLE) {
233 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
234 }
jvanverth633b3562016-03-23 11:01:22 -0700235
Greg Danielf730c182018-07-02 20:15:37 +0000236 fMemoryAllocator.reset();
237
238 fQueue = VK_NULL_HANDLE;
239 fDevice = VK_NULL_HANDLE;
240 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400241}
242
243GrVkGpu::~GrVkGpu() {
244 if (!fDisconnected) {
245 this->destroyResources();
246 }
247 delete fCompiler;
248}
249
250
251void GrVkGpu::disconnect(DisconnectType type) {
252 INHERITED::disconnect(type);
253 if (!fDisconnected) {
254 if (DisconnectType::kCleanup == type) {
255 this->destroyResources();
256 } else {
Greg Danieladb4bfe2018-08-23 16:15:05 -0400257 if (fCurrentCmdBuffer) {
258 fCurrentCmdBuffer->unrefAndAbandon();
259 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400260 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
261 fSemaphoresToWaitOn[i]->unrefAndAbandon();
262 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400263 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
264 fSemaphoresToSignal[i]->unrefAndAbandon();
265 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400266 fCopyManager.abandonResources();
267
268 // must call this just before we destroy the command pool and VkDevice
269 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400270
271 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400272 }
273 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400274 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400275 fCurrentCmdBuffer = nullptr;
276 fCmdPool = VK_NULL_HANDLE;
277 fDisconnected = true;
278 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500279}
280
281///////////////////////////////////////////////////////////////////////////////
282
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400283GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -0400284 GrRenderTarget* rt, GrSurfaceOrigin origin,
Greg Daniel9b7bfd02018-10-08 17:18:10 -0400285 const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400286 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
287 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400288 if (!fCachedRTCommandBuffer) {
289 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
290 }
291
Greg Daniel9b7bfd02018-10-08 17:18:10 -0400292 fCachedRTCommandBuffer->set(rt, origin, bounds, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400293 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400294}
295
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400296GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
297 if (!fCachedTexCommandBuffer) {
298 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
299 }
300
301 fCachedTexCommandBuffer->set(texture, origin);
302 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700303}
304
Greg Daniela5cb7812017-06-16 09:45:32 -0400305void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500306 SkASSERT(fCurrentCmdBuffer);
307 fCurrentCmdBuffer->end(this);
308
Greg Daniela5cb7812017-06-16 09:45:32 -0400309 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500310
311 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
312 fSemaphoresToWaitOn[i]->unref(this);
313 }
314 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400315 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
316 fSemaphoresToSignal[i]->unref(this);
317 }
318 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500319
Greg Daniel164a9f02016-02-22 09:56:40 -0500320 fResourceProvider.checkCommandBuffers();
321
322 // Release old command buffer and create a new one
323 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700324 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500325 SkASSERT(fCurrentCmdBuffer);
326
327 fCurrentCmdBuffer->begin(this);
328}
329
330///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700331GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
332 const void* data) {
333 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700334 switch (type) {
335 case kVertex_GrBufferType:
336 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
337 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700338 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700339 break;
cdalton397536c2016-03-25 12:15:03 -0700340 case kIndex_GrBufferType:
341 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
342 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700343 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700344 break;
cdalton397536c2016-03-25 12:15:03 -0700345 case kXferCpuToGpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400346 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
347 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700348 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700349 break;
cdalton397536c2016-03-25 12:15:03 -0700350 case kXferGpuToCpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400351 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
352 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700353 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700354 break;
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400355 case kDrawIndirect_GrBufferType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400356 SK_ABORT("DrawIndirect Buffers not supported in vulkan backend.");
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400357 return nullptr;
cdalton397536c2016-03-25 12:15:03 -0700358 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400359 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700360 return nullptr;
361 }
cdalton1bf3e712016-04-19 10:00:02 -0700362 if (data && buff) {
363 buff->updateData(data, size);
364 }
365 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500366}
367
Brian Salomona9b04b92018-06-01 15:04:28 -0400368bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
369 GrColorType srcColorType, const GrMipLevel texels[],
370 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500371 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
372 if (!vkTex) {
373 return false;
374 }
375
jvanverth900bd4a2016-04-29 13:53:12 -0700376 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400377 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800378 return false;
379 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800380
Greg Daniel164a9f02016-02-22 09:56:40 -0500381 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400382 bool linearTiling = vkTex->isLinearTiled();
383 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400384 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400385 SkDebugf("Can't upload mipmap data to linear tiled texture");
386 return false;
387 }
388 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
389 // Need to change the layout to general in order to perform a host write
390 vkTex->setImageLayout(this,
391 VK_IMAGE_LAYOUT_GENERAL,
392 VK_ACCESS_HOST_WRITE_BIT,
393 VK_PIPELINE_STAGE_HOST_BIT,
394 false);
395 this->submitCommandBuffer(kForce_SyncQueue);
396 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400397 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400398 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500399 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400400 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400401 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
402 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500403 }
egdaniel4583ec52016-06-27 12:57:00 -0700404
jvanverth900bd4a2016-04-29 13:53:12 -0700405 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500406}
407
Brian Salomonc320b152018-02-20 14:05:36 -0500408bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
409 GrColorType bufferColorType, GrBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400410 size_t bufferOffset, size_t rowBytes) {
411 // Vulkan only supports 4-byte aligned offsets
412 if (SkToBool(bufferOffset & 0x2)) {
413 return false;
414 }
415 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
416 if (!vkTex) {
417 return false;
418 }
419 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
420 if (!vkBuffer) {
421 return false;
422 }
423
Greg Daniel660cc992017-06-26 14:55:05 -0400424 SkDEBUGCODE(
425 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
426 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
427 SkASSERT(bounds.contains(subRect));
428 )
Brian Salomonc320b152018-02-20 14:05:36 -0500429 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400430 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500431 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400432 }
433
434 // Set up copy region
435 VkBufferImageCopy region;
436 memset(&region, 0, sizeof(VkBufferImageCopy));
437 region.bufferOffset = bufferOffset;
438 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
439 region.bufferImageHeight = 0;
440 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
441 region.imageOffset = { left, top, 0 };
442 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
443
444 // Change layout of our target so it can be copied to
445 vkTex->setImageLayout(this,
446 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
447 VK_ACCESS_TRANSFER_WRITE_BIT,
448 VK_PIPELINE_STAGE_TRANSFER_BIT,
449 false);
450
451 // Copy the buffer to the image
452 fCurrentCmdBuffer->copyBufferToImage(this,
453 vkBuffer,
454 vkTex,
455 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
456 1,
457 &region);
458
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400459 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400460 return true;
461}
462
Brian Salomon1fabd512018-02-09 09:54:25 -0500463void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
464 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700465 SkASSERT(dst);
466 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
467
egdanielfd016d72016-09-27 12:13:05 -0700468 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
469 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
470 }
471
egdaniel4bcd62e2016-08-31 07:37:31 -0700472 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500473 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
474 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
475 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
476 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
477 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700478
Greg Danielbc26c392017-04-18 13:32:10 -0400479 GrVkImage* dstImage;
480 GrRenderTarget* dstRT = dst->asRenderTarget();
481 if (dstRT) {
482 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400483 dstImage = vkRT;
484 } else {
485 SkASSERT(dst->asTexture());
486 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
487 }
488 dstImage->setImageLayout(this,
489 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
490 VK_ACCESS_TRANSFER_WRITE_BIT,
491 VK_PIPELINE_STAGE_TRANSFER_BIT,
492 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700493
494 src->msaaImage()->setImageLayout(this,
495 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
496 VK_ACCESS_TRANSFER_READ_BIT,
497 VK_PIPELINE_STAGE_TRANSFER_BIT,
498 false);
499
Greg Danielbc26c392017-04-18 13:32:10 -0400500 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700501}
502
Brian Salomon1fabd512018-02-09 09:54:25 -0500503void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700504 if (target->needsResolve()) {
505 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700506 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
507 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500508
egdaniel4bcd62e2016-08-31 07:37:31 -0700509 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700510
Brian Salomon1fabd512018-02-09 09:54:25 -0500511 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700512
513 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500514
515 if (requiresSubmit) {
516 this->submitCommandBuffer(kSkip_SyncQueue);
517 }
egdaniel52ad2512016-08-04 12:50:01 -0700518 }
519}
520
Brian Salomona9b04b92018-06-01 15:04:28 -0400521bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
522 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500523 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700524 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500525
Greg Daniel660cc992017-06-26 14:55:05 -0400526 SkDEBUGCODE(
527 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
528 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
529 SkASSERT(bounds.contains(subRect));
530 )
Brian Salomonc320b152018-02-20 14:05:36 -0500531 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500532 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400533 if (!rowBytes) {
534 rowBytes = trimRowBytes;
535 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500536
jvanverth900bd4a2016-04-29 13:53:12 -0700537 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
538 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
539 const VkImageSubresource subres = {
540 VK_IMAGE_ASPECT_COLOR_BIT,
541 0, // mipLevel
542 0, // arraySlice
543 };
544 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500545
jvanverth900bd4a2016-04-29 13:53:12 -0700546 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500547
jvanverth900bd4a2016-04-29 13:53:12 -0700548 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700549 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700550 &subres,
551 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500552
jvanverth1e305ba2016-06-01 09:39:15 -0700553 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400554 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700555 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400556 SkASSERT(size + offset <= alloc.fSize);
557 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
558 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700559 return false;
560 }
Greg Daniel81df0412018-05-31 13:13:33 -0400561 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700562
Brian Salomona9b04b92018-06-01 15:04:28 -0400563 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
564 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700565
Greg Daniele35a99e2018-03-02 11:44:22 -0500566 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400567 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700568
569 return true;
570}
571
Brian Salomona9b04b92018-06-01 15:04:28 -0400572bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
573 GrColorType dataColorType, const GrMipLevel texels[],
574 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700575 SkASSERT(!tex->isLinearTiled());
576 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400577 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700578 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
579
Greg Danieldd20e912017-04-07 14:42:23 -0400580 // We assume that if the texture has mip levels, we either upload to all the levels or just the
581 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400582 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400583
jvanverth900bd4a2016-04-29 13:53:12 -0700584 if (width == 0 || height == 0) {
585 return false;
586 }
587
Greg Daniel475eb702018-09-28 14:16:50 -0400588 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
589 return false;
590 }
591
592 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
593 // dst RGB texture. Thus we do not upload mip levels for that.
594 if (dataColorType == GrColorType::kRGB_888x) {
595 SkASSERT(tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM &&
596 tex->config() == kRGB_888_GrPixelConfig);
597 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
598 // blit or draw.
599 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
600 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
601 return false;
602 }
603 mipLevelCount = 1;
604 }
605
Brian Salomond1eaf492017-05-18 10:02:08 -0400606 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500607 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700608
609 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700610 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
611 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400612 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
613
Greg Daniel475eb702018-09-28 14:16:50 -0400614 texelsShallowCopy.reset(mipLevelCount);
615 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700616
Robert Phillips590533f2017-07-11 14:22:35 -0400617 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700618 individualMipOffsets.push_back(0);
619 size_t combinedBufferSize = width * bpp * height;
620 int currentWidth = width;
621 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400622 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400623 combinedBufferSize = 0;
624 }
625
Greg Daniel468fd632017-03-22 17:03:45 -0400626 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
627 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
628 SkASSERT((bpp & (bpp - 1)) == 0);
629 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400630 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700631 currentWidth = SkTMax(1, currentWidth/2);
632 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400633
Greg Daniel55afd6d2017-09-29 09:32:44 -0400634 if (texelsShallowCopy[currentMipLevel].fPixels) {
635 const size_t trimmedSize = currentWidth * bpp * currentHeight;
636 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
637 if (alignmentDiff != 0) {
638 combinedBufferSize += alignmentMask - alignmentDiff + 1;
639 }
640 individualMipOffsets.push_back(combinedBufferSize);
641 combinedBufferSize += trimmedSize;
642 } else {
643 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400644 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400645 }
646 if (0 == combinedBufferSize) {
647 // We don't actually have any data to upload so just return success
648 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700649 }
650
651 // allocate buffer to hold our mip data
652 GrVkTransferBuffer* transferBuffer =
653 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400654 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700655 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400656 }
jvanverth900bd4a2016-04-29 13:53:12 -0700657
Greg Daniel475eb702018-09-28 14:16:50 -0400658 int uploadLeft = left;
659 int uploadTop = top;
660 GrVkTexture* uploadTexture = tex;
661 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
662 // R8G8B8A8_UNORM image and then copy it.
663 sk_sp<GrVkTexture> copyTexture;
664 if (dataColorType == GrColorType::kRGB_888x) {
665 GrSurfaceDesc surfDesc;
666 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
667 surfDesc.fWidth = width;
668 surfDesc.fHeight = height;
669 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
670 surfDesc.fSampleCnt = 1;
671
672 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
673 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
674 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
675
676 GrVkImage::ImageDesc imageDesc;
677 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
678 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
679 imageDesc.fWidth = width;
680 imageDesc.fHeight = height;
681 imageDesc.fLevels = 1;
682 imageDesc.fSamples = 1;
683 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
684 imageDesc.fUsageFlags = usageFlags;
685 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
686
687 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
688 GrMipMapsStatus::kNotAllocated);
689 if (!copyTexture) {
690 return false;
691 }
692 uploadTexture = copyTexture.get();
693 uploadLeft = 0;
694 uploadTop = 0;
695 }
696
jvanverth900bd4a2016-04-29 13:53:12 -0700697 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400698 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700699
jvanverthc578b0632016-05-02 10:58:12 -0700700 currentWidth = width;
701 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400702 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400703 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400704 if (texelsShallowCopy[currentMipLevel].fPixels) {
705 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
706 const size_t trimRowBytes = currentWidth * bpp;
707 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
708 ? texelsShallowCopy[currentMipLevel].fRowBytes
709 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700710
Greg Daniel55afd6d2017-09-29 09:32:44 -0400711 // copy data into the buffer, skipping the trailing bytes
712 char* dst = buffer + individualMipOffsets[currentMipLevel];
713 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400714 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400715
716 VkBufferImageCopy& region = regions.push_back();
717 memset(&region, 0, sizeof(VkBufferImageCopy));
718 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
719 region.bufferRowLength = currentWidth;
720 region.bufferImageHeight = currentHeight;
721 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400722 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400723 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700724 }
jvanverthc578b0632016-05-02 10:58:12 -0700725 currentWidth = SkTMax(1, currentWidth/2);
726 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400727 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700728 }
729
jvanverth9d54afc2016-09-20 09:20:03 -0700730 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700731 transferBuffer->unmap();
732
jvanverth900bd4a2016-04-29 13:53:12 -0700733 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400734 uploadTexture->setImageLayout(this,
735 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
736 VK_ACCESS_TRANSFER_WRITE_BIT,
737 VK_PIPELINE_STAGE_TRANSFER_BIT,
738 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700739
740 // Copy the buffer to the image
741 fCurrentCmdBuffer->copyBufferToImage(this,
742 transferBuffer,
Greg Daniel475eb702018-09-28 14:16:50 -0400743 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700744 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
745 regions.count(),
746 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700747 transferBuffer->unref();
Greg Daniel475eb702018-09-28 14:16:50 -0400748
749 // If we copied the data into a temporary image first, copy that image into our main texture
750 // now.
751 if (copyTexture.get()) {
752 SkASSERT(dataColorType == GrColorType::kRGB_888x);
753 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
754 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
755 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
756 false));
757 }
Robert Phillips590533f2017-07-11 14:22:35 -0400758 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400759 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400760 }
jvanverth900bd4a2016-04-29 13:53:12 -0700761
Greg Daniel164a9f02016-02-22 09:56:40 -0500762 return true;
763}
764
765////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400766sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500767 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500768 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
769
770 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500771 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700772
Greg Daniel164a9f02016-02-22 09:56:40 -0500773 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
774 if (renderTarget) {
775 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
776 }
777
778 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
779 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
780 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700781 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500782 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
783 // texture.
784 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
785
Greg Daniel164a9f02016-02-22 09:56:40 -0500786 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700787 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500788 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400789 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500790 GrVkImage::ImageDesc imageDesc;
791 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
792 imageDesc.fFormat = pixelFormat;
793 imageDesc.fWidth = desc.fWidth;
794 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400795 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500796 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400797 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500798 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400799 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500800
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400801 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
802 if (mipLevels > 1) {
803 mipMapsStatus = GrMipMapsStatus::kValid;
804 for (int i = 0; i < mipLevels; ++i) {
805 if (!texels[i].fPixels) {
806 mipMapsStatus = GrMipMapsStatus::kDirty;
807 break;
808 }
Greg Daniel834f1202017-10-09 15:06:20 -0400809 }
810 }
811
Robert Phillips67d52cf2017-06-05 13:38:13 -0400812 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500813 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -0400814 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
815 imageDesc,
816 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500817 } else {
Greg Daniel475eb702018-09-28 14:16:50 -0400818 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500819 }
820
821 if (!tex) {
822 return nullptr;
823 }
824
Brian Salomonc320b152018-02-20 14:05:36 -0500825 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400826 if (mipLevelCount) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400827 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
828 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500829 tex->unref();
830 return nullptr;
831 }
832 }
833
Brian Salomond17b4a62017-05-23 16:53:47 -0400834 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
835 VkClearColorValue zeroClearColor;
836 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
837 VkImageSubresourceRange range;
838 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
839 range.baseArrayLayer = 0;
840 range.baseMipLevel = 0;
841 range.layerCount = 1;
842 range.levelCount = 1;
843 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
844 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400845 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400846 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400847 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -0500848}
849
850////////////////////////////////////////////////////////////////////////////////
851
Greg Daniel6888c0d2017-08-25 11:55:50 -0400852void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
853 VkDeviceSize dstOffset, VkDeviceSize size) {
854 VkBufferCopy copyRegion;
855 copyRegion.srcOffset = srcOffset;
856 copyRegion.dstOffset = dstOffset;
857 copyRegion.size = size;
858 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
859}
860
jvanverthdb379092016-07-07 11:18:46 -0700861bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
862 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700863 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700864 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700865
866 return true;
867}
868
869////////////////////////////////////////////////////////////////////////////////
870
Greg Daniel5254ccc2017-11-13 11:05:52 -0500871static bool check_backend_texture(const GrBackendTexture& backendTex,
872 GrPixelConfig config) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400873 GrVkImageInfo info;
874 if (!backendTex.getVkImageInfo(&info)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400875 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500876 }
877
Greg Daniel52e16d92018-04-10 09:34:07 -0400878 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -0400879 return false;
jvanverthfd359ca2016-03-18 11:57:24 -0700880 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000881
Greg Daniel52e16d92018-04-10 09:34:07 -0400882 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -0400883 return true;
884}
885
886sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400887 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500888 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400889 return nullptr;
890 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500891
Greg Daniel164a9f02016-02-22 09:56:40 -0500892 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400893 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000894 surfDesc.fWidth = backendTex.width();
895 surfDesc.fHeight = backendTex.height();
896 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500897 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500898
Greg Daniel52e16d92018-04-10 09:34:07 -0400899 GrVkImageInfo imageInfo;
900 if (!backendTex.getVkImageInfo(&imageInfo)) {
901 return nullptr;
902 }
903 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
904 SkASSERT(layout);
905 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, imageInfo, std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -0400906}
907
908sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400909 int sampleCnt,
910 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500911 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400912 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500913 }
Brian Salomond17f6582017-07-19 18:28:58 -0400914
915 GrSurfaceDesc surfDesc;
916 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
917 surfDesc.fWidth = backendTex.width();
918 surfDesc.fHeight = backendTex.height();
919 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500920 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -0400921
Greg Daniel52e16d92018-04-10 09:34:07 -0400922 GrVkImageInfo imageInfo;
923 if (!backendTex.getVkImageInfo(&imageInfo)) {
924 return nullptr;
925 }
926 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
927 SkASSERT(layout);
928
Brian Salomond17f6582017-07-19 18:28:58 -0400929 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
Greg Daniel52e16d92018-04-10 09:34:07 -0400930 imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500931}
932
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400933sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -0400934 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
935 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
936 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
937 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500938 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -0400939 return nullptr;
940 }
halcanary9d524f22016-03-29 09:03:52 -0700941
Greg Daniel323fbcf2018-04-10 13:46:30 -0400942 GrVkImageInfo info;
943 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000944 return nullptr;
945 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400946
947 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700948 return nullptr;
949 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500950
Greg Daniel164a9f02016-02-22 09:56:40 -0500951 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400952 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400953 desc.fWidth = backendRT.width();
954 desc.fHeight = backendRT.height();
955 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500956 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500957
Greg Daniel323fbcf2018-04-10 13:46:30 -0400958 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -0400959
Greg Daniel323fbcf2018-04-10 13:46:30 -0400960 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -0400961 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -0500962
963 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
964 SkASSERT(!backendRT.stencilBits());
965 if (tgt) {
966 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -0500967 }
Brian Salomonafdc6b12018-03-09 12:02:32 -0500968
Ben Wagnerff134f22018-04-24 16:29:16 -0400969 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500970}
971
Greg Daniel7ef28f32017-04-20 16:41:55 +0000972sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000973 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400974
Greg Daniel52e16d92018-04-10 09:34:07 -0400975 GrVkImageInfo imageInfo;
976 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000977 return nullptr;
978 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400979 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -0400980 return nullptr;
981 }
982
983 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000984 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000985 desc.fWidth = tex.width();
986 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400987 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500988 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
989 if (!desc.fSampleCnt) {
990 return nullptr;
991 }
Brian Osman33910292017-04-18 14:38:53 -0400992
Greg Daniel52e16d92018-04-10 09:34:07 -0400993 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
994 SkASSERT(layout);
995
Ben Wagnerff134f22018-04-24 16:29:16 -0400996 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -0400997}
998
Brian Salomon930f9392018-06-20 16:25:26 -0400999bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1000 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001001 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001002 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001003 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001004 return false;
jvanverth62340062016-04-26 08:01:44 -07001005 }
1006
jvanverth62340062016-04-26 08:01:44 -07001007 // determine if we can blit to and from this format
1008 const GrVkCaps& caps = this->vkCaps();
1009 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001010 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1011 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001012 return false;
jvanverth62340062016-04-26 08:01:44 -07001013 }
1014
egdanielfd016d72016-09-27 12:13:05 -07001015 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1016 this->submitCommandBuffer(kSkip_SyncQueue);
1017 }
1018
egdaniel7ac5da82016-07-15 13:41:42 -07001019 int width = tex->width();
1020 int height = tex->height();
1021 VkImageBlit blitRegion;
1022 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001023
jvanverth82c05582016-05-03 11:19:01 -07001024 // SkMipMap doesn't include the base level in the level count so we have to add 1
1025 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001026 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001027
Greg Danielda86e282018-06-13 09:41:19 -04001028 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001029 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1030 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001031
jvanverth50c46c72016-05-06 12:31:28 -07001032 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001033 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001034 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1035 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001036 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1037 nullptr, // pNext
1038 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1039 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1040 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1041 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1042 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1043 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1044 vkTex->image(), // image
1045 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001046 };
1047
jvanverth62340062016-04-26 08:01:44 -07001048 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001049 uint32_t mipLevel = 1;
1050 while (mipLevel < levelCount) {
1051 int prevWidth = width;
1052 int prevHeight = height;
1053 width = SkTMax(1, width / 2);
1054 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001055
jvanverth50c46c72016-05-06 12:31:28 -07001056 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1057 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1058 false, &imageMemoryBarrier);
1059
1060 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001061 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001062 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001063 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1064 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001065 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001066 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001067 vkTex->resource(),
1068 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001069 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001070 vkTex->resource(),
1071 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001072 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001073 1,
1074 &blitRegion,
1075 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001076 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001077 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001078 // This barrier logically is not needed, but it changes the final level to the same layout as
1079 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1080 // future layout changes easier.
1081 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1082 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1083 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001084 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1085 return true;
jvanverth62340062016-04-26 08:01:44 -07001086}
1087
Greg Daniel164a9f02016-02-22 09:56:40 -05001088////////////////////////////////////////////////////////////////////////////////
1089
1090GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1091 int width,
1092 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001093 SkASSERT(width >= rt->width());
1094 SkASSERT(height >= rt->height());
1095
1096 int samples = rt->numStencilSamples();
1097
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001098 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001099
1100 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001101 width,
1102 height,
1103 samples,
1104 sFmt));
1105 fStats.incStencilAttachmentCreates();
1106 return stencil;
1107}
1108
1109////////////////////////////////////////////////////////////////////////////////
1110
Brian Salomon52e943a2018-03-13 09:32:39 -04001111bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001112 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1113 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001114 VkDeviceSize size = dstRowBytes * h;
1115 VkDeviceSize offset = bufferOffset;
1116 SkASSERT(size + offset <= alloc.fSize);
1117 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1118 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001119 return false;
1120 }
Greg Daniel81df0412018-05-31 13:13:33 -04001121 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001122
Greg Daniel20ece3a2017-03-28 10:24:43 -04001123 if (srcData) {
1124 // If there is no padding on dst we can do a single memcopy.
1125 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001126 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001127 } else {
1128 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1129 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001130 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001131 }
Greg Daniel81df0412018-05-31 13:13:33 -04001132 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1133 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001134 return true;
1135}
1136
Brian Salomonf865b052018-03-09 09:01:53 -05001137#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001138bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1139 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001140 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001141 SkASSERT(texturable || renderable);
1142 if (!texturable) {
1143 SkASSERT(GrMipMapped::kNo == mipMapped);
1144 SkASSERT(!srcData);
1145 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001146 VkFormat pixelFormat;
1147 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001148 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001149 }
1150
Brian Salomon52e943a2018-03-13 09:32:39 -04001151 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1152 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001153 }
1154
Brian Salomon52e943a2018-03-13 09:32:39 -04001155 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1156 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001157 }
1158
1159 // Currently we don't support uploading pixel data when mipped.
1160 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001161 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001162 }
1163
Brian Salomon52e943a2018-03-13 09:32:39 -04001164 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001165 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1166 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001167 if (texturable) {
1168 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1169 }
1170 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001171 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1172 }
1173
1174 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001175 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001176 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001177
1178 // Create Image
1179 VkSampleCountFlagBits vkSamples;
1180 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001181 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001182 }
1183
1184 // Figure out the number of mip levels.
1185 uint32_t mipLevels = 1;
1186 if (GrMipMapped::kYes == mipMapped) {
1187 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1188 }
1189
1190 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001191 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1192 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001193 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001194 VK_IMAGE_TYPE_2D, // VkImageType
1195 pixelFormat, // VkFormat
1196 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1197 mipLevels, // mipLevels
1198 1, // arrayLayers
1199 vkSamples, // samples
1200 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1201 usageFlags, // VkImageUsageFlags
1202 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1203 0, // queueFamilyCount
1204 0, // pQueueFamilyIndices
1205 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001206 };
1207
Brian Salomon52e943a2018-03-13 09:32:39 -04001208 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1209 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001210
Brian Salomonde9f5462018-03-07 14:23:58 -05001211 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001212 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001213 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001214 }
1215
1216 // We need to declare these early so that we can delete them at the end outside of the if block.
Greg Daniel8385a8a2018-02-26 13:29:37 -05001217 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001218 VkBuffer buffer = VK_NULL_HANDLE;
1219
1220 VkResult err;
1221 const VkCommandBufferAllocateInfo cmdInfo = {
1222 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1223 nullptr, // pNext
1224 fCmdPool, // commandPool
1225 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1226 1 // bufferCount
1227 };
1228
1229 VkCommandBuffer cmdBuffer;
1230 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1231 if (err) {
1232 GrVkMemory::FreeImageMemory(this, false, alloc);
1233 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001234 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001235 }
1236
1237 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1238 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1239 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1240 cmdBufferBeginInfo.pNext = nullptr;
1241 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1242 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1243
1244 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1245 SkASSERT(!err);
1246
1247 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001248 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001249
Robert Phillips646f6372018-09-25 09:31:10 -04001250 const size_t trimRowBytes = w * bpp;
1251 if (!srcRowBytes) {
1252 srcRowBytes = trimRowBytes;
1253 }
1254
Brian Salomonde9f5462018-03-07 14:23:58 -05001255 SkTArray<size_t> individualMipOffsets(mipLevels);
1256 individualMipOffsets.push_back(0);
1257 size_t combinedBufferSize = w * bpp * h;
1258 int currentWidth = w;
1259 int currentHeight = h;
1260 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1261 // config. This works with the assumption that the bytes in pixel config is always a power
1262 // of 2.
1263 SkASSERT((bpp & (bpp - 1)) == 0);
1264 const size_t alignmentMask = 0x3 | (bpp - 1);
1265 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1266 currentWidth = SkTMax(1, currentWidth / 2);
1267 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001268
Brian Salomonde9f5462018-03-07 14:23:58 -05001269 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1270 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1271 if (alignmentDiff != 0) {
1272 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001273 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001274 individualMipOffsets.push_back(combinedBufferSize);
1275 combinedBufferSize += trimmedSize;
1276 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001277
Brian Salomonde9f5462018-03-07 14:23:58 -05001278 VkBufferCreateInfo bufInfo;
1279 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1280 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1281 bufInfo.flags = 0;
1282 bufInfo.size = combinedBufferSize;
1283 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1284 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1285 bufInfo.queueFamilyIndexCount = 0;
1286 bufInfo.pQueueFamilyIndices = nullptr;
1287 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001288
Brian Salomonde9f5462018-03-07 14:23:58 -05001289 if (err) {
1290 GrVkMemory::FreeImageMemory(this, false, alloc);
1291 VK_CALL(DestroyImage(fDevice, image, nullptr));
1292 VK_CALL(EndCommandBuffer(cmdBuffer));
1293 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001294 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001295 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001296
Brian Salomonde9f5462018-03-07 14:23:58 -05001297 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1298 &bufferAlloc)) {
1299 GrVkMemory::FreeImageMemory(this, false, alloc);
1300 VK_CALL(DestroyImage(fDevice, image, nullptr));
1301 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1302 VK_CALL(EndCommandBuffer(cmdBuffer));
1303 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001304 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001305 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001306
Brian Salomonde9f5462018-03-07 14:23:58 -05001307 currentWidth = w;
1308 currentHeight = h;
1309 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1310 SkASSERT(0 == currentMipLevel || !srcData);
1311 size_t currentRowBytes = bpp * currentWidth;
1312 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Robert Phillips646f6372018-09-25 09:31:10 -04001313 if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1314 currentRowBytes, trimRowBytes, currentHeight)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001315 GrVkMemory::FreeImageMemory(this, false, alloc);
1316 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001317 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001318 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1319 VK_CALL(EndCommandBuffer(cmdBuffer));
1320 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001321 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001322 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001323 currentWidth = SkTMax(1, currentWidth / 2);
1324 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001325 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001326
1327 // Set image layout and add barrier
1328 VkImageMemoryBarrier barrier;
1329 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1330 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1331 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001332 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001333 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1334 barrier.oldLayout = initialLayout;
1335 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1336 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1337 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1338 barrier.image = image;
1339 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1340
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001341 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001342 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1343 &barrier));
1344 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1345
1346 SkTArray<VkBufferImageCopy> regions(mipLevels);
1347
1348 currentWidth = w;
1349 currentHeight = h;
1350 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1351 // Submit copy command
1352 VkBufferImageCopy& region = regions.push_back();
1353 memset(&region, 0, sizeof(VkBufferImageCopy));
1354 region.bufferOffset = individualMipOffsets[currentMipLevel];
1355 region.bufferRowLength = currentWidth;
1356 region.bufferImageHeight = currentHeight;
1357 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1358 region.imageOffset = {0, 0, 0};
1359 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1360 currentWidth = SkTMax(1, currentWidth / 2);
1361 currentHeight = SkTMax(1, currentHeight / 2);
1362 }
1363
1364 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1365 regions.begin()));
1366
Brian Salomon52e943a2018-03-13 09:32:39 -04001367 if (texturable) {
1368 // Change Image layout to shader read since if we use this texture as a borrowed textures
1369 // within Ganesh we require that its layout be set to that
1370 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1371 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1372 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001373 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001374 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1375 barrier.oldLayout = initialLayout;
1376 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1377 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1378 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1379 barrier.image = image;
1380 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001381 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001382 GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomon52e943a2018-03-13 09:32:39 -04001383 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1384 0,
1385 0, nullptr,
1386 0, nullptr,
1387 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001388 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001389 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001390
1391 // End CommandBuffer
1392 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1393 SkASSERT(!err);
1394
1395 // Create Fence for queue
1396 VkFence fence;
1397 VkFenceCreateInfo fenceInfo;
1398 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1399 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1400
1401 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1402 SkASSERT(!err);
1403
1404 VkSubmitInfo submitInfo;
1405 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1406 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1407 submitInfo.pNext = nullptr;
1408 submitInfo.waitSemaphoreCount = 0;
1409 submitInfo.pWaitSemaphores = nullptr;
1410 submitInfo.pWaitDstStageMask = 0;
1411 submitInfo.commandBufferCount = 1;
1412 submitInfo.pCommandBuffers = &cmdBuffer;
1413 submitInfo.signalSemaphoreCount = 0;
1414 submitInfo.pSignalSemaphores = nullptr;
1415 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1416 SkASSERT(!err);
1417
1418 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1419 if (VK_TIMEOUT == err) {
1420 GrVkMemory::FreeImageMemory(this, false, alloc);
1421 VK_CALL(DestroyImage(fDevice, image, nullptr));
1422 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1423 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1424 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1425 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1426 SkDebugf("Fence failed to signal: %d\n", err);
1427 SK_ABORT("failing");
1428 }
1429 SkASSERT(!err);
1430
1431 // Clean up transfer resources
1432 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1433 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1434 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1435 }
1436 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1437 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1438
Brian Salomon52e943a2018-03-13 09:32:39 -04001439 info->fImage = image;
1440 info->fAlloc = alloc;
1441 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1442 info->fImageLayout = initialLayout;
1443 info->fFormat = pixelFormat;
1444 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001445
Brian Salomon52e943a2018-03-13 09:32:39 -04001446 return true;
1447}
1448
1449GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001450 GrColorType colorType,
1451 bool isRenderTarget,
1452 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001453 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001454
1455 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1456 return GrBackendTexture();
1457 }
1458
Robert Phillips646f6372018-09-25 09:31:10 -04001459 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1460 if (!this->caps()->isConfigTexturable(config)) {
1461 return GrBackendTexture();
1462 }
1463
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001464 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001465 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001466 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001467 return {};
1468 }
Greg Daniel108bb232018-07-03 16:18:29 -04001469 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1470 // Lots of tests don't go through Skia's public interface which will set the config so for
1471 // testing we make sure we set a config here.
1472 beTex.setPixelConfig(config);
1473 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001474}
1475
1476bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
1477 SkASSERT(kVulkan_GrBackend == tex.fBackend);
1478
Greg Daniel52e16d92018-04-10 09:34:07 -04001479 GrVkImageInfo backend;
1480 if (!tex.getVkImageInfo(&backend)) {
1481 return false;
1482 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001483
Greg Daniel52e16d92018-04-10 09:34:07 -04001484 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001485 VkMemoryRequirements req;
1486 memset(&req, 0, sizeof(req));
1487 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001488 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001489 &req));
1490 // TODO: find a better check
1491 // This will probably fail with a different driver
1492 return (req.size > 0) && (req.size <= 8192 * 8192);
1493 }
1494
1495 return false;
1496}
1497
Brian Salomon26102cb2018-03-09 09:33:19 -05001498void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
1499 SkASSERT(kVulkan_GrBackend == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001500
Greg Daniel52e16d92018-04-10 09:34:07 -04001501 GrVkImageInfo info;
1502 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001503 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001504 }
1505}
1506
Brian Osman2d010b62018-08-09 10:55:09 -04001507GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001508 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1509 return GrBackendRenderTarget();
1510 }
1511
Brian Salomon8a375832018-03-14 10:21:40 -04001512 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001513 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001514 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001515 if (kUnknown_GrPixelConfig == config) {
1516 return {};
1517 }
Robert Phillips646f6372018-09-25 09:31:10 -04001518 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001519 &info)) {
1520 return {};
1521 }
Greg Daniel108bb232018-07-03 16:18:29 -04001522 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1523 // Lots of tests don't go through Skia's public interface which will set the config so for
1524 // testing we make sure we set a config here.
1525 beRT.setPixelConfig(config);
1526 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001527}
1528
Brian Salomon52e943a2018-03-13 09:32:39 -04001529void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1530 SkASSERT(kVulkan_GrBackend == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001531
Greg Daniel323fbcf2018-04-10 13:46:30 -04001532 GrVkImageInfo info;
1533 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001534 // something in the command buffer may still be using this, so force submit
1535 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001536 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001537 }
1538}
Brian Salomonf865b052018-03-09 09:01:53 -05001539
Greg Daniel26b50a42018-03-08 09:49:58 -05001540void GrVkGpu::testingOnly_flushGpuAndSync() {
1541 this->submitCommandBuffer(kForce_SyncQueue);
1542}
Brian Salomonf865b052018-03-09 09:01:53 -05001543#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001544
Greg Daniel164a9f02016-02-22 09:56:40 -05001545////////////////////////////////////////////////////////////////////////////////
1546
1547void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1548 VkPipelineStageFlags dstStageMask,
1549 bool byRegion,
1550 VkMemoryBarrier* barrier) const {
1551 SkASSERT(fCurrentCmdBuffer);
1552 fCurrentCmdBuffer->pipelineBarrier(this,
1553 srcStageMask,
1554 dstStageMask,
1555 byRegion,
1556 GrVkCommandBuffer::kMemory_BarrierType,
1557 barrier);
1558}
1559
1560void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1561 VkPipelineStageFlags dstStageMask,
1562 bool byRegion,
1563 VkBufferMemoryBarrier* barrier) const {
1564 SkASSERT(fCurrentCmdBuffer);
1565 fCurrentCmdBuffer->pipelineBarrier(this,
1566 srcStageMask,
1567 dstStageMask,
1568 byRegion,
1569 GrVkCommandBuffer::kBufferMemory_BarrierType,
1570 barrier);
1571}
1572
1573void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1574 VkPipelineStageFlags dstStageMask,
1575 bool byRegion,
1576 VkImageMemoryBarrier* barrier) const {
1577 SkASSERT(fCurrentCmdBuffer);
1578 fCurrentCmdBuffer->pipelineBarrier(this,
1579 srcStageMask,
1580 dstStageMask,
1581 byRegion,
1582 GrVkCommandBuffer::kImageMemory_BarrierType,
1583 barrier);
1584}
1585
Greg Daniel51316782017-08-02 15:10:09 +00001586void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1587 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1588 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001589 this->submitCommandBuffer(kSkip_SyncQueue);
1590}
1591
Greg Daniel25af6712018-04-25 10:44:38 -04001592static int get_surface_sample_cnt(GrSurface* surf) {
1593 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1594 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001595 }
Greg Daniel25af6712018-04-25 10:44:38 -04001596 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001597}
1598
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001599void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1600 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001601 GrVkImage* dstImage,
1602 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001603 const SkIRect& srcRect,
1604 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001605#ifdef SK_DEBUG
1606 int dstSampleCnt = get_surface_sample_cnt(dst);
1607 int srcSampleCnt = get_surface_sample_cnt(src);
1608 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1609 src->config(), srcSampleCnt, srcOrigin));
1610
1611#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001612
Greg Daniel164a9f02016-02-22 09:56:40 -05001613 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1614 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001615 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001616 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1617 VK_ACCESS_TRANSFER_WRITE_BIT,
1618 VK_PIPELINE_STAGE_TRANSFER_BIT,
1619 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001620
egdaniel17b89252016-04-05 07:23:38 -07001621 srcImage->setImageLayout(this,
1622 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001623 VK_ACCESS_TRANSFER_READ_BIT,
1624 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001625 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001626
1627 // Flip rect if necessary
1628 SkIRect srcVkRect = srcRect;
1629 int32_t dstY = dstPoint.fY;
1630
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001631 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1632 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001633 srcVkRect.fTop = src->height() - srcRect.fBottom;
1634 srcVkRect.fBottom = src->height() - srcRect.fTop;
1635 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1636 }
1637
1638 VkImageCopy copyRegion;
1639 memset(&copyRegion, 0, sizeof(VkImageCopy));
1640 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1641 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1642 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1643 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001644 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001645
1646 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001647 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001648 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001649 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001650 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1651 1,
1652 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001653
1654 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1655 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001656 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001657}
1658
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001659void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1660 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001661 GrVkImage* dstImage,
1662 GrVkImage* srcImage,
1663 const SkIRect& srcRect,
1664 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001665#ifdef SK_DEBUG
1666 int dstSampleCnt = get_surface_sample_cnt(dst);
1667 int srcSampleCnt = get_surface_sample_cnt(src);
1668 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1669 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001670
Greg Daniel25af6712018-04-25 10:44:38 -04001671#endif
egdaniel17b89252016-04-05 07:23:38 -07001672 dstImage->setImageLayout(this,
1673 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001674 VK_ACCESS_TRANSFER_WRITE_BIT,
1675 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001676 false);
1677
egdaniel17b89252016-04-05 07:23:38 -07001678 srcImage->setImageLayout(this,
1679 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001680 VK_ACCESS_TRANSFER_READ_BIT,
1681 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001682 false);
1683
1684 // Flip rect if necessary
1685 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001686 srcVkRect.fLeft = srcRect.fLeft;
1687 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001688 SkIRect dstRect;
1689 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001690 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001691
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001692 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001693 srcVkRect.fTop = src->height() - srcRect.fBottom;
1694 srcVkRect.fBottom = src->height() - srcRect.fTop;
1695 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001696 srcVkRect.fTop = srcRect.fTop;
1697 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001698 }
1699
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001700 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001701 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1702 } else {
1703 dstRect.fTop = dstPoint.fY;
1704 }
1705 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1706
1707 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1708 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001709 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001710 using std::swap;
1711 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001712 }
1713
1714 VkImageBlit blitRegion;
1715 memset(&blitRegion, 0, sizeof(VkImageBlit));
1716 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1717 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001718 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001719 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1720 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001721 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001722
1723 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001724 *srcImage,
1725 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001726 1,
1727 &blitRegion,
1728 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001729
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001730 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001731 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001732}
1733
Brian Salomon1fabd512018-02-09 09:54:25 -05001734void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1735 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1736 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001737 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001738 SkIRect srcRect = origSrcRect;
1739 SkIPoint dstPoint = origDstPoint;
1740 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1741 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1742 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1743 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1744 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1745 }
1746 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001747 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1748 srcRect.width(), srcRect.height());
1749 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001750}
1751
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001752bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1753 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001754 const SkIRect& srcRect, const SkIPoint& dstPoint,
1755 bool canDiscardOutsideDstRect) {
Greg Daniel25af6712018-04-25 10:44:38 -04001756 GrPixelConfig dstConfig = dst->config();
1757 GrPixelConfig srcConfig = src->config();
1758
1759 int dstSampleCnt = get_surface_sample_cnt(dst);
1760 int srcSampleCnt = get_surface_sample_cnt(src);
1761
1762 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1763 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001764 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001765 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001766 }
1767
egdanielfd016d72016-09-27 12:13:05 -07001768 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1769 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
1770 }
1771
Greg Daniel25af6712018-04-25 10:44:38 -04001772 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1773 srcConfig, SkToBool(src->asTexture()))) {
1774 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1775 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001776 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1777 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001778 return true;
1779 }
1780
egdaniel17b89252016-04-05 07:23:38 -07001781 GrVkImage* dstImage;
1782 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001783 GrRenderTarget* dstRT = dst->asRenderTarget();
1784 if (dstRT) {
1785 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1786 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1787 } else {
1788 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001789 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001790 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001791 GrRenderTarget* srcRT = src->asRenderTarget();
1792 if (srcRT) {
1793 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1794 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001795 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001796 SkASSERT(src->asTexture());
1797 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001798 }
1799
Greg Daniel25af6712018-04-25 10:44:38 -04001800 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
1801 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001802 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1803 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07001804 return true;
1805 }
1806
Greg Daniel25af6712018-04-25 10:44:38 -04001807 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
1808 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001809 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1810 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001811 return true;
1812 }
1813
Greg Daniel164a9f02016-02-22 09:56:40 -05001814 return false;
1815}
1816
Brian Salomona6948702018-06-01 15:33:20 -04001817bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
1818 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05001819 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001820 return false;
1821 }
1822
egdaniel66933552016-08-24 07:22:19 -07001823 GrVkImage* image = nullptr;
1824 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1825 if (rt) {
1826 // resolve the render target if necessary
1827 switch (rt->getResolveType()) {
1828 case GrVkRenderTarget::kCantResolve_ResolveType:
1829 return false;
1830 case GrVkRenderTarget::kAutoResolves_ResolveType:
1831 break;
1832 case GrVkRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05001833 this->internalResolveRenderTarget(rt, false);
egdaniel66933552016-08-24 07:22:19 -07001834 break;
1835 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001836 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07001837 }
1838 image = rt;
1839 } else {
1840 image = static_cast<GrVkTexture*>(surface->asTexture());
1841 }
1842
1843 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001844 return false;
1845 }
1846
Greg Daniel475eb702018-09-28 14:16:50 -04001847 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
1848 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
1849 // image and then do the read pixels from that.
1850 sk_sp<GrVkTextureRenderTarget> copySurface;
1851 if (dstColorType == GrColorType::kRGB_888x) {
1852 SkASSERT(image->imageFormat() == VK_FORMAT_R8G8B8_UNORM &&
1853 surface->config() == kRGB_888_GrPixelConfig);
1854
1855 // Make a new surface that is RGBA to copy the RGB surface into.
1856 GrSurfaceDesc surfDesc;
1857 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1858 surfDesc.fWidth = width;
1859 surfDesc.fHeight = height;
1860 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
1861 surfDesc.fSampleCnt = 1;
1862
1863 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1864 VK_IMAGE_USAGE_SAMPLED_BIT |
1865 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1866 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1867
1868 GrVkImage::ImageDesc imageDesc;
1869 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1870 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
1871 imageDesc.fWidth = width;
1872 imageDesc.fHeight = height;
1873 imageDesc.fLevels = 1;
1874 imageDesc.fSamples = 1;
1875 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1876 imageDesc.fUsageFlags = usageFlags;
1877 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1878
1879 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
1880 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
1881 if (!copySurface) {
1882 return false;
1883 }
1884
1885 int srcSampleCount = 0;
1886 if (rt) {
1887 srcSampleCount = rt->numColorSamples();
1888 }
1889 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
1890 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin,
1891 surface->config(), srcSampleCount, kOrigin) &&
1892 !this->vkCaps().canCopyAsDraw(copySurface->config(), false,
1893 surface->config(), SkToBool(surface->asTexture()))) {
1894 return false;
1895 }
1896 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
1897 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
1898 srcRect, SkIPoint::Make(0,0))) {
1899 return false;
1900 }
1901 top = 0;
1902 left = 0;
1903 dstColorType = GrColorType::kRGBA_8888;
1904 image = copySurface.get();
1905 }
1906
Greg Daniel164a9f02016-02-22 09:56:40 -05001907 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001908 image->setImageLayout(this,
1909 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1910 VK_ACCESS_TRANSFER_READ_BIT,
1911 VK_PIPELINE_STAGE_TRANSFER_BIT,
1912 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001913
Brian Salomonc320b152018-02-20 14:05:36 -05001914 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07001915 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05001916
Greg Daniel164a9f02016-02-22 09:56:40 -05001917 VkBufferImageCopy region;
1918 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001919
1920 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1921 if (copyFromOrigin) {
1922 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04001923 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07001924 } else {
Brian Salomona6948702018-06-01 15:33:20 -04001925 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07001926 region.imageOffset = offset;
1927 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1928 }
1929
1930 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04001931 size_t imageRows = region.imageExtent.height;
egdaniel6fa0a912016-09-12 11:51:29 -07001932 GrVkTransferBuffer* transferBuffer =
Greg Daniel3cdfa092018-02-26 16:14:10 -05001933 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
egdaniel6fa0a912016-09-12 11:51:29 -07001934 kXferGpuToCpu_GrBufferType,
1935 kStream_GrAccessPattern));
1936
1937 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001938 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001939 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001940 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1941 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001942
1943 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001944 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001945 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1946 transferBuffer,
1947 1,
1948 &region);
1949
1950 // make sure the copy to buffer has finished
1951 transferBuffer->addMemoryBarrier(this,
1952 VK_ACCESS_TRANSFER_WRITE_BIT,
1953 VK_ACCESS_HOST_READ_BIT,
1954 VK_PIPELINE_STAGE_TRANSFER_BIT,
1955 VK_PIPELINE_STAGE_HOST_BIT,
1956 false);
1957
1958 // We need to submit the current command buffer to the Queue and make sure it finishes before
1959 // we can copy the data out of the buffer.
1960 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00001961 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05001962 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04001963 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05001964
egdaniel6fa0a912016-09-12 11:51:29 -07001965 if (copyFromOrigin) {
1966 uint32_t skipRows = region.imageExtent.height - height;
1967 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1968 }
1969
Brian Salomona6948702018-06-01 15:33:20 -04001970 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05001971
1972 transferBuffer->unmap();
1973 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001974 return true;
1975}
egdaniel066df7c2016-06-08 14:02:27 -07001976
egdaniel27bb2842016-07-07 11:58:35 -07001977// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1978// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1979// the the entire attachment. Similar requirements for the y and height components.
1980void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1981 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1982 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001983 if ((0 != granularity.width && 1 != granularity.width)) {
1984 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1985 int rightAdj = srcBounds.fRight % granularity.width;
1986 if (rightAdj != 0) {
1987 rightAdj = granularity.width - rightAdj;
1988 }
1989 dstBounds->fRight = srcBounds.fRight + rightAdj;
1990 if (dstBounds->fRight > maxWidth) {
1991 dstBounds->fRight = maxWidth;
1992 dstBounds->fLeft = 0;
1993 } else {
1994 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1995 }
egdaniel27bb2842016-07-07 11:58:35 -07001996 } else {
egdanield5797b32016-09-20 12:57:45 -07001997 dstBounds->fLeft = srcBounds.fLeft;
1998 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07001999 }
2000
2001 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002002 if ((0 != granularity.height && 1 != granularity.height)) {
2003 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2004 int bottomAdj = srcBounds.fBottom % granularity.height;
2005 if (bottomAdj != 0) {
2006 bottomAdj = granularity.height - bottomAdj;
2007 }
2008 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2009 if (dstBounds->fBottom > maxHeight) {
2010 dstBounds->fBottom = maxHeight;
2011 dstBounds->fTop = 0;
2012 } else {
2013 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2014 }
egdaniel27bb2842016-07-07 11:58:35 -07002015 } else {
egdanield5797b32016-09-20 12:57:45 -07002016 dstBounds->fTop = srcBounds.fTop;
2017 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002018 }
2019}
2020
Greg Daniel22bc8652017-03-22 15:45:43 -04002021void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002022 const GrVkRenderPass* renderPass,
2023 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002024 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002025 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07002026 const SkIRect* pBounds = &bounds;
2027 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002028 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002029 flippedBounds = bounds;
2030 flippedBounds.fTop = target->height() - bounds.fBottom;
2031 flippedBounds.fBottom = target->height() - bounds.fTop;
2032 pBounds = &flippedBounds;
2033 }
2034
egdaniel27bb2842016-07-07 11:58:35 -07002035 // The bounds we use for the render pass should be of the granularity supported
2036 // by the device.
2037 const VkExtent2D& granularity = renderPass->granularity();
2038 SkIRect adjustedBounds;
2039 if ((0 != granularity.width && 1 != granularity.width) ||
2040 (0 != granularity.height && 1 != granularity.height)) {
2041 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2042 target->width(), target->height());
2043 pBounds = &adjustedBounds;
2044 }
2045
Robert Phillips95214472017-08-08 18:00:03 -04002046#ifdef SK_DEBUG
2047 uint32_t index;
2048 bool result = renderPass->colorAttachmentIndex(&index);
2049 SkASSERT(result && 0 == index);
2050 result = renderPass->stencilAttachmentIndex(&index);
2051 if (result) {
2052 SkASSERT(1 == index);
2053 }
2054#endif
2055 VkClearValue clears[2];
2056 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002057 clears[1].depthStencil.depth = 0.0f;
2058 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002059
2060 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002061 for (int i = 0; i < buffers.count(); ++i) {
2062 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2063 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002064 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002065
Brian Salomon1fabd512018-02-09 09:54:25 -05002066 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002067}
egdaniel9cb63402016-06-23 08:37:05 -07002068
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002069void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2070 if (buffer->asRTCommandBuffer()) {
2071 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2072
2073 fCachedRTCommandBuffer->submit();
2074 fCachedRTCommandBuffer->reset();
2075 } else {
2076 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2077
2078 fCachedTexCommandBuffer->submit();
2079 fCachedTexCommandBuffer->reset();
2080 }
2081}
2082
Greg Daniel6be35232017-03-01 17:01:09 -05002083GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002084 VkFenceCreateInfo createInfo;
2085 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2086 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2087 createInfo.pNext = nullptr;
2088 createInfo.flags = 0;
2089 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002090
2091 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2092 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2093
2094 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002095 return (GrFence)fence;
2096}
2097
Greg Daniel6be35232017-03-01 17:01:09 -05002098bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2099 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2100
2101 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002102 return (VK_SUCCESS == result);
2103}
2104
2105void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002106 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2107}
2108
Greg Daniela5cb7812017-06-16 09:45:32 -04002109sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2110 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002111}
2112
Greg Daniel48661b82018-01-22 16:11:35 -05002113sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2114 GrResourceProvider::SemaphoreWrapType wrapType,
2115 GrWrapOwnership ownership) {
2116 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002117}
2118
Greg Daniel48661b82018-01-22 16:11:35 -05002119void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05002120 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2121
Greg Daniel48661b82018-01-22 16:11:35 -05002122 GrVkSemaphore::Resource* resource = vkSem->getResource();
2123 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002124 resource->ref();
2125 fSemaphoresToSignal.push_back(resource);
2126 }
Greg Daniela5cb7812017-06-16 09:45:32 -04002127
2128 if (flush) {
2129 this->submitCommandBuffer(kSkip_SyncQueue);
2130 }
Greg Daniel6be35232017-03-01 17:01:09 -05002131}
2132
Greg Daniel48661b82018-01-22 16:11:35 -05002133void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002134 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2135
Greg Daniel48661b82018-01-22 16:11:35 -05002136 GrVkSemaphore::Resource* resource = vkSem->getResource();
2137 if (resource->shouldWait()) {
2138 resource->ref();
2139 fSemaphoresToWaitOn.push_back(resource);
2140 }
jvanverth84741b32016-09-30 08:39:02 -07002141}
Brian Osman13dddce2017-05-09 13:19:50 -04002142
2143sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2144 SkASSERT(texture);
2145 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2146 vkTexture->setImageLayout(this,
2147 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2148 VK_ACCESS_SHADER_READ_BIT,
2149 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
2150 false);
2151 this->submitCommandBuffer(kSkip_SyncQueue);
2152
2153 // The image layout change serves as a barrier, so no semaphore is needed
2154 return nullptr;
2155}
Greg Danielf5d87582017-12-18 14:48:15 -05002156