blob: e8d62afab73118cc2e73a45a15f6973f29502a40 [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)
Greg Danielecddbc02018-08-30 16:39:34 -0400123 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500124 , fResourceProvider(this)
125 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000126 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700127
Greg Daniel81df0412018-05-31 13:13:33 -0400128 if (!fMemoryAllocator) {
129 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000130 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400131 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400132 }
133
ethannicholasb3058bd2016-07-01 08:22:01 -0700134 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700135
Greg Daniel92aef4b2018-08-02 13:55:49 -0400136 uint32_t instanceVersion = backendContext.fInstanceVersion ? backendContext.fInstanceVersion
137 : backendContext.fMinAPIVersion;
138
Greg Daniela0651ac2018-08-08 09:23:18 -0400139 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400140 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400141 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400142 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400143 } else if (backendContext.fDeviceFeatures) {
144 VkPhysicalDeviceFeatures2 features2;
145 features2.pNext = nullptr;
146 features2.features = *backendContext.fDeviceFeatures;
147 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
148 features2, instanceVersion, *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400149 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400150 VkPhysicalDeviceFeatures2 features;
151 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
152 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400153 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400154 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400155 }
156 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400157 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400158 }
159 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400160 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400161 }
162 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -0400163 features, instanceVersion, GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400164 }
jvanverth633b3562016-03-23 11:01:22 -0700165 fCaps.reset(SkRef(fVkCaps.get()));
166
Greg Danielf730c182018-07-02 20:15:37 +0000167 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
168 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700169
170 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700171 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
172 nullptr, // pNext
173 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
174 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
Greg Danielf730c182018-07-02 20:15:37 +0000175 backendContext.fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700176 };
halcanary9d524f22016-03-29 09:03:52 -0700177 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700178 &fCmdPool));
179
180 // must call this after creating the CommandPool
181 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700182 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700183 SkASSERT(fCurrentCmdBuffer);
184 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500185}
186
Greg Daniel8606cf82017-05-08 16:17:53 -0400187void GrVkGpu::destroyResources() {
188 if (fCurrentCmdBuffer) {
189 fCurrentCmdBuffer->end(this);
190 fCurrentCmdBuffer->unref(this);
191 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500192
193 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700194 fResourceProvider.checkCommandBuffers();
Jim Van Verth09557d72016-11-07 11:10:21 -0500195 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700196
197 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
198 // on the command buffers even though they have completed. This causes an assert to fire when
199 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500200 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700201#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500202 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700203#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500204 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700205#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500206 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700207#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500208 }
egdanielf8c2be32016-06-24 13:18:27 -0700209#endif
210
egdanielbe9d8212016-09-20 08:54:23 -0700211#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400212 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700213#endif
halcanary9d524f22016-03-29 09:03:52 -0700214
Greg Daniel6be35232017-03-01 17:01:09 -0500215 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
216 fSemaphoresToWaitOn[i]->unref(this);
217 }
218 fSemaphoresToWaitOn.reset();
219
Greg Daniela5cb7812017-06-16 09:45:32 -0400220 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
221 fSemaphoresToSignal[i]->unref(this);
222 }
223 fSemaphoresToSignal.reset();
224
225
egdanielbc9b2962016-09-27 08:00:53 -0700226 fCopyManager.destroyResources(this);
227
Jim Van Verth09557d72016-11-07 11:10:21 -0500228 // must call this just before we destroy the command pool and VkDevice
229 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500230
Greg Daniel8606cf82017-05-08 16:17:53 -0400231 if (fCmdPool != VK_NULL_HANDLE) {
232 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
233 }
jvanverth633b3562016-03-23 11:01:22 -0700234
Greg Danielf730c182018-07-02 20:15:37 +0000235 fMemoryAllocator.reset();
236
237 fQueue = VK_NULL_HANDLE;
238 fDevice = VK_NULL_HANDLE;
239 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400240}
241
242GrVkGpu::~GrVkGpu() {
243 if (!fDisconnected) {
244 this->destroyResources();
245 }
246 delete fCompiler;
247}
248
249
250void GrVkGpu::disconnect(DisconnectType type) {
251 INHERITED::disconnect(type);
252 if (!fDisconnected) {
253 if (DisconnectType::kCleanup == type) {
254 this->destroyResources();
255 } else {
Greg Danieladb4bfe2018-08-23 16:15:05 -0400256 if (fCurrentCmdBuffer) {
257 fCurrentCmdBuffer->unrefAndAbandon();
258 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400259 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
260 fSemaphoresToWaitOn[i]->unrefAndAbandon();
261 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400262 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
263 fSemaphoresToSignal[i]->unrefAndAbandon();
264 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400265 fCopyManager.abandonResources();
266
267 // must call this just before we destroy the command pool and VkDevice
268 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400269
270 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400271 }
272 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400273 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400274 fCurrentCmdBuffer = nullptr;
275 fCmdPool = VK_NULL_HANDLE;
276 fDisconnected = true;
277 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500278}
279
280///////////////////////////////////////////////////////////////////////////////
281
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400282GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -0400283 GrRenderTarget* rt, GrSurfaceOrigin origin,
Greg Daniel500d58b2017-08-24 15:59:33 -0400284 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
285 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400286 if (!fCachedRTCommandBuffer) {
287 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
288 }
289
290 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
291 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400292}
293
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400294GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
295 if (!fCachedTexCommandBuffer) {
296 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
297 }
298
299 fCachedTexCommandBuffer->set(texture, origin);
300 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700301}
302
Greg Daniela5cb7812017-06-16 09:45:32 -0400303void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500304 SkASSERT(fCurrentCmdBuffer);
305 fCurrentCmdBuffer->end(this);
306
Greg Daniela5cb7812017-06-16 09:45:32 -0400307 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500308
309 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
310 fSemaphoresToWaitOn[i]->unref(this);
311 }
312 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400313 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
314 fSemaphoresToSignal[i]->unref(this);
315 }
316 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500317
Greg Daniel164a9f02016-02-22 09:56:40 -0500318 fResourceProvider.checkCommandBuffers();
319
320 // Release old command buffer and create a new one
321 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700322 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500323 SkASSERT(fCurrentCmdBuffer);
324
325 fCurrentCmdBuffer->begin(this);
326}
327
328///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700329GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
330 const void* data) {
331 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700332 switch (type) {
333 case kVertex_GrBufferType:
334 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
335 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700336 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700337 break;
cdalton397536c2016-03-25 12:15:03 -0700338 case kIndex_GrBufferType:
339 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
340 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700341 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700342 break;
cdalton397536c2016-03-25 12:15:03 -0700343 case kXferCpuToGpu_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::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700347 break;
cdalton397536c2016-03-25 12:15:03 -0700348 case kXferGpuToCpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400349 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
350 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700351 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700352 break;
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400353 case kDrawIndirect_GrBufferType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400354 SK_ABORT("DrawIndirect Buffers not supported in vulkan backend.");
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400355 return nullptr;
cdalton397536c2016-03-25 12:15:03 -0700356 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400357 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700358 return nullptr;
359 }
cdalton1bf3e712016-04-19 10:00:02 -0700360 if (data && buff) {
361 buff->updateData(data, size);
362 }
363 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500364}
365
Brian Salomona9b04b92018-06-01 15:04:28 -0400366bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
367 GrColorType srcColorType, const GrMipLevel texels[],
368 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500369 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
370 if (!vkTex) {
371 return false;
372 }
373
jvanverth900bd4a2016-04-29 13:53:12 -0700374 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400375 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800376 return false;
377 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800378
Greg Daniel164a9f02016-02-22 09:56:40 -0500379 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400380 bool linearTiling = vkTex->isLinearTiled();
381 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400382 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400383 SkDebugf("Can't upload mipmap data to linear tiled texture");
384 return false;
385 }
386 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
387 // Need to change the layout to general in order to perform a host write
388 vkTex->setImageLayout(this,
389 VK_IMAGE_LAYOUT_GENERAL,
390 VK_ACCESS_HOST_WRITE_BIT,
391 VK_PIPELINE_STAGE_HOST_BIT,
392 false);
393 this->submitCommandBuffer(kForce_SyncQueue);
394 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400395 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400396 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500397 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400398 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400399 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
400 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500401 }
egdaniel4583ec52016-06-27 12:57:00 -0700402
jvanverth900bd4a2016-04-29 13:53:12 -0700403 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500404}
405
Brian Salomonc320b152018-02-20 14:05:36 -0500406bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
407 GrColorType bufferColorType, GrBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400408 size_t bufferOffset, size_t rowBytes) {
409 // Vulkan only supports 4-byte aligned offsets
410 if (SkToBool(bufferOffset & 0x2)) {
411 return false;
412 }
413 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
414 if (!vkTex) {
415 return false;
416 }
417 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
418 if (!vkBuffer) {
419 return false;
420 }
421
Greg Daniel660cc992017-06-26 14:55:05 -0400422 SkDEBUGCODE(
423 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
424 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
425 SkASSERT(bounds.contains(subRect));
426 )
Brian Salomonc320b152018-02-20 14:05:36 -0500427 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400428 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500429 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400430 }
431
432 // Set up copy region
433 VkBufferImageCopy region;
434 memset(&region, 0, sizeof(VkBufferImageCopy));
435 region.bufferOffset = bufferOffset;
436 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
437 region.bufferImageHeight = 0;
438 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
439 region.imageOffset = { left, top, 0 };
440 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
441
442 // Change layout of our target so it can be copied to
443 vkTex->setImageLayout(this,
444 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
445 VK_ACCESS_TRANSFER_WRITE_BIT,
446 VK_PIPELINE_STAGE_TRANSFER_BIT,
447 false);
448
449 // Copy the buffer to the image
450 fCurrentCmdBuffer->copyBufferToImage(this,
451 vkBuffer,
452 vkTex,
453 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
454 1,
455 &region);
456
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400457 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400458 return true;
459}
460
Brian Salomon1fabd512018-02-09 09:54:25 -0500461void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
462 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700463 SkASSERT(dst);
464 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
465
egdanielfd016d72016-09-27 12:13:05 -0700466 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
467 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
468 }
469
egdaniel4bcd62e2016-08-31 07:37:31 -0700470 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500471 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
472 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
473 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
474 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
475 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700476
Greg Danielbc26c392017-04-18 13:32:10 -0400477 GrVkImage* dstImage;
478 GrRenderTarget* dstRT = dst->asRenderTarget();
479 if (dstRT) {
480 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400481 dstImage = vkRT;
482 } else {
483 SkASSERT(dst->asTexture());
484 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
485 }
486 dstImage->setImageLayout(this,
487 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
488 VK_ACCESS_TRANSFER_WRITE_BIT,
489 VK_PIPELINE_STAGE_TRANSFER_BIT,
490 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700491
492 src->msaaImage()->setImageLayout(this,
493 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
494 VK_ACCESS_TRANSFER_READ_BIT,
495 VK_PIPELINE_STAGE_TRANSFER_BIT,
496 false);
497
Greg Danielbc26c392017-04-18 13:32:10 -0400498 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700499}
500
Brian Salomon1fabd512018-02-09 09:54:25 -0500501void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700502 if (target->needsResolve()) {
503 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700504 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
505 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500506
egdaniel4bcd62e2016-08-31 07:37:31 -0700507 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700508
Brian Salomon1fabd512018-02-09 09:54:25 -0500509 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700510
511 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500512
513 if (requiresSubmit) {
514 this->submitCommandBuffer(kSkip_SyncQueue);
515 }
egdaniel52ad2512016-08-04 12:50:01 -0700516 }
517}
518
Brian Salomona9b04b92018-06-01 15:04:28 -0400519bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
520 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500521 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700522 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500523
Greg Daniel660cc992017-06-26 14:55:05 -0400524 SkDEBUGCODE(
525 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
526 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
527 SkASSERT(bounds.contains(subRect));
528 )
Brian Salomonc320b152018-02-20 14:05:36 -0500529 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500530 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400531 if (!rowBytes) {
532 rowBytes = trimRowBytes;
533 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500534
jvanverth900bd4a2016-04-29 13:53:12 -0700535 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
536 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
537 const VkImageSubresource subres = {
538 VK_IMAGE_ASPECT_COLOR_BIT,
539 0, // mipLevel
540 0, // arraySlice
541 };
542 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500543
jvanverth900bd4a2016-04-29 13:53:12 -0700544 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500545
jvanverth900bd4a2016-04-29 13:53:12 -0700546 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700547 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700548 &subres,
549 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500550
jvanverth1e305ba2016-06-01 09:39:15 -0700551 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400552 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700553 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400554 SkASSERT(size + offset <= alloc.fSize);
555 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
556 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700557 return false;
558 }
Greg Daniel81df0412018-05-31 13:13:33 -0400559 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700560
Brian Salomona9b04b92018-06-01 15:04:28 -0400561 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
562 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700563
Greg Daniele35a99e2018-03-02 11:44:22 -0500564 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400565 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700566
567 return true;
568}
569
Brian Salomona9b04b92018-06-01 15:04:28 -0400570bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
571 GrColorType dataColorType, const GrMipLevel texels[],
572 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700573 SkASSERT(!tex->isLinearTiled());
574 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400575 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700576 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
577
Greg Danieldd20e912017-04-07 14:42:23 -0400578 // We assume that if the texture has mip levels, we either upload to all the levels or just the
579 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400580 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400581
jvanverth900bd4a2016-04-29 13:53:12 -0700582 if (width == 0 || height == 0) {
583 return false;
584 }
585
Brian Salomond1eaf492017-05-18 10:02:08 -0400586 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500587 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700588
589 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700590 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
591 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400592 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
593
594 if (mipLevelCount) {
595 texelsShallowCopy.reset(mipLevelCount);
596 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
597 }
jvanverth900bd4a2016-04-29 13:53:12 -0700598
Robert Phillips590533f2017-07-11 14:22:35 -0400599 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700600 individualMipOffsets.push_back(0);
601 size_t combinedBufferSize = width * bpp * height;
602 int currentWidth = width;
603 int currentHeight = height;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400604 if (mipLevelCount > 0 && !texelsShallowCopy[0].fPixels) {
605 combinedBufferSize = 0;
606 }
607
Greg Daniel468fd632017-03-22 17:03:45 -0400608 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
609 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
610 SkASSERT((bpp & (bpp - 1)) == 0);
611 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400612 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700613 currentWidth = SkTMax(1, currentWidth/2);
614 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400615
Greg Daniel55afd6d2017-09-29 09:32:44 -0400616 if (texelsShallowCopy[currentMipLevel].fPixels) {
617 const size_t trimmedSize = currentWidth * bpp * currentHeight;
618 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
619 if (alignmentDiff != 0) {
620 combinedBufferSize += alignmentMask - alignmentDiff + 1;
621 }
622 individualMipOffsets.push_back(combinedBufferSize);
623 combinedBufferSize += trimmedSize;
624 } else {
625 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400626 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400627 }
628 if (0 == combinedBufferSize) {
629 // We don't actually have any data to upload so just return success
630 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700631 }
632
633 // allocate buffer to hold our mip data
634 GrVkTransferBuffer* transferBuffer =
635 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel6888c0d2017-08-25 11:55:50 -0400636 if(!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700637 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400638 }
jvanverth900bd4a2016-04-29 13:53:12 -0700639
640 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400641 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700642
jvanverthc578b0632016-05-02 10:58:12 -0700643 currentWidth = width;
644 currentHeight = height;
Greg Daniela1b282b2017-03-28 14:56:46 -0400645 int layerHeight = tex->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400646 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400647 if (texelsShallowCopy[currentMipLevel].fPixels) {
648 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
649 const size_t trimRowBytes = currentWidth * bpp;
650 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
651 ? texelsShallowCopy[currentMipLevel].fRowBytes
652 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700653
Greg Daniel55afd6d2017-09-29 09:32:44 -0400654 // copy data into the buffer, skipping the trailing bytes
655 char* dst = buffer + individualMipOffsets[currentMipLevel];
656 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400657 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400658
659 VkBufferImageCopy& region = regions.push_back();
660 memset(&region, 0, sizeof(VkBufferImageCopy));
661 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
662 region.bufferRowLength = currentWidth;
663 region.bufferImageHeight = currentHeight;
664 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Brian Salomona9b04b92018-06-01 15:04:28 -0400665 region.imageOffset = {left, top, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400666 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700667 }
jvanverthc578b0632016-05-02 10:58:12 -0700668 currentWidth = SkTMax(1, currentWidth/2);
669 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400670 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700671 }
672
jvanverth9d54afc2016-09-20 09:20:03 -0700673 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700674 transferBuffer->unmap();
675
jvanverth900bd4a2016-04-29 13:53:12 -0700676 // Change layout of our target so it can be copied to
jvanverth900bd4a2016-04-29 13:53:12 -0700677 tex->setImageLayout(this,
678 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -0700679 VK_ACCESS_TRANSFER_WRITE_BIT,
680 VK_PIPELINE_STAGE_TRANSFER_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700681 false);
682
683 // Copy the buffer to the image
684 fCurrentCmdBuffer->copyBufferToImage(this,
685 transferBuffer,
686 tex,
687 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
688 regions.count(),
689 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700690 transferBuffer->unref();
Robert Phillips590533f2017-07-11 14:22:35 -0400691 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400692 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400693 }
jvanverth900bd4a2016-04-29 13:53:12 -0700694
Greg Daniel164a9f02016-02-22 09:56:40 -0500695 return true;
696}
697
698////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400699sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500700 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500701 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
702
703 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500704 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700705
Greg Daniel164a9f02016-02-22 09:56:40 -0500706 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
707 if (renderTarget) {
708 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
709 }
710
711 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
712 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
713 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700714 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500715 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
716 // texture.
717 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
718
Greg Daniel164a9f02016-02-22 09:56:40 -0500719 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700720 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500721 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400722 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500723 GrVkImage::ImageDesc imageDesc;
724 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
725 imageDesc.fFormat = pixelFormat;
726 imageDesc.fWidth = desc.fWidth;
727 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400728 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500729 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400730 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500731 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400732 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500733
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400734 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
735 if (mipLevels > 1) {
736 mipMapsStatus = GrMipMapsStatus::kValid;
737 for (int i = 0; i < mipLevels; ++i) {
738 if (!texels[i].fPixels) {
739 mipMapsStatus = GrMipMapsStatus::kDirty;
740 break;
741 }
Greg Daniel834f1202017-10-09 15:06:20 -0400742 }
743 }
744
Robert Phillips67d52cf2017-06-05 13:38:13 -0400745 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500746 if (renderTarget) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700747 tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
Greg Daniel834f1202017-10-09 15:06:20 -0400748 imageDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400749 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500750 } else {
Greg Daniel834f1202017-10-09 15:06:20 -0400751 tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400752 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500753 }
754
755 if (!tex) {
756 return nullptr;
757 }
758
Brian Salomonc320b152018-02-20 14:05:36 -0500759 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400760 if (mipLevelCount) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400761 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
762 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500763 tex->unref();
764 return nullptr;
765 }
766 }
767
Brian Salomond17b4a62017-05-23 16:53:47 -0400768 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
769 VkClearColorValue zeroClearColor;
770 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
771 VkImageSubresourceRange range;
772 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
773 range.baseArrayLayer = 0;
774 range.baseMipLevel = 0;
775 range.layerCount = 1;
776 range.levelCount = 1;
777 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
778 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400779 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400780 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400781 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -0500782}
783
784////////////////////////////////////////////////////////////////////////////////
785
Greg Daniel6888c0d2017-08-25 11:55:50 -0400786void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
787 VkDeviceSize dstOffset, VkDeviceSize size) {
788 VkBufferCopy copyRegion;
789 copyRegion.srcOffset = srcOffset;
790 copyRegion.dstOffset = dstOffset;
791 copyRegion.size = size;
792 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
793}
794
jvanverthdb379092016-07-07 11:18:46 -0700795bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
796 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700797 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700798 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700799
800 return true;
801}
802
803////////////////////////////////////////////////////////////////////////////////
804
Greg Daniel5254ccc2017-11-13 11:05:52 -0500805static bool check_backend_texture(const GrBackendTexture& backendTex,
806 GrPixelConfig config) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400807 GrVkImageInfo info;
808 if (!backendTex.getVkImageInfo(&info)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400809 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500810 }
811
Greg Daniel52e16d92018-04-10 09:34:07 -0400812 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -0400813 return false;
jvanverthfd359ca2016-03-18 11:57:24 -0700814 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000815
Greg Daniel52e16d92018-04-10 09:34:07 -0400816 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -0400817 return true;
818}
819
820sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400821 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500822 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400823 return nullptr;
824 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500825
Greg Daniel164a9f02016-02-22 09:56:40 -0500826 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400827 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000828 surfDesc.fWidth = backendTex.width();
829 surfDesc.fHeight = backendTex.height();
830 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500831 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500832
Greg Daniel52e16d92018-04-10 09:34:07 -0400833 GrVkImageInfo imageInfo;
834 if (!backendTex.getVkImageInfo(&imageInfo)) {
835 return nullptr;
836 }
837 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
838 SkASSERT(layout);
839 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, imageInfo, std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -0400840}
841
842sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400843 int sampleCnt,
844 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500845 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400846 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500847 }
Brian Salomond17f6582017-07-19 18:28:58 -0400848
849 GrSurfaceDesc surfDesc;
850 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
851 surfDesc.fWidth = backendTex.width();
852 surfDesc.fHeight = backendTex.height();
853 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500854 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -0400855
Greg Daniel52e16d92018-04-10 09:34:07 -0400856 GrVkImageInfo imageInfo;
857 if (!backendTex.getVkImageInfo(&imageInfo)) {
858 return nullptr;
859 }
860 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
861 SkASSERT(layout);
862
Brian Salomond17f6582017-07-19 18:28:58 -0400863 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
Greg Daniel52e16d92018-04-10 09:34:07 -0400864 imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500865}
866
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400867sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -0400868 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
869 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
870 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
871 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500872 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -0400873 return nullptr;
874 }
halcanary9d524f22016-03-29 09:03:52 -0700875
Greg Daniel323fbcf2018-04-10 13:46:30 -0400876 GrVkImageInfo info;
877 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000878 return nullptr;
879 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400880
881 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700882 return nullptr;
883 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500884
Greg Daniel164a9f02016-02-22 09:56:40 -0500885 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400886 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400887 desc.fWidth = backendRT.width();
888 desc.fHeight = backendRT.height();
889 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500890 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500891
Greg Daniel323fbcf2018-04-10 13:46:30 -0400892 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -0400893
Greg Daniel323fbcf2018-04-10 13:46:30 -0400894 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -0400895 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -0500896
897 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
898 SkASSERT(!backendRT.stencilBits());
899 if (tgt) {
900 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -0500901 }
Brian Salomonafdc6b12018-03-09 12:02:32 -0500902
Ben Wagnerff134f22018-04-24 16:29:16 -0400903 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500904}
905
Greg Daniel7ef28f32017-04-20 16:41:55 +0000906sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000907 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400908
Greg Daniel52e16d92018-04-10 09:34:07 -0400909 GrVkImageInfo imageInfo;
910 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000911 return nullptr;
912 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400913 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -0400914 return nullptr;
915 }
916
917 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000918 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000919 desc.fWidth = tex.width();
920 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400921 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500922 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
923 if (!desc.fSampleCnt) {
924 return nullptr;
925 }
Brian Osman33910292017-04-18 14:38:53 -0400926
Greg Daniel52e16d92018-04-10 09:34:07 -0400927 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
928 SkASSERT(layout);
929
Ben Wagnerff134f22018-04-24 16:29:16 -0400930 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -0400931}
932
Brian Salomon930f9392018-06-20 16:25:26 -0400933bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
934 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -0700935 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -0400936 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700937 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -0400938 return false;
jvanverth62340062016-04-26 08:01:44 -0700939 }
940
jvanverth62340062016-04-26 08:01:44 -0700941 // determine if we can blit to and from this format
942 const GrVkCaps& caps = this->vkCaps();
943 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -0700944 !caps.configCanBeSrcofBlit(tex->config(), false) ||
945 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -0400946 return false;
jvanverth62340062016-04-26 08:01:44 -0700947 }
948
egdanielfd016d72016-09-27 12:13:05 -0700949 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
950 this->submitCommandBuffer(kSkip_SyncQueue);
951 }
952
egdaniel7ac5da82016-07-15 13:41:42 -0700953 int width = tex->width();
954 int height = tex->height();
955 VkImageBlit blitRegion;
956 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -0700957
jvanverth82c05582016-05-03 11:19:01 -0700958 // SkMipMap doesn't include the base level in the level count so we have to add 1
959 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -0400960 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -0700961
Greg Danielda86e282018-06-13 09:41:19 -0400962 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -0400963 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
964 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -0700965
jvanverth50c46c72016-05-06 12:31:28 -0700966 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -0400967 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -0700968 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
969 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -0400970 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
971 nullptr, // pNext
972 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
973 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
974 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
975 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
976 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
977 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
978 vkTex->image(), // image
979 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -0700980 };
981
jvanverth62340062016-04-26 08:01:44 -0700982 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -0700983 uint32_t mipLevel = 1;
984 while (mipLevel < levelCount) {
985 int prevWidth = width;
986 int prevHeight = height;
987 width = SkTMax(1, width / 2);
988 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -0700989
jvanverth50c46c72016-05-06 12:31:28 -0700990 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
991 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
992 false, &imageMemoryBarrier);
993
994 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700995 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700996 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700997 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
998 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700999 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001000 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001001 vkTex->resource(),
1002 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001003 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001004 vkTex->resource(),
1005 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001006 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001007 1,
1008 &blitRegion,
1009 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001010 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001011 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001012 // This barrier logically is not needed, but it changes the final level to the same layout as
1013 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1014 // future layout changes easier.
1015 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1016 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1017 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001018 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1019 return true;
jvanverth62340062016-04-26 08:01:44 -07001020}
1021
Greg Daniel164a9f02016-02-22 09:56:40 -05001022////////////////////////////////////////////////////////////////////////////////
1023
1024GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1025 int width,
1026 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001027 SkASSERT(width >= rt->width());
1028 SkASSERT(height >= rt->height());
1029
1030 int samples = rt->numStencilSamples();
1031
egdaniel8f1dcaa2016-04-01 10:10:45 -07001032 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001033
1034 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001035 width,
1036 height,
1037 samples,
1038 sFmt));
1039 fStats.incStencilAttachmentCreates();
1040 return stencil;
1041}
1042
1043////////////////////////////////////////////////////////////////////////////////
1044
Brian Salomon52e943a2018-03-13 09:32:39 -04001045bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
1046 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001047 VkDeviceSize size = dstRowBytes * h;
1048 VkDeviceSize offset = bufferOffset;
1049 SkASSERT(size + offset <= alloc.fSize);
1050 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1051 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001052 return false;
1053 }
Greg Daniel81df0412018-05-31 13:13:33 -04001054 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001055
Greg Daniel20ece3a2017-03-28 10:24:43 -04001056 if (srcData) {
1057 // If there is no padding on dst we can do a single memcopy.
1058 // This assumes the srcData comes in with no padding.
1059 SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes),
1060 srcData, srcRowBytes, srcRowBytes, h);
1061 } else {
1062 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1063 // with some data.
1064 if (srcRowBytes == static_cast<size_t>(dstRowBytes)) {
1065 memset(mapPtr, 0, srcRowBytes * h);
1066 } else {
1067 for (int i = 0; i < h; ++i) {
1068 memset(mapPtr, 0, srcRowBytes);
1069 mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes));
1070 }
1071 }
1072 }
Greg Daniel81df0412018-05-31 13:13:33 -04001073 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1074 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001075 return true;
1076}
1077
Brian Salomonf865b052018-03-09 09:01:53 -05001078#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001079bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1080 bool renderable, GrMipMapped mipMapped, const void* srcData,
1081 GrVkImageInfo* info) {
1082 SkASSERT(texturable || renderable);
1083 if (!texturable) {
1084 SkASSERT(GrMipMapped::kNo == mipMapped);
1085 SkASSERT(!srcData);
1086 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001087 VkFormat pixelFormat;
1088 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001089 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001090 }
1091
Brian Salomon52e943a2018-03-13 09:32:39 -04001092 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1093 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001094 }
1095
Brian Salomon52e943a2018-03-13 09:32:39 -04001096 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1097 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001098 }
1099
1100 // Currently we don't support uploading pixel data when mipped.
1101 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001102 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001103 }
1104
Brian Salomon52e943a2018-03-13 09:32:39 -04001105 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001106 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1107 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001108 if (texturable) {
1109 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1110 }
1111 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001112 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1113 }
1114
1115 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001116 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001117 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001118
1119 // Create Image
1120 VkSampleCountFlagBits vkSamples;
1121 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001122 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001123 }
1124
1125 // Figure out the number of mip levels.
1126 uint32_t mipLevels = 1;
1127 if (GrMipMapped::kYes == mipMapped) {
1128 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1129 }
1130
1131 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001132 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1133 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001134 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001135 VK_IMAGE_TYPE_2D, // VkImageType
1136 pixelFormat, // VkFormat
1137 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1138 mipLevels, // mipLevels
1139 1, // arrayLayers
1140 vkSamples, // samples
1141 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1142 usageFlags, // VkImageUsageFlags
1143 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1144 0, // queueFamilyCount
1145 0, // pQueueFamilyIndices
1146 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001147 };
1148
Brian Salomon52e943a2018-03-13 09:32:39 -04001149 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1150 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001151
Brian Salomonde9f5462018-03-07 14:23:58 -05001152 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001153 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001154 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001155 }
1156
1157 // 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 -05001158 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001159 VkBuffer buffer = VK_NULL_HANDLE;
1160
1161 VkResult err;
1162 const VkCommandBufferAllocateInfo cmdInfo = {
1163 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1164 nullptr, // pNext
1165 fCmdPool, // commandPool
1166 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1167 1 // bufferCount
1168 };
1169
1170 VkCommandBuffer cmdBuffer;
1171 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1172 if (err) {
1173 GrVkMemory::FreeImageMemory(this, false, alloc);
1174 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001175 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001176 }
1177
1178 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1179 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1180 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1181 cmdBufferBeginInfo.pNext = nullptr;
1182 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1183 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1184
1185 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1186 SkASSERT(!err);
1187
1188 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001189 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001190
Brian Salomonde9f5462018-03-07 14:23:58 -05001191 SkTArray<size_t> individualMipOffsets(mipLevels);
1192 individualMipOffsets.push_back(0);
1193 size_t combinedBufferSize = w * bpp * h;
1194 int currentWidth = w;
1195 int currentHeight = h;
1196 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1197 // config. This works with the assumption that the bytes in pixel config is always a power
1198 // of 2.
1199 SkASSERT((bpp & (bpp - 1)) == 0);
1200 const size_t alignmentMask = 0x3 | (bpp - 1);
1201 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1202 currentWidth = SkTMax(1, currentWidth / 2);
1203 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001204
Brian Salomonde9f5462018-03-07 14:23:58 -05001205 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1206 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1207 if (alignmentDiff != 0) {
1208 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001209 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001210 individualMipOffsets.push_back(combinedBufferSize);
1211 combinedBufferSize += trimmedSize;
1212 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001213
Brian Salomonde9f5462018-03-07 14:23:58 -05001214 VkBufferCreateInfo bufInfo;
1215 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1216 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1217 bufInfo.flags = 0;
1218 bufInfo.size = combinedBufferSize;
1219 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1220 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1221 bufInfo.queueFamilyIndexCount = 0;
1222 bufInfo.pQueueFamilyIndices = nullptr;
1223 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001224
Brian Salomonde9f5462018-03-07 14:23:58 -05001225 if (err) {
1226 GrVkMemory::FreeImageMemory(this, false, alloc);
1227 VK_CALL(DestroyImage(fDevice, image, nullptr));
1228 VK_CALL(EndCommandBuffer(cmdBuffer));
1229 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001230 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001231 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001232
Brian Salomonde9f5462018-03-07 14:23:58 -05001233 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1234 &bufferAlloc)) {
1235 GrVkMemory::FreeImageMemory(this, false, alloc);
1236 VK_CALL(DestroyImage(fDevice, image, nullptr));
1237 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1238 VK_CALL(EndCommandBuffer(cmdBuffer));
1239 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001240 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001241 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001242
Brian Salomonde9f5462018-03-07 14:23:58 -05001243 currentWidth = w;
1244 currentHeight = h;
1245 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1246 SkASSERT(0 == currentMipLevel || !srcData);
1247 size_t currentRowBytes = bpp * currentWidth;
1248 size_t bufferOffset = individualMipOffsets[currentMipLevel];
1249 if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1250 currentRowBytes, currentHeight)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001251 GrVkMemory::FreeImageMemory(this, false, alloc);
1252 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001253 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001254 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1255 VK_CALL(EndCommandBuffer(cmdBuffer));
1256 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001257 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001258 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001259 currentWidth = SkTMax(1, currentWidth / 2);
1260 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001261 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001262
1263 // Set image layout and add barrier
1264 VkImageMemoryBarrier barrier;
1265 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1266 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1267 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001268 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001269 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1270 barrier.oldLayout = initialLayout;
1271 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1272 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1273 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1274 barrier.image = image;
1275 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1276
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001277 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001278 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1279 &barrier));
1280 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1281
1282 SkTArray<VkBufferImageCopy> regions(mipLevels);
1283
1284 currentWidth = w;
1285 currentHeight = h;
1286 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1287 // Submit copy command
1288 VkBufferImageCopy& region = regions.push_back();
1289 memset(&region, 0, sizeof(VkBufferImageCopy));
1290 region.bufferOffset = individualMipOffsets[currentMipLevel];
1291 region.bufferRowLength = currentWidth;
1292 region.bufferImageHeight = currentHeight;
1293 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1294 region.imageOffset = {0, 0, 0};
1295 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1296 currentWidth = SkTMax(1, currentWidth / 2);
1297 currentHeight = SkTMax(1, currentHeight / 2);
1298 }
1299
1300 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1301 regions.begin()));
1302
Brian Salomon52e943a2018-03-13 09:32:39 -04001303 if (texturable) {
1304 // Change Image layout to shader read since if we use this texture as a borrowed textures
1305 // within Ganesh we require that its layout be set to that
1306 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1307 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1308 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001309 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001310 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1311 barrier.oldLayout = initialLayout;
1312 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1313 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1314 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1315 barrier.image = image;
1316 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001317 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001318 GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomon52e943a2018-03-13 09:32:39 -04001319 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1320 0,
1321 0, nullptr,
1322 0, nullptr,
1323 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001324 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001325 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001326
1327 // End CommandBuffer
1328 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1329 SkASSERT(!err);
1330
1331 // Create Fence for queue
1332 VkFence fence;
1333 VkFenceCreateInfo fenceInfo;
1334 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1335 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1336
1337 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1338 SkASSERT(!err);
1339
1340 VkSubmitInfo submitInfo;
1341 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1342 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1343 submitInfo.pNext = nullptr;
1344 submitInfo.waitSemaphoreCount = 0;
1345 submitInfo.pWaitSemaphores = nullptr;
1346 submitInfo.pWaitDstStageMask = 0;
1347 submitInfo.commandBufferCount = 1;
1348 submitInfo.pCommandBuffers = &cmdBuffer;
1349 submitInfo.signalSemaphoreCount = 0;
1350 submitInfo.pSignalSemaphores = nullptr;
1351 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1352 SkASSERT(!err);
1353
1354 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1355 if (VK_TIMEOUT == err) {
1356 GrVkMemory::FreeImageMemory(this, false, alloc);
1357 VK_CALL(DestroyImage(fDevice, image, nullptr));
1358 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1359 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1360 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1361 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1362 SkDebugf("Fence failed to signal: %d\n", err);
1363 SK_ABORT("failing");
1364 }
1365 SkASSERT(!err);
1366
1367 // Clean up transfer resources
1368 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1369 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1370 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1371 }
1372 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1373 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1374
Brian Salomon52e943a2018-03-13 09:32:39 -04001375 info->fImage = image;
1376 info->fAlloc = alloc;
1377 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1378 info->fImageLayout = initialLayout;
1379 info->fFormat = pixelFormat;
1380 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001381
Brian Salomon52e943a2018-03-13 09:32:39 -04001382 return true;
1383}
1384
1385GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
1386 GrPixelConfig config, bool isRenderTarget,
1387 GrMipMapped mipMapped) {
Brian Salomon8a375832018-03-14 10:21:40 -04001388 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001389
1390 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1391 return GrBackendTexture();
1392 }
1393
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001394 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001395 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
1396 &info)) {
1397 return {};
1398 }
Greg Daniel108bb232018-07-03 16:18:29 -04001399 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1400 // Lots of tests don't go through Skia's public interface which will set the config so for
1401 // testing we make sure we set a config here.
1402 beTex.setPixelConfig(config);
1403 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001404}
1405
1406bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
1407 SkASSERT(kVulkan_GrBackend == tex.fBackend);
1408
Greg Daniel52e16d92018-04-10 09:34:07 -04001409 GrVkImageInfo backend;
1410 if (!tex.getVkImageInfo(&backend)) {
1411 return false;
1412 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001413
Greg Daniel52e16d92018-04-10 09:34:07 -04001414 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001415 VkMemoryRequirements req;
1416 memset(&req, 0, sizeof(req));
1417 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001418 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001419 &req));
1420 // TODO: find a better check
1421 // This will probably fail with a different driver
1422 return (req.size > 0) && (req.size <= 8192 * 8192);
1423 }
1424
1425 return false;
1426}
1427
Brian Salomon26102cb2018-03-09 09:33:19 -05001428void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
1429 SkASSERT(kVulkan_GrBackend == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001430
Greg Daniel52e16d92018-04-10 09:34:07 -04001431 GrVkImageInfo info;
1432 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001433 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001434 }
1435}
1436
Brian Osman2d010b62018-08-09 10:55:09 -04001437GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001438 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1439 return GrBackendRenderTarget();
1440 }
1441
Brian Salomon8a375832018-03-14 10:21:40 -04001442 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001443 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001444 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001445 if (kUnknown_GrPixelConfig == config) {
1446 return {};
1447 }
1448 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr,
1449 &info)) {
1450 return {};
1451 }
Greg Daniel108bb232018-07-03 16:18:29 -04001452 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1453 // Lots of tests don't go through Skia's public interface which will set the config so for
1454 // testing we make sure we set a config here.
1455 beRT.setPixelConfig(config);
1456 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001457}
1458
Brian Salomon52e943a2018-03-13 09:32:39 -04001459void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1460 SkASSERT(kVulkan_GrBackend == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001461
Greg Daniel323fbcf2018-04-10 13:46:30 -04001462 GrVkImageInfo info;
1463 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001464 // something in the command buffer may still be using this, so force submit
1465 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001466 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001467 }
1468}
Brian Salomonf865b052018-03-09 09:01:53 -05001469
Greg Daniel26b50a42018-03-08 09:49:58 -05001470void GrVkGpu::testingOnly_flushGpuAndSync() {
1471 this->submitCommandBuffer(kForce_SyncQueue);
1472}
Brian Salomonf865b052018-03-09 09:01:53 -05001473#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001474
Greg Daniel164a9f02016-02-22 09:56:40 -05001475////////////////////////////////////////////////////////////////////////////////
1476
1477void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1478 VkPipelineStageFlags dstStageMask,
1479 bool byRegion,
1480 VkMemoryBarrier* barrier) const {
1481 SkASSERT(fCurrentCmdBuffer);
1482 fCurrentCmdBuffer->pipelineBarrier(this,
1483 srcStageMask,
1484 dstStageMask,
1485 byRegion,
1486 GrVkCommandBuffer::kMemory_BarrierType,
1487 barrier);
1488}
1489
1490void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1491 VkPipelineStageFlags dstStageMask,
1492 bool byRegion,
1493 VkBufferMemoryBarrier* barrier) const {
1494 SkASSERT(fCurrentCmdBuffer);
1495 fCurrentCmdBuffer->pipelineBarrier(this,
1496 srcStageMask,
1497 dstStageMask,
1498 byRegion,
1499 GrVkCommandBuffer::kBufferMemory_BarrierType,
1500 barrier);
1501}
1502
1503void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1504 VkPipelineStageFlags dstStageMask,
1505 bool byRegion,
1506 VkImageMemoryBarrier* barrier) const {
1507 SkASSERT(fCurrentCmdBuffer);
1508 fCurrentCmdBuffer->pipelineBarrier(this,
1509 srcStageMask,
1510 dstStageMask,
1511 byRegion,
1512 GrVkCommandBuffer::kImageMemory_BarrierType,
1513 barrier);
1514}
1515
Greg Daniel51316782017-08-02 15:10:09 +00001516void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1517 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1518 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001519 this->submitCommandBuffer(kSkip_SyncQueue);
1520}
1521
Robert Phillips95214472017-08-08 18:00:03 -04001522void GrVkGpu::clearStencil(GrRenderTarget* target, int clearValue) {
1523 if (!target) {
egdaniel3d5d9ac2016-03-01 12:56:15 -08001524 return;
1525 }
1526 GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
1527 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
1528
1529
1530 VkClearDepthStencilValue vkStencilColor;
Robert Phillips95214472017-08-08 18:00:03 -04001531 vkStencilColor.depth = 0.0f;
1532 vkStencilColor.stencil = clearValue;
egdaniel3d5d9ac2016-03-01 12:56:15 -08001533
egdaniel3d5d9ac2016-03-01 12:56:15 -08001534 vkStencil->setImageLayout(this,
1535 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001536 VK_ACCESS_TRANSFER_WRITE_BIT,
1537 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel3d5d9ac2016-03-01 12:56:15 -08001538 false);
1539
egdaniel3d5d9ac2016-03-01 12:56:15 -08001540 VkImageSubresourceRange subRange;
1541 memset(&subRange, 0, sizeof(VkImageSubresourceRange));
1542 subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1543 subRange.baseMipLevel = 0;
1544 subRange.levelCount = 1;
1545 subRange.baseArrayLayer = 0;
1546 subRange.layerCount = 1;
1547
1548 // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
1549 // draw. Thus we should look into using the load op functions on the render pass to clear out
1550 // the stencil there.
1551 fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
1552}
1553
Greg Daniel25af6712018-04-25 10:44:38 -04001554static int get_surface_sample_cnt(GrSurface* surf) {
1555 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1556 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001557 }
Greg Daniel25af6712018-04-25 10:44:38 -04001558 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001559}
1560
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001561void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1562 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001563 GrVkImage* dstImage,
1564 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001565 const SkIRect& srcRect,
1566 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001567#ifdef SK_DEBUG
1568 int dstSampleCnt = get_surface_sample_cnt(dst);
1569 int srcSampleCnt = get_surface_sample_cnt(src);
1570 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1571 src->config(), srcSampleCnt, srcOrigin));
1572
1573#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001574
Greg Daniel164a9f02016-02-22 09:56:40 -05001575 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1576 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001577 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001578 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1579 VK_ACCESS_TRANSFER_WRITE_BIT,
1580 VK_PIPELINE_STAGE_TRANSFER_BIT,
1581 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001582
egdaniel17b89252016-04-05 07:23:38 -07001583 srcImage->setImageLayout(this,
1584 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001585 VK_ACCESS_TRANSFER_READ_BIT,
1586 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001587 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001588
1589 // Flip rect if necessary
1590 SkIRect srcVkRect = srcRect;
1591 int32_t dstY = dstPoint.fY;
1592
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001593 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1594 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001595 srcVkRect.fTop = src->height() - srcRect.fBottom;
1596 srcVkRect.fBottom = src->height() - srcRect.fTop;
1597 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1598 }
1599
1600 VkImageCopy copyRegion;
1601 memset(&copyRegion, 0, sizeof(VkImageCopy));
1602 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1603 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1604 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1605 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001606 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001607
1608 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001609 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001610 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001611 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001612 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1613 1,
1614 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001615
1616 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1617 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001618 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001619}
1620
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001621void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1622 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001623 GrVkImage* dstImage,
1624 GrVkImage* srcImage,
1625 const SkIRect& srcRect,
1626 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001627#ifdef SK_DEBUG
1628 int dstSampleCnt = get_surface_sample_cnt(dst);
1629 int srcSampleCnt = get_surface_sample_cnt(src);
1630 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1631 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001632
Greg Daniel25af6712018-04-25 10:44:38 -04001633#endif
egdaniel17b89252016-04-05 07:23:38 -07001634 dstImage->setImageLayout(this,
1635 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001636 VK_ACCESS_TRANSFER_WRITE_BIT,
1637 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001638 false);
1639
egdaniel17b89252016-04-05 07:23:38 -07001640 srcImage->setImageLayout(this,
1641 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001642 VK_ACCESS_TRANSFER_READ_BIT,
1643 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001644 false);
1645
1646 // Flip rect if necessary
1647 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001648 srcVkRect.fLeft = srcRect.fLeft;
1649 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001650 SkIRect dstRect;
1651 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001652 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001653
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001654 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001655 srcVkRect.fTop = src->height() - srcRect.fBottom;
1656 srcVkRect.fBottom = src->height() - srcRect.fTop;
1657 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001658 srcVkRect.fTop = srcRect.fTop;
1659 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001660 }
1661
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001662 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001663 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1664 } else {
1665 dstRect.fTop = dstPoint.fY;
1666 }
1667 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1668
1669 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1670 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001671 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001672 using std::swap;
1673 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001674 }
1675
1676 VkImageBlit blitRegion;
1677 memset(&blitRegion, 0, sizeof(VkImageBlit));
1678 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1679 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001680 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001681 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1682 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001683 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001684
1685 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001686 *srcImage,
1687 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001688 1,
1689 &blitRegion,
1690 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001691
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001692 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001693 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001694}
1695
Brian Salomon1fabd512018-02-09 09:54:25 -05001696void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1697 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1698 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001699 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001700 SkIRect srcRect = origSrcRect;
1701 SkIPoint dstPoint = origDstPoint;
1702 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1703 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1704 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1705 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1706 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1707 }
1708 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001709 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1710 srcRect.width(), srcRect.height());
1711 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001712}
1713
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001714bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1715 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001716 const SkIRect& srcRect, const SkIPoint& dstPoint,
1717 bool canDiscardOutsideDstRect) {
Greg Daniel25af6712018-04-25 10:44:38 -04001718 GrPixelConfig dstConfig = dst->config();
1719 GrPixelConfig srcConfig = src->config();
1720
1721 int dstSampleCnt = get_surface_sample_cnt(dst);
1722 int srcSampleCnt = get_surface_sample_cnt(src);
1723
1724 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1725 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001726 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001727 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001728 }
1729
egdanielfd016d72016-09-27 12:13:05 -07001730 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1731 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
1732 }
1733
Greg Daniel25af6712018-04-25 10:44:38 -04001734 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1735 srcConfig, SkToBool(src->asTexture()))) {
1736 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1737 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001738 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1739 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001740 return true;
1741 }
1742
egdaniel17b89252016-04-05 07:23:38 -07001743 GrVkImage* dstImage;
1744 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001745 GrRenderTarget* dstRT = dst->asRenderTarget();
1746 if (dstRT) {
1747 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1748 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1749 } else {
1750 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001751 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001752 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001753 GrRenderTarget* srcRT = src->asRenderTarget();
1754 if (srcRT) {
1755 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1756 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001757 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001758 SkASSERT(src->asTexture());
1759 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001760 }
1761
Greg Daniel25af6712018-04-25 10:44:38 -04001762 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
1763 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001764 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1765 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07001766 return true;
1767 }
1768
Greg Daniel25af6712018-04-25 10:44:38 -04001769 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
1770 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001771 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1772 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001773 return true;
1774 }
1775
Greg Daniel164a9f02016-02-22 09:56:40 -05001776 return false;
1777}
1778
Brian Salomona6948702018-06-01 15:33:20 -04001779bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
1780 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05001781 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001782 return false;
1783 }
1784
egdaniel66933552016-08-24 07:22:19 -07001785 GrVkImage* image = nullptr;
1786 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1787 if (rt) {
1788 // resolve the render target if necessary
1789 switch (rt->getResolveType()) {
1790 case GrVkRenderTarget::kCantResolve_ResolveType:
1791 return false;
1792 case GrVkRenderTarget::kAutoResolves_ResolveType:
1793 break;
1794 case GrVkRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05001795 this->internalResolveRenderTarget(rt, false);
egdaniel66933552016-08-24 07:22:19 -07001796 break;
1797 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001798 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07001799 }
1800 image = rt;
1801 } else {
1802 image = static_cast<GrVkTexture*>(surface->asTexture());
1803 }
1804
1805 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001806 return false;
1807 }
1808
1809 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001810 image->setImageLayout(this,
1811 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1812 VK_ACCESS_TRANSFER_READ_BIT,
1813 VK_PIPELINE_STAGE_TRANSFER_BIT,
1814 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001815
Brian Salomonc320b152018-02-20 14:05:36 -05001816 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07001817 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05001818
Greg Daniel164a9f02016-02-22 09:56:40 -05001819 VkBufferImageCopy region;
1820 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001821
1822 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1823 if (copyFromOrigin) {
1824 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04001825 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07001826 } else {
Brian Salomona6948702018-06-01 15:33:20 -04001827 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07001828 region.imageOffset = offset;
1829 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1830 }
1831
1832 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04001833 size_t imageRows = region.imageExtent.height;
egdaniel6fa0a912016-09-12 11:51:29 -07001834 GrVkTransferBuffer* transferBuffer =
Greg Daniel3cdfa092018-02-26 16:14:10 -05001835 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
egdaniel6fa0a912016-09-12 11:51:29 -07001836 kXferGpuToCpu_GrBufferType,
1837 kStream_GrAccessPattern));
1838
1839 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001840 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001841 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001842 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1843 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001844
1845 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001846 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001847 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1848 transferBuffer,
1849 1,
1850 &region);
1851
1852 // make sure the copy to buffer has finished
1853 transferBuffer->addMemoryBarrier(this,
1854 VK_ACCESS_TRANSFER_WRITE_BIT,
1855 VK_ACCESS_HOST_READ_BIT,
1856 VK_PIPELINE_STAGE_TRANSFER_BIT,
1857 VK_PIPELINE_STAGE_HOST_BIT,
1858 false);
1859
1860 // We need to submit the current command buffer to the Queue and make sure it finishes before
1861 // we can copy the data out of the buffer.
1862 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00001863 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05001864 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04001865 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05001866
egdaniel6fa0a912016-09-12 11:51:29 -07001867 if (copyFromOrigin) {
1868 uint32_t skipRows = region.imageExtent.height - height;
1869 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1870 }
1871
Brian Salomona6948702018-06-01 15:33:20 -04001872 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05001873
1874 transferBuffer->unmap();
1875 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001876 return true;
1877}
egdaniel066df7c2016-06-08 14:02:27 -07001878
egdaniel27bb2842016-07-07 11:58:35 -07001879// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1880// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1881// the the entire attachment. Similar requirements for the y and height components.
1882void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1883 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1884 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001885 if ((0 != granularity.width && 1 != granularity.width)) {
1886 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1887 int rightAdj = srcBounds.fRight % granularity.width;
1888 if (rightAdj != 0) {
1889 rightAdj = granularity.width - rightAdj;
1890 }
1891 dstBounds->fRight = srcBounds.fRight + rightAdj;
1892 if (dstBounds->fRight > maxWidth) {
1893 dstBounds->fRight = maxWidth;
1894 dstBounds->fLeft = 0;
1895 } else {
1896 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1897 }
egdaniel27bb2842016-07-07 11:58:35 -07001898 } else {
egdanield5797b32016-09-20 12:57:45 -07001899 dstBounds->fLeft = srcBounds.fLeft;
1900 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07001901 }
1902
1903 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07001904 if ((0 != granularity.height && 1 != granularity.height)) {
1905 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
1906 int bottomAdj = srcBounds.fBottom % granularity.height;
1907 if (bottomAdj != 0) {
1908 bottomAdj = granularity.height - bottomAdj;
1909 }
1910 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
1911 if (dstBounds->fBottom > maxHeight) {
1912 dstBounds->fBottom = maxHeight;
1913 dstBounds->fTop = 0;
1914 } else {
1915 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
1916 }
egdaniel27bb2842016-07-07 11:58:35 -07001917 } else {
egdanield5797b32016-09-20 12:57:45 -07001918 dstBounds->fTop = srcBounds.fTop;
1919 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07001920 }
1921}
1922
Greg Daniel22bc8652017-03-22 15:45:43 -04001923void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07001924 const GrVkRenderPass* renderPass,
1925 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001926 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07001927 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07001928 const SkIRect* pBounds = &bounds;
1929 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001930 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07001931 flippedBounds = bounds;
1932 flippedBounds.fTop = target->height() - bounds.fBottom;
1933 flippedBounds.fBottom = target->height() - bounds.fTop;
1934 pBounds = &flippedBounds;
1935 }
1936
egdaniel27bb2842016-07-07 11:58:35 -07001937 // The bounds we use for the render pass should be of the granularity supported
1938 // by the device.
1939 const VkExtent2D& granularity = renderPass->granularity();
1940 SkIRect adjustedBounds;
1941 if ((0 != granularity.width && 1 != granularity.width) ||
1942 (0 != granularity.height && 1 != granularity.height)) {
1943 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
1944 target->width(), target->height());
1945 pBounds = &adjustedBounds;
1946 }
1947
Robert Phillips95214472017-08-08 18:00:03 -04001948#ifdef SK_DEBUG
1949 uint32_t index;
1950 bool result = renderPass->colorAttachmentIndex(&index);
1951 SkASSERT(result && 0 == index);
1952 result = renderPass->stencilAttachmentIndex(&index);
1953 if (result) {
1954 SkASSERT(1 == index);
1955 }
1956#endif
1957 VkClearValue clears[2];
1958 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04001959 clears[1].depthStencil.depth = 0.0f;
1960 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04001961
1962 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04001963 for (int i = 0; i < buffers.count(); ++i) {
1964 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
1965 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001966 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07001967
Brian Salomon1fabd512018-02-09 09:54:25 -05001968 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05001969}
egdaniel9cb63402016-06-23 08:37:05 -07001970
Robert Phillips5b5d84c2018-08-09 15:12:18 -04001971void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
1972 if (buffer->asRTCommandBuffer()) {
1973 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
1974
1975 fCachedRTCommandBuffer->submit();
1976 fCachedRTCommandBuffer->reset();
1977 } else {
1978 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
1979
1980 fCachedTexCommandBuffer->submit();
1981 fCachedTexCommandBuffer->reset();
1982 }
1983}
1984
Greg Daniel6be35232017-03-01 17:01:09 -05001985GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07001986 VkFenceCreateInfo createInfo;
1987 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
1988 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1989 createInfo.pNext = nullptr;
1990 createInfo.flags = 0;
1991 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05001992
1993 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
1994 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
1995
1996 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07001997 return (GrFence)fence;
1998}
1999
Greg Daniel6be35232017-03-01 17:01:09 -05002000bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2001 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2002
2003 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002004 return (VK_SUCCESS == result);
2005}
2006
2007void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002008 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2009}
2010
Greg Daniela5cb7812017-06-16 09:45:32 -04002011sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2012 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002013}
2014
Greg Daniel48661b82018-01-22 16:11:35 -05002015sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2016 GrResourceProvider::SemaphoreWrapType wrapType,
2017 GrWrapOwnership ownership) {
2018 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002019}
2020
Greg Daniel48661b82018-01-22 16:11:35 -05002021void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05002022 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2023
Greg Daniel48661b82018-01-22 16:11:35 -05002024 GrVkSemaphore::Resource* resource = vkSem->getResource();
2025 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002026 resource->ref();
2027 fSemaphoresToSignal.push_back(resource);
2028 }
Greg Daniela5cb7812017-06-16 09:45:32 -04002029
2030 if (flush) {
2031 this->submitCommandBuffer(kSkip_SyncQueue);
2032 }
Greg Daniel6be35232017-03-01 17:01:09 -05002033}
2034
Greg Daniel48661b82018-01-22 16:11:35 -05002035void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002036 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2037
Greg Daniel48661b82018-01-22 16:11:35 -05002038 GrVkSemaphore::Resource* resource = vkSem->getResource();
2039 if (resource->shouldWait()) {
2040 resource->ref();
2041 fSemaphoresToWaitOn.push_back(resource);
2042 }
jvanverth84741b32016-09-30 08:39:02 -07002043}
Brian Osman13dddce2017-05-09 13:19:50 -04002044
2045sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2046 SkASSERT(texture);
2047 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2048 vkTexture->setImageLayout(this,
2049 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2050 VK_ACCESS_SHADER_READ_BIT,
2051 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
2052 false);
2053 this->submitCommandBuffer(kSkip_SyncQueue);
2054
2055 // The image layout change serves as a barrier, so no semaphore is needed
2056 return nullptr;
2057}
Greg Danielf5d87582017-12-18 14:48:15 -05002058