blob: b0c5aee3669d1d98b819adb50546d8842b1408ff [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)
121 , fDevice(backendContext.fDevice)
122 , fQueue(backendContext.fQueue)
Brian Salomon384fab42017-12-07 12:33:05 -0500123 , fResourceProvider(this)
124 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000125 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700126
Greg Daniel81df0412018-05-31 13:13:33 -0400127 if (!fMemoryAllocator) {
128 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000129 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400130 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400131 }
132
ethannicholasb3058bd2016-07-01 08:22:01 -0700133 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700134
Greg Daniel92aef4b2018-08-02 13:55:49 -0400135 uint32_t instanceVersion = backendContext.fInstanceVersion ? backendContext.fInstanceVersion
136 : backendContext.fMinAPIVersion;
137
Greg Daniela0651ac2018-08-08 09:23:18 -0400138 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400139 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400140 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400141 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400142 } else if (backendContext.fDeviceFeatures) {
143 VkPhysicalDeviceFeatures2 features2;
144 features2.pNext = nullptr;
145 features2.features = *backendContext.fDeviceFeatures;
146 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
147 features2, instanceVersion, *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400148 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400149 VkPhysicalDeviceFeatures2 features;
150 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
151 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400152 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400153 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400154 }
155 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400156 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400157 }
158 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400159 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400160 }
161 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -0400162 features, instanceVersion, GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400163 }
jvanverth633b3562016-03-23 11:01:22 -0700164 fCaps.reset(SkRef(fVkCaps.get()));
165
Greg Danielf730c182018-07-02 20:15:37 +0000166 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
167 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700168
169 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700170 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
171 nullptr, // pNext
172 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
173 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
Greg Danielf730c182018-07-02 20:15:37 +0000174 backendContext.fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700175 };
halcanary9d524f22016-03-29 09:03:52 -0700176 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700177 &fCmdPool));
178
179 // must call this after creating the CommandPool
180 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700181 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700182 SkASSERT(fCurrentCmdBuffer);
183 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500184}
185
Greg Daniel8606cf82017-05-08 16:17:53 -0400186void GrVkGpu::destroyResources() {
187 if (fCurrentCmdBuffer) {
188 fCurrentCmdBuffer->end(this);
189 fCurrentCmdBuffer->unref(this);
190 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500191
192 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700193 fResourceProvider.checkCommandBuffers();
Jim Van Verth09557d72016-11-07 11:10:21 -0500194 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700195
196 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
197 // on the command buffers even though they have completed. This causes an assert to fire when
198 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500199 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700200#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500201 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700202#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500203 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700204#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500205 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700206#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500207 }
egdanielf8c2be32016-06-24 13:18:27 -0700208#endif
209
egdanielbe9d8212016-09-20 08:54:23 -0700210#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400211 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700212#endif
halcanary9d524f22016-03-29 09:03:52 -0700213
Greg Daniel6be35232017-03-01 17:01:09 -0500214 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
215 fSemaphoresToWaitOn[i]->unref(this);
216 }
217 fSemaphoresToWaitOn.reset();
218
Greg Daniela5cb7812017-06-16 09:45:32 -0400219 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
220 fSemaphoresToSignal[i]->unref(this);
221 }
222 fSemaphoresToSignal.reset();
223
224
egdanielbc9b2962016-09-27 08:00:53 -0700225 fCopyManager.destroyResources(this);
226
Jim Van Verth09557d72016-11-07 11:10:21 -0500227 // must call this just before we destroy the command pool and VkDevice
228 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500229
Greg Daniel8606cf82017-05-08 16:17:53 -0400230 if (fCmdPool != VK_NULL_HANDLE) {
231 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
232 }
jvanverth633b3562016-03-23 11:01:22 -0700233
Greg Danielf730c182018-07-02 20:15:37 +0000234 fMemoryAllocator.reset();
235
236 fQueue = VK_NULL_HANDLE;
237 fDevice = VK_NULL_HANDLE;
238 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400239}
240
241GrVkGpu::~GrVkGpu() {
242 if (!fDisconnected) {
243 this->destroyResources();
244 }
245 delete fCompiler;
246}
247
248
249void GrVkGpu::disconnect(DisconnectType type) {
250 INHERITED::disconnect(type);
251 if (!fDisconnected) {
252 if (DisconnectType::kCleanup == type) {
253 this->destroyResources();
254 } else {
255 fCurrentCmdBuffer->unrefAndAbandon();
256 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
257 fSemaphoresToWaitOn[i]->unrefAndAbandon();
258 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400259 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
260 fSemaphoresToSignal[i]->unrefAndAbandon();
261 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400262 fCopyManager.abandonResources();
263
264 // must call this just before we destroy the command pool and VkDevice
265 fResourceProvider.abandonResources();
266 }
267 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400268 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400269 fCurrentCmdBuffer = nullptr;
270 fCmdPool = VK_NULL_HANDLE;
271 fDisconnected = true;
272 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500273}
274
275///////////////////////////////////////////////////////////////////////////////
276
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400277GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -0400278 GrRenderTarget* rt, GrSurfaceOrigin origin,
Greg Daniel500d58b2017-08-24 15:59:33 -0400279 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
280 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400281 if (!fCachedRTCommandBuffer) {
282 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
283 }
284
285 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
286 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400287}
288
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400289GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
290 if (!fCachedTexCommandBuffer) {
291 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
292 }
293
294 fCachedTexCommandBuffer->set(texture, origin);
295 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700296}
297
Greg Daniela5cb7812017-06-16 09:45:32 -0400298void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500299 SkASSERT(fCurrentCmdBuffer);
300 fCurrentCmdBuffer->end(this);
301
Greg Daniela5cb7812017-06-16 09:45:32 -0400302 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500303
304 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
305 fSemaphoresToWaitOn[i]->unref(this);
306 }
307 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400308 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
309 fSemaphoresToSignal[i]->unref(this);
310 }
311 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500312
Greg Daniel164a9f02016-02-22 09:56:40 -0500313 fResourceProvider.checkCommandBuffers();
314
315 // Release old command buffer and create a new one
316 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700317 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500318 SkASSERT(fCurrentCmdBuffer);
319
320 fCurrentCmdBuffer->begin(this);
321}
322
323///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700324GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
325 const void* data) {
326 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700327 switch (type) {
328 case kVertex_GrBufferType:
329 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
330 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700331 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700332 break;
cdalton397536c2016-03-25 12:15:03 -0700333 case kIndex_GrBufferType:
334 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
335 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700336 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700337 break;
cdalton397536c2016-03-25 12:15:03 -0700338 case kXferCpuToGpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400339 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
340 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700341 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700342 break;
cdalton397536c2016-03-25 12:15:03 -0700343 case kXferGpuToCpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400344 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
345 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700346 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700347 break;
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400348 case kDrawIndirect_GrBufferType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400349 SK_ABORT("DrawIndirect Buffers not supported in vulkan backend.");
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400350 return nullptr;
cdalton397536c2016-03-25 12:15:03 -0700351 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400352 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700353 return nullptr;
354 }
cdalton1bf3e712016-04-19 10:00:02 -0700355 if (data && buff) {
356 buff->updateData(data, size);
357 }
358 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500359}
360
Brian Salomona9b04b92018-06-01 15:04:28 -0400361bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
362 GrColorType srcColorType, const GrMipLevel texels[],
363 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500364 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
365 if (!vkTex) {
366 return false;
367 }
368
jvanverth900bd4a2016-04-29 13:53:12 -0700369 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400370 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800371 return false;
372 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800373
Greg Daniel164a9f02016-02-22 09:56:40 -0500374 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400375 bool linearTiling = vkTex->isLinearTiled();
376 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400377 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400378 SkDebugf("Can't upload mipmap data to linear tiled texture");
379 return false;
380 }
381 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
382 // Need to change the layout to general in order to perform a host write
383 vkTex->setImageLayout(this,
384 VK_IMAGE_LAYOUT_GENERAL,
385 VK_ACCESS_HOST_WRITE_BIT,
386 VK_PIPELINE_STAGE_HOST_BIT,
387 false);
388 this->submitCommandBuffer(kForce_SyncQueue);
389 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400390 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400391 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500392 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400393 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400394 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
395 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500396 }
egdaniel4583ec52016-06-27 12:57:00 -0700397
jvanverth900bd4a2016-04-29 13:53:12 -0700398 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500399}
400
Brian Salomonc320b152018-02-20 14:05:36 -0500401bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
402 GrColorType bufferColorType, GrBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400403 size_t bufferOffset, size_t rowBytes) {
404 // Vulkan only supports 4-byte aligned offsets
405 if (SkToBool(bufferOffset & 0x2)) {
406 return false;
407 }
408 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
409 if (!vkTex) {
410 return false;
411 }
412 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
413 if (!vkBuffer) {
414 return false;
415 }
416
Greg Daniel660cc992017-06-26 14:55:05 -0400417 SkDEBUGCODE(
418 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
419 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
420 SkASSERT(bounds.contains(subRect));
421 )
Brian Salomonc320b152018-02-20 14:05:36 -0500422 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400423 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500424 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400425 }
426
427 // Set up copy region
428 VkBufferImageCopy region;
429 memset(&region, 0, sizeof(VkBufferImageCopy));
430 region.bufferOffset = bufferOffset;
431 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
432 region.bufferImageHeight = 0;
433 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
434 region.imageOffset = { left, top, 0 };
435 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
436
437 // Change layout of our target so it can be copied to
438 vkTex->setImageLayout(this,
439 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
440 VK_ACCESS_TRANSFER_WRITE_BIT,
441 VK_PIPELINE_STAGE_TRANSFER_BIT,
442 false);
443
444 // Copy the buffer to the image
445 fCurrentCmdBuffer->copyBufferToImage(this,
446 vkBuffer,
447 vkTex,
448 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
449 1,
450 &region);
451
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400452 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400453 return true;
454}
455
Brian Salomon1fabd512018-02-09 09:54:25 -0500456void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
457 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700458 SkASSERT(dst);
459 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
460
egdanielfd016d72016-09-27 12:13:05 -0700461 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
462 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
463 }
464
egdaniel4bcd62e2016-08-31 07:37:31 -0700465 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500466 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
467 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
468 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
469 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
470 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700471
Greg Danielbc26c392017-04-18 13:32:10 -0400472 GrVkImage* dstImage;
473 GrRenderTarget* dstRT = dst->asRenderTarget();
474 if (dstRT) {
475 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400476 dstImage = vkRT;
477 } else {
478 SkASSERT(dst->asTexture());
479 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
480 }
481 dstImage->setImageLayout(this,
482 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
483 VK_ACCESS_TRANSFER_WRITE_BIT,
484 VK_PIPELINE_STAGE_TRANSFER_BIT,
485 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700486
487 src->msaaImage()->setImageLayout(this,
488 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
489 VK_ACCESS_TRANSFER_READ_BIT,
490 VK_PIPELINE_STAGE_TRANSFER_BIT,
491 false);
492
Greg Danielbc26c392017-04-18 13:32:10 -0400493 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700494}
495
Brian Salomon1fabd512018-02-09 09:54:25 -0500496void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700497 if (target->needsResolve()) {
498 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700499 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
500 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500501
egdaniel4bcd62e2016-08-31 07:37:31 -0700502 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700503
Brian Salomon1fabd512018-02-09 09:54:25 -0500504 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700505
506 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500507
508 if (requiresSubmit) {
509 this->submitCommandBuffer(kSkip_SyncQueue);
510 }
egdaniel52ad2512016-08-04 12:50:01 -0700511 }
512}
513
Brian Salomona9b04b92018-06-01 15:04:28 -0400514bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
515 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500516 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700517 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500518
Greg Daniel660cc992017-06-26 14:55:05 -0400519 SkDEBUGCODE(
520 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
521 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
522 SkASSERT(bounds.contains(subRect));
523 )
Brian Salomonc320b152018-02-20 14:05:36 -0500524 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500525 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400526 if (!rowBytes) {
527 rowBytes = trimRowBytes;
528 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500529
jvanverth900bd4a2016-04-29 13:53:12 -0700530 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
531 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
532 const VkImageSubresource subres = {
533 VK_IMAGE_ASPECT_COLOR_BIT,
534 0, // mipLevel
535 0, // arraySlice
536 };
537 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500538
jvanverth900bd4a2016-04-29 13:53:12 -0700539 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500540
jvanverth900bd4a2016-04-29 13:53:12 -0700541 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700542 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700543 &subres,
544 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500545
jvanverth1e305ba2016-06-01 09:39:15 -0700546 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400547 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700548 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400549 SkASSERT(size + offset <= alloc.fSize);
550 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
551 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700552 return false;
553 }
Greg Daniel81df0412018-05-31 13:13:33 -0400554 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700555
Brian Salomona9b04b92018-06-01 15:04:28 -0400556 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
557 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700558
Greg Daniele35a99e2018-03-02 11:44:22 -0500559 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400560 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700561
562 return true;
563}
564
Brian Salomona9b04b92018-06-01 15:04:28 -0400565bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
566 GrColorType dataColorType, const GrMipLevel texels[],
567 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700568 SkASSERT(!tex->isLinearTiled());
569 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400570 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700571 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
572
Greg Danieldd20e912017-04-07 14:42:23 -0400573 // We assume that if the texture has mip levels, we either upload to all the levels or just the
574 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400575 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400576
jvanverth900bd4a2016-04-29 13:53:12 -0700577 if (width == 0 || height == 0) {
578 return false;
579 }
580
Brian Salomond1eaf492017-05-18 10:02:08 -0400581 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500582 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700583
584 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700585 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
586 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400587 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
588
589 if (mipLevelCount) {
590 texelsShallowCopy.reset(mipLevelCount);
591 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
592 }
jvanverth900bd4a2016-04-29 13:53:12 -0700593
Robert Phillips590533f2017-07-11 14:22:35 -0400594 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700595 individualMipOffsets.push_back(0);
596 size_t combinedBufferSize = width * bpp * height;
597 int currentWidth = width;
598 int currentHeight = height;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400599 if (mipLevelCount > 0 && !texelsShallowCopy[0].fPixels) {
600 combinedBufferSize = 0;
601 }
602
Greg Daniel468fd632017-03-22 17:03:45 -0400603 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
604 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
605 SkASSERT((bpp & (bpp - 1)) == 0);
606 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400607 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700608 currentWidth = SkTMax(1, currentWidth/2);
609 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400610
Greg Daniel55afd6d2017-09-29 09:32:44 -0400611 if (texelsShallowCopy[currentMipLevel].fPixels) {
612 const size_t trimmedSize = currentWidth * bpp * currentHeight;
613 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
614 if (alignmentDiff != 0) {
615 combinedBufferSize += alignmentMask - alignmentDiff + 1;
616 }
617 individualMipOffsets.push_back(combinedBufferSize);
618 combinedBufferSize += trimmedSize;
619 } else {
620 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400621 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400622 }
623 if (0 == combinedBufferSize) {
624 // We don't actually have any data to upload so just return success
625 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700626 }
627
628 // allocate buffer to hold our mip data
629 GrVkTransferBuffer* transferBuffer =
630 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel6888c0d2017-08-25 11:55:50 -0400631 if(!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700632 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400633 }
jvanverth900bd4a2016-04-29 13:53:12 -0700634
635 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400636 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700637
jvanverthc578b0632016-05-02 10:58:12 -0700638 currentWidth = width;
639 currentHeight = height;
Greg Daniela1b282b2017-03-28 14:56:46 -0400640 int layerHeight = tex->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400641 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400642 if (texelsShallowCopy[currentMipLevel].fPixels) {
643 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
644 const size_t trimRowBytes = currentWidth * bpp;
645 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
646 ? texelsShallowCopy[currentMipLevel].fRowBytes
647 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700648
Greg Daniel55afd6d2017-09-29 09:32:44 -0400649 // copy data into the buffer, skipping the trailing bytes
650 char* dst = buffer + individualMipOffsets[currentMipLevel];
651 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400652 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400653
654 VkBufferImageCopy& region = regions.push_back();
655 memset(&region, 0, sizeof(VkBufferImageCopy));
656 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
657 region.bufferRowLength = currentWidth;
658 region.bufferImageHeight = currentHeight;
659 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Brian Salomona9b04b92018-06-01 15:04:28 -0400660 region.imageOffset = {left, top, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400661 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700662 }
jvanverthc578b0632016-05-02 10:58:12 -0700663 currentWidth = SkTMax(1, currentWidth/2);
664 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400665 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700666 }
667
jvanverth9d54afc2016-09-20 09:20:03 -0700668 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700669 transferBuffer->unmap();
670
jvanverth900bd4a2016-04-29 13:53:12 -0700671 // Change layout of our target so it can be copied to
jvanverth900bd4a2016-04-29 13:53:12 -0700672 tex->setImageLayout(this,
673 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -0700674 VK_ACCESS_TRANSFER_WRITE_BIT,
675 VK_PIPELINE_STAGE_TRANSFER_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700676 false);
677
678 // Copy the buffer to the image
679 fCurrentCmdBuffer->copyBufferToImage(this,
680 transferBuffer,
681 tex,
682 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
683 regions.count(),
684 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700685 transferBuffer->unref();
Robert Phillips590533f2017-07-11 14:22:35 -0400686 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400687 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400688 }
jvanverth900bd4a2016-04-29 13:53:12 -0700689
Greg Daniel164a9f02016-02-22 09:56:40 -0500690 return true;
691}
692
693////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400694sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500695 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500696 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
697
698 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500699 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700700
Greg Daniel164a9f02016-02-22 09:56:40 -0500701 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
702 if (renderTarget) {
703 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
704 }
705
706 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
707 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
708 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700709 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500710 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
711 // texture.
712 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
713
Greg Daniel164a9f02016-02-22 09:56:40 -0500714 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700715 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500716 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400717 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500718 GrVkImage::ImageDesc imageDesc;
719 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
720 imageDesc.fFormat = pixelFormat;
721 imageDesc.fWidth = desc.fWidth;
722 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400723 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500724 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400725 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500726 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400727 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500728
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400729 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
730 if (mipLevels > 1) {
731 mipMapsStatus = GrMipMapsStatus::kValid;
732 for (int i = 0; i < mipLevels; ++i) {
733 if (!texels[i].fPixels) {
734 mipMapsStatus = GrMipMapsStatus::kDirty;
735 break;
736 }
Greg Daniel834f1202017-10-09 15:06:20 -0400737 }
738 }
739
Robert Phillips67d52cf2017-06-05 13:38:13 -0400740 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500741 if (renderTarget) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700742 tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
Greg Daniel834f1202017-10-09 15:06:20 -0400743 imageDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400744 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500745 } else {
Greg Daniel834f1202017-10-09 15:06:20 -0400746 tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400747 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500748 }
749
750 if (!tex) {
751 return nullptr;
752 }
753
Brian Salomonc320b152018-02-20 14:05:36 -0500754 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400755 if (mipLevelCount) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400756 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
757 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500758 tex->unref();
759 return nullptr;
760 }
761 }
762
Brian Salomond17b4a62017-05-23 16:53:47 -0400763 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
764 VkClearColorValue zeroClearColor;
765 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
766 VkImageSubresourceRange range;
767 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
768 range.baseArrayLayer = 0;
769 range.baseMipLevel = 0;
770 range.layerCount = 1;
771 range.levelCount = 1;
772 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
773 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400774 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400775 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400776 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -0500777}
778
779////////////////////////////////////////////////////////////////////////////////
780
Greg Daniel6888c0d2017-08-25 11:55:50 -0400781void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
782 VkDeviceSize dstOffset, VkDeviceSize size) {
783 VkBufferCopy copyRegion;
784 copyRegion.srcOffset = srcOffset;
785 copyRegion.dstOffset = dstOffset;
786 copyRegion.size = size;
787 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
788}
789
jvanverthdb379092016-07-07 11:18:46 -0700790bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
791 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700792 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700793 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700794
795 return true;
796}
797
798////////////////////////////////////////////////////////////////////////////////
799
Greg Daniel5254ccc2017-11-13 11:05:52 -0500800static bool check_backend_texture(const GrBackendTexture& backendTex,
801 GrPixelConfig config) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400802 GrVkImageInfo info;
803 if (!backendTex.getVkImageInfo(&info)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400804 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500805 }
806
Greg Daniel52e16d92018-04-10 09:34:07 -0400807 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -0400808 return false;
jvanverthfd359ca2016-03-18 11:57:24 -0700809 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000810
Greg Daniel52e16d92018-04-10 09:34:07 -0400811 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -0400812 return true;
813}
814
815sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400816 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500817 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400818 return nullptr;
819 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500820
Greg Daniel164a9f02016-02-22 09:56:40 -0500821 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400822 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000823 surfDesc.fWidth = backendTex.width();
824 surfDesc.fHeight = backendTex.height();
825 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500826 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500827
Greg Daniel52e16d92018-04-10 09:34:07 -0400828 GrVkImageInfo imageInfo;
829 if (!backendTex.getVkImageInfo(&imageInfo)) {
830 return nullptr;
831 }
832 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
833 SkASSERT(layout);
834 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, imageInfo, std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -0400835}
836
837sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400838 int sampleCnt,
839 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500840 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400841 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500842 }
Brian Salomond17f6582017-07-19 18:28:58 -0400843
844 GrSurfaceDesc surfDesc;
845 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
846 surfDesc.fWidth = backendTex.width();
847 surfDesc.fHeight = backendTex.height();
848 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500849 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -0400850
Greg Daniel52e16d92018-04-10 09:34:07 -0400851 GrVkImageInfo imageInfo;
852 if (!backendTex.getVkImageInfo(&imageInfo)) {
853 return nullptr;
854 }
855 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
856 SkASSERT(layout);
857
Brian Salomond17f6582017-07-19 18:28:58 -0400858 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
Greg Daniel52e16d92018-04-10 09:34:07 -0400859 imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500860}
861
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400862sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -0400863 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
864 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
865 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
866 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500867 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -0400868 return nullptr;
869 }
halcanary9d524f22016-03-29 09:03:52 -0700870
Greg Daniel323fbcf2018-04-10 13:46:30 -0400871 GrVkImageInfo info;
872 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000873 return nullptr;
874 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400875
876 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700877 return nullptr;
878 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500879
Greg Daniel164a9f02016-02-22 09:56:40 -0500880 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400881 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400882 desc.fWidth = backendRT.width();
883 desc.fHeight = backendRT.height();
884 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500885 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500886
Greg Daniel323fbcf2018-04-10 13:46:30 -0400887 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -0400888
Greg Daniel323fbcf2018-04-10 13:46:30 -0400889 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -0400890 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -0500891
892 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
893 SkASSERT(!backendRT.stencilBits());
894 if (tgt) {
895 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -0500896 }
Brian Salomonafdc6b12018-03-09 12:02:32 -0500897
Ben Wagnerff134f22018-04-24 16:29:16 -0400898 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500899}
900
Greg Daniel7ef28f32017-04-20 16:41:55 +0000901sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000902 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400903
Greg Daniel52e16d92018-04-10 09:34:07 -0400904 GrVkImageInfo imageInfo;
905 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000906 return nullptr;
907 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400908 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -0400909 return nullptr;
910 }
911
912 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000913 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000914 desc.fWidth = tex.width();
915 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400916 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500917 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
918 if (!desc.fSampleCnt) {
919 return nullptr;
920 }
Brian Osman33910292017-04-18 14:38:53 -0400921
Greg Daniel52e16d92018-04-10 09:34:07 -0400922 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
923 SkASSERT(layout);
924
Ben Wagnerff134f22018-04-24 16:29:16 -0400925 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -0400926}
927
Brian Salomon930f9392018-06-20 16:25:26 -0400928bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
929 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -0700930 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -0400931 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700932 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -0400933 return false;
jvanverth62340062016-04-26 08:01:44 -0700934 }
935
jvanverth62340062016-04-26 08:01:44 -0700936 // determine if we can blit to and from this format
937 const GrVkCaps& caps = this->vkCaps();
938 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -0700939 !caps.configCanBeSrcofBlit(tex->config(), false) ||
940 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -0400941 return false;
jvanverth62340062016-04-26 08:01:44 -0700942 }
943
egdanielfd016d72016-09-27 12:13:05 -0700944 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
945 this->submitCommandBuffer(kSkip_SyncQueue);
946 }
947
egdaniel7ac5da82016-07-15 13:41:42 -0700948 int width = tex->width();
949 int height = tex->height();
950 VkImageBlit blitRegion;
951 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -0700952
jvanverth82c05582016-05-03 11:19:01 -0700953 // SkMipMap doesn't include the base level in the level count so we have to add 1
954 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -0400955 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -0700956
Greg Danielda86e282018-06-13 09:41:19 -0400957 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -0400958 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
959 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -0700960
jvanverth50c46c72016-05-06 12:31:28 -0700961 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -0400962 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -0700963 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
964 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -0400965 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
966 nullptr, // pNext
967 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
968 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
969 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
970 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
971 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
972 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
973 vkTex->image(), // image
974 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -0700975 };
976
jvanverth62340062016-04-26 08:01:44 -0700977 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -0700978 uint32_t mipLevel = 1;
979 while (mipLevel < levelCount) {
980 int prevWidth = width;
981 int prevHeight = height;
982 width = SkTMax(1, width / 2);
983 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -0700984
jvanverth50c46c72016-05-06 12:31:28 -0700985 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
986 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
987 false, &imageMemoryBarrier);
988
989 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700990 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700991 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700992 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
993 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700994 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -0700995 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -0400996 vkTex->resource(),
997 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -0500998 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -0400999 vkTex->resource(),
1000 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001001 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001002 1,
1003 &blitRegion,
1004 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001005 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001006 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001007 // This barrier logically is not needed, but it changes the final level to the same layout as
1008 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1009 // future layout changes easier.
1010 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1011 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1012 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001013 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1014 return true;
jvanverth62340062016-04-26 08:01:44 -07001015}
1016
Greg Daniel164a9f02016-02-22 09:56:40 -05001017////////////////////////////////////////////////////////////////////////////////
1018
1019GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1020 int width,
1021 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001022 SkASSERT(width >= rt->width());
1023 SkASSERT(height >= rt->height());
1024
1025 int samples = rt->numStencilSamples();
1026
egdaniel8f1dcaa2016-04-01 10:10:45 -07001027 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001028
1029 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001030 width,
1031 height,
1032 samples,
1033 sFmt));
1034 fStats.incStencilAttachmentCreates();
1035 return stencil;
1036}
1037
1038////////////////////////////////////////////////////////////////////////////////
1039
Brian Salomon52e943a2018-03-13 09:32:39 -04001040bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
1041 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001042 VkDeviceSize size = dstRowBytes * h;
1043 VkDeviceSize offset = bufferOffset;
1044 SkASSERT(size + offset <= alloc.fSize);
1045 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1046 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001047 return false;
1048 }
Greg Daniel81df0412018-05-31 13:13:33 -04001049 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001050
Greg Daniel20ece3a2017-03-28 10:24:43 -04001051 if (srcData) {
1052 // If there is no padding on dst we can do a single memcopy.
1053 // This assumes the srcData comes in with no padding.
1054 SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes),
1055 srcData, srcRowBytes, srcRowBytes, h);
1056 } else {
1057 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1058 // with some data.
1059 if (srcRowBytes == static_cast<size_t>(dstRowBytes)) {
1060 memset(mapPtr, 0, srcRowBytes * h);
1061 } else {
1062 for (int i = 0; i < h; ++i) {
1063 memset(mapPtr, 0, srcRowBytes);
1064 mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes));
1065 }
1066 }
1067 }
Greg Daniel81df0412018-05-31 13:13:33 -04001068 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1069 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001070 return true;
1071}
1072
Brian Salomonf865b052018-03-09 09:01:53 -05001073#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001074bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1075 bool renderable, GrMipMapped mipMapped, const void* srcData,
1076 GrVkImageInfo* info) {
1077 SkASSERT(texturable || renderable);
1078 if (!texturable) {
1079 SkASSERT(GrMipMapped::kNo == mipMapped);
1080 SkASSERT(!srcData);
1081 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001082 VkFormat pixelFormat;
1083 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001084 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001085 }
1086
Brian Salomon52e943a2018-03-13 09:32:39 -04001087 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1088 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001089 }
1090
Brian Salomon52e943a2018-03-13 09:32:39 -04001091 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1092 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001093 }
1094
1095 // Currently we don't support uploading pixel data when mipped.
1096 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001097 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001098 }
1099
Brian Salomon52e943a2018-03-13 09:32:39 -04001100 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001101 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1102 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001103 if (texturable) {
1104 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1105 }
1106 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001107 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1108 }
1109
1110 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001111 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001112 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001113
1114 // Create Image
1115 VkSampleCountFlagBits vkSamples;
1116 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001117 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001118 }
1119
1120 // Figure out the number of mip levels.
1121 uint32_t mipLevels = 1;
1122 if (GrMipMapped::kYes == mipMapped) {
1123 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1124 }
1125
1126 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001127 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1128 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001129 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001130 VK_IMAGE_TYPE_2D, // VkImageType
1131 pixelFormat, // VkFormat
1132 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1133 mipLevels, // mipLevels
1134 1, // arrayLayers
1135 vkSamples, // samples
1136 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1137 usageFlags, // VkImageUsageFlags
1138 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1139 0, // queueFamilyCount
1140 0, // pQueueFamilyIndices
1141 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001142 };
1143
Brian Salomon52e943a2018-03-13 09:32:39 -04001144 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1145 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001146
Brian Salomonde9f5462018-03-07 14:23:58 -05001147 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001148 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001149 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001150 }
1151
1152 // 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 -05001153 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001154 VkBuffer buffer = VK_NULL_HANDLE;
1155
1156 VkResult err;
1157 const VkCommandBufferAllocateInfo cmdInfo = {
1158 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1159 nullptr, // pNext
1160 fCmdPool, // commandPool
1161 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1162 1 // bufferCount
1163 };
1164
1165 VkCommandBuffer cmdBuffer;
1166 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1167 if (err) {
1168 GrVkMemory::FreeImageMemory(this, false, alloc);
1169 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001170 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001171 }
1172
1173 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1174 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1175 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1176 cmdBufferBeginInfo.pNext = nullptr;
1177 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1178 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1179
1180 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1181 SkASSERT(!err);
1182
1183 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001184 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001185
Brian Salomonde9f5462018-03-07 14:23:58 -05001186 SkTArray<size_t> individualMipOffsets(mipLevels);
1187 individualMipOffsets.push_back(0);
1188 size_t combinedBufferSize = w * bpp * h;
1189 int currentWidth = w;
1190 int currentHeight = h;
1191 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1192 // config. This works with the assumption that the bytes in pixel config is always a power
1193 // of 2.
1194 SkASSERT((bpp & (bpp - 1)) == 0);
1195 const size_t alignmentMask = 0x3 | (bpp - 1);
1196 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1197 currentWidth = SkTMax(1, currentWidth / 2);
1198 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001199
Brian Salomonde9f5462018-03-07 14:23:58 -05001200 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1201 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1202 if (alignmentDiff != 0) {
1203 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001204 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001205 individualMipOffsets.push_back(combinedBufferSize);
1206 combinedBufferSize += trimmedSize;
1207 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001208
Brian Salomonde9f5462018-03-07 14:23:58 -05001209 VkBufferCreateInfo bufInfo;
1210 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1211 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1212 bufInfo.flags = 0;
1213 bufInfo.size = combinedBufferSize;
1214 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1215 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1216 bufInfo.queueFamilyIndexCount = 0;
1217 bufInfo.pQueueFamilyIndices = nullptr;
1218 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001219
Brian Salomonde9f5462018-03-07 14:23:58 -05001220 if (err) {
1221 GrVkMemory::FreeImageMemory(this, false, alloc);
1222 VK_CALL(DestroyImage(fDevice, image, nullptr));
1223 VK_CALL(EndCommandBuffer(cmdBuffer));
1224 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001225 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001226 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001227
Brian Salomonde9f5462018-03-07 14:23:58 -05001228 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1229 &bufferAlloc)) {
1230 GrVkMemory::FreeImageMemory(this, false, alloc);
1231 VK_CALL(DestroyImage(fDevice, image, nullptr));
1232 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1233 VK_CALL(EndCommandBuffer(cmdBuffer));
1234 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001235 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001236 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001237
Brian Salomonde9f5462018-03-07 14:23:58 -05001238 currentWidth = w;
1239 currentHeight = h;
1240 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1241 SkASSERT(0 == currentMipLevel || !srcData);
1242 size_t currentRowBytes = bpp * currentWidth;
1243 size_t bufferOffset = individualMipOffsets[currentMipLevel];
1244 if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1245 currentRowBytes, currentHeight)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001246 GrVkMemory::FreeImageMemory(this, false, alloc);
1247 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001248 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001249 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1250 VK_CALL(EndCommandBuffer(cmdBuffer));
1251 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001252 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001253 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001254 currentWidth = SkTMax(1, currentWidth / 2);
1255 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001256 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001257
1258 // Set image layout and add barrier
1259 VkImageMemoryBarrier barrier;
1260 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1261 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1262 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001263 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001264 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1265 barrier.oldLayout = initialLayout;
1266 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1267 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1268 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1269 barrier.image = image;
1270 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1271
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001272 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001273 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1274 &barrier));
1275 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1276
1277 SkTArray<VkBufferImageCopy> regions(mipLevels);
1278
1279 currentWidth = w;
1280 currentHeight = h;
1281 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1282 // Submit copy command
1283 VkBufferImageCopy& region = regions.push_back();
1284 memset(&region, 0, sizeof(VkBufferImageCopy));
1285 region.bufferOffset = individualMipOffsets[currentMipLevel];
1286 region.bufferRowLength = currentWidth;
1287 region.bufferImageHeight = currentHeight;
1288 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1289 region.imageOffset = {0, 0, 0};
1290 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1291 currentWidth = SkTMax(1, currentWidth / 2);
1292 currentHeight = SkTMax(1, currentHeight / 2);
1293 }
1294
1295 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1296 regions.begin()));
1297
Brian Salomon52e943a2018-03-13 09:32:39 -04001298 if (texturable) {
1299 // Change Image layout to shader read since if we use this texture as a borrowed textures
1300 // within Ganesh we require that its layout be set to that
1301 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1302 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1303 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001304 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001305 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1306 barrier.oldLayout = initialLayout;
1307 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1308 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1309 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1310 barrier.image = image;
1311 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001312 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001313 GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomon52e943a2018-03-13 09:32:39 -04001314 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1315 0,
1316 0, nullptr,
1317 0, nullptr,
1318 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001319 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001320 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001321
1322 // End CommandBuffer
1323 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1324 SkASSERT(!err);
1325
1326 // Create Fence for queue
1327 VkFence fence;
1328 VkFenceCreateInfo fenceInfo;
1329 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1330 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1331
1332 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1333 SkASSERT(!err);
1334
1335 VkSubmitInfo submitInfo;
1336 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1337 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1338 submitInfo.pNext = nullptr;
1339 submitInfo.waitSemaphoreCount = 0;
1340 submitInfo.pWaitSemaphores = nullptr;
1341 submitInfo.pWaitDstStageMask = 0;
1342 submitInfo.commandBufferCount = 1;
1343 submitInfo.pCommandBuffers = &cmdBuffer;
1344 submitInfo.signalSemaphoreCount = 0;
1345 submitInfo.pSignalSemaphores = nullptr;
1346 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1347 SkASSERT(!err);
1348
1349 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1350 if (VK_TIMEOUT == err) {
1351 GrVkMemory::FreeImageMemory(this, false, alloc);
1352 VK_CALL(DestroyImage(fDevice, image, nullptr));
1353 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1354 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1355 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1356 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1357 SkDebugf("Fence failed to signal: %d\n", err);
1358 SK_ABORT("failing");
1359 }
1360 SkASSERT(!err);
1361
1362 // Clean up transfer resources
1363 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1364 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1365 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1366 }
1367 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1368 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1369
Brian Salomon52e943a2018-03-13 09:32:39 -04001370 info->fImage = image;
1371 info->fAlloc = alloc;
1372 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1373 info->fImageLayout = initialLayout;
1374 info->fFormat = pixelFormat;
1375 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001376
Brian Salomon52e943a2018-03-13 09:32:39 -04001377 return true;
1378}
1379
1380GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
1381 GrPixelConfig config, bool isRenderTarget,
1382 GrMipMapped mipMapped) {
Brian Salomon8a375832018-03-14 10:21:40 -04001383 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001384
1385 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1386 return GrBackendTexture();
1387 }
1388
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001389 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001390 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
1391 &info)) {
1392 return {};
1393 }
Greg Daniel108bb232018-07-03 16:18:29 -04001394 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1395 // Lots of tests don't go through Skia's public interface which will set the config so for
1396 // testing we make sure we set a config here.
1397 beTex.setPixelConfig(config);
1398 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001399}
1400
1401bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
1402 SkASSERT(kVulkan_GrBackend == tex.fBackend);
1403
Greg Daniel52e16d92018-04-10 09:34:07 -04001404 GrVkImageInfo backend;
1405 if (!tex.getVkImageInfo(&backend)) {
1406 return false;
1407 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001408
Greg Daniel52e16d92018-04-10 09:34:07 -04001409 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001410 VkMemoryRequirements req;
1411 memset(&req, 0, sizeof(req));
1412 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001413 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001414 &req));
1415 // TODO: find a better check
1416 // This will probably fail with a different driver
1417 return (req.size > 0) && (req.size <= 8192 * 8192);
1418 }
1419
1420 return false;
1421}
1422
Brian Salomon26102cb2018-03-09 09:33:19 -05001423void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
1424 SkASSERT(kVulkan_GrBackend == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001425
Greg Daniel52e16d92018-04-10 09:34:07 -04001426 GrVkImageInfo info;
1427 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001428 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001429 }
1430}
1431
Brian Osman2d010b62018-08-09 10:55:09 -04001432GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001433 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1434 return GrBackendRenderTarget();
1435 }
1436
Brian Salomon8a375832018-03-14 10:21:40 -04001437 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001438 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001439 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001440 if (kUnknown_GrPixelConfig == config) {
1441 return {};
1442 }
1443 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr,
1444 &info)) {
1445 return {};
1446 }
Greg Daniel108bb232018-07-03 16:18:29 -04001447 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1448 // Lots of tests don't go through Skia's public interface which will set the config so for
1449 // testing we make sure we set a config here.
1450 beRT.setPixelConfig(config);
1451 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001452}
1453
Brian Salomon52e943a2018-03-13 09:32:39 -04001454void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1455 SkASSERT(kVulkan_GrBackend == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001456
Greg Daniel323fbcf2018-04-10 13:46:30 -04001457 GrVkImageInfo info;
1458 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001459 // something in the command buffer may still be using this, so force submit
1460 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001461 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001462 }
1463}
Brian Salomonf865b052018-03-09 09:01:53 -05001464
Greg Daniel26b50a42018-03-08 09:49:58 -05001465void GrVkGpu::testingOnly_flushGpuAndSync() {
1466 this->submitCommandBuffer(kForce_SyncQueue);
1467}
Brian Salomonf865b052018-03-09 09:01:53 -05001468#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001469
Greg Daniel164a9f02016-02-22 09:56:40 -05001470////////////////////////////////////////////////////////////////////////////////
1471
1472void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1473 VkPipelineStageFlags dstStageMask,
1474 bool byRegion,
1475 VkMemoryBarrier* barrier) const {
1476 SkASSERT(fCurrentCmdBuffer);
1477 fCurrentCmdBuffer->pipelineBarrier(this,
1478 srcStageMask,
1479 dstStageMask,
1480 byRegion,
1481 GrVkCommandBuffer::kMemory_BarrierType,
1482 barrier);
1483}
1484
1485void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1486 VkPipelineStageFlags dstStageMask,
1487 bool byRegion,
1488 VkBufferMemoryBarrier* barrier) const {
1489 SkASSERT(fCurrentCmdBuffer);
1490 fCurrentCmdBuffer->pipelineBarrier(this,
1491 srcStageMask,
1492 dstStageMask,
1493 byRegion,
1494 GrVkCommandBuffer::kBufferMemory_BarrierType,
1495 barrier);
1496}
1497
1498void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1499 VkPipelineStageFlags dstStageMask,
1500 bool byRegion,
1501 VkImageMemoryBarrier* barrier) const {
1502 SkASSERT(fCurrentCmdBuffer);
1503 fCurrentCmdBuffer->pipelineBarrier(this,
1504 srcStageMask,
1505 dstStageMask,
1506 byRegion,
1507 GrVkCommandBuffer::kImageMemory_BarrierType,
1508 barrier);
1509}
1510
Greg Daniel51316782017-08-02 15:10:09 +00001511void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1512 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1513 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001514 this->submitCommandBuffer(kSkip_SyncQueue);
1515}
1516
Robert Phillips95214472017-08-08 18:00:03 -04001517void GrVkGpu::clearStencil(GrRenderTarget* target, int clearValue) {
1518 if (!target) {
egdaniel3d5d9ac2016-03-01 12:56:15 -08001519 return;
1520 }
1521 GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
1522 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
1523
1524
1525 VkClearDepthStencilValue vkStencilColor;
Robert Phillips95214472017-08-08 18:00:03 -04001526 vkStencilColor.depth = 0.0f;
1527 vkStencilColor.stencil = clearValue;
egdaniel3d5d9ac2016-03-01 12:56:15 -08001528
egdaniel3d5d9ac2016-03-01 12:56:15 -08001529 vkStencil->setImageLayout(this,
1530 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001531 VK_ACCESS_TRANSFER_WRITE_BIT,
1532 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel3d5d9ac2016-03-01 12:56:15 -08001533 false);
1534
egdaniel3d5d9ac2016-03-01 12:56:15 -08001535 VkImageSubresourceRange subRange;
1536 memset(&subRange, 0, sizeof(VkImageSubresourceRange));
1537 subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1538 subRange.baseMipLevel = 0;
1539 subRange.levelCount = 1;
1540 subRange.baseArrayLayer = 0;
1541 subRange.layerCount = 1;
1542
1543 // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
1544 // draw. Thus we should look into using the load op functions on the render pass to clear out
1545 // the stencil there.
1546 fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
1547}
1548
Greg Daniel25af6712018-04-25 10:44:38 -04001549static int get_surface_sample_cnt(GrSurface* surf) {
1550 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1551 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001552 }
Greg Daniel25af6712018-04-25 10:44:38 -04001553 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001554}
1555
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001556void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1557 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001558 GrVkImage* dstImage,
1559 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001560 const SkIRect& srcRect,
1561 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001562#ifdef SK_DEBUG
1563 int dstSampleCnt = get_surface_sample_cnt(dst);
1564 int srcSampleCnt = get_surface_sample_cnt(src);
1565 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1566 src->config(), srcSampleCnt, srcOrigin));
1567
1568#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001569
Greg Daniel164a9f02016-02-22 09:56:40 -05001570 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1571 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001572 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001573 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1574 VK_ACCESS_TRANSFER_WRITE_BIT,
1575 VK_PIPELINE_STAGE_TRANSFER_BIT,
1576 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001577
egdaniel17b89252016-04-05 07:23:38 -07001578 srcImage->setImageLayout(this,
1579 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001580 VK_ACCESS_TRANSFER_READ_BIT,
1581 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001582 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001583
1584 // Flip rect if necessary
1585 SkIRect srcVkRect = srcRect;
1586 int32_t dstY = dstPoint.fY;
1587
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001588 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1589 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001590 srcVkRect.fTop = src->height() - srcRect.fBottom;
1591 srcVkRect.fBottom = src->height() - srcRect.fTop;
1592 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1593 }
1594
1595 VkImageCopy copyRegion;
1596 memset(&copyRegion, 0, sizeof(VkImageCopy));
1597 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1598 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1599 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1600 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001601 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001602
1603 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001604 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001605 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001606 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001607 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1608 1,
1609 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001610
1611 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1612 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001613 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001614}
1615
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001616void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1617 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001618 GrVkImage* dstImage,
1619 GrVkImage* srcImage,
1620 const SkIRect& srcRect,
1621 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001622#ifdef SK_DEBUG
1623 int dstSampleCnt = get_surface_sample_cnt(dst);
1624 int srcSampleCnt = get_surface_sample_cnt(src);
1625 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1626 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001627
Greg Daniel25af6712018-04-25 10:44:38 -04001628#endif
egdaniel17b89252016-04-05 07:23:38 -07001629 dstImage->setImageLayout(this,
1630 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001631 VK_ACCESS_TRANSFER_WRITE_BIT,
1632 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001633 false);
1634
egdaniel17b89252016-04-05 07:23:38 -07001635 srcImage->setImageLayout(this,
1636 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001637 VK_ACCESS_TRANSFER_READ_BIT,
1638 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001639 false);
1640
1641 // Flip rect if necessary
1642 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001643 srcVkRect.fLeft = srcRect.fLeft;
1644 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001645 SkIRect dstRect;
1646 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001647 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001648
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001649 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001650 srcVkRect.fTop = src->height() - srcRect.fBottom;
1651 srcVkRect.fBottom = src->height() - srcRect.fTop;
1652 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001653 srcVkRect.fTop = srcRect.fTop;
1654 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001655 }
1656
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001657 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001658 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1659 } else {
1660 dstRect.fTop = dstPoint.fY;
1661 }
1662 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1663
1664 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1665 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001666 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001667 using std::swap;
1668 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001669 }
1670
1671 VkImageBlit blitRegion;
1672 memset(&blitRegion, 0, sizeof(VkImageBlit));
1673 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1674 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001675 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001676 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1677 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001678 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001679
1680 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001681 *srcImage,
1682 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001683 1,
1684 &blitRegion,
1685 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001686
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001687 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001688 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001689}
1690
Brian Salomon1fabd512018-02-09 09:54:25 -05001691void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1692 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1693 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001694 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001695 SkIRect srcRect = origSrcRect;
1696 SkIPoint dstPoint = origDstPoint;
1697 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1698 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1699 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1700 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1701 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1702 }
1703 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001704 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1705 srcRect.width(), srcRect.height());
1706 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001707}
1708
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001709bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1710 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001711 const SkIRect& srcRect, const SkIPoint& dstPoint,
1712 bool canDiscardOutsideDstRect) {
Greg Daniel25af6712018-04-25 10:44:38 -04001713 GrPixelConfig dstConfig = dst->config();
1714 GrPixelConfig srcConfig = src->config();
1715
1716 int dstSampleCnt = get_surface_sample_cnt(dst);
1717 int srcSampleCnt = get_surface_sample_cnt(src);
1718
1719 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1720 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001721 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001722 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001723 }
1724
egdanielfd016d72016-09-27 12:13:05 -07001725 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1726 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
1727 }
1728
Greg Daniel25af6712018-04-25 10:44:38 -04001729 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1730 srcConfig, SkToBool(src->asTexture()))) {
1731 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1732 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001733 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1734 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001735 return true;
1736 }
1737
egdaniel17b89252016-04-05 07:23:38 -07001738 GrVkImage* dstImage;
1739 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001740 GrRenderTarget* dstRT = dst->asRenderTarget();
1741 if (dstRT) {
1742 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1743 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1744 } else {
1745 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001746 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001747 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001748 GrRenderTarget* srcRT = src->asRenderTarget();
1749 if (srcRT) {
1750 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1751 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001752 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001753 SkASSERT(src->asTexture());
1754 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001755 }
1756
Greg Daniel25af6712018-04-25 10:44:38 -04001757 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
1758 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001759 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1760 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07001761 return true;
1762 }
1763
Greg Daniel25af6712018-04-25 10:44:38 -04001764 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
1765 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001766 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1767 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001768 return true;
1769 }
1770
Greg Daniel164a9f02016-02-22 09:56:40 -05001771 return false;
1772}
1773
Brian Salomona6948702018-06-01 15:33:20 -04001774bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
1775 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05001776 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001777 return false;
1778 }
1779
egdaniel66933552016-08-24 07:22:19 -07001780 GrVkImage* image = nullptr;
1781 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1782 if (rt) {
1783 // resolve the render target if necessary
1784 switch (rt->getResolveType()) {
1785 case GrVkRenderTarget::kCantResolve_ResolveType:
1786 return false;
1787 case GrVkRenderTarget::kAutoResolves_ResolveType:
1788 break;
1789 case GrVkRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05001790 this->internalResolveRenderTarget(rt, false);
egdaniel66933552016-08-24 07:22:19 -07001791 break;
1792 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001793 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07001794 }
1795 image = rt;
1796 } else {
1797 image = static_cast<GrVkTexture*>(surface->asTexture());
1798 }
1799
1800 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001801 return false;
1802 }
1803
1804 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001805 image->setImageLayout(this,
1806 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1807 VK_ACCESS_TRANSFER_READ_BIT,
1808 VK_PIPELINE_STAGE_TRANSFER_BIT,
1809 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001810
Brian Salomonc320b152018-02-20 14:05:36 -05001811 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07001812 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05001813
Greg Daniel164a9f02016-02-22 09:56:40 -05001814 VkBufferImageCopy region;
1815 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001816
1817 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1818 if (copyFromOrigin) {
1819 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04001820 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07001821 } else {
Brian Salomona6948702018-06-01 15:33:20 -04001822 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07001823 region.imageOffset = offset;
1824 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1825 }
1826
1827 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04001828 size_t imageRows = region.imageExtent.height;
egdaniel6fa0a912016-09-12 11:51:29 -07001829 GrVkTransferBuffer* transferBuffer =
Greg Daniel3cdfa092018-02-26 16:14:10 -05001830 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
egdaniel6fa0a912016-09-12 11:51:29 -07001831 kXferGpuToCpu_GrBufferType,
1832 kStream_GrAccessPattern));
1833
1834 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001835 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001836 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001837 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1838 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001839
1840 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001841 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001842 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1843 transferBuffer,
1844 1,
1845 &region);
1846
1847 // make sure the copy to buffer has finished
1848 transferBuffer->addMemoryBarrier(this,
1849 VK_ACCESS_TRANSFER_WRITE_BIT,
1850 VK_ACCESS_HOST_READ_BIT,
1851 VK_PIPELINE_STAGE_TRANSFER_BIT,
1852 VK_PIPELINE_STAGE_HOST_BIT,
1853 false);
1854
1855 // We need to submit the current command buffer to the Queue and make sure it finishes before
1856 // we can copy the data out of the buffer.
1857 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00001858 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05001859 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04001860 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05001861
egdaniel6fa0a912016-09-12 11:51:29 -07001862 if (copyFromOrigin) {
1863 uint32_t skipRows = region.imageExtent.height - height;
1864 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1865 }
1866
Brian Salomona6948702018-06-01 15:33:20 -04001867 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05001868
1869 transferBuffer->unmap();
1870 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001871 return true;
1872}
egdaniel066df7c2016-06-08 14:02:27 -07001873
egdaniel27bb2842016-07-07 11:58:35 -07001874// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1875// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1876// the the entire attachment. Similar requirements for the y and height components.
1877void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1878 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1879 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001880 if ((0 != granularity.width && 1 != granularity.width)) {
1881 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1882 int rightAdj = srcBounds.fRight % granularity.width;
1883 if (rightAdj != 0) {
1884 rightAdj = granularity.width - rightAdj;
1885 }
1886 dstBounds->fRight = srcBounds.fRight + rightAdj;
1887 if (dstBounds->fRight > maxWidth) {
1888 dstBounds->fRight = maxWidth;
1889 dstBounds->fLeft = 0;
1890 } else {
1891 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1892 }
egdaniel27bb2842016-07-07 11:58:35 -07001893 } else {
egdanield5797b32016-09-20 12:57:45 -07001894 dstBounds->fLeft = srcBounds.fLeft;
1895 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07001896 }
1897
1898 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07001899 if ((0 != granularity.height && 1 != granularity.height)) {
1900 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
1901 int bottomAdj = srcBounds.fBottom % granularity.height;
1902 if (bottomAdj != 0) {
1903 bottomAdj = granularity.height - bottomAdj;
1904 }
1905 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
1906 if (dstBounds->fBottom > maxHeight) {
1907 dstBounds->fBottom = maxHeight;
1908 dstBounds->fTop = 0;
1909 } else {
1910 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
1911 }
egdaniel27bb2842016-07-07 11:58:35 -07001912 } else {
egdanield5797b32016-09-20 12:57:45 -07001913 dstBounds->fTop = srcBounds.fTop;
1914 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07001915 }
1916}
1917
Greg Daniel22bc8652017-03-22 15:45:43 -04001918void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07001919 const GrVkRenderPass* renderPass,
1920 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001921 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07001922 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07001923 const SkIRect* pBounds = &bounds;
1924 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001925 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07001926 flippedBounds = bounds;
1927 flippedBounds.fTop = target->height() - bounds.fBottom;
1928 flippedBounds.fBottom = target->height() - bounds.fTop;
1929 pBounds = &flippedBounds;
1930 }
1931
egdaniel27bb2842016-07-07 11:58:35 -07001932 // The bounds we use for the render pass should be of the granularity supported
1933 // by the device.
1934 const VkExtent2D& granularity = renderPass->granularity();
1935 SkIRect adjustedBounds;
1936 if ((0 != granularity.width && 1 != granularity.width) ||
1937 (0 != granularity.height && 1 != granularity.height)) {
1938 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
1939 target->width(), target->height());
1940 pBounds = &adjustedBounds;
1941 }
1942
Robert Phillips95214472017-08-08 18:00:03 -04001943#ifdef SK_DEBUG
1944 uint32_t index;
1945 bool result = renderPass->colorAttachmentIndex(&index);
1946 SkASSERT(result && 0 == index);
1947 result = renderPass->stencilAttachmentIndex(&index);
1948 if (result) {
1949 SkASSERT(1 == index);
1950 }
1951#endif
1952 VkClearValue clears[2];
1953 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04001954 clears[1].depthStencil.depth = 0.0f;
1955 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04001956
1957 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04001958 for (int i = 0; i < buffers.count(); ++i) {
1959 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
1960 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001961 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07001962
Brian Salomon1fabd512018-02-09 09:54:25 -05001963 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05001964}
egdaniel9cb63402016-06-23 08:37:05 -07001965
Robert Phillips5b5d84c2018-08-09 15:12:18 -04001966void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
1967 if (buffer->asRTCommandBuffer()) {
1968 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
1969
1970 fCachedRTCommandBuffer->submit();
1971 fCachedRTCommandBuffer->reset();
1972 } else {
1973 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
1974
1975 fCachedTexCommandBuffer->submit();
1976 fCachedTexCommandBuffer->reset();
1977 }
1978}
1979
Greg Daniel6be35232017-03-01 17:01:09 -05001980GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07001981 VkFenceCreateInfo createInfo;
1982 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
1983 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1984 createInfo.pNext = nullptr;
1985 createInfo.flags = 0;
1986 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05001987
1988 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
1989 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
1990
1991 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07001992 return (GrFence)fence;
1993}
1994
Greg Daniel6be35232017-03-01 17:01:09 -05001995bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
1996 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
1997
1998 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07001999 return (VK_SUCCESS == result);
2000}
2001
2002void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002003 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2004}
2005
Greg Daniela5cb7812017-06-16 09:45:32 -04002006sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2007 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002008}
2009
Greg Daniel48661b82018-01-22 16:11:35 -05002010sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2011 GrResourceProvider::SemaphoreWrapType wrapType,
2012 GrWrapOwnership ownership) {
2013 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002014}
2015
Greg Daniel48661b82018-01-22 16:11:35 -05002016void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05002017 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2018
Greg Daniel48661b82018-01-22 16:11:35 -05002019 GrVkSemaphore::Resource* resource = vkSem->getResource();
2020 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002021 resource->ref();
2022 fSemaphoresToSignal.push_back(resource);
2023 }
Greg Daniela5cb7812017-06-16 09:45:32 -04002024
2025 if (flush) {
2026 this->submitCommandBuffer(kSkip_SyncQueue);
2027 }
Greg Daniel6be35232017-03-01 17:01:09 -05002028}
2029
Greg Daniel48661b82018-01-22 16:11:35 -05002030void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002031 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2032
Greg Daniel48661b82018-01-22 16:11:35 -05002033 GrVkSemaphore::Resource* resource = vkSem->getResource();
2034 if (resource->shouldWait()) {
2035 resource->ref();
2036 fSemaphoresToWaitOn.push_back(resource);
2037 }
jvanverth84741b32016-09-30 08:39:02 -07002038}
Brian Osman13dddce2017-05-09 13:19:50 -04002039
2040sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2041 SkASSERT(texture);
2042 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2043 vkTexture->setImageLayout(this,
2044 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2045 VK_ACCESS_SHADER_READ_BIT,
2046 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
2047 false);
2048 this->submitCommandBuffer(kSkip_SyncQueue);
2049
2050 // The image layout change serves as a barrier, so no semaphore is needed
2051 return nullptr;
2052}
Greg Danielf5d87582017-12-18 14:48:15 -05002053