blob: ee9270b7d3030c95c02c84c2e7fe0e1847f3d872 [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 Daniel41f0e282019-01-28 13:15:05 -050071 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
72 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
73 backendContext.fGetProc("vkEnumerateInstanceVersion",
74 VK_NULL_HANDLE, VK_NULL_HANDLE));
75 uint32_t instanceVersion = 0;
76 if (!localEnumerateInstanceVersion) {
77 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
78 } else {
79 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
80 if (err) {
81 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
82 return nullptr;
83 }
84 }
85
Greg Danielc0b03d82018-08-03 14:41:15 -040086 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
87 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
88 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
89 backendContext.fInstance,
90 VK_NULL_HANDLE));
91
92 if (!localGetPhysicalDeviceProperties) {
93 return nullptr;
94 }
95 VkPhysicalDeviceProperties physDeviceProperties;
96 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
97 uint32_t physDevVersion = physDeviceProperties.apiVersion;
98
Greg Daniel41f0e282019-01-28 13:15:05 -050099 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
100 : instanceVersion;
101
102 instanceVersion = SkTMin(instanceVersion, apiVersion);
103 physDevVersion = SkTMin(physDevVersion, apiVersion);
104
Greg Daniel98bffae2018-08-01 13:25:41 -0400105 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400106
Greg Daniel98bffae2018-08-01 13:25:41 -0400107 if (backendContext.fVkExtensions) {
108 interface.reset(new GrVkInterface(backendContext.fGetProc,
109 backendContext.fInstance,
110 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500111 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400112 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400113 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500114 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400115 return nullptr;
116 }
117 } else {
118 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
119 // make an empty GrVkExtensions and pass that to the GrVkInterface.
120 GrVkExtensions extensions;
121 interface.reset(new GrVkInterface(backendContext.fGetProc,
122 backendContext.fInstance,
123 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500124 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400125 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400126 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500127 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400128 return nullptr;
129 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 }
131
Greg Daniel41f0e282019-01-28 13:15:05 -0500132 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
133 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500134}
135
136////////////////////////////////////////////////////////////////////////////////
137
halcanary9d524f22016-03-29 09:03:52 -0700138GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500139 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
140 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500141 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400142 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000143 , fMemoryAllocator(backendContext.fMemoryAllocator)
144 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400145 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000146 , fDevice(backendContext.fDevice)
147 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400148 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500149 , fResourceProvider(this)
150 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000151 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700152
Greg Daniel81df0412018-05-31 13:13:33 -0400153 if (!fMemoryAllocator) {
154 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000155 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400156 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400157 }
158
ethannicholasb3058bd2016-07-01 08:22:01 -0700159 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700160
Greg Daniela0651ac2018-08-08 09:23:18 -0400161 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400162 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400163 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500164 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400165 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400166 } else if (backendContext.fDeviceFeatures) {
167 VkPhysicalDeviceFeatures2 features2;
168 features2.pNext = nullptr;
169 features2.features = *backendContext.fDeviceFeatures;
170 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500171 features2, instanceVersion, physicalDeviceVersion,
172 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400173 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400174 VkPhysicalDeviceFeatures2 features;
175 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
176 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400177 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400178 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400179 }
180 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400181 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400182 }
183 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400185 }
186 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500187 features, instanceVersion, physicalDeviceVersion,
188 GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400189 }
jvanverth633b3562016-03-23 11:01:22 -0700190 fCaps.reset(SkRef(fVkCaps.get()));
191
Greg Danielf730c182018-07-02 20:15:37 +0000192 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
193 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700194
Greg Daniela870b462019-01-08 15:49:46 -0500195 fResourceProvider.init();
196
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500197 fCmdPool = fResourceProvider.findOrCreateCommandPool();
198 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000199 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700200 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500201}
202
Greg Daniel8606cf82017-05-08 16:17:53 -0400203void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500204 if (fCmdPool) {
205 fCmdPool->getPrimaryCommandBuffer()->end(this);
206 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400207 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
209 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500210 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700211
212 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
213 // on the command buffers even though they have completed. This causes an assert to fire when
214 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500215 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700216#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500217 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700218#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500219 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700220#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500221 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700222#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500223 }
egdanielf8c2be32016-06-24 13:18:27 -0700224#endif
225
egdanielbe9d8212016-09-20 08:54:23 -0700226#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400227 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700228#endif
halcanary9d524f22016-03-29 09:03:52 -0700229
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500230 if (fCmdPool) {
231 fCmdPool->unref(this);
232 fCmdPool = nullptr;
233 }
234
Greg Daniel6be35232017-03-01 17:01:09 -0500235 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
236 fSemaphoresToWaitOn[i]->unref(this);
237 }
238 fSemaphoresToWaitOn.reset();
239
Greg Daniela5cb7812017-06-16 09:45:32 -0400240 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
241 fSemaphoresToSignal[i]->unref(this);
242 }
243 fSemaphoresToSignal.reset();
244
245
egdanielbc9b2962016-09-27 08:00:53 -0700246 fCopyManager.destroyResources(this);
247
Jim Van Verth09557d72016-11-07 11:10:21 -0500248 // must call this just before we destroy the command pool and VkDevice
249 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500250
Greg Danielf730c182018-07-02 20:15:37 +0000251 fMemoryAllocator.reset();
252
253 fQueue = VK_NULL_HANDLE;
254 fDevice = VK_NULL_HANDLE;
255 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400256}
257
258GrVkGpu::~GrVkGpu() {
259 if (!fDisconnected) {
260 this->destroyResources();
261 }
262 delete fCompiler;
263}
264
265
266void GrVkGpu::disconnect(DisconnectType type) {
267 INHERITED::disconnect(type);
268 if (!fDisconnected) {
269 if (DisconnectType::kCleanup == type) {
270 this->destroyResources();
271 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500272 if (fCmdPool) {
273 fCmdPool->unrefAndAbandon();
274 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400275 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400276 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
277 fSemaphoresToWaitOn[i]->unrefAndAbandon();
278 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400279 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
280 fSemaphoresToSignal[i]->unrefAndAbandon();
281 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400282 fCopyManager.abandonResources();
283
284 // must call this just before we destroy the command pool and VkDevice
285 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400286
287 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400288 }
289 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400290 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400291 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400292 fDisconnected = true;
293 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500294}
295
296///////////////////////////////////////////////////////////////////////////////
297
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400298GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400299 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400300 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
301 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400302 if (!fCachedRTCommandBuffer) {
303 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
304 }
305
Greg Daniela41a74a2018-10-09 12:59:23 +0000306 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400307 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400308}
309
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400310GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
311 if (!fCachedTexCommandBuffer) {
312 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
313 }
314
315 fCachedTexCommandBuffer->set(texture, origin);
316 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700317}
318
Greg Daniela5cb7812017-06-16 09:45:32 -0400319void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500320 SkASSERT(fCurrentCmdBuffer);
321 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500322 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400323 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500324
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400325 // We must delete and drawables that have been waitint till submit for us to destroy.
326 fDrawables.reset();
327
Greg Daniel6be35232017-03-01 17:01:09 -0500328 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
329 fSemaphoresToWaitOn[i]->unref(this);
330 }
331 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400332 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
333 fSemaphoresToSignal[i]->unref(this);
334 }
335 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500336
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500337 // Release old command pool and create a new one
338 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500339 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500340 fCmdPool = fResourceProvider.findOrCreateCommandPool();
341 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500342 fCurrentCmdBuffer->begin(this);
343}
344
345///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500346sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
347 GrAccessPattern accessPattern, const void* data) {
348 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700349 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500350 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700351 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
352 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500353 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700354 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500355 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700356 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
357 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500358 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700359 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500360 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400361 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
362 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500363 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700364 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500365 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400366 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
367 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500368 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700369 break;
cdalton397536c2016-03-25 12:15:03 -0700370 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400371 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700372 return nullptr;
373 }
cdalton1bf3e712016-04-19 10:00:02 -0700374 if (data && buff) {
375 buff->updateData(data, size);
376 }
377 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500378}
379
Brian Salomona9b04b92018-06-01 15:04:28 -0400380bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
381 GrColorType srcColorType, const GrMipLevel texels[],
382 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500383 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
384 if (!vkTex) {
385 return false;
386 }
387
jvanverth900bd4a2016-04-29 13:53:12 -0700388 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400389 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800390 return false;
391 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800392
Jim Van Verth1676cb92019-01-15 13:24:45 -0500393 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500394 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400395 bool linearTiling = vkTex->isLinearTiled();
396 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400397 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400398 SkDebugf("Can't upload mipmap data to linear tiled texture");
399 return false;
400 }
401 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
402 // Need to change the layout to general in order to perform a host write
403 vkTex->setImageLayout(this,
404 VK_IMAGE_LAYOUT_GENERAL,
405 VK_ACCESS_HOST_WRITE_BIT,
406 VK_PIPELINE_STAGE_HOST_BIT,
407 false);
408 this->submitCommandBuffer(kForce_SyncQueue);
409 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400410 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400411 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500412 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400413 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400414 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
415 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500416 }
egdaniel4583ec52016-06-27 12:57:00 -0700417
jvanverth900bd4a2016-04-29 13:53:12 -0700418 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500419}
420
Brian Salomonc320b152018-02-20 14:05:36 -0500421bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomondbf70722019-02-07 11:31:24 -0500422 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400423 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500424 // Can't transfer compressed data
425 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
426
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400427 // Vulkan only supports 4-byte aligned offsets
428 if (SkToBool(bufferOffset & 0x2)) {
429 return false;
430 }
431 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
432 if (!vkTex) {
433 return false;
434 }
435 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
436 if (!vkBuffer) {
437 return false;
438 }
439
Greg Daniel660cc992017-06-26 14:55:05 -0400440 SkDEBUGCODE(
441 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
442 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
443 SkASSERT(bounds.contains(subRect));
444 )
Brian Salomonc320b152018-02-20 14:05:36 -0500445 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400446 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500447 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400448 }
449
450 // Set up copy region
451 VkBufferImageCopy region;
452 memset(&region, 0, sizeof(VkBufferImageCopy));
453 region.bufferOffset = bufferOffset;
454 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
455 region.bufferImageHeight = 0;
456 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
457 region.imageOffset = { left, top, 0 };
458 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
459
460 // Change layout of our target so it can be copied to
461 vkTex->setImageLayout(this,
462 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
463 VK_ACCESS_TRANSFER_WRITE_BIT,
464 VK_PIPELINE_STAGE_TRANSFER_BIT,
465 false);
466
467 // Copy the buffer to the image
468 fCurrentCmdBuffer->copyBufferToImage(this,
469 vkBuffer,
470 vkTex,
471 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
472 1,
473 &region);
474
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400475 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400476 return true;
477}
478
Brian Salomon1fabd512018-02-09 09:54:25 -0500479void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
480 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700481 SkASSERT(dst);
482 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
483
egdaniel4bcd62e2016-08-31 07:37:31 -0700484 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500485 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
486 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
487 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
488 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
489 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700490
Greg Danielbc26c392017-04-18 13:32:10 -0400491 GrVkImage* dstImage;
492 GrRenderTarget* dstRT = dst->asRenderTarget();
493 if (dstRT) {
494 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400495 dstImage = vkRT;
496 } else {
497 SkASSERT(dst->asTexture());
498 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
499 }
500 dstImage->setImageLayout(this,
501 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
502 VK_ACCESS_TRANSFER_WRITE_BIT,
503 VK_PIPELINE_STAGE_TRANSFER_BIT,
504 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700505
506 src->msaaImage()->setImageLayout(this,
507 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
508 VK_ACCESS_TRANSFER_READ_BIT,
509 VK_PIPELINE_STAGE_TRANSFER_BIT,
510 false);
511
Greg Danielbc26c392017-04-18 13:32:10 -0400512 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700513}
514
Brian Salomon1fabd512018-02-09 09:54:25 -0500515void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700516 if (target->needsResolve()) {
517 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700518 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
519 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500520
egdaniel4bcd62e2016-08-31 07:37:31 -0700521 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700522
Brian Salomon1fabd512018-02-09 09:54:25 -0500523 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700524
525 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500526
527 if (requiresSubmit) {
528 this->submitCommandBuffer(kSkip_SyncQueue);
529 }
egdaniel52ad2512016-08-04 12:50:01 -0700530 }
531}
532
Brian Salomona9b04b92018-06-01 15:04:28 -0400533bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
534 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500535 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700536 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500537
Jim Van Verth1676cb92019-01-15 13:24:45 -0500538 // If we're uploading compressed data then we should be using uploadCompressedTexData
539 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
540 GrSRGBEncoded::kNo)));
541
Greg Daniel660cc992017-06-26 14:55:05 -0400542 SkDEBUGCODE(
543 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
544 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
545 SkASSERT(bounds.contains(subRect));
546 )
Brian Salomonc320b152018-02-20 14:05:36 -0500547 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500548 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400549 if (!rowBytes) {
550 rowBytes = trimRowBytes;
551 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500552
jvanverth900bd4a2016-04-29 13:53:12 -0700553 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
554 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
555 const VkImageSubresource subres = {
556 VK_IMAGE_ASPECT_COLOR_BIT,
557 0, // mipLevel
558 0, // arraySlice
559 };
560 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500561
jvanverth900bd4a2016-04-29 13:53:12 -0700562 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500563
jvanverth900bd4a2016-04-29 13:53:12 -0700564 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700565 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700566 &subres,
567 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500568
jvanverth1e305ba2016-06-01 09:39:15 -0700569 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400570 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700571 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400572 SkASSERT(size + offset <= alloc.fSize);
573 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
574 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700575 return false;
576 }
Greg Daniel81df0412018-05-31 13:13:33 -0400577 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700578
Brian Salomona9b04b92018-06-01 15:04:28 -0400579 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
580 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700581
Greg Daniele35a99e2018-03-02 11:44:22 -0500582 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400583 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700584
585 return true;
586}
587
Brian Salomona9b04b92018-06-01 15:04:28 -0400588bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
589 GrColorType dataColorType, const GrMipLevel texels[],
590 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700591 SkASSERT(!tex->isLinearTiled());
592 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400593 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700594 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
595
Greg Danieldd20e912017-04-07 14:42:23 -0400596 // We assume that if the texture has mip levels, we either upload to all the levels or just the
597 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400598 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400599
Jim Van Verth1676cb92019-01-15 13:24:45 -0500600 // If we're uploading compressed data then we should be using uploadCompressedTexData
601 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
602 GrSRGBEncoded::kNo)));
603
jvanverth900bd4a2016-04-29 13:53:12 -0700604 if (width == 0 || height == 0) {
605 return false;
606 }
607
Greg Daniel475eb702018-09-28 14:16:50 -0400608 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
609 return false;
610 }
611
612 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
613 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500614 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
615 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400616 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
617 // blit or draw.
618 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
619 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
620 return false;
621 }
622 mipLevelCount = 1;
623 }
624
Brian Salomond1eaf492017-05-18 10:02:08 -0400625 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500626 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700627
628 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700629 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
630 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400631 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
632
Greg Daniel475eb702018-09-28 14:16:50 -0400633 texelsShallowCopy.reset(mipLevelCount);
634 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700635
Robert Phillips590533f2017-07-11 14:22:35 -0400636 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700637 individualMipOffsets.push_back(0);
638 size_t combinedBufferSize = width * bpp * height;
639 int currentWidth = width;
640 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400641 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400642 combinedBufferSize = 0;
643 }
644
Greg Daniel468fd632017-03-22 17:03:45 -0400645 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
646 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
647 SkASSERT((bpp & (bpp - 1)) == 0);
648 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400649 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700650 currentWidth = SkTMax(1, currentWidth/2);
651 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400652
Greg Daniel55afd6d2017-09-29 09:32:44 -0400653 if (texelsShallowCopy[currentMipLevel].fPixels) {
654 const size_t trimmedSize = currentWidth * bpp * currentHeight;
655 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
656 if (alignmentDiff != 0) {
657 combinedBufferSize += alignmentMask - alignmentDiff + 1;
658 }
659 individualMipOffsets.push_back(combinedBufferSize);
660 combinedBufferSize += trimmedSize;
661 } else {
662 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400663 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400664 }
665 if (0 == combinedBufferSize) {
666 // We don't actually have any data to upload so just return success
667 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700668 }
669
670 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500671 sk_sp<GrVkTransferBuffer> transferBuffer =
672 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400673 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700674 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400675 }
jvanverth900bd4a2016-04-29 13:53:12 -0700676
Greg Daniel475eb702018-09-28 14:16:50 -0400677 int uploadLeft = left;
678 int uploadTop = top;
679 GrVkTexture* uploadTexture = tex;
680 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
681 // R8G8B8A8_UNORM image and then copy it.
682 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500683 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400684 GrSurfaceDesc surfDesc;
685 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
686 surfDesc.fWidth = width;
687 surfDesc.fHeight = height;
688 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
689 surfDesc.fSampleCnt = 1;
690
691 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
692 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
693 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
694
695 GrVkImage::ImageDesc imageDesc;
696 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
697 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
698 imageDesc.fWidth = width;
699 imageDesc.fHeight = height;
700 imageDesc.fLevels = 1;
701 imageDesc.fSamples = 1;
702 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
703 imageDesc.fUsageFlags = usageFlags;
704 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
705
706 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
707 GrMipMapsStatus::kNotAllocated);
708 if (!copyTexture) {
709 return false;
710 }
711 uploadTexture = copyTexture.get();
712 uploadLeft = 0;
713 uploadTop = 0;
714 }
715
jvanverth900bd4a2016-04-29 13:53:12 -0700716 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400717 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700718
jvanverthc578b0632016-05-02 10:58:12 -0700719 currentWidth = width;
720 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400721 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400722 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400723 if (texelsShallowCopy[currentMipLevel].fPixels) {
724 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
725 const size_t trimRowBytes = currentWidth * bpp;
726 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
727 ? texelsShallowCopy[currentMipLevel].fRowBytes
728 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700729
Greg Daniel55afd6d2017-09-29 09:32:44 -0400730 // copy data into the buffer, skipping the trailing bytes
731 char* dst = buffer + individualMipOffsets[currentMipLevel];
732 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400733 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400734
735 VkBufferImageCopy& region = regions.push_back();
736 memset(&region, 0, sizeof(VkBufferImageCopy));
737 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
738 region.bufferRowLength = currentWidth;
739 region.bufferImageHeight = currentHeight;
740 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400741 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400742 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700743 }
jvanverthc578b0632016-05-02 10:58:12 -0700744 currentWidth = SkTMax(1, currentWidth/2);
745 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400746 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700747 }
748
jvanverth9d54afc2016-09-20 09:20:03 -0700749 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700750 transferBuffer->unmap();
751
jvanverth900bd4a2016-04-29 13:53:12 -0700752 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400753 uploadTexture->setImageLayout(this,
754 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
755 VK_ACCESS_TRANSFER_WRITE_BIT,
756 VK_PIPELINE_STAGE_TRANSFER_BIT,
757 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700758
759 // Copy the buffer to the image
760 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500761 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400762 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700763 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
764 regions.count(),
765 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400766
767 // If we copied the data into a temporary image first, copy that image into our main texture
768 // now.
769 if (copyTexture.get()) {
770 SkASSERT(dataColorType == GrColorType::kRGB_888x);
771 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
772 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
773 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
774 false));
775 }
Robert Phillips590533f2017-07-11 14:22:35 -0400776 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400777 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400778 }
jvanverth900bd4a2016-04-29 13:53:12 -0700779
Greg Daniel164a9f02016-02-22 09:56:40 -0500780 return true;
781}
782
Jim Van Verth1676cb92019-01-15 13:24:45 -0500783// It's probably possible to roll this into uploadTexDataOptimal,
784// but for now it's easier to maintain as a separate entity.
785bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
786 GrColorType dataColorType, const GrMipLevel texels[],
787 int mipLevelCount) {
788 SkASSERT(!tex->isLinearTiled());
789 // For now the assumption is that our rect is the entire texture.
790 // Compressed textures are read-only so this should be a reasonable assumption.
791 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
792
793 // We assume that if the texture has mip levels, we either upload to all the levels or just the
794 // first.
795 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
796
797 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
798 GrSRGBEncoded::kNo)));
799
800 if (width == 0 || height == 0) {
801 return false;
802 }
803
804 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
805 return false;
806 }
807
808 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
809
810 SkTArray<size_t> individualMipOffsets(mipLevelCount);
811 individualMipOffsets.push_back(0);
812 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
813 int currentWidth = width;
814 int currentHeight = height;
815 if (!texels[0].fPixels) {
816 return false;
817 }
818
819 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
820 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
821 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
822 currentWidth = SkTMax(1, currentWidth / 2);
823 currentHeight = SkTMax(1, currentHeight / 2);
824
825 if (texels[currentMipLevel].fPixels) {
826 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
827 currentHeight);
828 individualMipOffsets.push_back(combinedBufferSize);
829 combinedBufferSize += dataSize;
830 } else {
831 return false;
832 }
833 }
834 if (0 == combinedBufferSize) {
835 // We don't have any data to upload so fail (compressed textures are read-only).
836 return false;
837 }
838
839 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500840 sk_sp<GrVkTransferBuffer> transferBuffer =
841 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500842 if (!transferBuffer) {
843 return false;
844 }
845
846 int uploadLeft = left;
847 int uploadTop = top;
848 GrVkTexture* uploadTexture = tex;
849
850 char* buffer = (char*)transferBuffer->map();
851 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
852
853 currentWidth = width;
854 currentHeight = height;
855 int layerHeight = uploadTexture->height();
856 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
857 if (texels[currentMipLevel].fPixels) {
858 // Again, we're assuming that our rect is the entire texture
859 SkASSERT(currentHeight == layerHeight);
860 SkASSERT(0 == uploadLeft && 0 == uploadTop);
861
862 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
863 currentHeight);
864
865 // copy data into the buffer, skipping the trailing bytes
866 char* dst = buffer + individualMipOffsets[currentMipLevel];
867 const char* src = (const char*)texels[currentMipLevel].fPixels;
868 memcpy(dst, src, dataSize);
869
870 VkBufferImageCopy& region = regions.push_back();
871 memset(&region, 0, sizeof(VkBufferImageCopy));
872 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
873 region.bufferRowLength = currentWidth;
874 region.bufferImageHeight = currentHeight;
875 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
876 region.imageOffset = { uploadLeft, uploadTop, 0 };
877 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
878 }
879 currentWidth = SkTMax(1, currentWidth / 2);
880 currentHeight = SkTMax(1, currentHeight / 2);
881 layerHeight = currentHeight;
882 }
883
884 // no need to flush non-coherent memory, unmap will do that for us
885 transferBuffer->unmap();
886
887 // Change layout of our target so it can be copied to
888 uploadTexture->setImageLayout(this,
889 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
890 VK_ACCESS_TRANSFER_WRITE_BIT,
891 VK_PIPELINE_STAGE_TRANSFER_BIT,
892 false);
893
894 // Copy the buffer to the image
895 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500896 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500897 uploadTexture,
898 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
899 regions.count(),
900 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500901
902 if (1 == mipLevelCount) {
903 tex->texturePriv().markMipMapsDirty();
904 }
905
906 return true;
907}
908
Greg Daniel164a9f02016-02-22 09:56:40 -0500909////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400910sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500911 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500912 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
913
914 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500915 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700916
Greg Daniel164a9f02016-02-22 09:56:40 -0500917 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
918 if (renderTarget) {
919 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
920 }
921
922 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
923 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
924 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700925 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500926 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
927 // texture.
928 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
929
Greg Daniel164a9f02016-02-22 09:56:40 -0500930 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700931 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500932 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400933 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500934 GrVkImage::ImageDesc imageDesc;
935 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
936 imageDesc.fFormat = pixelFormat;
937 imageDesc.fWidth = desc.fWidth;
938 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400939 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500940 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400941 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500942 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400943 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500944
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400945 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
946 if (mipLevels > 1) {
947 mipMapsStatus = GrMipMapsStatus::kValid;
948 for (int i = 0; i < mipLevels; ++i) {
949 if (!texels[i].fPixels) {
950 mipMapsStatus = GrMipMapsStatus::kDirty;
951 break;
952 }
Greg Daniel834f1202017-10-09 15:06:20 -0400953 }
954 }
955
Robert Phillips67d52cf2017-06-05 13:38:13 -0400956 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500957 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -0400958 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
959 imageDesc,
960 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500961 } else {
Greg Daniel475eb702018-09-28 14:16:50 -0400962 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500963 }
964
965 if (!tex) {
966 return nullptr;
967 }
968
Jim Van Verth1676cb92019-01-15 13:24:45 -0500969 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -0500970 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400971 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500972 bool success;
973 if (isCompressed) {
974 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
975 colorType, texels, mipLevelCount);
976 } else {
977 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
978 colorType, texels, mipLevelCount);
979 }
980 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500981 tex->unref();
982 return nullptr;
983 }
984 }
985
Jim Van Verth1676cb92019-01-15 13:24:45 -0500986 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400987 VkClearColorValue zeroClearColor;
988 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
989 VkImageSubresourceRange range;
990 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
991 range.baseArrayLayer = 0;
992 range.baseMipLevel = 0;
993 range.layerCount = 1;
994 range.levelCount = 1;
995 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
996 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400997 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400998 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400999 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001000}
1001
1002////////////////////////////////////////////////////////////////////////////////
1003
Greg Daniel6888c0d2017-08-25 11:55:50 -04001004void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1005 VkDeviceSize dstOffset, VkDeviceSize size) {
1006 VkBufferCopy copyRegion;
1007 copyRegion.srcOffset = srcOffset;
1008 copyRegion.dstOffset = dstOffset;
1009 copyRegion.size = size;
1010 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1011}
1012
jvanverthdb379092016-07-07 11:18:46 -07001013bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1014 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001015 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001016 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001017
1018 return true;
1019}
1020
1021////////////////////////////////////////////////////////////////////////////////
1022
Greg Daniel7e000222018-12-03 10:08:21 -05001023static bool check_image_info(const GrVkCaps& caps,
1024 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001025 GrPixelConfig config,
1026 bool isWrappedRT) {
1027 if (VK_NULL_HANDLE == info.fImage) {
1028 return false;
1029 }
1030
1031 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001032 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001033 }
1034
Greg Daniel7e000222018-12-03 10:08:21 -05001035 if (info.fYcbcrConversionInfo.isValid()) {
1036 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1037 return false;
1038 }
jvanverthfd359ca2016-03-18 11:57:24 -07001039 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001040
Greg Danielcb324152019-02-25 11:36:53 -05001041 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1042 return false;
1043 }
1044
Greg Daniel52e16d92018-04-10 09:34:07 -04001045 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001046 return true;
1047}
1048
1049sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001050 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1051 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001052 GrVkImageInfo imageInfo;
1053 if (!backendTex.getVkImageInfo(&imageInfo)) {
1054 return nullptr;
1055 }
1056
Greg Danielcb324152019-02-25 11:36:53 -05001057 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001058 return nullptr;
1059 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001060
Greg Daniel164a9f02016-02-22 09:56:40 -05001061 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001062 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001063 surfDesc.fWidth = backendTex.width();
1064 surfDesc.fHeight = backendTex.height();
1065 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001066 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001067
Greg Daniel52e16d92018-04-10 09:34:07 -04001068 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1069 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001070 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1071 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001072}
1073
1074sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001075 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001076 GrWrapOwnership ownership,
1077 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001078 GrVkImageInfo imageInfo;
1079 if (!backendTex.getVkImageInfo(&imageInfo)) {
1080 return nullptr;
1081 }
1082
Greg Danielcb324152019-02-25 11:36:53 -05001083 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001084 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001085 }
Brian Salomond17f6582017-07-19 18:28:58 -04001086
1087 GrSurfaceDesc surfDesc;
1088 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1089 surfDesc.fWidth = backendTex.width();
1090 surfDesc.fHeight = backendTex.height();
1091 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001092 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001093
Greg Daniel52e16d92018-04-10 09:34:07 -04001094 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1095 SkASSERT(layout);
1096
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001097 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1098 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001099}
1100
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001101sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001102 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1103 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1104 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1105 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001106 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001107 return nullptr;
1108 }
halcanary9d524f22016-03-29 09:03:52 -07001109
Greg Daniel323fbcf2018-04-10 13:46:30 -04001110 GrVkImageInfo info;
1111 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001112 return nullptr;
1113 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001114
Greg Danielcb324152019-02-25 11:36:53 -05001115 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001116 return nullptr;
1117 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001118
Greg Daniel164a9f02016-02-22 09:56:40 -05001119 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001120 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001121 desc.fWidth = backendRT.width();
1122 desc.fHeight = backendRT.height();
1123 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001124 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001125
Greg Daniel323fbcf2018-04-10 13:46:30 -04001126 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001127
Greg Daniel323fbcf2018-04-10 13:46:30 -04001128 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001129 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001130
1131 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1132 SkASSERT(!backendRT.stencilBits());
1133 if (tgt) {
1134 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001135 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001136
Ben Wagnerff134f22018-04-24 16:29:16 -04001137 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001138}
1139
Greg Daniel7ef28f32017-04-20 16:41:55 +00001140sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001141 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001142
Greg Daniel52e16d92018-04-10 09:34:07 -04001143 GrVkImageInfo imageInfo;
1144 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001145 return nullptr;
1146 }
Greg Danielcb324152019-02-25 11:36:53 -05001147 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001148 return nullptr;
1149 }
1150
Greg Danielcb324152019-02-25 11:36:53 -05001151
Brian Osman33910292017-04-18 14:38:53 -04001152 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001153 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001154 desc.fWidth = tex.width();
1155 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001156 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001157 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1158 if (!desc.fSampleCnt) {
1159 return nullptr;
1160 }
Brian Osman33910292017-04-18 14:38:53 -04001161
Greg Daniel52e16d92018-04-10 09:34:07 -04001162 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1163 SkASSERT(layout);
1164
Ben Wagnerff134f22018-04-24 16:29:16 -04001165 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001166}
1167
Greg Danielb46add82019-01-02 14:51:29 -05001168sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1169 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1170 int maxSize = this->caps()->maxTextureSize();
1171 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1172 return nullptr;
1173 }
1174
1175 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1176 if (!backendFormat.isValid()) {
1177 return nullptr;
1178 }
1179 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1180 imageInfo.colorType());
1181 if (config == kUnknown_GrPixelConfig) {
1182 return nullptr;
1183 }
1184
1185 GrSurfaceDesc desc;
1186 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1187 desc.fWidth = imageInfo.width();
1188 desc.fHeight = imageInfo.height();
1189 desc.fConfig = config;
1190 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1191 if (!desc.fSampleCnt) {
1192 return nullptr;
1193 }
1194
1195 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1196}
1197
Brian Salomon930f9392018-06-20 16:25:26 -04001198bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1199 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001200 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001201 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001202 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001203 return false;
jvanverth62340062016-04-26 08:01:44 -07001204 }
1205
jvanverth62340062016-04-26 08:01:44 -07001206 // determine if we can blit to and from this format
1207 const GrVkCaps& caps = this->vkCaps();
1208 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001209 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1210 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001211 return false;
jvanverth62340062016-04-26 08:01:44 -07001212 }
1213
egdaniel7ac5da82016-07-15 13:41:42 -07001214 int width = tex->width();
1215 int height = tex->height();
1216 VkImageBlit blitRegion;
1217 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001218
jvanverth82c05582016-05-03 11:19:01 -07001219 // SkMipMap doesn't include the base level in the level count so we have to add 1
1220 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001221 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001222
Greg Danielda86e282018-06-13 09:41:19 -04001223 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001224 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1225 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001226
jvanverth50c46c72016-05-06 12:31:28 -07001227 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001228 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001229 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1230 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001231 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1232 nullptr, // pNext
1233 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1234 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1235 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1236 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1237 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1238 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1239 vkTex->image(), // image
1240 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001241 };
1242
jvanverth62340062016-04-26 08:01:44 -07001243 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001244 uint32_t mipLevel = 1;
1245 while (mipLevel < levelCount) {
1246 int prevWidth = width;
1247 int prevHeight = height;
1248 width = SkTMax(1, width / 2);
1249 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001250
jvanverth50c46c72016-05-06 12:31:28 -07001251 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001252 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1253 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001254
1255 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001256 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001257 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001258 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1259 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001260 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001261 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001262 vkTex->resource(),
1263 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001264 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001265 vkTex->resource(),
1266 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001267 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001268 1,
1269 &blitRegion,
1270 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001271 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001272 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001273 // This barrier logically is not needed, but it changes the final level to the same layout as
1274 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1275 // future layout changes easier.
1276 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001277 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1278 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001279 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1280 return true;
jvanverth62340062016-04-26 08:01:44 -07001281}
1282
Greg Daniel164a9f02016-02-22 09:56:40 -05001283////////////////////////////////////////////////////////////////////////////////
1284
1285GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1286 int width,
1287 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001288 SkASSERT(width >= rt->width());
1289 SkASSERT(height >= rt->height());
1290
1291 int samples = rt->numStencilSamples();
1292
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001293 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001294
1295 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001296 width,
1297 height,
1298 samples,
1299 sFmt));
1300 fStats.incStencilAttachmentCreates();
1301 return stencil;
1302}
1303
1304////////////////////////////////////////////////////////////////////////////////
1305
Brian Salomon52e943a2018-03-13 09:32:39 -04001306bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001307 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1308 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001309 VkDeviceSize size = dstRowBytes * h;
1310 VkDeviceSize offset = bufferOffset;
1311 SkASSERT(size + offset <= alloc.fSize);
1312 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1313 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001314 return false;
1315 }
Greg Daniel81df0412018-05-31 13:13:33 -04001316 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001317
Greg Daniel20ece3a2017-03-28 10:24:43 -04001318 if (srcData) {
1319 // If there is no padding on dst we can do a single memcopy.
1320 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001321 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001322 } else {
1323 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1324 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001325 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001326 }
Greg Daniel81df0412018-05-31 13:13:33 -04001327 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1328 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001329 return true;
1330}
1331
Brian Salomonf865b052018-03-09 09:01:53 -05001332#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001333bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1334 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001335 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001336 SkASSERT(texturable || renderable);
1337 if (!texturable) {
1338 SkASSERT(GrMipMapped::kNo == mipMapped);
1339 SkASSERT(!srcData);
1340 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001341 VkFormat pixelFormat;
1342 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001343 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001344 }
1345
Brian Salomon52e943a2018-03-13 09:32:39 -04001346 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1347 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001348 }
1349
Brian Salomon52e943a2018-03-13 09:32:39 -04001350 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1351 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001352 }
1353
1354 // Currently we don't support uploading pixel data when mipped.
1355 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001356 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001357 }
1358
Brian Salomon52e943a2018-03-13 09:32:39 -04001359 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001360 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1361 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001362 if (texturable) {
1363 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1364 }
1365 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001366 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1367 }
1368
1369 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001370 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001371 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001372
1373 // Create Image
1374 VkSampleCountFlagBits vkSamples;
1375 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001376 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001377 }
1378
1379 // Figure out the number of mip levels.
1380 uint32_t mipLevels = 1;
1381 if (GrMipMapped::kYes == mipMapped) {
1382 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1383 }
1384
1385 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001386 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1387 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001388 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001389 VK_IMAGE_TYPE_2D, // VkImageType
1390 pixelFormat, // VkFormat
1391 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1392 mipLevels, // mipLevels
1393 1, // arrayLayers
1394 vkSamples, // samples
1395 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1396 usageFlags, // VkImageUsageFlags
1397 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1398 0, // queueFamilyCount
1399 0, // pQueueFamilyIndices
1400 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001401 };
1402
Brian Salomon52e943a2018-03-13 09:32:39 -04001403 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1404 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001405
Brian Salomonde9f5462018-03-07 14:23:58 -05001406 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001407 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001408 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001409 }
1410
1411 // 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 -05001412 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001413 VkBuffer buffer = VK_NULL_HANDLE;
1414
1415 VkResult err;
1416 const VkCommandBufferAllocateInfo cmdInfo = {
1417 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1418 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001419 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001420 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1421 1 // bufferCount
1422 };
1423
1424 VkCommandBuffer cmdBuffer;
1425 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1426 if (err) {
1427 GrVkMemory::FreeImageMemory(this, false, alloc);
1428 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001429 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001430 }
1431
1432 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1433 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1434 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1435 cmdBufferBeginInfo.pNext = nullptr;
1436 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1437 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1438
1439 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1440 SkASSERT(!err);
1441
1442 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001443 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001444
Robert Phillips646f6372018-09-25 09:31:10 -04001445 const size_t trimRowBytes = w * bpp;
1446 if (!srcRowBytes) {
1447 srcRowBytes = trimRowBytes;
1448 }
1449
Brian Salomonde9f5462018-03-07 14:23:58 -05001450 SkTArray<size_t> individualMipOffsets(mipLevels);
1451 individualMipOffsets.push_back(0);
1452 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001453 if (GrPixelConfigIsCompressed(config)) {
1454 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1455 bpp = 4; // we have at least this alignment, which will pass the code below
1456 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001457 int currentWidth = w;
1458 int currentHeight = h;
1459 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1460 // config. This works with the assumption that the bytes in pixel config is always a power
1461 // of 2.
1462 SkASSERT((bpp & (bpp - 1)) == 0);
1463 const size_t alignmentMask = 0x3 | (bpp - 1);
1464 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1465 currentWidth = SkTMax(1, currentWidth / 2);
1466 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001467
Jim Van Verth1676cb92019-01-15 13:24:45 -05001468 size_t trimmedSize;
1469 if (GrPixelConfigIsCompressed(config)) {
1470 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1471 } else {
1472 trimmedSize = currentWidth * bpp * currentHeight;
1473 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001474 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1475 if (alignmentDiff != 0) {
1476 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001477 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001478 individualMipOffsets.push_back(combinedBufferSize);
1479 combinedBufferSize += trimmedSize;
1480 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001481
Brian Salomonde9f5462018-03-07 14:23:58 -05001482 VkBufferCreateInfo bufInfo;
1483 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1484 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1485 bufInfo.flags = 0;
1486 bufInfo.size = combinedBufferSize;
1487 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1488 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1489 bufInfo.queueFamilyIndexCount = 0;
1490 bufInfo.pQueueFamilyIndices = nullptr;
1491 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001492
Brian Salomonde9f5462018-03-07 14:23:58 -05001493 if (err) {
1494 GrVkMemory::FreeImageMemory(this, false, alloc);
1495 VK_CALL(DestroyImage(fDevice, image, nullptr));
1496 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001497 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001498 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001499 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001500
Brian Salomonde9f5462018-03-07 14:23:58 -05001501 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1502 &bufferAlloc)) {
1503 GrVkMemory::FreeImageMemory(this, false, alloc);
1504 VK_CALL(DestroyImage(fDevice, image, nullptr));
1505 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1506 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001507 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001508 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001509 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001510
Brian Salomonde9f5462018-03-07 14:23:58 -05001511 currentWidth = w;
1512 currentHeight = h;
1513 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1514 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001515 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001516 bool result;
1517 if (GrPixelConfigIsCompressed(config)) {
1518 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1519 size_t currentRowBytes = levelSize / currentHeight;
1520 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1521 currentRowBytes, currentRowBytes, currentHeight);
1522 } else {
1523 size_t currentRowBytes = bpp * currentWidth;
1524 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1525 currentRowBytes, trimRowBytes, currentHeight);
1526 }
1527 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001528 GrVkMemory::FreeImageMemory(this, false, alloc);
1529 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001530 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001531 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1532 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001533 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001534 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001535 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001536 currentWidth = SkTMax(1, currentWidth / 2);
1537 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001538 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001539
1540 // Set image layout and add barrier
1541 VkImageMemoryBarrier barrier;
1542 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1543 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1544 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001545 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001546 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1547 barrier.oldLayout = initialLayout;
1548 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1549 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1550 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1551 barrier.image = image;
1552 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1553
Greg Danielf7828d02018-10-09 12:01:32 -04001554 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001555 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1556 &barrier));
1557 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1558
1559 SkTArray<VkBufferImageCopy> regions(mipLevels);
1560
1561 currentWidth = w;
1562 currentHeight = h;
1563 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1564 // Submit copy command
1565 VkBufferImageCopy& region = regions.push_back();
1566 memset(&region, 0, sizeof(VkBufferImageCopy));
1567 region.bufferOffset = individualMipOffsets[currentMipLevel];
1568 region.bufferRowLength = currentWidth;
1569 region.bufferImageHeight = currentHeight;
1570 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1571 region.imageOffset = {0, 0, 0};
1572 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1573 currentWidth = SkTMax(1, currentWidth / 2);
1574 currentHeight = SkTMax(1, currentHeight / 2);
1575 }
1576
1577 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1578 regions.begin()));
1579
Brian Salomon52e943a2018-03-13 09:32:39 -04001580 if (texturable) {
1581 // Change Image layout to shader read since if we use this texture as a borrowed textures
1582 // within Ganesh we require that its layout be set to that
1583 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1584 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1585 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001586 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001587 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1588 barrier.oldLayout = initialLayout;
1589 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1590 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1591 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1592 barrier.image = image;
1593 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001594 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001595 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1596 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001597 0,
1598 0, nullptr,
1599 0, nullptr,
1600 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001601 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001602 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001603
1604 // End CommandBuffer
1605 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1606 SkASSERT(!err);
1607
1608 // Create Fence for queue
1609 VkFence fence;
1610 VkFenceCreateInfo fenceInfo;
1611 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1612 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1613
1614 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1615 SkASSERT(!err);
1616
1617 VkSubmitInfo submitInfo;
1618 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1619 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1620 submitInfo.pNext = nullptr;
1621 submitInfo.waitSemaphoreCount = 0;
1622 submitInfo.pWaitSemaphores = nullptr;
1623 submitInfo.pWaitDstStageMask = 0;
1624 submitInfo.commandBufferCount = 1;
1625 submitInfo.pCommandBuffers = &cmdBuffer;
1626 submitInfo.signalSemaphoreCount = 0;
1627 submitInfo.pSignalSemaphores = nullptr;
1628 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1629 SkASSERT(!err);
1630
1631 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1632 if (VK_TIMEOUT == err) {
1633 GrVkMemory::FreeImageMemory(this, false, alloc);
1634 VK_CALL(DestroyImage(fDevice, image, nullptr));
1635 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1636 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001637 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001638 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1639 SkDebugf("Fence failed to signal: %d\n", err);
1640 SK_ABORT("failing");
1641 }
1642 SkASSERT(!err);
1643
1644 // Clean up transfer resources
1645 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1646 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1647 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1648 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001649 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001650 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1651
Brian Salomon52e943a2018-03-13 09:32:39 -04001652 info->fImage = image;
1653 info->fAlloc = alloc;
1654 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1655 info->fImageLayout = initialLayout;
1656 info->fFormat = pixelFormat;
1657 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001658
Brian Salomon52e943a2018-03-13 09:32:39 -04001659 return true;
1660}
1661
1662GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001663 GrColorType colorType,
1664 bool isRenderTarget,
1665 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001666 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001667
1668 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1669 return GrBackendTexture();
1670 }
1671
Robert Phillips646f6372018-09-25 09:31:10 -04001672 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1673 if (!this->caps()->isConfigTexturable(config)) {
1674 return GrBackendTexture();
1675 }
1676
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001677 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001678 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001679 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001680 return {};
1681 }
Greg Daniel108bb232018-07-03 16:18:29 -04001682 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1683 // Lots of tests don't go through Skia's public interface which will set the config so for
1684 // testing we make sure we set a config here.
1685 beTex.setPixelConfig(config);
1686 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001687}
1688
1689bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001690 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001691
Greg Daniel52e16d92018-04-10 09:34:07 -04001692 GrVkImageInfo backend;
1693 if (!tex.getVkImageInfo(&backend)) {
1694 return false;
1695 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001696
Greg Daniel52e16d92018-04-10 09:34:07 -04001697 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001698 VkMemoryRequirements req;
1699 memset(&req, 0, sizeof(req));
1700 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001701 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001702 &req));
1703 // TODO: find a better check
1704 // This will probably fail with a different driver
1705 return (req.size > 0) && (req.size <= 8192 * 8192);
1706 }
1707
1708 return false;
1709}
1710
Brian Salomon26102cb2018-03-09 09:33:19 -05001711void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001712 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001713
Greg Daniel52e16d92018-04-10 09:34:07 -04001714 GrVkImageInfo info;
1715 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001716 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001717 }
1718}
1719
Brian Osman2d010b62018-08-09 10:55:09 -04001720GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001721 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1722 return GrBackendRenderTarget();
1723 }
1724
Brian Salomon8a375832018-03-14 10:21:40 -04001725 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001726 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001727 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001728 if (kUnknown_GrPixelConfig == config) {
1729 return {};
1730 }
Robert Phillips646f6372018-09-25 09:31:10 -04001731 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001732 &info)) {
1733 return {};
1734 }
Greg Daniel108bb232018-07-03 16:18:29 -04001735 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1736 // Lots of tests don't go through Skia's public interface which will set the config so for
1737 // testing we make sure we set a config here.
1738 beRT.setPixelConfig(config);
1739 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001740}
1741
Brian Salomon52e943a2018-03-13 09:32:39 -04001742void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001743 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001744
Greg Daniel323fbcf2018-04-10 13:46:30 -04001745 GrVkImageInfo info;
1746 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001747 // something in the command buffer may still be using this, so force submit
1748 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001749 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001750 }
1751}
Brian Salomonf865b052018-03-09 09:01:53 -05001752
Greg Daniel26b50a42018-03-08 09:49:58 -05001753void GrVkGpu::testingOnly_flushGpuAndSync() {
1754 this->submitCommandBuffer(kForce_SyncQueue);
1755}
Brian Salomonf865b052018-03-09 09:01:53 -05001756#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001757
Greg Daniel164a9f02016-02-22 09:56:40 -05001758////////////////////////////////////////////////////////////////////////////////
1759
1760void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1761 VkPipelineStageFlags dstStageMask,
1762 bool byRegion,
1763 VkMemoryBarrier* barrier) const {
1764 SkASSERT(fCurrentCmdBuffer);
1765 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001766 nullptr,
Greg Daniel164a9f02016-02-22 09:56:40 -05001767 srcStageMask,
1768 dstStageMask,
1769 byRegion,
1770 GrVkCommandBuffer::kMemory_BarrierType,
1771 barrier);
1772}
1773
Greg Daniel59dc1482019-02-22 10:46:38 -05001774void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1775 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001776 VkPipelineStageFlags dstStageMask,
1777 bool byRegion,
1778 VkBufferMemoryBarrier* barrier) const {
1779 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001780 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001781 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001782 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001783 srcStageMask,
1784 dstStageMask,
1785 byRegion,
1786 GrVkCommandBuffer::kBufferMemory_BarrierType,
1787 barrier);
1788}
1789
Greg Daniel59dc1482019-02-22 10:46:38 -05001790void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1791 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001792 VkPipelineStageFlags dstStageMask,
1793 bool byRegion,
1794 VkImageMemoryBarrier* barrier) const {
1795 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001796 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001797 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001798 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001799 srcStageMask,
1800 dstStageMask,
1801 byRegion,
1802 GrVkCommandBuffer::kImageMemory_BarrierType,
1803 barrier);
1804}
1805
Greg Danielbae71212019-03-01 15:24:35 -05001806void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access,
1807 SkSurface::FlushFlags flags, bool insertedSemaphore) {
Greg Daniel51316782017-08-02 15:10:09 +00001808 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1809 // not effect what we do here.
Greg Danielbae71212019-03-01 15:24:35 -05001810 if (proxy && access == SkSurface::BackendSurfaceAccess::kPresent) {
1811 GrVkImage* image;
1812 SkASSERT(proxy->isInstantiated());
1813 if (GrTexture* tex = proxy->peekTexture()) {
1814 image = static_cast<GrVkTexture*>(tex);
1815 } else {
1816 GrRenderTarget* rt = proxy->peekRenderTarget();
1817 SkASSERT(rt);
1818 image = static_cast<GrVkRenderTarget*>(rt);
1819 }
1820 image->prepareForPresent(this);
1821 }
1822 if (flags & SkSurface::kSyncCpu_FlushFlag) {
1823 this->submitCommandBuffer(kForce_SyncQueue);
1824 } else {
1825 this->submitCommandBuffer(kSkip_SyncQueue);
1826 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001827}
1828
Greg Daniel25af6712018-04-25 10:44:38 -04001829static int get_surface_sample_cnt(GrSurface* surf) {
1830 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1831 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001832 }
Greg Daniel25af6712018-04-25 10:44:38 -04001833 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001834}
1835
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001836void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1837 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001838 GrVkImage* dstImage,
1839 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001840 const SkIRect& srcRect,
1841 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001842#ifdef SK_DEBUG
1843 int dstSampleCnt = get_surface_sample_cnt(dst);
1844 int srcSampleCnt = get_surface_sample_cnt(src);
1845 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1846 src->config(), srcSampleCnt, srcOrigin));
1847
1848#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001849
Greg Daniel164a9f02016-02-22 09:56:40 -05001850 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1851 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001852 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001853 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1854 VK_ACCESS_TRANSFER_WRITE_BIT,
1855 VK_PIPELINE_STAGE_TRANSFER_BIT,
1856 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001857
egdaniel17b89252016-04-05 07:23:38 -07001858 srcImage->setImageLayout(this,
1859 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001860 VK_ACCESS_TRANSFER_READ_BIT,
1861 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001862 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001863
1864 // Flip rect if necessary
1865 SkIRect srcVkRect = srcRect;
1866 int32_t dstY = dstPoint.fY;
1867
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001868 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1869 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001870 srcVkRect.fTop = src->height() - srcRect.fBottom;
1871 srcVkRect.fBottom = src->height() - srcRect.fTop;
1872 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1873 }
1874
1875 VkImageCopy copyRegion;
1876 memset(&copyRegion, 0, sizeof(VkImageCopy));
1877 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1878 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1879 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1880 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001881 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001882
1883 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001884 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001885 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001886 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001887 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1888 1,
1889 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001890
1891 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1892 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001893 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001894}
1895
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001896void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1897 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001898 GrVkImage* dstImage,
1899 GrVkImage* srcImage,
1900 const SkIRect& srcRect,
1901 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001902#ifdef SK_DEBUG
1903 int dstSampleCnt = get_surface_sample_cnt(dst);
1904 int srcSampleCnt = get_surface_sample_cnt(src);
1905 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1906 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001907
Greg Daniel25af6712018-04-25 10:44:38 -04001908#endif
egdaniel17b89252016-04-05 07:23:38 -07001909 dstImage->setImageLayout(this,
1910 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001911 VK_ACCESS_TRANSFER_WRITE_BIT,
1912 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001913 false);
1914
egdaniel17b89252016-04-05 07:23:38 -07001915 srcImage->setImageLayout(this,
1916 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001917 VK_ACCESS_TRANSFER_READ_BIT,
1918 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001919 false);
1920
1921 // Flip rect if necessary
1922 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001923 srcVkRect.fLeft = srcRect.fLeft;
1924 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001925 SkIRect dstRect;
1926 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001927 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001928
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001929 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001930 srcVkRect.fTop = src->height() - srcRect.fBottom;
1931 srcVkRect.fBottom = src->height() - srcRect.fTop;
1932 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001933 srcVkRect.fTop = srcRect.fTop;
1934 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001935 }
1936
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001937 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001938 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1939 } else {
1940 dstRect.fTop = dstPoint.fY;
1941 }
1942 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1943
1944 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1945 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001946 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001947 using std::swap;
1948 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001949 }
1950
1951 VkImageBlit blitRegion;
1952 memset(&blitRegion, 0, sizeof(VkImageBlit));
1953 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1954 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001955 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001956 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1957 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001958 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001959
1960 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001961 *srcImage,
1962 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001963 1,
1964 &blitRegion,
1965 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001966
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001967 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001968 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001969}
1970
Brian Salomon1fabd512018-02-09 09:54:25 -05001971void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1972 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1973 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001974 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001975 SkIRect srcRect = origSrcRect;
1976 SkIPoint dstPoint = origDstPoint;
1977 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1978 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1979 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1980 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1981 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1982 }
1983 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001984 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1985 srcRect.width(), srcRect.height());
1986 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001987}
1988
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001989bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1990 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001991 const SkIRect& srcRect, const SkIPoint& dstPoint,
1992 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001993#ifdef SK_DEBUG
1994 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
1995 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
1996 }
1997 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
1998 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
1999 }
2000#endif
2001
Greg Daniel25af6712018-04-25 10:44:38 -04002002 GrPixelConfig dstConfig = dst->config();
2003 GrPixelConfig srcConfig = src->config();
2004
2005 int dstSampleCnt = get_surface_sample_cnt(dst);
2006 int srcSampleCnt = get_surface_sample_cnt(src);
2007
2008 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
2009 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002010 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07002011 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07002012 }
2013
Greg Daniel25af6712018-04-25 10:44:38 -04002014 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
2015 srcConfig, SkToBool(src->asTexture()))) {
2016 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2017 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05002018 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2019 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07002020 return true;
2021 }
2022
egdaniel17b89252016-04-05 07:23:38 -07002023 GrVkImage* dstImage;
2024 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002025 GrRenderTarget* dstRT = dst->asRenderTarget();
2026 if (dstRT) {
2027 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002028 if (vkRT->wrapsSecondaryCommandBuffer()) {
2029 return false;
2030 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002031 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2032 } else {
2033 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002034 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002035 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002036 GrRenderTarget* srcRT = src->asRenderTarget();
2037 if (srcRT) {
2038 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2039 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002040 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002041 SkASSERT(src->asTexture());
2042 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002043 }
2044
Greg Daniel25af6712018-04-25 10:44:38 -04002045 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
2046 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002047 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2048 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002049 return true;
2050 }
2051
Greg Daniel25af6712018-04-25 10:44:38 -04002052 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
2053 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002054 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2055 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002056 return true;
2057 }
2058
Greg Daniel164a9f02016-02-22 09:56:40 -05002059 return false;
2060}
2061
Brian Salomona6948702018-06-01 15:33:20 -04002062bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2063 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002064 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002065 return false;
2066 }
2067
egdaniel66933552016-08-24 07:22:19 -07002068 GrVkImage* image = nullptr;
2069 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2070 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002071 // Reading from render targets that wrap a secondary command buffer is not allowed since
2072 // it would require us to know the VkImage, which we don't have, as well as need us to
2073 // stop and start the VkRenderPass which we don't have access to.
2074 if (rt->wrapsSecondaryCommandBuffer()) {
2075 return false;
2076 }
egdaniel66933552016-08-24 07:22:19 -07002077 // resolve the render target if necessary
2078 switch (rt->getResolveType()) {
2079 case GrVkRenderTarget::kCantResolve_ResolveType:
2080 return false;
2081 case GrVkRenderTarget::kAutoResolves_ResolveType:
2082 break;
2083 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002084 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002085 break;
2086 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002087 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002088 }
2089 image = rt;
2090 } else {
2091 image = static_cast<GrVkTexture*>(surface->asTexture());
2092 }
2093
2094 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002095 return false;
2096 }
2097
Greg Daniel475eb702018-09-28 14:16:50 -04002098 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2099 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2100 // image and then do the read pixels from that.
2101 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002102 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2103 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002104
2105 // Make a new surface that is RGBA to copy the RGB surface into.
2106 GrSurfaceDesc surfDesc;
2107 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2108 surfDesc.fWidth = width;
2109 surfDesc.fHeight = height;
2110 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2111 surfDesc.fSampleCnt = 1;
2112
2113 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2114 VK_IMAGE_USAGE_SAMPLED_BIT |
2115 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2116 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2117
2118 GrVkImage::ImageDesc imageDesc;
2119 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2120 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2121 imageDesc.fWidth = width;
2122 imageDesc.fHeight = height;
2123 imageDesc.fLevels = 1;
2124 imageDesc.fSamples = 1;
2125 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2126 imageDesc.fUsageFlags = usageFlags;
2127 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2128
2129 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2130 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2131 if (!copySurface) {
2132 return false;
2133 }
2134
2135 int srcSampleCount = 0;
2136 if (rt) {
2137 srcSampleCount = rt->numColorSamples();
2138 }
2139 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
2140 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin,
2141 surface->config(), srcSampleCount, kOrigin) &&
2142 !this->vkCaps().canCopyAsDraw(copySurface->config(), false,
2143 surface->config(), SkToBool(surface->asTexture()))) {
2144 return false;
2145 }
2146 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2147 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2148 srcRect, SkIPoint::Make(0,0))) {
2149 return false;
2150 }
2151 top = 0;
2152 left = 0;
2153 dstColorType = GrColorType::kRGBA_8888;
2154 image = copySurface.get();
2155 }
2156
Greg Daniel164a9f02016-02-22 09:56:40 -05002157 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002158 image->setImageLayout(this,
2159 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2160 VK_ACCESS_TRANSFER_READ_BIT,
2161 VK_PIPELINE_STAGE_TRANSFER_BIT,
2162 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002163
Brian Salomonc320b152018-02-20 14:05:36 -05002164 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002165 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002166
Greg Daniel164a9f02016-02-22 09:56:40 -05002167 VkBufferImageCopy region;
2168 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002169
2170 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2171 if (copyFromOrigin) {
2172 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002173 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002174 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002175 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002176 region.imageOffset = offset;
2177 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2178 }
2179
2180 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002181 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002182 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002183 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002184 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002185 kStream_GrAccessPattern)
2186 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002187
2188 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002189 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002190 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002191 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2192 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002193
2194 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002195 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002196 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002197 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002198 1,
2199 &region);
2200
2201 // make sure the copy to buffer has finished
2202 transferBuffer->addMemoryBarrier(this,
2203 VK_ACCESS_TRANSFER_WRITE_BIT,
2204 VK_ACCESS_HOST_READ_BIT,
2205 VK_PIPELINE_STAGE_TRANSFER_BIT,
2206 VK_PIPELINE_STAGE_HOST_BIT,
2207 false);
2208
2209 // We need to submit the current command buffer to the Queue and make sure it finishes before
2210 // we can copy the data out of the buffer.
2211 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002212 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002213 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002214 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002215
egdaniel6fa0a912016-09-12 11:51:29 -07002216 if (copyFromOrigin) {
2217 uint32_t skipRows = region.imageExtent.height - height;
2218 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2219 }
2220
Brian Salomona6948702018-06-01 15:33:20 -04002221 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002222
2223 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002224 return true;
2225}
egdaniel066df7c2016-06-08 14:02:27 -07002226
egdaniel27bb2842016-07-07 11:58:35 -07002227// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2228// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2229// the the entire attachment. Similar requirements for the y and height components.
2230void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2231 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2232 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002233 if ((0 != granularity.width && 1 != granularity.width)) {
2234 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2235 int rightAdj = srcBounds.fRight % granularity.width;
2236 if (rightAdj != 0) {
2237 rightAdj = granularity.width - rightAdj;
2238 }
2239 dstBounds->fRight = srcBounds.fRight + rightAdj;
2240 if (dstBounds->fRight > maxWidth) {
2241 dstBounds->fRight = maxWidth;
2242 dstBounds->fLeft = 0;
2243 } else {
2244 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2245 }
egdaniel27bb2842016-07-07 11:58:35 -07002246 } else {
egdanield5797b32016-09-20 12:57:45 -07002247 dstBounds->fLeft = srcBounds.fLeft;
2248 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002249 }
2250
2251 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002252 if ((0 != granularity.height && 1 != granularity.height)) {
2253 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2254 int bottomAdj = srcBounds.fBottom % granularity.height;
2255 if (bottomAdj != 0) {
2256 bottomAdj = granularity.height - bottomAdj;
2257 }
2258 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2259 if (dstBounds->fBottom > maxHeight) {
2260 dstBounds->fBottom = maxHeight;
2261 dstBounds->fTop = 0;
2262 } else {
2263 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2264 }
egdaniel27bb2842016-07-07 11:58:35 -07002265 } else {
egdanield5797b32016-09-20 12:57:45 -07002266 dstBounds->fTop = srcBounds.fTop;
2267 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002268 }
2269}
2270
Greg Daniel22bc8652017-03-22 15:45:43 -04002271void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002272 const GrVkRenderPass* renderPass,
2273 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002274 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002275 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002276 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002277 const SkIRect* pBounds = &bounds;
2278 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002279 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002280 flippedBounds = bounds;
2281 flippedBounds.fTop = target->height() - bounds.fBottom;
2282 flippedBounds.fBottom = target->height() - bounds.fTop;
2283 pBounds = &flippedBounds;
2284 }
2285
egdaniel27bb2842016-07-07 11:58:35 -07002286 // The bounds we use for the render pass should be of the granularity supported
2287 // by the device.
2288 const VkExtent2D& granularity = renderPass->granularity();
2289 SkIRect adjustedBounds;
2290 if ((0 != granularity.width && 1 != granularity.width) ||
2291 (0 != granularity.height && 1 != granularity.height)) {
2292 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2293 target->width(), target->height());
2294 pBounds = &adjustedBounds;
2295 }
2296
Robert Phillips95214472017-08-08 18:00:03 -04002297#ifdef SK_DEBUG
2298 uint32_t index;
2299 bool result = renderPass->colorAttachmentIndex(&index);
2300 SkASSERT(result && 0 == index);
2301 result = renderPass->stencilAttachmentIndex(&index);
2302 if (result) {
2303 SkASSERT(1 == index);
2304 }
2305#endif
2306 VkClearValue clears[2];
2307 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002308 clears[1].depthStencil.depth = 0.0f;
2309 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002310
2311 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002312 for (int i = 0; i < buffers.count(); ++i) {
2313 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2314 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002315 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002316
Brian Salomon1fabd512018-02-09 09:54:25 -05002317 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002318}
egdaniel9cb63402016-06-23 08:37:05 -07002319
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002320void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2321 if (buffer->asRTCommandBuffer()) {
2322 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2323
2324 fCachedRTCommandBuffer->submit();
2325 fCachedRTCommandBuffer->reset();
2326 } else {
2327 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2328
2329 fCachedTexCommandBuffer->submit();
2330 fCachedTexCommandBuffer->reset();
2331 }
2332}
2333
Greg Daniel6be35232017-03-01 17:01:09 -05002334GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002335 VkFenceCreateInfo createInfo;
2336 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2337 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2338 createInfo.pNext = nullptr;
2339 createInfo.flags = 0;
2340 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002341
2342 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2343 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2344
2345 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002346 return (GrFence)fence;
2347}
2348
Greg Daniel6be35232017-03-01 17:01:09 -05002349bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2350 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2351
2352 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002353 return (VK_SUCCESS == result);
2354}
2355
2356void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002357 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2358}
2359
Greg Daniela5cb7812017-06-16 09:45:32 -04002360sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2361 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002362}
2363
Greg Daniel48661b82018-01-22 16:11:35 -05002364sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2365 GrResourceProvider::SemaphoreWrapType wrapType,
2366 GrWrapOwnership ownership) {
2367 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002368}
2369
Greg Daniel858e12c2018-12-06 11:11:37 -05002370void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002371 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2372
Greg Daniel48661b82018-01-22 16:11:35 -05002373 GrVkSemaphore::Resource* resource = vkSem->getResource();
2374 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002375 resource->ref();
2376 fSemaphoresToSignal.push_back(resource);
2377 }
Greg Daniel6be35232017-03-01 17:01:09 -05002378}
2379
Greg Daniel48661b82018-01-22 16:11:35 -05002380void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002381 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2382
Greg Daniel48661b82018-01-22 16:11:35 -05002383 GrVkSemaphore::Resource* resource = vkSem->getResource();
2384 if (resource->shouldWait()) {
2385 resource->ref();
2386 fSemaphoresToWaitOn.push_back(resource);
2387 }
jvanverth84741b32016-09-30 08:39:02 -07002388}
Brian Osman13dddce2017-05-09 13:19:50 -04002389
2390sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2391 SkASSERT(texture);
2392 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2393 vkTexture->setImageLayout(this,
2394 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2395 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002396 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002397 false);
2398 this->submitCommandBuffer(kSkip_SyncQueue);
2399
2400 // The image layout change serves as a barrier, so no semaphore is needed
2401 return nullptr;
2402}
Greg Danielf5d87582017-12-18 14:48:15 -05002403
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002404void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2405 fDrawables.emplace_back(std::move(drawable));
2406}
2407
Greg Daniel7a82edf2018-12-04 10:54:34 -05002408uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2409 const GrBackendFormat& format) {
2410 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2411 SkASSERT(ycbcrInfo);
2412 if (!ycbcrInfo->isValid()) {
2413 return 0;
2414 }
2415
2416 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2417 samplerState, *ycbcrInfo);
2418
2419 return sampler->uniqueID();
2420}
2421
Greg Daniela870b462019-01-08 15:49:46 -05002422void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002423 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002424 this->resourceProvider().storePipelineCacheData();
2425 }
2426}