blob: 7d71d22c6e2ce9987819a6486a896d7422ee2014 [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
Ethan Nicholas8e265a72018-12-12 16:22:40 -050010#include "GrContextPriv.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040011#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrContextOptions.h"
14#include "GrGeometryProcessor.h"
15#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070016#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "GrPipeline.h"
18#include "GrRenderTargetPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "GrTexturePriv.h"
Greg Daniel81df0412018-05-31 13:13:33 -040020#include "GrVkAMDMemoryAllocator.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "GrVkCommandBuffer.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050022#include "GrVkCommandPool.h"
egdaniel066df7c2016-06-08 14:02:27 -070023#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050024#include "GrVkImage.h"
25#include "GrVkIndexBuffer.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040026#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050027#include "GrVkMemory.h"
28#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070029#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050030#include "GrVkRenderPass.h"
31#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050032#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050033#include "GrVkTexture.h"
34#include "GrVkTextureRenderTarget.h"
35#include "GrVkTransferBuffer.h"
36#include "GrVkVertexBuffer.h"
Matt Sarett485c4992017-02-14 14:18:27 -050037#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070038#include "SkMipMap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "SkSLCompiler.h"
40#include "SkTo.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040041
Greg Daniela31f4e52018-08-01 16:48:52 -040042#include "vk/GrVkExtensions.h"
jvanverthfd359ca2016-03-18 11:57:24 -070043#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050044
Ben Wagnerf08d1d02018-06-18 15:11:00 -040045#include <utility>
46
Forrest Reiling44f85712017-03-27 23:22:20 -070047#if !defined(SK_BUILD_FOR_WIN)
48#include <unistd.h>
49#endif // !defined(SK_BUILD_FOR_WIN)
50
Greg Danieldef55462018-08-01 13:40:14 -040051#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
52#include "SkLeanWindows.h"
53#endif
54
Greg Daniel164a9f02016-02-22 09:56:40 -050055#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
56#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
57#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
58
Greg Danielf730c182018-07-02 20:15:37 +000059sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050060 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000061 if (backendContext.fInstance == VK_NULL_HANDLE ||
62 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
63 backendContext.fDevice == VK_NULL_HANDLE ||
64 backendContext.fQueue == VK_NULL_HANDLE) {
65 return nullptr;
66 }
Greg Danield3e65aa2018-08-01 09:19:45 -040067 if (!backendContext.fGetProc) {
68 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040069 }
Greg Danield3e65aa2018-08-01 09:19:45 -040070
Greg Danielc0b03d82018-08-03 14:41:15 -040071 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
72 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
73 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
74 backendContext.fInstance,
75 VK_NULL_HANDLE));
76
77 if (!localGetPhysicalDeviceProperties) {
78 return nullptr;
79 }
80 VkPhysicalDeviceProperties physDeviceProperties;
81 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
82 uint32_t physDevVersion = physDeviceProperties.apiVersion;
83
Greg Daniel98bffae2018-08-01 13:25:41 -040084 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -040085
Greg Daniel98bffae2018-08-01 13:25:41 -040086 if (backendContext.fVkExtensions) {
87 interface.reset(new GrVkInterface(backendContext.fGetProc,
88 backendContext.fInstance,
89 backendContext.fDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -040090 backendContext.fInstanceVersion,
91 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -040092 backendContext.fVkExtensions));
Greg Danielc0b03d82018-08-03 14:41:15 -040093 if (!interface->validate(backendContext.fInstanceVersion, physDevVersion,
94 backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -040095 return nullptr;
96 }
97 } else {
98 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
99 // make an empty GrVkExtensions and pass that to the GrVkInterface.
100 GrVkExtensions extensions;
101 interface.reset(new GrVkInterface(backendContext.fGetProc,
102 backendContext.fInstance,
103 backendContext.fDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -0400104 backendContext.fInstanceVersion,
105 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400106 &extensions));
Greg Danielc0b03d82018-08-03 14:41:15 -0400107 if (!interface->validate(backendContext.fInstanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400108 return nullptr;
109 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500110 }
111
Greg Danielc8cd45a2018-07-12 10:02:37 -0400112 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface));
Greg Daniel164a9f02016-02-22 09:56:40 -0500113}
114
115////////////////////////////////////////////////////////////////////////////////
116
halcanary9d524f22016-03-29 09:03:52 -0700117GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400118 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface)
Brian Salomon384fab42017-12-07 12:33:05 -0500119 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400120 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000121 , fMemoryAllocator(backendContext.fMemoryAllocator)
122 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400123 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000124 , fDevice(backendContext.fDevice)
125 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400126 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500127 , fResourceProvider(this)
128 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000129 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700130
Greg Daniel81df0412018-05-31 13:13:33 -0400131 if (!fMemoryAllocator) {
132 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000133 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400134 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400135 }
136
ethannicholasb3058bd2016-07-01 08:22:01 -0700137 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700138
Greg Daniel92aef4b2018-08-02 13:55:49 -0400139 uint32_t instanceVersion = backendContext.fInstanceVersion ? backendContext.fInstanceVersion
140 : backendContext.fMinAPIVersion;
141
Greg Daniela0651ac2018-08-08 09:23:18 -0400142 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400143 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400144 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400145 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400146 } else if (backendContext.fDeviceFeatures) {
147 VkPhysicalDeviceFeatures2 features2;
148 features2.pNext = nullptr;
149 features2.features = *backendContext.fDeviceFeatures;
150 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
151 features2, instanceVersion, *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400152 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400153 VkPhysicalDeviceFeatures2 features;
154 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
155 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400156 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400157 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400158 }
159 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400160 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400161 }
162 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400163 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400164 }
165 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielc0b03d82018-08-03 14:41:15 -0400166 features, instanceVersion, GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400167 }
jvanverth633b3562016-03-23 11:01:22 -0700168 fCaps.reset(SkRef(fVkCaps.get()));
169
Greg Danielf730c182018-07-02 20:15:37 +0000170 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
171 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700172
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500173 fCmdPool = fResourceProvider.findOrCreateCommandPool();
174 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000175 SkASSERT(fCurrentCmdBuffer);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500176 fResourceProvider.init();
jvanverth633b3562016-03-23 11:01:22 -0700177 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500178}
179
Greg Daniel8606cf82017-05-08 16:17:53 -0400180void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500181 if (fCmdPool) {
182 fCmdPool->getPrimaryCommandBuffer()->end(this);
183 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400184 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500185
186 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500187 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700188
189 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
190 // on the command buffers even though they have completed. This causes an assert to fire when
191 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500192 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700193#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500194 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700195#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500196 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700197#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500198 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700199#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500200 }
egdanielf8c2be32016-06-24 13:18:27 -0700201#endif
202
egdanielbe9d8212016-09-20 08:54:23 -0700203#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400204 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700205#endif
halcanary9d524f22016-03-29 09:03:52 -0700206
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500207 if (fCmdPool) {
208 fCmdPool->unref(this);
209 fCmdPool = nullptr;
210 }
211
Greg Daniel6be35232017-03-01 17:01:09 -0500212 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
213 fSemaphoresToWaitOn[i]->unref(this);
214 }
215 fSemaphoresToWaitOn.reset();
216
Greg Daniela5cb7812017-06-16 09:45:32 -0400217 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
218 fSemaphoresToSignal[i]->unref(this);
219 }
220 fSemaphoresToSignal.reset();
221
222
egdanielbc9b2962016-09-27 08:00:53 -0700223 fCopyManager.destroyResources(this);
224
Jim Van Verth09557d72016-11-07 11:10:21 -0500225 // must call this just before we destroy the command pool and VkDevice
226 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500227
Greg Danielf730c182018-07-02 20:15:37 +0000228 fMemoryAllocator.reset();
229
230 fQueue = VK_NULL_HANDLE;
231 fDevice = VK_NULL_HANDLE;
232 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400233}
234
235GrVkGpu::~GrVkGpu() {
236 if (!fDisconnected) {
237 this->destroyResources();
238 }
239 delete fCompiler;
240}
241
242
243void GrVkGpu::disconnect(DisconnectType type) {
244 INHERITED::disconnect(type);
245 if (!fDisconnected) {
246 if (DisconnectType::kCleanup == type) {
247 this->destroyResources();
248 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500249 if (fCmdPool) {
250 fCmdPool->unrefAndAbandon();
251 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400252 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400253 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
254 fSemaphoresToWaitOn[i]->unrefAndAbandon();
255 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400256 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
257 fSemaphoresToSignal[i]->unrefAndAbandon();
258 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400259 fCopyManager.abandonResources();
260
261 // must call this just before we destroy the command pool and VkDevice
262 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400263
264 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400265 }
266 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400267 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400268 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400269 fDisconnected = true;
270 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500271}
272
273///////////////////////////////////////////////////////////////////////////////
274
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400275GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400276 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400277 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
278 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400279 if (!fCachedRTCommandBuffer) {
280 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
281 }
282
Greg Daniela41a74a2018-10-09 12:59:23 +0000283 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400284 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400285}
286
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400287GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
288 if (!fCachedTexCommandBuffer) {
289 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
290 }
291
292 fCachedTexCommandBuffer->set(texture, origin);
293 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700294}
295
Greg Daniela5cb7812017-06-16 09:45:32 -0400296void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500297 SkASSERT(fCurrentCmdBuffer);
298 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500299 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400300 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500301
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400302 // We must delete and drawables that have been waitint till submit for us to destroy.
303 fDrawables.reset();
304
Greg Daniel6be35232017-03-01 17:01:09 -0500305 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
306 fSemaphoresToWaitOn[i]->unref(this);
307 }
308 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400309 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
310 fSemaphoresToSignal[i]->unref(this);
311 }
312 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500313
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500314 // Release old command pool and create a new one
315 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500316 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500317 fCmdPool = fResourceProvider.findOrCreateCommandPool();
318 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500319 fCurrentCmdBuffer->begin(this);
320}
321
322///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700323GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
324 const void* data) {
325 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700326 switch (type) {
327 case kVertex_GrBufferType:
328 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
329 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700330 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700331 break;
cdalton397536c2016-03-25 12:15:03 -0700332 case kIndex_GrBufferType:
333 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
334 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700335 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700336 break;
cdalton397536c2016-03-25 12:15:03 -0700337 case kXferCpuToGpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400338 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
339 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700340 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700341 break;
cdalton397536c2016-03-25 12:15:03 -0700342 case kXferGpuToCpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400343 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
344 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700345 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700346 break;
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400347 case kDrawIndirect_GrBufferType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400348 SK_ABORT("DrawIndirect Buffers not supported in vulkan backend.");
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400349 return nullptr;
cdalton397536c2016-03-25 12:15:03 -0700350 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400351 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700352 return nullptr;
353 }
cdalton1bf3e712016-04-19 10:00:02 -0700354 if (data && buff) {
355 buff->updateData(data, size);
356 }
357 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500358}
359
Brian Salomona9b04b92018-06-01 15:04:28 -0400360bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
361 GrColorType srcColorType, const GrMipLevel texels[],
362 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500363 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
364 if (!vkTex) {
365 return false;
366 }
367
jvanverth900bd4a2016-04-29 13:53:12 -0700368 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400369 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800370 return false;
371 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800372
Greg Daniel164a9f02016-02-22 09:56:40 -0500373 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400374 bool linearTiling = vkTex->isLinearTiled();
375 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400376 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400377 SkDebugf("Can't upload mipmap data to linear tiled texture");
378 return false;
379 }
380 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
381 // Need to change the layout to general in order to perform a host write
382 vkTex->setImageLayout(this,
383 VK_IMAGE_LAYOUT_GENERAL,
384 VK_ACCESS_HOST_WRITE_BIT,
385 VK_PIPELINE_STAGE_HOST_BIT,
386 false);
387 this->submitCommandBuffer(kForce_SyncQueue);
388 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400389 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400390 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500391 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400392 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400393 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
394 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500395 }
egdaniel4583ec52016-06-27 12:57:00 -0700396
jvanverth900bd4a2016-04-29 13:53:12 -0700397 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500398}
399
Brian Salomonc320b152018-02-20 14:05:36 -0500400bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
401 GrColorType bufferColorType, GrBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400402 size_t bufferOffset, size_t rowBytes) {
403 // Vulkan only supports 4-byte aligned offsets
404 if (SkToBool(bufferOffset & 0x2)) {
405 return false;
406 }
407 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
408 if (!vkTex) {
409 return false;
410 }
411 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
412 if (!vkBuffer) {
413 return false;
414 }
415
Greg Daniel660cc992017-06-26 14:55:05 -0400416 SkDEBUGCODE(
417 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
418 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
419 SkASSERT(bounds.contains(subRect));
420 )
Brian Salomonc320b152018-02-20 14:05:36 -0500421 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400422 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500423 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400424 }
425
426 // Set up copy region
427 VkBufferImageCopy region;
428 memset(&region, 0, sizeof(VkBufferImageCopy));
429 region.bufferOffset = bufferOffset;
430 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
431 region.bufferImageHeight = 0;
432 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
433 region.imageOffset = { left, top, 0 };
434 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
435
436 // Change layout of our target so it can be copied to
437 vkTex->setImageLayout(this,
438 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
439 VK_ACCESS_TRANSFER_WRITE_BIT,
440 VK_PIPELINE_STAGE_TRANSFER_BIT,
441 false);
442
443 // Copy the buffer to the image
444 fCurrentCmdBuffer->copyBufferToImage(this,
445 vkBuffer,
446 vkTex,
447 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
448 1,
449 &region);
450
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400451 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400452 return true;
453}
454
Brian Salomon1fabd512018-02-09 09:54:25 -0500455void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
456 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700457 SkASSERT(dst);
458 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
459
egdaniel4bcd62e2016-08-31 07:37:31 -0700460 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500461 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
462 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
463 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
464 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
465 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700466
Greg Danielbc26c392017-04-18 13:32:10 -0400467 GrVkImage* dstImage;
468 GrRenderTarget* dstRT = dst->asRenderTarget();
469 if (dstRT) {
470 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400471 dstImage = vkRT;
472 } else {
473 SkASSERT(dst->asTexture());
474 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
475 }
476 dstImage->setImageLayout(this,
477 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
478 VK_ACCESS_TRANSFER_WRITE_BIT,
479 VK_PIPELINE_STAGE_TRANSFER_BIT,
480 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700481
482 src->msaaImage()->setImageLayout(this,
483 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
484 VK_ACCESS_TRANSFER_READ_BIT,
485 VK_PIPELINE_STAGE_TRANSFER_BIT,
486 false);
487
Greg Danielbc26c392017-04-18 13:32:10 -0400488 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700489}
490
Brian Salomon1fabd512018-02-09 09:54:25 -0500491void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700492 if (target->needsResolve()) {
493 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700494 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
495 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500496
egdaniel4bcd62e2016-08-31 07:37:31 -0700497 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700498
Brian Salomon1fabd512018-02-09 09:54:25 -0500499 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700500
501 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500502
503 if (requiresSubmit) {
504 this->submitCommandBuffer(kSkip_SyncQueue);
505 }
egdaniel52ad2512016-08-04 12:50:01 -0700506 }
507}
508
Brian Salomona9b04b92018-06-01 15:04:28 -0400509bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
510 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500511 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700512 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500513
Greg Daniel660cc992017-06-26 14:55:05 -0400514 SkDEBUGCODE(
515 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
516 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
517 SkASSERT(bounds.contains(subRect));
518 )
Brian Salomonc320b152018-02-20 14:05:36 -0500519 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500520 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400521 if (!rowBytes) {
522 rowBytes = trimRowBytes;
523 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500524
jvanverth900bd4a2016-04-29 13:53:12 -0700525 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
526 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
527 const VkImageSubresource subres = {
528 VK_IMAGE_ASPECT_COLOR_BIT,
529 0, // mipLevel
530 0, // arraySlice
531 };
532 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500533
jvanverth900bd4a2016-04-29 13:53:12 -0700534 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500535
jvanverth900bd4a2016-04-29 13:53:12 -0700536 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700537 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700538 &subres,
539 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500540
jvanverth1e305ba2016-06-01 09:39:15 -0700541 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400542 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700543 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400544 SkASSERT(size + offset <= alloc.fSize);
545 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
546 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700547 return false;
548 }
Greg Daniel81df0412018-05-31 13:13:33 -0400549 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700550
Brian Salomona9b04b92018-06-01 15:04:28 -0400551 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
552 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700553
Greg Daniele35a99e2018-03-02 11:44:22 -0500554 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400555 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700556
557 return true;
558}
559
Brian Salomona9b04b92018-06-01 15:04:28 -0400560bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
561 GrColorType dataColorType, const GrMipLevel texels[],
562 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700563 SkASSERT(!tex->isLinearTiled());
564 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400565 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700566 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
567
Greg Danieldd20e912017-04-07 14:42:23 -0400568 // We assume that if the texture has mip levels, we either upload to all the levels or just the
569 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400570 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400571
jvanverth900bd4a2016-04-29 13:53:12 -0700572 if (width == 0 || height == 0) {
573 return false;
574 }
575
Greg Daniel475eb702018-09-28 14:16:50 -0400576 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
577 return false;
578 }
579
580 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
581 // dst RGB texture. Thus we do not upload mip levels for that.
582 if (dataColorType == GrColorType::kRGB_888x) {
583 SkASSERT(tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM &&
584 tex->config() == kRGB_888_GrPixelConfig);
585 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
586 // blit or draw.
587 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
588 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
589 return false;
590 }
591 mipLevelCount = 1;
592 }
593
Brian Salomond1eaf492017-05-18 10:02:08 -0400594 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500595 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700596
597 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700598 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
599 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400600 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
601
Greg Daniel475eb702018-09-28 14:16:50 -0400602 texelsShallowCopy.reset(mipLevelCount);
603 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700604
Robert Phillips590533f2017-07-11 14:22:35 -0400605 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700606 individualMipOffsets.push_back(0);
607 size_t combinedBufferSize = width * bpp * height;
608 int currentWidth = width;
609 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400610 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400611 combinedBufferSize = 0;
612 }
613
Greg Daniel468fd632017-03-22 17:03:45 -0400614 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
615 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
616 SkASSERT((bpp & (bpp - 1)) == 0);
617 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400618 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700619 currentWidth = SkTMax(1, currentWidth/2);
620 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400621
Greg Daniel55afd6d2017-09-29 09:32:44 -0400622 if (texelsShallowCopy[currentMipLevel].fPixels) {
623 const size_t trimmedSize = currentWidth * bpp * currentHeight;
624 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
625 if (alignmentDiff != 0) {
626 combinedBufferSize += alignmentMask - alignmentDiff + 1;
627 }
628 individualMipOffsets.push_back(combinedBufferSize);
629 combinedBufferSize += trimmedSize;
630 } else {
631 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400632 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400633 }
634 if (0 == combinedBufferSize) {
635 // We don't actually have any data to upload so just return success
636 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700637 }
638
639 // allocate buffer to hold our mip data
640 GrVkTransferBuffer* transferBuffer =
641 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400642 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700643 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400644 }
jvanverth900bd4a2016-04-29 13:53:12 -0700645
Greg Daniel475eb702018-09-28 14:16:50 -0400646 int uploadLeft = left;
647 int uploadTop = top;
648 GrVkTexture* uploadTexture = tex;
649 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
650 // R8G8B8A8_UNORM image and then copy it.
651 sk_sp<GrVkTexture> copyTexture;
652 if (dataColorType == GrColorType::kRGB_888x) {
653 GrSurfaceDesc surfDesc;
654 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
655 surfDesc.fWidth = width;
656 surfDesc.fHeight = height;
657 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
658 surfDesc.fSampleCnt = 1;
659
660 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
661 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
662 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
663
664 GrVkImage::ImageDesc imageDesc;
665 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
666 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
667 imageDesc.fWidth = width;
668 imageDesc.fHeight = height;
669 imageDesc.fLevels = 1;
670 imageDesc.fSamples = 1;
671 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
672 imageDesc.fUsageFlags = usageFlags;
673 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
674
675 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
676 GrMipMapsStatus::kNotAllocated);
677 if (!copyTexture) {
678 return false;
679 }
680 uploadTexture = copyTexture.get();
681 uploadLeft = 0;
682 uploadTop = 0;
683 }
684
jvanverth900bd4a2016-04-29 13:53:12 -0700685 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400686 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700687
jvanverthc578b0632016-05-02 10:58:12 -0700688 currentWidth = width;
689 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400690 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400691 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400692 if (texelsShallowCopy[currentMipLevel].fPixels) {
693 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
694 const size_t trimRowBytes = currentWidth * bpp;
695 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
696 ? texelsShallowCopy[currentMipLevel].fRowBytes
697 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700698
Greg Daniel55afd6d2017-09-29 09:32:44 -0400699 // copy data into the buffer, skipping the trailing bytes
700 char* dst = buffer + individualMipOffsets[currentMipLevel];
701 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400702 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400703
704 VkBufferImageCopy& region = regions.push_back();
705 memset(&region, 0, sizeof(VkBufferImageCopy));
706 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
707 region.bufferRowLength = currentWidth;
708 region.bufferImageHeight = currentHeight;
709 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400710 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400711 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700712 }
jvanverthc578b0632016-05-02 10:58:12 -0700713 currentWidth = SkTMax(1, currentWidth/2);
714 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400715 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700716 }
717
jvanverth9d54afc2016-09-20 09:20:03 -0700718 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700719 transferBuffer->unmap();
720
jvanverth900bd4a2016-04-29 13:53:12 -0700721 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400722 uploadTexture->setImageLayout(this,
723 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
724 VK_ACCESS_TRANSFER_WRITE_BIT,
725 VK_PIPELINE_STAGE_TRANSFER_BIT,
726 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700727
728 // Copy the buffer to the image
729 fCurrentCmdBuffer->copyBufferToImage(this,
730 transferBuffer,
Greg Daniel475eb702018-09-28 14:16:50 -0400731 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700732 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
733 regions.count(),
734 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700735 transferBuffer->unref();
Greg Daniel475eb702018-09-28 14:16:50 -0400736
737 // If we copied the data into a temporary image first, copy that image into our main texture
738 // now.
739 if (copyTexture.get()) {
740 SkASSERT(dataColorType == GrColorType::kRGB_888x);
741 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
742 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
743 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
744 false));
745 }
Robert Phillips590533f2017-07-11 14:22:35 -0400746 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400747 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400748 }
jvanverth900bd4a2016-04-29 13:53:12 -0700749
Greg Daniel164a9f02016-02-22 09:56:40 -0500750 return true;
751}
752
753////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400754sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500755 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500756 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
757
758 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500759 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700760
Greg Daniel164a9f02016-02-22 09:56:40 -0500761 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
762 if (renderTarget) {
763 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
764 }
765
766 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
767 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
768 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700769 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500770 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
771 // texture.
772 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
773
Greg Daniel164a9f02016-02-22 09:56:40 -0500774 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700775 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500776 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400777 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500778 GrVkImage::ImageDesc imageDesc;
779 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
780 imageDesc.fFormat = pixelFormat;
781 imageDesc.fWidth = desc.fWidth;
782 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400783 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500784 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400785 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500786 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400787 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500788
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400789 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
790 if (mipLevels > 1) {
791 mipMapsStatus = GrMipMapsStatus::kValid;
792 for (int i = 0; i < mipLevels; ++i) {
793 if (!texels[i].fPixels) {
794 mipMapsStatus = GrMipMapsStatus::kDirty;
795 break;
796 }
Greg Daniel834f1202017-10-09 15:06:20 -0400797 }
798 }
799
Robert Phillips67d52cf2017-06-05 13:38:13 -0400800 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500801 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -0400802 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
803 imageDesc,
804 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500805 } else {
Greg Daniel475eb702018-09-28 14:16:50 -0400806 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500807 }
808
809 if (!tex) {
810 return nullptr;
811 }
812
Brian Salomonc320b152018-02-20 14:05:36 -0500813 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400814 if (mipLevelCount) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400815 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
816 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500817 tex->unref();
818 return nullptr;
819 }
820 }
821
Brian Salomond17b4a62017-05-23 16:53:47 -0400822 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
823 VkClearColorValue zeroClearColor;
824 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
825 VkImageSubresourceRange range;
826 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
827 range.baseArrayLayer = 0;
828 range.baseMipLevel = 0;
829 range.layerCount = 1;
830 range.levelCount = 1;
831 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
832 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400833 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400834 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400835 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -0500836}
837
838////////////////////////////////////////////////////////////////////////////////
839
Greg Daniel6888c0d2017-08-25 11:55:50 -0400840void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
841 VkDeviceSize dstOffset, VkDeviceSize size) {
842 VkBufferCopy copyRegion;
843 copyRegion.srcOffset = srcOffset;
844 copyRegion.dstOffset = dstOffset;
845 copyRegion.size = size;
846 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
847}
848
jvanverthdb379092016-07-07 11:18:46 -0700849bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
850 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700851 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700852 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700853
854 return true;
855}
856
857////////////////////////////////////////////////////////////////////////////////
858
Greg Daniel7e000222018-12-03 10:08:21 -0500859static bool check_image_info(const GrVkCaps& caps,
860 const GrVkImageInfo& info,
861 GrPixelConfig config) {
862 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -0400863 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500864 }
865
Greg Daniel7e000222018-12-03 10:08:21 -0500866 if (info.fYcbcrConversionInfo.isValid()) {
867 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
868 return false;
869 }
jvanverthfd359ca2016-03-18 11:57:24 -0700870 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000871
Greg Daniel52e16d92018-04-10 09:34:07 -0400872 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -0400873 return true;
874}
875
876sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonc67c31c2018-12-06 10:00:03 -0500877 GrWrapOwnership ownership, GrIOType ioType,
878 bool purgeImmediately) {
Greg Daniel7e000222018-12-03 10:08:21 -0500879 GrVkImageInfo imageInfo;
880 if (!backendTex.getVkImageInfo(&imageInfo)) {
881 return nullptr;
882 }
883
884 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400885 return nullptr;
886 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500887
Greg Daniel164a9f02016-02-22 09:56:40 -0500888 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400889 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000890 surfDesc.fWidth = backendTex.width();
891 surfDesc.fHeight = backendTex.height();
892 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500893 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500894
Greg Daniel52e16d92018-04-10 09:34:07 -0400895 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
896 SkASSERT(layout);
Brian Salomonc67c31c2018-12-06 10:00:03 -0500897 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, ioType, purgeImmediately,
Greg Daniel2268ad22018-11-15 09:27:38 -0500898 imageInfo, std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -0400899}
900
901sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400902 int sampleCnt,
903 GrWrapOwnership ownership) {
Greg Daniel7e000222018-12-03 10:08:21 -0500904 GrVkImageInfo imageInfo;
905 if (!backendTex.getVkImageInfo(&imageInfo)) {
906 return nullptr;
907 }
908
909 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400910 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500911 }
Brian Salomond17f6582017-07-19 18:28:58 -0400912
913 GrSurfaceDesc surfDesc;
914 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
915 surfDesc.fWidth = backendTex.width();
916 surfDesc.fHeight = backendTex.height();
917 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500918 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -0400919
Greg Daniel52e16d92018-04-10 09:34:07 -0400920 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
921 SkASSERT(layout);
922
Brian Salomond17f6582017-07-19 18:28:58 -0400923 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
Greg Daniel52e16d92018-04-10 09:34:07 -0400924 imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500925}
926
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400927sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -0400928 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
929 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
930 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
931 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500932 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -0400933 return nullptr;
934 }
halcanary9d524f22016-03-29 09:03:52 -0700935
Greg Daniel323fbcf2018-04-10 13:46:30 -0400936 GrVkImageInfo info;
937 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000938 return nullptr;
939 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400940
941 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700942 return nullptr;
943 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500944
Greg Daniel164a9f02016-02-22 09:56:40 -0500945 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400946 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400947 desc.fWidth = backendRT.width();
948 desc.fHeight = backendRT.height();
949 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500950 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500951
Greg Daniel323fbcf2018-04-10 13:46:30 -0400952 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -0400953
Greg Daniel323fbcf2018-04-10 13:46:30 -0400954 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -0400955 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -0500956
957 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
958 SkASSERT(!backendRT.stencilBits());
959 if (tgt) {
960 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -0500961 }
Brian Salomonafdc6b12018-03-09 12:02:32 -0500962
Ben Wagnerff134f22018-04-24 16:29:16 -0400963 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500964}
965
Greg Daniel7ef28f32017-04-20 16:41:55 +0000966sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000967 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400968
Greg Daniel52e16d92018-04-10 09:34:07 -0400969 GrVkImageInfo imageInfo;
970 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000971 return nullptr;
972 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400973 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -0400974 return nullptr;
975 }
976
977 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000978 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000979 desc.fWidth = tex.width();
980 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400981 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500982 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
983 if (!desc.fSampleCnt) {
984 return nullptr;
985 }
Brian Osman33910292017-04-18 14:38:53 -0400986
Greg Daniel52e16d92018-04-10 09:34:07 -0400987 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
988 SkASSERT(layout);
989
Ben Wagnerff134f22018-04-24 16:29:16 -0400990 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -0400991}
992
Greg Danielb46add82019-01-02 14:51:29 -0500993sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
994 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
995 int maxSize = this->caps()->maxTextureSize();
996 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
997 return nullptr;
998 }
999
1000 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1001 if (!backendFormat.isValid()) {
1002 return nullptr;
1003 }
1004 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1005 imageInfo.colorType());
1006 if (config == kUnknown_GrPixelConfig) {
1007 return nullptr;
1008 }
1009
1010 GrSurfaceDesc desc;
1011 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1012 desc.fWidth = imageInfo.width();
1013 desc.fHeight = imageInfo.height();
1014 desc.fConfig = config;
1015 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1016 if (!desc.fSampleCnt) {
1017 return nullptr;
1018 }
1019
1020 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1021}
1022
Brian Salomon930f9392018-06-20 16:25:26 -04001023bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1024 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001025 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001026 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001027 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001028 return false;
jvanverth62340062016-04-26 08:01:44 -07001029 }
1030
jvanverth62340062016-04-26 08:01:44 -07001031 // determine if we can blit to and from this format
1032 const GrVkCaps& caps = this->vkCaps();
1033 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001034 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1035 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001036 return false;
jvanverth62340062016-04-26 08:01:44 -07001037 }
1038
egdaniel7ac5da82016-07-15 13:41:42 -07001039 int width = tex->width();
1040 int height = tex->height();
1041 VkImageBlit blitRegion;
1042 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001043
jvanverth82c05582016-05-03 11:19:01 -07001044 // SkMipMap doesn't include the base level in the level count so we have to add 1
1045 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001046 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001047
Greg Danielda86e282018-06-13 09:41:19 -04001048 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001049 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1050 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001051
jvanverth50c46c72016-05-06 12:31:28 -07001052 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001053 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001054 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1055 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001056 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1057 nullptr, // pNext
1058 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1059 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1060 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1061 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1062 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1063 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1064 vkTex->image(), // image
1065 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001066 };
1067
jvanverth62340062016-04-26 08:01:44 -07001068 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001069 uint32_t mipLevel = 1;
1070 while (mipLevel < levelCount) {
1071 int prevWidth = width;
1072 int prevHeight = height;
1073 width = SkTMax(1, width / 2);
1074 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001075
jvanverth50c46c72016-05-06 12:31:28 -07001076 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1077 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1078 false, &imageMemoryBarrier);
1079
1080 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001081 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001082 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001083 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1084 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001085 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001086 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001087 vkTex->resource(),
1088 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001089 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001090 vkTex->resource(),
1091 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001092 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001093 1,
1094 &blitRegion,
1095 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001096 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001097 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001098 // This barrier logically is not needed, but it changes the final level to the same layout as
1099 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1100 // future layout changes easier.
1101 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1102 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1103 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001104 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1105 return true;
jvanverth62340062016-04-26 08:01:44 -07001106}
1107
Greg Daniel164a9f02016-02-22 09:56:40 -05001108////////////////////////////////////////////////////////////////////////////////
1109
1110GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1111 int width,
1112 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001113 SkASSERT(width >= rt->width());
1114 SkASSERT(height >= rt->height());
1115
1116 int samples = rt->numStencilSamples();
1117
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001118 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001119
1120 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001121 width,
1122 height,
1123 samples,
1124 sFmt));
1125 fStats.incStencilAttachmentCreates();
1126 return stencil;
1127}
1128
1129////////////////////////////////////////////////////////////////////////////////
1130
Brian Salomon52e943a2018-03-13 09:32:39 -04001131bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001132 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1133 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001134 VkDeviceSize size = dstRowBytes * h;
1135 VkDeviceSize offset = bufferOffset;
1136 SkASSERT(size + offset <= alloc.fSize);
1137 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1138 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001139 return false;
1140 }
Greg Daniel81df0412018-05-31 13:13:33 -04001141 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001142
Greg Daniel20ece3a2017-03-28 10:24:43 -04001143 if (srcData) {
1144 // If there is no padding on dst we can do a single memcopy.
1145 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001146 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001147 } else {
1148 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1149 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001150 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001151 }
Greg Daniel81df0412018-05-31 13:13:33 -04001152 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1153 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001154 return true;
1155}
1156
Brian Salomonf865b052018-03-09 09:01:53 -05001157#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001158bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1159 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001160 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001161 SkASSERT(texturable || renderable);
1162 if (!texturable) {
1163 SkASSERT(GrMipMapped::kNo == mipMapped);
1164 SkASSERT(!srcData);
1165 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001166 VkFormat pixelFormat;
1167 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001168 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001169 }
1170
Brian Salomon52e943a2018-03-13 09:32:39 -04001171 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1172 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001173 }
1174
Brian Salomon52e943a2018-03-13 09:32:39 -04001175 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1176 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001177 }
1178
1179 // Currently we don't support uploading pixel data when mipped.
1180 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001181 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001182 }
1183
Brian Salomon52e943a2018-03-13 09:32:39 -04001184 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001185 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1186 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001187 if (texturable) {
1188 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1189 }
1190 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001191 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1192 }
1193
1194 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001195 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001196 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001197
1198 // Create Image
1199 VkSampleCountFlagBits vkSamples;
1200 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001201 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001202 }
1203
1204 // Figure out the number of mip levels.
1205 uint32_t mipLevels = 1;
1206 if (GrMipMapped::kYes == mipMapped) {
1207 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1208 }
1209
1210 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001211 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1212 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001213 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001214 VK_IMAGE_TYPE_2D, // VkImageType
1215 pixelFormat, // VkFormat
1216 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1217 mipLevels, // mipLevels
1218 1, // arrayLayers
1219 vkSamples, // samples
1220 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1221 usageFlags, // VkImageUsageFlags
1222 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1223 0, // queueFamilyCount
1224 0, // pQueueFamilyIndices
1225 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001226 };
1227
Brian Salomon52e943a2018-03-13 09:32:39 -04001228 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1229 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001230
Brian Salomonde9f5462018-03-07 14:23:58 -05001231 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001232 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001233 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001234 }
1235
1236 // 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 -05001237 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001238 VkBuffer buffer = VK_NULL_HANDLE;
1239
1240 VkResult err;
1241 const VkCommandBufferAllocateInfo cmdInfo = {
1242 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1243 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001244 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001245 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1246 1 // bufferCount
1247 };
1248
1249 VkCommandBuffer cmdBuffer;
1250 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1251 if (err) {
1252 GrVkMemory::FreeImageMemory(this, false, alloc);
1253 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001254 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001255 }
1256
1257 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1258 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1259 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1260 cmdBufferBeginInfo.pNext = nullptr;
1261 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1262 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1263
1264 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1265 SkASSERT(!err);
1266
1267 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001268 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001269
Robert Phillips646f6372018-09-25 09:31:10 -04001270 const size_t trimRowBytes = w * bpp;
1271 if (!srcRowBytes) {
1272 srcRowBytes = trimRowBytes;
1273 }
1274
Brian Salomonde9f5462018-03-07 14:23:58 -05001275 SkTArray<size_t> individualMipOffsets(mipLevels);
1276 individualMipOffsets.push_back(0);
1277 size_t combinedBufferSize = w * bpp * h;
1278 int currentWidth = w;
1279 int currentHeight = h;
1280 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1281 // config. This works with the assumption that the bytes in pixel config is always a power
1282 // of 2.
1283 SkASSERT((bpp & (bpp - 1)) == 0);
1284 const size_t alignmentMask = 0x3 | (bpp - 1);
1285 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1286 currentWidth = SkTMax(1, currentWidth / 2);
1287 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001288
Brian Salomonde9f5462018-03-07 14:23:58 -05001289 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1290 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1291 if (alignmentDiff != 0) {
1292 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001293 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001294 individualMipOffsets.push_back(combinedBufferSize);
1295 combinedBufferSize += trimmedSize;
1296 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001297
Brian Salomonde9f5462018-03-07 14:23:58 -05001298 VkBufferCreateInfo bufInfo;
1299 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1300 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1301 bufInfo.flags = 0;
1302 bufInfo.size = combinedBufferSize;
1303 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1304 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1305 bufInfo.queueFamilyIndexCount = 0;
1306 bufInfo.pQueueFamilyIndices = nullptr;
1307 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001308
Brian Salomonde9f5462018-03-07 14:23:58 -05001309 if (err) {
1310 GrVkMemory::FreeImageMemory(this, false, alloc);
1311 VK_CALL(DestroyImage(fDevice, image, nullptr));
1312 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001313 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001314 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001315 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001316
Brian Salomonde9f5462018-03-07 14:23:58 -05001317 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1318 &bufferAlloc)) {
1319 GrVkMemory::FreeImageMemory(this, false, alloc);
1320 VK_CALL(DestroyImage(fDevice, image, nullptr));
1321 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1322 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001323 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001324 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001325 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001326
Brian Salomonde9f5462018-03-07 14:23:58 -05001327 currentWidth = w;
1328 currentHeight = h;
1329 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1330 SkASSERT(0 == currentMipLevel || !srcData);
1331 size_t currentRowBytes = bpp * currentWidth;
1332 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Robert Phillips646f6372018-09-25 09:31:10 -04001333 if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1334 currentRowBytes, trimRowBytes, currentHeight)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001335 GrVkMemory::FreeImageMemory(this, false, alloc);
1336 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001337 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001338 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1339 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001340 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001341 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001342 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001343 currentWidth = SkTMax(1, currentWidth / 2);
1344 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001345 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001346
1347 // Set image layout and add barrier
1348 VkImageMemoryBarrier barrier;
1349 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1350 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1351 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001352 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001353 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1354 barrier.oldLayout = initialLayout;
1355 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1356 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1357 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1358 barrier.image = image;
1359 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1360
Greg Danielf7828d02018-10-09 12:01:32 -04001361 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001362 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1363 &barrier));
1364 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1365
1366 SkTArray<VkBufferImageCopy> regions(mipLevels);
1367
1368 currentWidth = w;
1369 currentHeight = h;
1370 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1371 // Submit copy command
1372 VkBufferImageCopy& region = regions.push_back();
1373 memset(&region, 0, sizeof(VkBufferImageCopy));
1374 region.bufferOffset = individualMipOffsets[currentMipLevel];
1375 region.bufferRowLength = currentWidth;
1376 region.bufferImageHeight = currentHeight;
1377 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1378 region.imageOffset = {0, 0, 0};
1379 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1380 currentWidth = SkTMax(1, currentWidth / 2);
1381 currentHeight = SkTMax(1, currentHeight / 2);
1382 }
1383
1384 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1385 regions.begin()));
1386
Brian Salomon52e943a2018-03-13 09:32:39 -04001387 if (texturable) {
1388 // Change Image layout to shader read since if we use this texture as a borrowed textures
1389 // within Ganesh we require that its layout be set to that
1390 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1391 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1392 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001393 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001394 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1395 barrier.oldLayout = initialLayout;
1396 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1397 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1398 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1399 barrier.image = image;
1400 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001401 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001402 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1403 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001404 0,
1405 0, nullptr,
1406 0, nullptr,
1407 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001408 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001409 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001410
1411 // End CommandBuffer
1412 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1413 SkASSERT(!err);
1414
1415 // Create Fence for queue
1416 VkFence fence;
1417 VkFenceCreateInfo fenceInfo;
1418 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1419 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1420
1421 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1422 SkASSERT(!err);
1423
1424 VkSubmitInfo submitInfo;
1425 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1426 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1427 submitInfo.pNext = nullptr;
1428 submitInfo.waitSemaphoreCount = 0;
1429 submitInfo.pWaitSemaphores = nullptr;
1430 submitInfo.pWaitDstStageMask = 0;
1431 submitInfo.commandBufferCount = 1;
1432 submitInfo.pCommandBuffers = &cmdBuffer;
1433 submitInfo.signalSemaphoreCount = 0;
1434 submitInfo.pSignalSemaphores = nullptr;
1435 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1436 SkASSERT(!err);
1437
1438 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1439 if (VK_TIMEOUT == err) {
1440 GrVkMemory::FreeImageMemory(this, false, alloc);
1441 VK_CALL(DestroyImage(fDevice, image, nullptr));
1442 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1443 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001444 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001445 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1446 SkDebugf("Fence failed to signal: %d\n", err);
1447 SK_ABORT("failing");
1448 }
1449 SkASSERT(!err);
1450
1451 // Clean up transfer resources
1452 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1453 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1454 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1455 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001456 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001457 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1458
Brian Salomon52e943a2018-03-13 09:32:39 -04001459 info->fImage = image;
1460 info->fAlloc = alloc;
1461 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1462 info->fImageLayout = initialLayout;
1463 info->fFormat = pixelFormat;
1464 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001465
Brian Salomon52e943a2018-03-13 09:32:39 -04001466 return true;
1467}
1468
1469GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001470 GrColorType colorType,
1471 bool isRenderTarget,
1472 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001473 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001474
1475 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1476 return GrBackendTexture();
1477 }
1478
Robert Phillips646f6372018-09-25 09:31:10 -04001479 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1480 if (!this->caps()->isConfigTexturable(config)) {
1481 return GrBackendTexture();
1482 }
1483
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001484 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001485 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001486 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001487 return {};
1488 }
Greg Daniel108bb232018-07-03 16:18:29 -04001489 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1490 // Lots of tests don't go through Skia's public interface which will set the config so for
1491 // testing we make sure we set a config here.
1492 beTex.setPixelConfig(config);
1493 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001494}
1495
1496bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001497 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001498
Greg Daniel52e16d92018-04-10 09:34:07 -04001499 GrVkImageInfo backend;
1500 if (!tex.getVkImageInfo(&backend)) {
1501 return false;
1502 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001503
Greg Daniel52e16d92018-04-10 09:34:07 -04001504 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001505 VkMemoryRequirements req;
1506 memset(&req, 0, sizeof(req));
1507 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001508 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001509 &req));
1510 // TODO: find a better check
1511 // This will probably fail with a different driver
1512 return (req.size > 0) && (req.size <= 8192 * 8192);
1513 }
1514
1515 return false;
1516}
1517
Brian Salomon26102cb2018-03-09 09:33:19 -05001518void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001519 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001520
Greg Daniel52e16d92018-04-10 09:34:07 -04001521 GrVkImageInfo info;
1522 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001523 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001524 }
1525}
1526
Brian Osman2d010b62018-08-09 10:55:09 -04001527GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001528 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1529 return GrBackendRenderTarget();
1530 }
1531
Brian Salomon8a375832018-03-14 10:21:40 -04001532 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001533 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001534 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001535 if (kUnknown_GrPixelConfig == config) {
1536 return {};
1537 }
Robert Phillips646f6372018-09-25 09:31:10 -04001538 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001539 &info)) {
1540 return {};
1541 }
Greg Daniel108bb232018-07-03 16:18:29 -04001542 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1543 // Lots of tests don't go through Skia's public interface which will set the config so for
1544 // testing we make sure we set a config here.
1545 beRT.setPixelConfig(config);
1546 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001547}
1548
Brian Salomon52e943a2018-03-13 09:32:39 -04001549void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001550 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001551
Greg Daniel323fbcf2018-04-10 13:46:30 -04001552 GrVkImageInfo info;
1553 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001554 // something in the command buffer may still be using this, so force submit
1555 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001556 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001557 }
1558}
Brian Salomonf865b052018-03-09 09:01:53 -05001559
Greg Daniel26b50a42018-03-08 09:49:58 -05001560void GrVkGpu::testingOnly_flushGpuAndSync() {
1561 this->submitCommandBuffer(kForce_SyncQueue);
1562}
Brian Salomonf865b052018-03-09 09:01:53 -05001563#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001564
Greg Daniel164a9f02016-02-22 09:56:40 -05001565////////////////////////////////////////////////////////////////////////////////
1566
1567void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1568 VkPipelineStageFlags dstStageMask,
1569 bool byRegion,
1570 VkMemoryBarrier* barrier) const {
1571 SkASSERT(fCurrentCmdBuffer);
1572 fCurrentCmdBuffer->pipelineBarrier(this,
1573 srcStageMask,
1574 dstStageMask,
1575 byRegion,
1576 GrVkCommandBuffer::kMemory_BarrierType,
1577 barrier);
1578}
1579
1580void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1581 VkPipelineStageFlags dstStageMask,
1582 bool byRegion,
1583 VkBufferMemoryBarrier* barrier) const {
1584 SkASSERT(fCurrentCmdBuffer);
1585 fCurrentCmdBuffer->pipelineBarrier(this,
1586 srcStageMask,
1587 dstStageMask,
1588 byRegion,
1589 GrVkCommandBuffer::kBufferMemory_BarrierType,
1590 barrier);
1591}
1592
1593void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1594 VkPipelineStageFlags dstStageMask,
1595 bool byRegion,
1596 VkImageMemoryBarrier* barrier) const {
1597 SkASSERT(fCurrentCmdBuffer);
1598 fCurrentCmdBuffer->pipelineBarrier(this,
1599 srcStageMask,
1600 dstStageMask,
1601 byRegion,
1602 GrVkCommandBuffer::kImageMemory_BarrierType,
1603 barrier);
1604}
1605
Greg Daniel51316782017-08-02 15:10:09 +00001606void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1607 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1608 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001609 this->submitCommandBuffer(kSkip_SyncQueue);
1610}
1611
Greg Daniel25af6712018-04-25 10:44:38 -04001612static int get_surface_sample_cnt(GrSurface* surf) {
1613 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1614 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001615 }
Greg Daniel25af6712018-04-25 10:44:38 -04001616 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001617}
1618
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001619void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1620 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001621 GrVkImage* dstImage,
1622 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001623 const SkIRect& srcRect,
1624 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001625#ifdef SK_DEBUG
1626 int dstSampleCnt = get_surface_sample_cnt(dst);
1627 int srcSampleCnt = get_surface_sample_cnt(src);
1628 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1629 src->config(), srcSampleCnt, srcOrigin));
1630
1631#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001632
Greg Daniel164a9f02016-02-22 09:56:40 -05001633 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1634 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001635 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001636 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1637 VK_ACCESS_TRANSFER_WRITE_BIT,
1638 VK_PIPELINE_STAGE_TRANSFER_BIT,
1639 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001640
egdaniel17b89252016-04-05 07:23:38 -07001641 srcImage->setImageLayout(this,
1642 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001643 VK_ACCESS_TRANSFER_READ_BIT,
1644 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001645 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001646
1647 // Flip rect if necessary
1648 SkIRect srcVkRect = srcRect;
1649 int32_t dstY = dstPoint.fY;
1650
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001651 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1652 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001653 srcVkRect.fTop = src->height() - srcRect.fBottom;
1654 srcVkRect.fBottom = src->height() - srcRect.fTop;
1655 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1656 }
1657
1658 VkImageCopy copyRegion;
1659 memset(&copyRegion, 0, sizeof(VkImageCopy));
1660 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1661 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1662 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1663 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001664 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001665
1666 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001667 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001668 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001669 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001670 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1671 1,
1672 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001673
1674 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1675 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001676 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001677}
1678
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001679void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1680 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001681 GrVkImage* dstImage,
1682 GrVkImage* srcImage,
1683 const SkIRect& srcRect,
1684 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001685#ifdef SK_DEBUG
1686 int dstSampleCnt = get_surface_sample_cnt(dst);
1687 int srcSampleCnt = get_surface_sample_cnt(src);
1688 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1689 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001690
Greg Daniel25af6712018-04-25 10:44:38 -04001691#endif
egdaniel17b89252016-04-05 07:23:38 -07001692 dstImage->setImageLayout(this,
1693 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001694 VK_ACCESS_TRANSFER_WRITE_BIT,
1695 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001696 false);
1697
egdaniel17b89252016-04-05 07:23:38 -07001698 srcImage->setImageLayout(this,
1699 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001700 VK_ACCESS_TRANSFER_READ_BIT,
1701 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001702 false);
1703
1704 // Flip rect if necessary
1705 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001706 srcVkRect.fLeft = srcRect.fLeft;
1707 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001708 SkIRect dstRect;
1709 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001710 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001711
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001712 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001713 srcVkRect.fTop = src->height() - srcRect.fBottom;
1714 srcVkRect.fBottom = src->height() - srcRect.fTop;
1715 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001716 srcVkRect.fTop = srcRect.fTop;
1717 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001718 }
1719
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001720 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001721 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1722 } else {
1723 dstRect.fTop = dstPoint.fY;
1724 }
1725 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1726
1727 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1728 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001729 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001730 using std::swap;
1731 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001732 }
1733
1734 VkImageBlit blitRegion;
1735 memset(&blitRegion, 0, sizeof(VkImageBlit));
1736 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1737 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001738 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001739 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1740 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001741 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001742
1743 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001744 *srcImage,
1745 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001746 1,
1747 &blitRegion,
1748 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001749
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001750 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001751 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001752}
1753
Brian Salomon1fabd512018-02-09 09:54:25 -05001754void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1755 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1756 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001757 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001758 SkIRect srcRect = origSrcRect;
1759 SkIPoint dstPoint = origDstPoint;
1760 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1761 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1762 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1763 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1764 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1765 }
1766 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001767 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1768 srcRect.width(), srcRect.height());
1769 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001770}
1771
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001772bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1773 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001774 const SkIRect& srcRect, const SkIPoint& dstPoint,
1775 bool canDiscardOutsideDstRect) {
Greg Daniel25af6712018-04-25 10:44:38 -04001776 GrPixelConfig dstConfig = dst->config();
1777 GrPixelConfig srcConfig = src->config();
1778
1779 int dstSampleCnt = get_surface_sample_cnt(dst);
1780 int srcSampleCnt = get_surface_sample_cnt(src);
1781
1782 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1783 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001784 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001785 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001786 }
1787
Greg Daniel25af6712018-04-25 10:44:38 -04001788 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1789 srcConfig, SkToBool(src->asTexture()))) {
1790 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1791 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001792 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1793 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001794 return true;
1795 }
1796
egdaniel17b89252016-04-05 07:23:38 -07001797 GrVkImage* dstImage;
1798 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001799 GrRenderTarget* dstRT = dst->asRenderTarget();
1800 if (dstRT) {
1801 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1802 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1803 } else {
1804 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001805 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001806 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001807 GrRenderTarget* srcRT = src->asRenderTarget();
1808 if (srcRT) {
1809 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1810 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001811 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001812 SkASSERT(src->asTexture());
1813 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001814 }
1815
Greg Daniel25af6712018-04-25 10:44:38 -04001816 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
1817 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001818 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1819 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07001820 return true;
1821 }
1822
Greg Daniel25af6712018-04-25 10:44:38 -04001823 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
1824 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001825 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1826 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001827 return true;
1828 }
1829
Greg Daniel164a9f02016-02-22 09:56:40 -05001830 return false;
1831}
1832
Brian Salomona6948702018-06-01 15:33:20 -04001833bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
1834 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05001835 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001836 return false;
1837 }
1838
egdaniel66933552016-08-24 07:22:19 -07001839 GrVkImage* image = nullptr;
1840 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1841 if (rt) {
1842 // resolve the render target if necessary
1843 switch (rt->getResolveType()) {
1844 case GrVkRenderTarget::kCantResolve_ResolveType:
1845 return false;
1846 case GrVkRenderTarget::kAutoResolves_ResolveType:
1847 break;
1848 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05001849 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07001850 break;
1851 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001852 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07001853 }
1854 image = rt;
1855 } else {
1856 image = static_cast<GrVkTexture*>(surface->asTexture());
1857 }
1858
1859 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001860 return false;
1861 }
1862
Greg Daniel475eb702018-09-28 14:16:50 -04001863 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
1864 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
1865 // image and then do the read pixels from that.
1866 sk_sp<GrVkTextureRenderTarget> copySurface;
1867 if (dstColorType == GrColorType::kRGB_888x) {
1868 SkASSERT(image->imageFormat() == VK_FORMAT_R8G8B8_UNORM &&
1869 surface->config() == kRGB_888_GrPixelConfig);
1870
1871 // Make a new surface that is RGBA to copy the RGB surface into.
1872 GrSurfaceDesc surfDesc;
1873 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1874 surfDesc.fWidth = width;
1875 surfDesc.fHeight = height;
1876 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
1877 surfDesc.fSampleCnt = 1;
1878
1879 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
1880 VK_IMAGE_USAGE_SAMPLED_BIT |
1881 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1882 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1883
1884 GrVkImage::ImageDesc imageDesc;
1885 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1886 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
1887 imageDesc.fWidth = width;
1888 imageDesc.fHeight = height;
1889 imageDesc.fLevels = 1;
1890 imageDesc.fSamples = 1;
1891 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1892 imageDesc.fUsageFlags = usageFlags;
1893 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
1894
1895 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
1896 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
1897 if (!copySurface) {
1898 return false;
1899 }
1900
1901 int srcSampleCount = 0;
1902 if (rt) {
1903 srcSampleCount = rt->numColorSamples();
1904 }
1905 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
1906 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin,
1907 surface->config(), srcSampleCount, kOrigin) &&
1908 !this->vkCaps().canCopyAsDraw(copySurface->config(), false,
1909 surface->config(), SkToBool(surface->asTexture()))) {
1910 return false;
1911 }
1912 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
1913 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
1914 srcRect, SkIPoint::Make(0,0))) {
1915 return false;
1916 }
1917 top = 0;
1918 left = 0;
1919 dstColorType = GrColorType::kRGBA_8888;
1920 image = copySurface.get();
1921 }
1922
Greg Daniel164a9f02016-02-22 09:56:40 -05001923 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001924 image->setImageLayout(this,
1925 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1926 VK_ACCESS_TRANSFER_READ_BIT,
1927 VK_PIPELINE_STAGE_TRANSFER_BIT,
1928 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001929
Brian Salomonc320b152018-02-20 14:05:36 -05001930 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07001931 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05001932
Greg Daniel164a9f02016-02-22 09:56:40 -05001933 VkBufferImageCopy region;
1934 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001935
1936 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1937 if (copyFromOrigin) {
1938 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04001939 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07001940 } else {
Brian Salomona6948702018-06-01 15:33:20 -04001941 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07001942 region.imageOffset = offset;
1943 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1944 }
1945
1946 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04001947 size_t imageRows = region.imageExtent.height;
egdaniel6fa0a912016-09-12 11:51:29 -07001948 GrVkTransferBuffer* transferBuffer =
Greg Daniel3cdfa092018-02-26 16:14:10 -05001949 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
egdaniel6fa0a912016-09-12 11:51:29 -07001950 kXferGpuToCpu_GrBufferType,
1951 kStream_GrAccessPattern));
1952
1953 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001954 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001955 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001956 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1957 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001958
1959 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001960 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001961 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1962 transferBuffer,
1963 1,
1964 &region);
1965
1966 // make sure the copy to buffer has finished
1967 transferBuffer->addMemoryBarrier(this,
1968 VK_ACCESS_TRANSFER_WRITE_BIT,
1969 VK_ACCESS_HOST_READ_BIT,
1970 VK_PIPELINE_STAGE_TRANSFER_BIT,
1971 VK_PIPELINE_STAGE_HOST_BIT,
1972 false);
1973
1974 // We need to submit the current command buffer to the Queue and make sure it finishes before
1975 // we can copy the data out of the buffer.
1976 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00001977 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05001978 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04001979 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05001980
egdaniel6fa0a912016-09-12 11:51:29 -07001981 if (copyFromOrigin) {
1982 uint32_t skipRows = region.imageExtent.height - height;
1983 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1984 }
1985
Brian Salomona6948702018-06-01 15:33:20 -04001986 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05001987
1988 transferBuffer->unmap();
1989 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001990 return true;
1991}
egdaniel066df7c2016-06-08 14:02:27 -07001992
egdaniel27bb2842016-07-07 11:58:35 -07001993// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1994// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1995// the the entire attachment. Similar requirements for the y and height components.
1996void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1997 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1998 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001999 if ((0 != granularity.width && 1 != granularity.width)) {
2000 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2001 int rightAdj = srcBounds.fRight % granularity.width;
2002 if (rightAdj != 0) {
2003 rightAdj = granularity.width - rightAdj;
2004 }
2005 dstBounds->fRight = srcBounds.fRight + rightAdj;
2006 if (dstBounds->fRight > maxWidth) {
2007 dstBounds->fRight = maxWidth;
2008 dstBounds->fLeft = 0;
2009 } else {
2010 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2011 }
egdaniel27bb2842016-07-07 11:58:35 -07002012 } else {
egdanield5797b32016-09-20 12:57:45 -07002013 dstBounds->fLeft = srcBounds.fLeft;
2014 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002015 }
2016
2017 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002018 if ((0 != granularity.height && 1 != granularity.height)) {
2019 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2020 int bottomAdj = srcBounds.fBottom % granularity.height;
2021 if (bottomAdj != 0) {
2022 bottomAdj = granularity.height - bottomAdj;
2023 }
2024 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2025 if (dstBounds->fBottom > maxHeight) {
2026 dstBounds->fBottom = maxHeight;
2027 dstBounds->fTop = 0;
2028 } else {
2029 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2030 }
egdaniel27bb2842016-07-07 11:58:35 -07002031 } else {
egdanield5797b32016-09-20 12:57:45 -07002032 dstBounds->fTop = srcBounds.fTop;
2033 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002034 }
2035}
2036
Greg Daniel22bc8652017-03-22 15:45:43 -04002037void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002038 const GrVkRenderPass* renderPass,
2039 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002040 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002041 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07002042 const SkIRect* pBounds = &bounds;
2043 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002044 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002045 flippedBounds = bounds;
2046 flippedBounds.fTop = target->height() - bounds.fBottom;
2047 flippedBounds.fBottom = target->height() - bounds.fTop;
2048 pBounds = &flippedBounds;
2049 }
2050
egdaniel27bb2842016-07-07 11:58:35 -07002051 // The bounds we use for the render pass should be of the granularity supported
2052 // by the device.
2053 const VkExtent2D& granularity = renderPass->granularity();
2054 SkIRect adjustedBounds;
2055 if ((0 != granularity.width && 1 != granularity.width) ||
2056 (0 != granularity.height && 1 != granularity.height)) {
2057 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2058 target->width(), target->height());
2059 pBounds = &adjustedBounds;
2060 }
2061
Robert Phillips95214472017-08-08 18:00:03 -04002062#ifdef SK_DEBUG
2063 uint32_t index;
2064 bool result = renderPass->colorAttachmentIndex(&index);
2065 SkASSERT(result && 0 == index);
2066 result = renderPass->stencilAttachmentIndex(&index);
2067 if (result) {
2068 SkASSERT(1 == index);
2069 }
2070#endif
2071 VkClearValue clears[2];
2072 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002073 clears[1].depthStencil.depth = 0.0f;
2074 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002075
2076 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002077 for (int i = 0; i < buffers.count(); ++i) {
2078 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2079 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002080 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002081
Brian Salomon1fabd512018-02-09 09:54:25 -05002082 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002083}
egdaniel9cb63402016-06-23 08:37:05 -07002084
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002085void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2086 if (buffer->asRTCommandBuffer()) {
2087 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2088
2089 fCachedRTCommandBuffer->submit();
2090 fCachedRTCommandBuffer->reset();
2091 } else {
2092 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2093
2094 fCachedTexCommandBuffer->submit();
2095 fCachedTexCommandBuffer->reset();
2096 }
2097}
2098
Greg Daniel6be35232017-03-01 17:01:09 -05002099GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002100 VkFenceCreateInfo createInfo;
2101 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2102 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2103 createInfo.pNext = nullptr;
2104 createInfo.flags = 0;
2105 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002106
2107 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2108 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2109
2110 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002111 return (GrFence)fence;
2112}
2113
Greg Daniel6be35232017-03-01 17:01:09 -05002114bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2115 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2116
2117 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002118 return (VK_SUCCESS == result);
2119}
2120
2121void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002122 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2123}
2124
Greg Daniela5cb7812017-06-16 09:45:32 -04002125sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2126 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002127}
2128
Greg Daniel48661b82018-01-22 16:11:35 -05002129sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2130 GrResourceProvider::SemaphoreWrapType wrapType,
2131 GrWrapOwnership ownership) {
2132 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002133}
2134
Greg Daniel858e12c2018-12-06 11:11:37 -05002135void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002136 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2137
Greg Daniel48661b82018-01-22 16:11:35 -05002138 GrVkSemaphore::Resource* resource = vkSem->getResource();
2139 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002140 resource->ref();
2141 fSemaphoresToSignal.push_back(resource);
2142 }
Greg Daniel6be35232017-03-01 17:01:09 -05002143}
2144
Greg Daniel48661b82018-01-22 16:11:35 -05002145void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002146 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2147
Greg Daniel48661b82018-01-22 16:11:35 -05002148 GrVkSemaphore::Resource* resource = vkSem->getResource();
2149 if (resource->shouldWait()) {
2150 resource->ref();
2151 fSemaphoresToWaitOn.push_back(resource);
2152 }
jvanverth84741b32016-09-30 08:39:02 -07002153}
Brian Osman13dddce2017-05-09 13:19:50 -04002154
2155sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2156 SkASSERT(texture);
2157 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2158 vkTexture->setImageLayout(this,
2159 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2160 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002161 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002162 false);
2163 this->submitCommandBuffer(kSkip_SyncQueue);
2164
2165 // The image layout change serves as a barrier, so no semaphore is needed
2166 return nullptr;
2167}
Greg Danielf5d87582017-12-18 14:48:15 -05002168
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002169void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2170 fDrawables.emplace_back(std::move(drawable));
2171}
2172
Greg Daniel7a82edf2018-12-04 10:54:34 -05002173uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2174 const GrBackendFormat& format) {
2175 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2176 SkASSERT(ycbcrInfo);
2177 if (!ycbcrInfo->isValid()) {
2178 return 0;
2179 }
2180
2181 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2182 samplerState, *ycbcrInfo);
2183
2184 return sampler->uniqueID();
2185}
2186