blob: 3dad9b6584720e259e188d58f990e2a05bdcb19d [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/gpu/GrContextOptions.h"
13#include "include/private/SkTo.h"
14#include "src/core/SkConvertPixels.h"
15#include "src/core/SkMipMap.h"
16#include "src/gpu/GrContextPriv.h"
Robert Phillipsbd1ef682019-05-31 12:48:49 -040017#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGeometryProcessor.h"
19#include "src/gpu/GrGpuResourceCacheAccess.h"
20#include "src/gpu/GrMesh.h"
21#include "src/gpu/GrPipeline.h"
Greg Daniel797efca2019-05-09 14:04:20 -040022#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRenderTargetPriv.h"
24#include "src/gpu/GrTexturePriv.h"
Greg Daniel797efca2019-05-09 14:04:20 -040025#include "src/gpu/SkGpuDevice.h"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040026#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/vk/GrVkAMDMemoryAllocator.h"
28#include "src/gpu/vk/GrVkCommandBuffer.h"
29#include "src/gpu/vk/GrVkCommandPool.h"
30#include "src/gpu/vk/GrVkGpuCommandBuffer.h"
31#include "src/gpu/vk/GrVkImage.h"
32#include "src/gpu/vk/GrVkIndexBuffer.h"
33#include "src/gpu/vk/GrVkInterface.h"
34#include "src/gpu/vk/GrVkMemory.h"
35#include "src/gpu/vk/GrVkPipeline.h"
36#include "src/gpu/vk/GrVkPipelineState.h"
37#include "src/gpu/vk/GrVkRenderPass.h"
38#include "src/gpu/vk/GrVkResourceProvider.h"
39#include "src/gpu/vk/GrVkSemaphore.h"
40#include "src/gpu/vk/GrVkTexture.h"
41#include "src/gpu/vk/GrVkTextureRenderTarget.h"
42#include "src/gpu/vk/GrVkTransferBuffer.h"
43#include "src/gpu/vk/GrVkVertexBuffer.h"
Greg Daniel797efca2019-05-09 14:04:20 -040044#include "src/image/SkImage_Gpu.h"
45#include "src/image/SkSurface_Gpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/SkSLCompiler.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040047
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "include/gpu/vk/GrVkExtensions.h"
49#include "include/gpu/vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050050
Ben Wagnerf08d1d02018-06-18 15:11:00 -040051#include <utility>
52
Forrest Reiling44f85712017-03-27 23:22:20 -070053#if !defined(SK_BUILD_FOR_WIN)
54#include <unistd.h>
55#endif // !defined(SK_BUILD_FOR_WIN)
56
Greg Danieldef55462018-08-01 13:40:14 -040057#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
Ben Wagnerab6eefe2019-05-20 11:02:49 -040058#include "src/core/SkLeanWindows.h"
Greg Danieldef55462018-08-01 13:40:14 -040059#endif
60
Greg Daniel164a9f02016-02-22 09:56:40 -050061#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
62#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
63#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
64
Greg Danielf730c182018-07-02 20:15:37 +000065sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050066 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000067 if (backendContext.fInstance == VK_NULL_HANDLE ||
68 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
69 backendContext.fDevice == VK_NULL_HANDLE ||
70 backendContext.fQueue == VK_NULL_HANDLE) {
71 return nullptr;
72 }
Greg Danield3e65aa2018-08-01 09:19:45 -040073 if (!backendContext.fGetProc) {
74 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040075 }
Greg Danield3e65aa2018-08-01 09:19:45 -040076
Greg Daniel41f0e282019-01-28 13:15:05 -050077 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
78 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
79 backendContext.fGetProc("vkEnumerateInstanceVersion",
80 VK_NULL_HANDLE, VK_NULL_HANDLE));
81 uint32_t instanceVersion = 0;
82 if (!localEnumerateInstanceVersion) {
83 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
84 } else {
85 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
86 if (err) {
87 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
88 return nullptr;
89 }
90 }
91
Greg Danielc0b03d82018-08-03 14:41:15 -040092 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
93 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
94 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
95 backendContext.fInstance,
96 VK_NULL_HANDLE));
97
98 if (!localGetPhysicalDeviceProperties) {
99 return nullptr;
100 }
101 VkPhysicalDeviceProperties physDeviceProperties;
102 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
103 uint32_t physDevVersion = physDeviceProperties.apiVersion;
104
Greg Daniel41f0e282019-01-28 13:15:05 -0500105 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
106 : instanceVersion;
107
108 instanceVersion = SkTMin(instanceVersion, apiVersion);
109 physDevVersion = SkTMin(physDevVersion, apiVersion);
110
Greg Daniel98bffae2018-08-01 13:25:41 -0400111 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400112
Greg Daniel98bffae2018-08-01 13:25:41 -0400113 if (backendContext.fVkExtensions) {
114 interface.reset(new GrVkInterface(backendContext.fGetProc,
115 backendContext.fInstance,
116 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500117 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400118 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400119 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500120 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400121 return nullptr;
122 }
123 } else {
Greg Daniel98bffae2018-08-01 13:25:41 -0400124 GrVkExtensions extensions;
Greg Daniel88e8ddc2019-04-25 16:37:08 -0400125 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
126 // need to know if this is enabled to know if we can transition to a present layout when
127 // flushing a surface.
128 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
129 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
130 extensions.init(backendContext.fGetProc, backendContext.fInstance,
131 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
132 }
Greg Daniel98bffae2018-08-01 13:25:41 -0400133 interface.reset(new GrVkInterface(backendContext.fGetProc,
134 backendContext.fInstance,
135 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500136 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400137 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400138 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500139 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400140 return nullptr;
141 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500142 }
143
Greg Daniel41f0e282019-01-28 13:15:05 -0500144 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
145 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500146}
147
148////////////////////////////////////////////////////////////////////////////////
149
halcanary9d524f22016-03-29 09:03:52 -0700150GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500151 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
152 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500153 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400154 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000155 , fMemoryAllocator(backendContext.fMemoryAllocator)
156 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400157 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000158 , fDevice(backendContext.fDevice)
159 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400160 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500161 , fResourceProvider(this)
162 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000163 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700164
Greg Daniel81df0412018-05-31 13:13:33 -0400165 if (!fMemoryAllocator) {
166 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000167 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400168 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400169 }
170
ethannicholasb3058bd2016-07-01 08:22:01 -0700171 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700172
Greg Daniela0651ac2018-08-08 09:23:18 -0400173 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400174 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400175 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500176 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400177 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400178 } else if (backendContext.fDeviceFeatures) {
179 VkPhysicalDeviceFeatures2 features2;
180 features2.pNext = nullptr;
181 features2.features = *backendContext.fDeviceFeatures;
182 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500183 features2, instanceVersion, physicalDeviceVersion,
184 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400185 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400186 VkPhysicalDeviceFeatures2 features;
187 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
188 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400189 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400190 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400191 }
192 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400193 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400194 }
195 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400196 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400197 }
Greg Danielf808c5e2019-04-30 14:48:27 -0400198 GrVkExtensions extensions;
199 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
200 // need to know if this is enabled to know if we can transition to a present layout when
201 // flushing a surface.
202 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
203 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
204 extensions.init(backendContext.fGetProc, backendContext.fInstance,
205 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
206 }
Greg Daniel36443602018-08-02 12:51:52 -0400207 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielf808c5e2019-04-30 14:48:27 -0400208 features, instanceVersion, physicalDeviceVersion, extensions));
Greg Daniel36443602018-08-02 12:51:52 -0400209 }
jvanverth633b3562016-03-23 11:01:22 -0700210 fCaps.reset(SkRef(fVkCaps.get()));
211
Greg Danielf730c182018-07-02 20:15:37 +0000212 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
213 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700214
Greg Daniela870b462019-01-08 15:49:46 -0500215 fResourceProvider.init();
216
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500217 fCmdPool = fResourceProvider.findOrCreateCommandPool();
218 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000219 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700220 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500221}
222
Greg Daniel8606cf82017-05-08 16:17:53 -0400223void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500224 if (fCmdPool) {
225 fCmdPool->getPrimaryCommandBuffer()->end(this);
226 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400227 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500228
229 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500230 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700231
232 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
233 // on the command buffers even though they have completed. This causes an assert to fire when
234 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500235 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700236#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500237 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700238#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500239 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700240#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500241 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700242#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500243 }
egdanielf8c2be32016-06-24 13:18:27 -0700244#endif
245
egdanielbe9d8212016-09-20 08:54:23 -0700246#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400247 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700248#endif
halcanary9d524f22016-03-29 09:03:52 -0700249
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500250 if (fCmdPool) {
251 fCmdPool->unref(this);
252 fCmdPool = nullptr;
253 }
254
Greg Daniel6be35232017-03-01 17:01:09 -0500255 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
256 fSemaphoresToWaitOn[i]->unref(this);
257 }
258 fSemaphoresToWaitOn.reset();
259
Greg Daniela5cb7812017-06-16 09:45:32 -0400260 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
261 fSemaphoresToSignal[i]->unref(this);
262 }
263 fSemaphoresToSignal.reset();
264
265
egdanielbc9b2962016-09-27 08:00:53 -0700266 fCopyManager.destroyResources(this);
267
Jim Van Verth09557d72016-11-07 11:10:21 -0500268 // must call this just before we destroy the command pool and VkDevice
269 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500270
Greg Danielf730c182018-07-02 20:15:37 +0000271 fMemoryAllocator.reset();
272
273 fQueue = VK_NULL_HANDLE;
274 fDevice = VK_NULL_HANDLE;
275 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400276}
277
278GrVkGpu::~GrVkGpu() {
279 if (!fDisconnected) {
280 this->destroyResources();
281 }
282 delete fCompiler;
283}
284
285
286void GrVkGpu::disconnect(DisconnectType type) {
287 INHERITED::disconnect(type);
288 if (!fDisconnected) {
289 if (DisconnectType::kCleanup == type) {
290 this->destroyResources();
291 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500292 if (fCmdPool) {
293 fCmdPool->unrefAndAbandon();
294 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400295 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400296 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
297 fSemaphoresToWaitOn[i]->unrefAndAbandon();
298 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400299 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
300 fSemaphoresToSignal[i]->unrefAndAbandon();
301 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400302 fCopyManager.abandonResources();
303
304 // must call this just before we destroy the command pool and VkDevice
305 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400306
307 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400308 }
309 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400310 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400311 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400312 fDisconnected = true;
313 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500314}
315
316///////////////////////////////////////////////////////////////////////////////
317
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400318GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400319 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400320 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
321 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400322 if (!fCachedRTCommandBuffer) {
323 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
324 }
325
Greg Daniela41a74a2018-10-09 12:59:23 +0000326 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400327 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400328}
329
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400330GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
331 if (!fCachedTexCommandBuffer) {
332 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
333 }
334
335 fCachedTexCommandBuffer->set(texture, origin);
336 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700337}
338
Greg Daniela3aa75a2019-04-12 14:24:55 -0400339void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
340 GrGpuFinishedContext finishedContext) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500341 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400342
343 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
344 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
345 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400346 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400347 if (finishedProc) {
348 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
349 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400350 return;
351 }
352
Greg Daniel164a9f02016-02-22 09:56:40 -0500353 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500354 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400355 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500356
Greg Daniela3aa75a2019-04-12 14:24:55 -0400357 if (finishedProc) {
358 // Make sure this is called after closing the current command pool
359 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
360 }
361
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400362 // We must delete and drawables that have been waitint till submit for us to destroy.
363 fDrawables.reset();
364
Greg Daniel6be35232017-03-01 17:01:09 -0500365 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
366 fSemaphoresToWaitOn[i]->unref(this);
367 }
368 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400369 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
370 fSemaphoresToSignal[i]->unref(this);
371 }
372 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500373
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500374 // Release old command pool and create a new one
375 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500376 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500377 fCmdPool = fResourceProvider.findOrCreateCommandPool();
378 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500379 fCurrentCmdBuffer->begin(this);
380}
381
382///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500383sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
384 GrAccessPattern accessPattern, const void* data) {
385 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700386 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500387 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700388 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
389 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500390 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700391 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500392 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700393 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
394 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500395 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700396 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500397 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400398 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
399 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500400 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700401 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500402 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400403 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
404 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500405 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700406 break;
cdalton397536c2016-03-25 12:15:03 -0700407 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400408 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700409 return nullptr;
410 }
cdalton1bf3e712016-04-19 10:00:02 -0700411 if (data && buff) {
412 buff->updateData(data, size);
413 }
414 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500415}
416
Brian Salomona9b04b92018-06-01 15:04:28 -0400417bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
418 GrColorType srcColorType, const GrMipLevel texels[],
419 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500420 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
421 if (!vkTex) {
422 return false;
423 }
424
jvanverth900bd4a2016-04-29 13:53:12 -0700425 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400426 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800427 return false;
428 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800429
Robert Phillips1f098982019-05-15 10:27:36 -0400430 SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500431 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400432 bool linearTiling = vkTex->isLinearTiled();
433 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400434 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400435 SkDebugf("Can't upload mipmap data to linear tiled texture");
436 return false;
437 }
438 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
439 // Need to change the layout to general in order to perform a host write
440 vkTex->setImageLayout(this,
441 VK_IMAGE_LAYOUT_GENERAL,
442 VK_ACCESS_HOST_WRITE_BIT,
443 VK_PIPELINE_STAGE_HOST_BIT,
444 false);
445 this->submitCommandBuffer(kForce_SyncQueue);
446 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400447 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400448 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500449 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400450 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400451 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
452 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500453 }
egdaniel4583ec52016-06-27 12:57:00 -0700454
jvanverth900bd4a2016-04-29 13:53:12 -0700455 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500456}
457
Brian Salomone05ba5a2019-04-08 11:59:07 -0400458bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
459 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
460 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400461 // Vulkan only supports 4-byte aligned offsets
462 if (SkToBool(bufferOffset & 0x2)) {
463 return false;
464 }
465 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
466 if (!vkTex) {
467 return false;
468 }
Robert Phillips1f098982019-05-15 10:27:36 -0400469
470 // Can't transfer compressed data
471 SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat()));
472
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400473 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
474 if (!vkBuffer) {
475 return false;
476 }
477
Greg Daniel660cc992017-06-26 14:55:05 -0400478 SkDEBUGCODE(
479 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
480 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
481 SkASSERT(bounds.contains(subRect));
482 )
Brian Salomonc320b152018-02-20 14:05:36 -0500483 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400484 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500485 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400486 }
487
488 // Set up copy region
489 VkBufferImageCopy region;
490 memset(&region, 0, sizeof(VkBufferImageCopy));
491 region.bufferOffset = bufferOffset;
492 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
493 region.bufferImageHeight = 0;
494 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
495 region.imageOffset = { left, top, 0 };
496 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
497
498 // Change layout of our target so it can be copied to
499 vkTex->setImageLayout(this,
500 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
501 VK_ACCESS_TRANSFER_WRITE_BIT,
502 VK_PIPELINE_STAGE_TRANSFER_BIT,
503 false);
504
505 // Copy the buffer to the image
506 fCurrentCmdBuffer->copyBufferToImage(this,
507 vkBuffer,
508 vkTex,
509 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
510 1,
511 &region);
512
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400513 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400514 return true;
515}
516
Brian Salomon26de56e2019-04-10 12:14:26 -0400517bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
518 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
519 size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400520 SkASSERT(surface);
521 SkASSERT(transferBuffer);
522
Brian Salomona585fe92019-04-09 14:57:00 -0400523 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
524
525 GrVkImage* srcImage;
526 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
527 // Reading from render targets that wrap a secondary command buffer is not allowed since
528 // it would require us to know the VkImage, which we don't have, as well as need us to
529 // stop and start the VkRenderPass which we don't have access to.
530 if (rt->wrapsSecondaryCommandBuffer()) {
531 return false;
532 }
533 // resolve the render target if necessary
534 switch (rt->getResolveType()) {
535 case GrVkRenderTarget::kCantResolve_ResolveType:
536 return false;
537 case GrVkRenderTarget::kAutoResolves_ResolveType:
538 break;
539 case GrVkRenderTarget::kCanResolve_ResolveType:
540 this->resolveRenderTargetNoFlush(rt);
541 break;
542 default:
543 SK_ABORT("Unknown resolve type");
544 }
545 srcImage = rt;
546 } else {
547 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
548 }
549
550 // Set up copy region
551 VkBufferImageCopy region;
552 memset(&region, 0, sizeof(VkBufferImageCopy));
553 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400554 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400555 region.bufferImageHeight = 0;
556 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
557 region.imageOffset = { left, top, 0 };
558 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
559
560 srcImage->setImageLayout(this,
561 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
562 VK_ACCESS_TRANSFER_READ_BIT,
563 VK_PIPELINE_STAGE_TRANSFER_BIT,
564 false);
565
566 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
567 vkBuffer, 1, &region);
568
569 // Make sure the copy to buffer has finished.
570 vkBuffer->addMemoryBarrier(this,
571 VK_ACCESS_TRANSFER_WRITE_BIT,
572 VK_ACCESS_HOST_READ_BIT,
573 VK_PIPELINE_STAGE_TRANSFER_BIT,
574 VK_PIPELINE_STAGE_HOST_BIT,
575 false);
576
577 // The caller is responsible for syncing.
578 this->submitCommandBuffer(kSkip_SyncQueue);
579
Brian Salomon26de56e2019-04-10 12:14:26 -0400580 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400581}
582
Brian Salomon1fabd512018-02-09 09:54:25 -0500583void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
584 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700585 SkASSERT(dst);
586 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
587
egdaniel4bcd62e2016-08-31 07:37:31 -0700588 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500589 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
590 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
591 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
592 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
593 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700594
Greg Danielbc26c392017-04-18 13:32:10 -0400595 GrVkImage* dstImage;
596 GrRenderTarget* dstRT = dst->asRenderTarget();
597 if (dstRT) {
598 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400599 dstImage = vkRT;
600 } else {
601 SkASSERT(dst->asTexture());
602 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
603 }
604 dstImage->setImageLayout(this,
605 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
606 VK_ACCESS_TRANSFER_WRITE_BIT,
607 VK_PIPELINE_STAGE_TRANSFER_BIT,
608 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700609
610 src->msaaImage()->setImageLayout(this,
611 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
612 VK_ACCESS_TRANSFER_READ_BIT,
613 VK_PIPELINE_STAGE_TRANSFER_BIT,
614 false);
615
Greg Danielbc26c392017-04-18 13:32:10 -0400616 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700617}
618
Brian Salomon1fabd512018-02-09 09:54:25 -0500619void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700620 if (target->needsResolve()) {
621 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700622 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
623 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500624
egdaniel4bcd62e2016-08-31 07:37:31 -0700625 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700626
Brian Salomon1fabd512018-02-09 09:54:25 -0500627 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700628
629 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500630
631 if (requiresSubmit) {
632 this->submitCommandBuffer(kSkip_SyncQueue);
633 }
egdaniel52ad2512016-08-04 12:50:01 -0700634 }
635}
636
Brian Salomona9b04b92018-06-01 15:04:28 -0400637bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
638 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500639 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700640 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500641
Jim Van Verth1676cb92019-01-15 13:24:45 -0500642 // If we're uploading compressed data then we should be using uploadCompressedTexData
643 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
644 GrSRGBEncoded::kNo)));
645
Greg Daniel660cc992017-06-26 14:55:05 -0400646 SkDEBUGCODE(
647 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
648 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
649 SkASSERT(bounds.contains(subRect));
650 )
Brian Salomonc320b152018-02-20 14:05:36 -0500651 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500652 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400653 if (!rowBytes) {
654 rowBytes = trimRowBytes;
655 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500656
jvanverth900bd4a2016-04-29 13:53:12 -0700657 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
658 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
659 const VkImageSubresource subres = {
660 VK_IMAGE_ASPECT_COLOR_BIT,
661 0, // mipLevel
662 0, // arraySlice
663 };
664 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500665
jvanverth900bd4a2016-04-29 13:53:12 -0700666 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500667
jvanverth900bd4a2016-04-29 13:53:12 -0700668 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700669 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700670 &subres,
671 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500672
jvanverth1e305ba2016-06-01 09:39:15 -0700673 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400674 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700675 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400676 SkASSERT(size + offset <= alloc.fSize);
677 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
678 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700679 return false;
680 }
Greg Daniel81df0412018-05-31 13:13:33 -0400681 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700682
Brian Salomona9b04b92018-06-01 15:04:28 -0400683 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
684 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700685
Greg Daniele35a99e2018-03-02 11:44:22 -0500686 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400687 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700688
689 return true;
690}
691
Brian Salomona9b04b92018-06-01 15:04:28 -0400692bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
693 GrColorType dataColorType, const GrMipLevel texels[],
694 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700695 SkASSERT(!tex->isLinearTiled());
696 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400697 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700698 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
699
Greg Danieldd20e912017-04-07 14:42:23 -0400700 // We assume that if the texture has mip levels, we either upload to all the levels or just the
701 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400702 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400703
Jim Van Verth1676cb92019-01-15 13:24:45 -0500704 // If we're uploading compressed data then we should be using uploadCompressedTexData
705 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
706 GrSRGBEncoded::kNo)));
707
jvanverth900bd4a2016-04-29 13:53:12 -0700708 if (width == 0 || height == 0) {
709 return false;
710 }
711
Greg Daniel475eb702018-09-28 14:16:50 -0400712 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
713 return false;
714 }
715
716 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
717 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500718 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
719 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400720 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
721 // blit or draw.
Greg Danielcaa795f2019-05-14 11:54:25 -0400722 if (!this->vkCaps().formatCanBeDstofBlit(VK_FORMAT_R8G8B8_UNORM, tex->isLinearTiled()) &&
Greg Daniel475eb702018-09-28 14:16:50 -0400723 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
724 return false;
725 }
726 mipLevelCount = 1;
727 }
728
Brian Salomond1eaf492017-05-18 10:02:08 -0400729 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500730 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700731
732 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700733 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
734 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400735 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
736
Greg Daniel475eb702018-09-28 14:16:50 -0400737 texelsShallowCopy.reset(mipLevelCount);
738 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700739
Robert Phillips590533f2017-07-11 14:22:35 -0400740 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700741 individualMipOffsets.push_back(0);
742 size_t combinedBufferSize = width * bpp * height;
743 int currentWidth = width;
744 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400745 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400746 combinedBufferSize = 0;
747 }
748
Greg Daniel468fd632017-03-22 17:03:45 -0400749 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
750 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
751 SkASSERT((bpp & (bpp - 1)) == 0);
752 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400753 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700754 currentWidth = SkTMax(1, currentWidth/2);
755 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400756
Greg Daniel55afd6d2017-09-29 09:32:44 -0400757 if (texelsShallowCopy[currentMipLevel].fPixels) {
758 const size_t trimmedSize = currentWidth * bpp * currentHeight;
759 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
760 if (alignmentDiff != 0) {
761 combinedBufferSize += alignmentMask - alignmentDiff + 1;
762 }
763 individualMipOffsets.push_back(combinedBufferSize);
764 combinedBufferSize += trimmedSize;
765 } else {
766 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400767 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400768 }
769 if (0 == combinedBufferSize) {
770 // We don't actually have any data to upload so just return success
771 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700772 }
773
774 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500775 sk_sp<GrVkTransferBuffer> transferBuffer =
776 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400777 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700778 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400779 }
jvanverth900bd4a2016-04-29 13:53:12 -0700780
Greg Daniel475eb702018-09-28 14:16:50 -0400781 int uploadLeft = left;
782 int uploadTop = top;
783 GrVkTexture* uploadTexture = tex;
784 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
785 // R8G8B8A8_UNORM image and then copy it.
786 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500787 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400788 GrSurfaceDesc surfDesc;
789 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
790 surfDesc.fWidth = width;
791 surfDesc.fHeight = height;
792 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
793 surfDesc.fSampleCnt = 1;
794
795 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
796 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
797 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
798
799 GrVkImage::ImageDesc imageDesc;
800 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
801 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
802 imageDesc.fWidth = width;
803 imageDesc.fHeight = height;
804 imageDesc.fLevels = 1;
805 imageDesc.fSamples = 1;
806 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
807 imageDesc.fUsageFlags = usageFlags;
808 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
809
810 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
811 GrMipMapsStatus::kNotAllocated);
812 if (!copyTexture) {
813 return false;
814 }
Greg Daniel5c7b5412019-05-10 11:39:55 -0400815
816 bool dstHasYcbcr = tex->ycbcrConversionInfo().isValid();
817 if (!this->vkCaps().canCopyAsBlit(tex->config(), 1, false, dstHasYcbcr,
818 copyTexture->config(), 1, false,
819 false) &&
820 !this->vkCaps().canCopyAsDraw(tex->config(), SkToBool(tex->asRenderTarget()),
821 dstHasYcbcr,
822 copyTexture->config(), true, false)) {
823 return false;
824 }
825
Greg Daniel475eb702018-09-28 14:16:50 -0400826 uploadTexture = copyTexture.get();
827 uploadLeft = 0;
828 uploadTop = 0;
829 }
830
jvanverth900bd4a2016-04-29 13:53:12 -0700831 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400832 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700833
jvanverthc578b0632016-05-02 10:58:12 -0700834 currentWidth = width;
835 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400836 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400837 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400838 if (texelsShallowCopy[currentMipLevel].fPixels) {
839 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
840 const size_t trimRowBytes = currentWidth * bpp;
841 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
842 ? texelsShallowCopy[currentMipLevel].fRowBytes
843 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700844
Greg Daniel55afd6d2017-09-29 09:32:44 -0400845 // copy data into the buffer, skipping the trailing bytes
846 char* dst = buffer + individualMipOffsets[currentMipLevel];
847 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400848 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400849
850 VkBufferImageCopy& region = regions.push_back();
851 memset(&region, 0, sizeof(VkBufferImageCopy));
852 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
853 region.bufferRowLength = currentWidth;
854 region.bufferImageHeight = currentHeight;
855 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400856 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400857 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700858 }
jvanverthc578b0632016-05-02 10:58:12 -0700859 currentWidth = SkTMax(1, currentWidth/2);
860 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400861 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700862 }
863
jvanverth9d54afc2016-09-20 09:20:03 -0700864 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700865 transferBuffer->unmap();
866
jvanverth900bd4a2016-04-29 13:53:12 -0700867 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400868 uploadTexture->setImageLayout(this,
869 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
870 VK_ACCESS_TRANSFER_WRITE_BIT,
871 VK_PIPELINE_STAGE_TRANSFER_BIT,
872 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700873
874 // Copy the buffer to the image
875 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500876 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400877 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700878 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
879 regions.count(),
880 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400881
882 // If we copied the data into a temporary image first, copy that image into our main texture
883 // now.
884 if (copyTexture.get()) {
885 SkASSERT(dataColorType == GrColorType::kRGB_888x);
886 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
887 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
888 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
889 false));
890 }
Robert Phillips590533f2017-07-11 14:22:35 -0400891 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400892 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400893 }
jvanverth900bd4a2016-04-29 13:53:12 -0700894
Greg Daniel164a9f02016-02-22 09:56:40 -0500895 return true;
896}
897
Jim Van Verth1676cb92019-01-15 13:24:45 -0500898// It's probably possible to roll this into uploadTexDataOptimal,
899// but for now it's easier to maintain as a separate entity.
900bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
901 GrColorType dataColorType, const GrMipLevel texels[],
902 int mipLevelCount) {
903 SkASSERT(!tex->isLinearTiled());
904 // For now the assumption is that our rect is the entire texture.
905 // Compressed textures are read-only so this should be a reasonable assumption.
906 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
907
908 // We assume that if the texture has mip levels, we either upload to all the levels or just the
909 // first.
910 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
911
912 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
913 GrSRGBEncoded::kNo)));
914
915 if (width == 0 || height == 0) {
916 return false;
917 }
918
919 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
920 return false;
921 }
922
923 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
924
925 SkTArray<size_t> individualMipOffsets(mipLevelCount);
926 individualMipOffsets.push_back(0);
Robert Phillips1f098982019-05-15 10:27:36 -0400927 size_t combinedBufferSize = GrVkFormatCompressedDataSize(tex->imageFormat(), width, height);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500928 int currentWidth = width;
929 int currentHeight = height;
930 if (!texels[0].fPixels) {
931 return false;
932 }
933
934 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
935 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
936 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
937 currentWidth = SkTMax(1, currentWidth / 2);
938 currentHeight = SkTMax(1, currentHeight / 2);
939
940 if (texels[currentMipLevel].fPixels) {
Robert Phillips1f098982019-05-15 10:27:36 -0400941 const size_t dataSize = GrVkFormatCompressedDataSize(tex->imageFormat(), currentWidth,
942 currentHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500943 individualMipOffsets.push_back(combinedBufferSize);
944 combinedBufferSize += dataSize;
945 } else {
946 return false;
947 }
948 }
949 if (0 == combinedBufferSize) {
950 // We don't have any data to upload so fail (compressed textures are read-only).
951 return false;
952 }
953
954 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500955 sk_sp<GrVkTransferBuffer> transferBuffer =
956 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500957 if (!transferBuffer) {
958 return false;
959 }
960
961 int uploadLeft = left;
962 int uploadTop = top;
963 GrVkTexture* uploadTexture = tex;
964
965 char* buffer = (char*)transferBuffer->map();
966 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
967
968 currentWidth = width;
969 currentHeight = height;
970 int layerHeight = uploadTexture->height();
971 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
972 if (texels[currentMipLevel].fPixels) {
973 // Again, we're assuming that our rect is the entire texture
974 SkASSERT(currentHeight == layerHeight);
975 SkASSERT(0 == uploadLeft && 0 == uploadTop);
976
Robert Phillips1f098982019-05-15 10:27:36 -0400977 const size_t dataSize = GrVkFormatCompressedDataSize(tex->imageFormat(), currentWidth,
978 currentHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500979
980 // copy data into the buffer, skipping the trailing bytes
981 char* dst = buffer + individualMipOffsets[currentMipLevel];
982 const char* src = (const char*)texels[currentMipLevel].fPixels;
983 memcpy(dst, src, dataSize);
984
985 VkBufferImageCopy& region = regions.push_back();
986 memset(&region, 0, sizeof(VkBufferImageCopy));
987 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
988 region.bufferRowLength = currentWidth;
989 region.bufferImageHeight = currentHeight;
990 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
991 region.imageOffset = { uploadLeft, uploadTop, 0 };
992 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
993 }
994 currentWidth = SkTMax(1, currentWidth / 2);
995 currentHeight = SkTMax(1, currentHeight / 2);
996 layerHeight = currentHeight;
997 }
998
999 // no need to flush non-coherent memory, unmap will do that for us
1000 transferBuffer->unmap();
1001
1002 // Change layout of our target so it can be copied to
1003 uploadTexture->setImageLayout(this,
1004 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1005 VK_ACCESS_TRANSFER_WRITE_BIT,
1006 VK_PIPELINE_STAGE_TRANSFER_BIT,
1007 false);
1008
1009 // Copy the buffer to the image
1010 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -05001011 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -05001012 uploadTexture,
1013 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1014 regions.count(),
1015 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -05001016
1017 if (1 == mipLevelCount) {
1018 tex->texturePriv().markMipMapsDirty();
1019 }
1020
1021 return true;
1022}
1023
Greg Daniel164a9f02016-02-22 09:56:40 -05001024////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -04001025sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001026 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001027 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1028
1029 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001030 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001031
Greg Daniel164a9f02016-02-22 09:56:40 -05001032 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1033 if (renderTarget) {
1034 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1035 }
1036
1037 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1038 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1039 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001040 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001041 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1042 // texture.
1043 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1044
Greg Daniel164a9f02016-02-22 09:56:40 -05001045 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001046 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001047 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001048 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001049 GrVkImage::ImageDesc imageDesc;
1050 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1051 imageDesc.fFormat = pixelFormat;
1052 imageDesc.fWidth = desc.fWidth;
1053 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001054 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001055 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001056 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001057 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001058 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001059
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001060 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1061 if (mipLevels > 1) {
1062 mipMapsStatus = GrMipMapsStatus::kValid;
1063 for (int i = 0; i < mipLevels; ++i) {
1064 if (!texels[i].fPixels) {
1065 mipMapsStatus = GrMipMapsStatus::kDirty;
1066 break;
1067 }
Greg Daniel834f1202017-10-09 15:06:20 -04001068 }
1069 }
1070
Robert Phillips67d52cf2017-06-05 13:38:13 -04001071 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001072 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001073 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1074 imageDesc,
1075 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001076 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001077 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001078 }
1079
1080 if (!tex) {
1081 return nullptr;
1082 }
1083
Robert Phillips1f098982019-05-15 10:27:36 -04001084 bool isCompressed = GrVkFormatIsCompressed(tex->imageFormat());
Brian Salomonc320b152018-02-20 14:05:36 -05001085 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001086 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001087 bool success;
1088 if (isCompressed) {
1089 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1090 colorType, texels, mipLevelCount);
1091 } else {
1092 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1093 colorType, texels, mipLevelCount);
1094 }
1095 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001096 tex->unref();
1097 return nullptr;
1098 }
1099 }
1100
Jim Van Verth1676cb92019-01-15 13:24:45 -05001101 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001102 VkClearColorValue zeroClearColor;
1103 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1104 VkImageSubresourceRange range;
1105 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1106 range.baseArrayLayer = 0;
1107 range.baseMipLevel = 0;
1108 range.layerCount = 1;
1109 range.levelCount = 1;
1110 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1111 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001112 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001113 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001114 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001115}
1116
1117////////////////////////////////////////////////////////////////////////////////
1118
Greg Daniel6888c0d2017-08-25 11:55:50 -04001119void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1120 VkDeviceSize dstOffset, VkDeviceSize size) {
1121 VkBufferCopy copyRegion;
1122 copyRegion.srcOffset = srcOffset;
1123 copyRegion.dstOffset = dstOffset;
1124 copyRegion.size = size;
1125 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1126}
1127
jvanverthdb379092016-07-07 11:18:46 -07001128bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1129 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001130 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001131 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001132
1133 return true;
1134}
1135
1136////////////////////////////////////////////////////////////////////////////////
1137
Greg Daniel7e000222018-12-03 10:08:21 -05001138static bool check_image_info(const GrVkCaps& caps,
1139 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001140 GrPixelConfig config,
1141 bool isWrappedRT) {
1142 if (VK_NULL_HANDLE == info.fImage) {
1143 return false;
1144 }
1145
1146 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001147 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001148 }
1149
Greg Daniel7e000222018-12-03 10:08:21 -05001150 if (info.fYcbcrConversionInfo.isValid()) {
1151 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1152 return false;
1153 }
jvanverthfd359ca2016-03-18 11:57:24 -07001154 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001155
Greg Danielcb324152019-02-25 11:36:53 -05001156 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1157 return false;
1158 }
1159
Greg Daniel52e16d92018-04-10 09:34:07 -04001160 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001161 return true;
1162}
1163
Greg Danielcaa795f2019-05-14 11:54:25 -04001164static bool check_tex_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) {
1165 if (info.fImageTiling == VK_IMAGE_TILING_OPTIMAL) {
Robert Phillips39ef2ef2019-05-15 08:45:53 -04001166 if (!caps.isFormatTexturable(info.fFormat)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001167 return false;
1168 }
1169 } else {
1170 SkASSERT(info.fImageTiling == VK_IMAGE_TILING_LINEAR);
Robert Phillips39ef2ef2019-05-15 08:45:53 -04001171 if (!caps.isFormatTexturableLinearly(info.fFormat)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001172 return false;
1173 }
1174 }
1175 return true;
1176}
1177
1178static bool check_rt_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) {
1179 if (!caps.maxRenderTargetSampleCount(info.fFormat)) {
1180 return false;
1181 }
1182 return true;
1183}
1184
Brian Salomond17f6582017-07-19 18:28:58 -04001185sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001186 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1187 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001188 GrVkImageInfo imageInfo;
1189 if (!backendTex.getVkImageInfo(&imageInfo)) {
1190 return nullptr;
1191 }
1192
Greg Danielcb324152019-02-25 11:36:53 -05001193 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001194 return nullptr;
1195 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001196 if (!check_tex_image_info(this->vkCaps(), imageInfo)) {
1197 return nullptr;
1198 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001199
Greg Daniel164a9f02016-02-22 09:56:40 -05001200 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001201 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001202 surfDesc.fWidth = backendTex.width();
1203 surfDesc.fHeight = backendTex.height();
1204 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001205 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001206
Greg Daniel52e16d92018-04-10 09:34:07 -04001207 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1208 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001209 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1210 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001211}
1212
1213sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001214 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001215 GrWrapOwnership ownership,
1216 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001217 GrVkImageInfo imageInfo;
1218 if (!backendTex.getVkImageInfo(&imageInfo)) {
1219 return nullptr;
1220 }
1221
Greg Danielcb324152019-02-25 11:36:53 -05001222 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001223 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001224 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001225 if (!check_tex_image_info(this->vkCaps(), imageInfo)) {
1226 return nullptr;
1227 }
1228 if (!check_rt_image_info(this->vkCaps(), imageInfo)) {
1229 return nullptr;
1230 }
Brian Salomond17f6582017-07-19 18:28:58 -04001231
1232 GrSurfaceDesc surfDesc;
1233 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1234 surfDesc.fWidth = backendTex.width();
1235 surfDesc.fHeight = backendTex.height();
1236 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001237 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001238
Greg Daniel52e16d92018-04-10 09:34:07 -04001239 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1240 SkASSERT(layout);
1241
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001242 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1243 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001244}
1245
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001246sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001247 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1248 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1249 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1250 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001251 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001252 return nullptr;
1253 }
halcanary9d524f22016-03-29 09:03:52 -07001254
Greg Daniel323fbcf2018-04-10 13:46:30 -04001255 GrVkImageInfo info;
1256 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001257 return nullptr;
1258 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001259
Greg Danielcb324152019-02-25 11:36:53 -05001260 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001261 return nullptr;
1262 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001263 if (!check_rt_image_info(this->vkCaps(), info)) {
1264 return nullptr;
1265 }
1266
Greg Daniel164a9f02016-02-22 09:56:40 -05001267
Greg Daniel164a9f02016-02-22 09:56:40 -05001268 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001269 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001270 desc.fWidth = backendRT.width();
1271 desc.fHeight = backendRT.height();
1272 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001273 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001274
Greg Daniel323fbcf2018-04-10 13:46:30 -04001275 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001276
Greg Daniel323fbcf2018-04-10 13:46:30 -04001277 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001278 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001279
1280 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1281 SkASSERT(!backendRT.stencilBits());
1282 if (tgt) {
1283 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001284 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001285
Ben Wagnerff134f22018-04-24 16:29:16 -04001286 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001287}
1288
Greg Daniel7ef28f32017-04-20 16:41:55 +00001289sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001290 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001291
Greg Daniel52e16d92018-04-10 09:34:07 -04001292 GrVkImageInfo imageInfo;
1293 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001294 return nullptr;
1295 }
Greg Danielcb324152019-02-25 11:36:53 -05001296 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001297 return nullptr;
1298 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001299 if (!check_rt_image_info(this->vkCaps(), imageInfo)) {
1300 return nullptr;
1301 }
Greg Danielcb324152019-02-25 11:36:53 -05001302
Brian Osman33910292017-04-18 14:38:53 -04001303 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001304 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001305 desc.fWidth = tex.width();
1306 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001307 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001308 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1309 if (!desc.fSampleCnt) {
1310 return nullptr;
1311 }
Brian Osman33910292017-04-18 14:38:53 -04001312
Greg Daniel52e16d92018-04-10 09:34:07 -04001313 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1314 SkASSERT(layout);
1315
Ben Wagnerff134f22018-04-24 16:29:16 -04001316 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001317}
1318
Greg Danielb46add82019-01-02 14:51:29 -05001319sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1320 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1321 int maxSize = this->caps()->maxTextureSize();
1322 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1323 return nullptr;
1324 }
1325
1326 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1327 if (!backendFormat.isValid()) {
1328 return nullptr;
1329 }
1330 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1331 imageInfo.colorType());
1332 if (config == kUnknown_GrPixelConfig) {
1333 return nullptr;
1334 }
1335
1336 GrSurfaceDesc desc;
1337 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1338 desc.fWidth = imageInfo.width();
1339 desc.fHeight = imageInfo.height();
1340 desc.fConfig = config;
1341 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1342 if (!desc.fSampleCnt) {
1343 return nullptr;
1344 }
1345
1346 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1347}
1348
Brian Salomon930f9392018-06-20 16:25:26 -04001349bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1350 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001351 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001352 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001353 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001354 return false;
jvanverth62340062016-04-26 08:01:44 -07001355 }
1356
jvanverth62340062016-04-26 08:01:44 -07001357 // determine if we can blit to and from this format
1358 const GrVkCaps& caps = this->vkCaps();
Greg Danielcaa795f2019-05-14 11:54:25 -04001359 if (!caps.formatCanBeDstofBlit(vkTex->imageFormat(), false) ||
1360 !caps.formatCanBeSrcofBlit(vkTex->imageFormat(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001361 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001362 return false;
jvanverth62340062016-04-26 08:01:44 -07001363 }
1364
egdaniel7ac5da82016-07-15 13:41:42 -07001365 int width = tex->width();
1366 int height = tex->height();
1367 VkImageBlit blitRegion;
1368 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001369
jvanverth82c05582016-05-03 11:19:01 -07001370 // SkMipMap doesn't include the base level in the level count so we have to add 1
1371 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001372 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001373
Greg Danielda86e282018-06-13 09:41:19 -04001374 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001375 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1376 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001377
jvanverth50c46c72016-05-06 12:31:28 -07001378 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001379 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001380 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1381 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001382 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1383 nullptr, // pNext
1384 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1385 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1386 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1387 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1388 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1389 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1390 vkTex->image(), // image
1391 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001392 };
1393
jvanverth62340062016-04-26 08:01:44 -07001394 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001395 uint32_t mipLevel = 1;
1396 while (mipLevel < levelCount) {
1397 int prevWidth = width;
1398 int prevHeight = height;
1399 width = SkTMax(1, width / 2);
1400 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001401
jvanverth50c46c72016-05-06 12:31:28 -07001402 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001403 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1404 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001405
1406 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001407 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001408 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001409 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1410 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001411 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001412 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001413 vkTex->resource(),
1414 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001415 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001416 vkTex->resource(),
1417 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001418 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001419 1,
1420 &blitRegion,
1421 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001422 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001423 }
Greg Danielee54f232019-04-03 14:58:40 -04001424 if (levelCount > 1) {
1425 // This barrier logically is not needed, but it changes the final level to the same layout
1426 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1427 // layouts and future layout changes easier. The alternative here would be to track layout
1428 // and memory accesses per layer which doesn't seem work it.
1429 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1430 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1431 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1432 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1433 }
Brian Salomon930f9392018-06-20 16:25:26 -04001434 return true;
jvanverth62340062016-04-26 08:01:44 -07001435}
1436
Greg Daniel164a9f02016-02-22 09:56:40 -05001437////////////////////////////////////////////////////////////////////////////////
1438
1439GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1440 int width,
1441 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001442 SkASSERT(width >= rt->width());
1443 SkASSERT(height >= rt->height());
1444
1445 int samples = rt->numStencilSamples();
1446
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001447 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001448
1449 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001450 width,
1451 height,
1452 samples,
1453 sFmt));
1454 fStats.incStencilAttachmentCreates();
1455 return stencil;
1456}
1457
1458////////////////////////////////////////////////////////////////////////////////
1459
Brian Salomon52e943a2018-03-13 09:32:39 -04001460bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001461 size_t bufferOffset, size_t srcRowBytes,
1462 GrPixelConfig destConfig, size_t dstRowBytes,
1463 size_t trimRowBytes, int width, int height, const SkColor4f* color) {
1464 SkASSERT(srcData || color);
1465
Robert Phillipsf62e5752019-05-30 10:36:13 -04001466 VkDeviceSize size = dstRowBytes * height;
Greg Daniel81df0412018-05-31 13:13:33 -04001467 VkDeviceSize offset = bufferOffset;
1468 SkASSERT(size + offset <= alloc.fSize);
1469 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1470 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001471 return false;
1472 }
Greg Daniel81df0412018-05-31 13:13:33 -04001473 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001474
Greg Daniel20ece3a2017-03-28 10:24:43 -04001475 if (srcData) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001476 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, height);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001477 } else {
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001478 if (kRGB_ETC1_GrPixelConfig == destConfig) {
1479 GrFillInETC1WithColor(width, height, *color, mapPtr);
1480 } else {
1481 GrFillBufferWithColor(destConfig, width, height, *color, mapPtr);
1482 }
Greg Daniel20ece3a2017-03-28 10:24:43 -04001483 }
Greg Daniel81df0412018-05-31 13:13:33 -04001484 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1485 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001486 return true;
1487}
1488
Robert Phillips1f098982019-05-15 10:27:36 -04001489static size_t compute_combined_buffer_size(VkFormat format, size_t bpp, int w, int h,
Robert Phillips42dda082019-05-14 13:29:45 -04001490 SkTArray<size_t>* individualMipOffsets,
1491 uint32_t mipLevels) {
1492
1493 size_t combinedBufferSize = w * bpp * h;
Robert Phillips1f098982019-05-15 10:27:36 -04001494 if (GrVkFormatIsCompressed(format)) {
1495 combinedBufferSize = GrVkFormatCompressedDataSize(format, w, h);
Robert Phillips42dda082019-05-14 13:29:45 -04001496 }
1497
1498 int currentWidth = w;
1499 int currentHeight = h;
1500
1501 // The Vulkan spec for copying a buffer to an image, requires that the alignment must be at
1502 // least 4 bytes and a multiple of the bytes per pixel of the image config.
1503 SkASSERT(bpp == 1 || bpp == 2 || bpp == 3 || bpp == 4 || bpp == 8 || bpp == 16);
1504 int desiredAlignment = (bpp == 3) ? 12 : (bpp > 4 ? bpp : 4);
1505
1506 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1507 currentWidth = SkTMax(1, currentWidth / 2);
1508 currentHeight = SkTMax(1, currentHeight / 2);
1509
1510 size_t trimmedSize;
Robert Phillips1f098982019-05-15 10:27:36 -04001511 if (GrVkFormatIsCompressed(format)) {
1512 trimmedSize = GrVkFormatCompressedDataSize(format, currentWidth, currentHeight);
Robert Phillips42dda082019-05-14 13:29:45 -04001513 } else {
1514 trimmedSize = currentWidth * bpp * currentHeight;
1515 }
1516 const size_t alignmentDiff = combinedBufferSize % desiredAlignment;
1517 if (alignmentDiff != 0) {
1518 combinedBufferSize += desiredAlignment - alignmentDiff;
1519 }
1520 SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bpp));
1521
1522 individualMipOffsets->push_back(combinedBufferSize);
1523 combinedBufferSize += trimmedSize;
1524 }
1525
1526 return combinedBufferSize;
1527}
1528
Brian Salomon52e943a2018-03-13 09:32:39 -04001529bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1530 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001531 size_t srcRowBytes, const SkColor4f& color,
1532 GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001533 SkASSERT(texturable || renderable);
1534 if (!texturable) {
1535 SkASSERT(GrMipMapped::kNo == mipMapped);
1536 SkASSERT(!srcData);
1537 }
Robert Phillips42dda082019-05-14 13:29:45 -04001538 VkFormat vkFormat;
1539 if (!GrPixelConfigToVkFormat(config, &vkFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001540 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001541 }
1542
Robert Phillips0c6daf02019-05-16 12:43:11 -04001543 if (texturable && !fVkCaps->isFormatTexturable(vkFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001544 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001545 }
1546
Robert Phillips0c6daf02019-05-16 12:43:11 -04001547 if (renderable && !fVkCaps->isFormatRenderable(vkFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001548 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001549 }
1550
1551 // Currently we don't support uploading pixel data when mipped.
1552 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001553 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001554 }
1555
Brian Salomon52e943a2018-03-13 09:32:39 -04001556 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001557 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1558 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001559 if (texturable) {
1560 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1561 }
1562 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001563 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1564 }
1565
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001566 // Figure out the number of mip levels.
1567 uint32_t mipLevels = 1;
1568 if (GrMipMapped::kYes == mipMapped) {
1569 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1570 }
1571
Robert Phillipsf62e5752019-05-30 10:36:13 -04001572 GrVkImage::ImageDesc imageDesc;
1573 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1574 imageDesc.fFormat = vkFormat;
1575 imageDesc.fWidth = w;
1576 imageDesc.fHeight = h;
1577 imageDesc.fLevels = mipLevels;
1578 imageDesc.fSamples = 1;
1579 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1580 imageDesc.fUsageFlags = usageFlags;
1581 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001582
Robert Phillipsf62e5752019-05-30 10:36:13 -04001583 if (!GrVkImage::InitImageInfo(this, imageDesc, info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001584 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001585 }
1586
1587 // 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 -05001588 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001589 VkBuffer buffer = VK_NULL_HANDLE;
1590
1591 VkResult err;
1592 const VkCommandBufferAllocateInfo cmdInfo = {
1593 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1594 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001595 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001596 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1597 1 // bufferCount
1598 };
1599
1600 VkCommandBuffer cmdBuffer;
1601 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1602 if (err) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001603 GrVkImage::DestroyImageInfo(this, info);
Brian Salomon52e943a2018-03-13 09:32:39 -04001604 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001605 }
1606
1607 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1608 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1609 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1610 cmdBufferBeginInfo.pNext = nullptr;
1611 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1612 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1613
1614 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1615 SkASSERT(!err);
1616
Robert Phillips1f098982019-05-15 10:27:36 -04001617 size_t bpp = GrVkBytesPerFormat(vkFormat);
Brian Salomonde9f5462018-03-07 14:23:58 -05001618 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001619
Brian Salomonde9f5462018-03-07 14:23:58 -05001620 SkTArray<size_t> individualMipOffsets(mipLevels);
1621 individualMipOffsets.push_back(0);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001622
Robert Phillips1f098982019-05-15 10:27:36 -04001623 size_t combinedBufferSize = compute_combined_buffer_size(vkFormat, bpp, w, h,
Robert Phillips42dda082019-05-14 13:29:45 -04001624 &individualMipOffsets, mipLevels);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001625
Brian Salomonde9f5462018-03-07 14:23:58 -05001626 VkBufferCreateInfo bufInfo;
1627 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1628 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1629 bufInfo.flags = 0;
1630 bufInfo.size = combinedBufferSize;
1631 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1632 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1633 bufInfo.queueFamilyIndexCount = 0;
1634 bufInfo.pQueueFamilyIndices = nullptr;
1635 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001636
Brian Salomonde9f5462018-03-07 14:23:58 -05001637 if (err) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001638 GrVkImage::DestroyImageInfo(this, info);
Brian Salomonde9f5462018-03-07 14:23:58 -05001639 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001640 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001641 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001642 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001643
Brian Salomonde9f5462018-03-07 14:23:58 -05001644 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1645 &bufferAlloc)) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001646 GrVkImage::DestroyImageInfo(this, info);
Brian Salomonde9f5462018-03-07 14:23:58 -05001647 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1648 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001649 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001650 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001651 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001652
Robert Phillips42dda082019-05-14 13:29:45 -04001653 int currentWidth = w;
1654 int currentHeight = h;
Brian Salomonde9f5462018-03-07 14:23:58 -05001655 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1656 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001657 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001658 bool result;
Robert Phillips1f098982019-05-15 10:27:36 -04001659 if (GrVkFormatIsCompressed(vkFormat)) {
1660 size_t levelSize = GrVkFormatCompressedDataSize(vkFormat, currentWidth, currentHeight);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001661 size_t currentRowBytes = levelSize / currentHeight;
1662 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001663 config, currentRowBytes, currentRowBytes,
1664 currentWidth, currentHeight, &color);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001665 } else {
Robert Phillips27eb5252019-06-03 12:59:40 -04001666 const size_t trimRowBytes = bpp * currentWidth;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001667 if (!srcRowBytes) {
1668 srcRowBytes = trimRowBytes;
1669 }
1670
Jim Van Verth1676cb92019-01-15 13:24:45 -05001671 size_t currentRowBytes = bpp * currentWidth;
1672 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001673 config, currentRowBytes, trimRowBytes,
1674 currentWidth, currentHeight, &color);
Jim Van Verth1676cb92019-01-15 13:24:45 -05001675 }
1676 if (!result) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001677 GrVkImage::DestroyImageInfo(this, info);
Brian Salomonde9f5462018-03-07 14:23:58 -05001678 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001679 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1680 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001681 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001682 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001683 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001684 currentWidth = SkTMax(1, currentWidth / 2);
1685 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001686 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001687
1688 // Set image layout and add barrier
1689 VkImageMemoryBarrier barrier;
1690 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1691 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1692 barrier.pNext = nullptr;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001693 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(info->fImageLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001694 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001695 barrier.oldLayout = info->fImageLayout;
Brian Salomonde9f5462018-03-07 14:23:58 -05001696 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1697 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1698 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001699 barrier.image = info->fImage;
Brian Salomonde9f5462018-03-07 14:23:58 -05001700 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1701
Robert Phillipsf62e5752019-05-30 10:36:13 -04001702 VK_CALL(CmdPipelineBarrier(cmdBuffer,
1703 GrVkImage::LayoutToPipelineSrcStageFlags(info->fImageLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001704 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1705 &barrier));
Robert Phillipsf62e5752019-05-30 10:36:13 -04001706 info->fImageLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
Brian Salomonde9f5462018-03-07 14:23:58 -05001707
1708 SkTArray<VkBufferImageCopy> regions(mipLevels);
1709
1710 currentWidth = w;
1711 currentHeight = h;
1712 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1713 // Submit copy command
1714 VkBufferImageCopy& region = regions.push_back();
1715 memset(&region, 0, sizeof(VkBufferImageCopy));
1716 region.bufferOffset = individualMipOffsets[currentMipLevel];
1717 region.bufferRowLength = currentWidth;
1718 region.bufferImageHeight = currentHeight;
Robert Phillips27eb5252019-06-03 12:59:40 -04001719 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, currentMipLevel, 0, 1};
Brian Salomonde9f5462018-03-07 14:23:58 -05001720 region.imageOffset = {0, 0, 0};
1721 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1722 currentWidth = SkTMax(1, currentWidth / 2);
1723 currentHeight = SkTMax(1, currentHeight / 2);
1724 }
1725
Robert Phillipsf62e5752019-05-30 10:36:13 -04001726 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, info->fImage, info->fImageLayout,
1727 regions.count(), regions.begin()));
Brian Salomonde9f5462018-03-07 14:23:58 -05001728
Brian Salomon52e943a2018-03-13 09:32:39 -04001729 if (texturable) {
1730 // Change Image layout to shader read since if we use this texture as a borrowed textures
1731 // within Ganesh we require that its layout be set to that
1732 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1733 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1734 barrier.pNext = nullptr;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001735 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(info->fImageLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001736 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001737 barrier.oldLayout = info->fImageLayout;
Brian Salomon52e943a2018-03-13 09:32:39 -04001738 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1739 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1740 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
Robert Phillipsf62e5752019-05-30 10:36:13 -04001741 barrier.image = info->fImage;
Brian Salomon52e943a2018-03-13 09:32:39 -04001742 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001743 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Robert Phillipsf62e5752019-05-30 10:36:13 -04001744 GrVkImage::LayoutToPipelineSrcStageFlags(info->fImageLayout),
Greg Danielf7828d02018-10-09 12:01:32 -04001745 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001746 0,
1747 0, nullptr,
1748 0, nullptr,
1749 1, &barrier));
Robert Phillipsf62e5752019-05-30 10:36:13 -04001750 info->fImageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001751 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001752
1753 // End CommandBuffer
1754 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1755 SkASSERT(!err);
1756
1757 // Create Fence for queue
1758 VkFence fence;
1759 VkFenceCreateInfo fenceInfo;
1760 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1761 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1762
1763 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1764 SkASSERT(!err);
1765
1766 VkSubmitInfo submitInfo;
1767 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1768 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1769 submitInfo.pNext = nullptr;
1770 submitInfo.waitSemaphoreCount = 0;
1771 submitInfo.pWaitSemaphores = nullptr;
1772 submitInfo.pWaitDstStageMask = 0;
1773 submitInfo.commandBufferCount = 1;
1774 submitInfo.pCommandBuffers = &cmdBuffer;
1775 submitInfo.signalSemaphoreCount = 0;
1776 submitInfo.pSignalSemaphores = nullptr;
1777 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1778 SkASSERT(!err);
1779
1780 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1781 if (VK_TIMEOUT == err) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001782 GrVkImage::DestroyImageInfo(this, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001783 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1784 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001785 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001786 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1787 SkDebugf("Fence failed to signal: %d\n", err);
1788 SK_ABORT("failing");
1789 }
1790 SkASSERT(!err);
1791
1792 // Clean up transfer resources
1793 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1794 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1795 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1796 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001797 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001798 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1799
Brian Salomon52e943a2018-03-13 09:32:39 -04001800 return true;
1801}
1802
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001803static bool vk_format_to_pixel_config(VkFormat format, GrPixelConfig* config) {
1804 GrPixelConfig dontCare;
1805 if (!config) {
1806 config = &dontCare;
1807 }
1808
1809 switch (format) {
1810 case VK_FORMAT_UNDEFINED:
1811 *config = kUnknown_GrPixelConfig;
1812 return false;
1813 case VK_FORMAT_R8G8B8A8_UNORM:
1814 *config = kRGBA_8888_GrPixelConfig;
1815 return true;
1816 case VK_FORMAT_R8G8B8_UNORM:
1817 *config = kRGB_888_GrPixelConfig;
1818 return true;
1819 case VK_FORMAT_R8G8_UNORM:
1820 *config = kRG_88_GrPixelConfig;
1821 return true;
1822 case VK_FORMAT_B8G8R8A8_UNORM:
1823 *config = kBGRA_8888_GrPixelConfig;
1824 return true;
1825 case VK_FORMAT_R8G8B8A8_SRGB:
1826 *config = kSRGBA_8888_GrPixelConfig;
1827 return true;
1828 case VK_FORMAT_B8G8R8A8_SRGB:
1829 *config = kSBGRA_8888_GrPixelConfig;
1830 return true;
1831 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1832 *config = kRGBA_1010102_GrPixelConfig;
1833 return true;
1834 case VK_FORMAT_R5G6B5_UNORM_PACK16:
1835 *config = kRGB_565_GrPixelConfig;
1836 return true;
1837 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
1838 *config = kRGBA_4444_GrPixelConfig; // we're swizzling in this case
1839 return true;
1840 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
1841 *config = kRGBA_4444_GrPixelConfig;
1842 return true;
1843 case VK_FORMAT_R8_UNORM:
1844 *config = kAlpha_8_GrPixelConfig;
1845 return true;
1846 case VK_FORMAT_R32G32B32A32_SFLOAT:
1847 *config = kRGBA_float_GrPixelConfig;
1848 return true;
1849 case VK_FORMAT_R32G32_SFLOAT:
1850 *config = kRG_float_GrPixelConfig;
1851 return true;
1852 case VK_FORMAT_R16G16B16A16_SFLOAT:
1853 *config = kRGBA_half_GrPixelConfig;
1854 return true;
1855 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
1856 *config = kRGB_ETC1_GrPixelConfig;
1857 return true;
1858 case VK_FORMAT_R16_SFLOAT:
1859 *config = kAlpha_half_GrPixelConfig;
1860 return true;
1861 default:
1862 return false;
1863 }
1864 SK_ABORT("Unexpected config");
1865 return false;
1866}
1867
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001868GrBackendTexture GrVkGpu::createBackendTexture(int w, int h,
1869 const GrBackendFormat& format,
1870 GrMipMapped mipMapped,
1871 GrRenderable renderable,
Robert Phillips459b2952019-05-23 09:38:27 -04001872 const void* srcData, size_t rowBytes,
1873 const SkColor4f& color) {
Brian Salomon8a375832018-03-14 10:21:40 -04001874 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001875
1876 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1877 return GrBackendTexture();
1878 }
1879
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001880 const VkFormat* vkFormat = format.getVkFormat();
1881 if (!vkFormat) {
1882 return GrBackendTexture();
1883 }
1884
1885 GrPixelConfig config;
1886
1887 if (!vk_format_to_pixel_config(*vkFormat, &config)) {
1888 return GrBackendTexture();
1889 }
Robert Phillips646f6372018-09-25 09:31:10 -04001890 if (!this->caps()->isConfigTexturable(config)) {
1891 return GrBackendTexture();
1892 }
1893
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001894 GrVkImageInfo info;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001895 if (!this->createTestingOnlyVkImage(config, w, h, true, GrRenderable::kYes == renderable,
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001896 mipMapped, srcData, rowBytes, color, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001897 return {};
1898 }
Greg Daniel108bb232018-07-03 16:18:29 -04001899 GrBackendTexture beTex = GrBackendTexture(w, h, info);
Robert Phillipsf0ced622019-05-16 09:06:25 -04001900#if GR_TEST_UTILS
Greg Daniel108bb232018-07-03 16:18:29 -04001901 // Lots of tests don't go through Skia's public interface which will set the config so for
1902 // testing we make sure we set a config here.
1903 beTex.setPixelConfig(config);
Robert Phillipsf0ced622019-05-16 09:06:25 -04001904#endif
Greg Daniel108bb232018-07-03 16:18:29 -04001905 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001906}
1907
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001908void GrVkGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04001909 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
1910
1911 GrVkImageInfo info;
1912 if (tex.getVkImageInfo(&info)) {
1913 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
1914 }
1915}
1916
1917#if GR_TEST_UTILS
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001918bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001919 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001920
Greg Daniel52e16d92018-04-10 09:34:07 -04001921 GrVkImageInfo backend;
1922 if (!tex.getVkImageInfo(&backend)) {
1923 return false;
1924 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001925
Greg Daniel52e16d92018-04-10 09:34:07 -04001926 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001927 VkMemoryRequirements req;
1928 memset(&req, 0, sizeof(req));
1929 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001930 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001931 &req));
1932 // TODO: find a better check
1933 // This will probably fail with a different driver
1934 return (req.size > 0) && (req.size <= 8192 * 8192);
1935 }
1936
1937 return false;
1938}
1939
Brian Osman2d010b62018-08-09 10:55:09 -04001940GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001941 this->handleDirtyContext();
1942
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001943 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1944 return GrBackendRenderTarget();
1945 }
1946
Brian Osman2d010b62018-08-09 10:55:09 -04001947 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001948 if (kUnknown_GrPixelConfig == config) {
1949 return {};
1950 }
Robert Phillipsf62e5752019-05-30 10:36:13 -04001951
1952 GrVkImageInfo info;
Robert Phillips646f6372018-09-25 09:31:10 -04001953 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001954 SkColors::kTransparent, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001955 return {};
1956 }
Greg Daniel108bb232018-07-03 16:18:29 -04001957 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1958 // Lots of tests don't go through Skia's public interface which will set the config so for
1959 // testing we make sure we set a config here.
1960 beRT.setPixelConfig(config);
1961 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001962}
1963
Brian Salomon52e943a2018-03-13 09:32:39 -04001964void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001965 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001966
Greg Daniel323fbcf2018-04-10 13:46:30 -04001967 GrVkImageInfo info;
1968 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001969 // something in the command buffer may still be using this, so force submit
1970 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001971 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001972 }
1973}
Brian Salomonf865b052018-03-09 09:01:53 -05001974
Greg Daniel26b50a42018-03-08 09:49:58 -05001975void GrVkGpu::testingOnly_flushGpuAndSync() {
1976 this->submitCommandBuffer(kForce_SyncQueue);
1977}
Brian Salomonf865b052018-03-09 09:01:53 -05001978#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001979
Greg Daniel164a9f02016-02-22 09:56:40 -05001980////////////////////////////////////////////////////////////////////////////////
1981
Greg Daniel59dc1482019-02-22 10:46:38 -05001982void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1983 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001984 VkPipelineStageFlags dstStageMask,
1985 bool byRegion,
1986 VkBufferMemoryBarrier* barrier) const {
1987 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001988 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001989 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001990 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001991 srcStageMask,
1992 dstStageMask,
1993 byRegion,
1994 GrVkCommandBuffer::kBufferMemory_BarrierType,
1995 barrier);
1996}
1997
Greg Daniel59dc1482019-02-22 10:46:38 -05001998void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1999 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05002000 VkPipelineStageFlags dstStageMask,
2001 bool byRegion,
2002 VkImageMemoryBarrier* barrier) const {
2003 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05002004 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05002005 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05002006 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05002007 srcStageMask,
2008 dstStageMask,
2009 byRegion,
2010 GrVkCommandBuffer::kImageMemory_BarrierType,
2011 barrier);
2012}
2013
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002014void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n,
Greg Daniel797efca2019-05-09 14:04:20 -04002015 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
2016 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002017 SkASSERT(n >= 0);
2018 SkASSERT(!n || proxies);
Greg Daniel51316782017-08-02 15:10:09 +00002019 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
2020 // not effect what we do here.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002021 if (n && access == SkSurface::BackendSurfaceAccess::kPresent) {
Greg Danielbae71212019-03-01 15:24:35 -05002022 GrVkImage* image;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002023 for (int i = 0; i < n; ++i) {
2024 SkASSERT(proxies[i]->isInstantiated());
2025 if (GrTexture* tex = proxies[i]->peekTexture()) {
2026 image = static_cast<GrVkTexture*>(tex);
2027 } else {
2028 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
2029 SkASSERT(rt);
2030 image = static_cast<GrVkRenderTarget*>(rt);
2031 }
2032 image->prepareForPresent(this);
Greg Danielbae71212019-03-01 15:24:35 -05002033 }
Greg Danielbae71212019-03-01 15:24:35 -05002034 }
Greg Daniel797efca2019-05-09 14:04:20 -04002035
2036 // Handle requests for preparing for external IO
2037 for (int i = 0; i < externalRequests.fNumImages; ++i) {
2038 SkImage* image = externalRequests.fImages[i];
2039 if (!image->isTextureBacked()) {
2040 continue;
2041 }
2042 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
2043 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext());
2044 SkASSERT(proxy);
2045
2046 if (!proxy->isInstantiated()) {
2047 auto resourceProvider = this->getContext()->priv().resourceProvider();
2048 if (!proxy->instantiate(resourceProvider)) {
2049 continue;
2050 }
2051 }
2052
2053 GrTexture* tex = proxy->peekTexture();
2054 if (!tex) {
2055 continue;
2056 }
2057 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
2058 vkTex->prepareForExternal(this);
2059 }
2060 for (int i = 0; i < externalRequests.fNumSurfaces; ++i) {
2061 SkSurface* surface = externalRequests.fSurfaces[i];
2062 if (!surface->getCanvas()->getGrContext()) {
2063 continue;
2064 }
2065 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface);
2066 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
2067 sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef();
2068 if (!proxy->isInstantiated()) {
2069 auto resourceProvider = this->getContext()->priv().resourceProvider();
2070 if (!proxy->instantiate(resourceProvider)) {
2071 continue;
2072 }
2073 }
2074
2075 GrRenderTarget* rt = proxy->peekRenderTarget();
2076 SkASSERT(rt);
2077 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
2078 if (externalRequests.fPrepareSurfaceForPresent &&
2079 externalRequests.fPrepareSurfaceForPresent[i]) {
2080 vkRT->prepareForPresent(this);
2081 } else {
2082 vkRT->prepareForExternal(this);
2083 }
2084 }
2085
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002086 if (info.fFlags & kSyncCpu_GrFlushFlag) {
2087 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002088 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002089 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002090 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002091}
2092
Greg Daniel25af6712018-04-25 10:44:38 -04002093static int get_surface_sample_cnt(GrSurface* surf) {
2094 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
2095 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07002096 }
Greg Daniel25af6712018-04-25 10:44:38 -04002097 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05002098}
2099
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002100void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2101 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07002102 GrVkImage* dstImage,
2103 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002104 const SkIRect& srcRect,
2105 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002106#ifdef SK_DEBUG
2107 int dstSampleCnt = get_surface_sample_cnt(dst);
2108 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002109 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2110 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2111 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
2112 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04002113
2114#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05002115
Greg Daniel164a9f02016-02-22 09:56:40 -05002116 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
2117 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07002118 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07002119 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2120 VK_ACCESS_TRANSFER_WRITE_BIT,
2121 VK_PIPELINE_STAGE_TRANSFER_BIT,
2122 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002123
egdaniel17b89252016-04-05 07:23:38 -07002124 srcImage->setImageLayout(this,
2125 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002126 VK_ACCESS_TRANSFER_READ_BIT,
2127 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002128 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002129
2130 // Flip rect if necessary
2131 SkIRect srcVkRect = srcRect;
2132 int32_t dstY = dstPoint.fY;
2133
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002134 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2135 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05002136 srcVkRect.fTop = src->height() - srcRect.fBottom;
2137 srcVkRect.fBottom = src->height() - srcRect.fTop;
2138 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
2139 }
2140
2141 VkImageCopy copyRegion;
2142 memset(&copyRegion, 0, sizeof(VkImageCopy));
2143 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2144 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
2145 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2146 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07002147 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002148
2149 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07002150 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002151 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07002152 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002153 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2154 1,
2155 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07002156
2157 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2158 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002159 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05002160}
2161
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002162void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2163 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07002164 GrVkImage* dstImage,
2165 GrVkImage* srcImage,
2166 const SkIRect& srcRect,
2167 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002168#ifdef SK_DEBUG
2169 int dstSampleCnt = get_surface_sample_cnt(dst);
2170 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002171 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2172 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04002173 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002174 dstHasYcbcr, src->config(), srcSampleCnt,
2175 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002176
Greg Daniel25af6712018-04-25 10:44:38 -04002177#endif
egdaniel17b89252016-04-05 07:23:38 -07002178 dstImage->setImageLayout(this,
2179 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002180 VK_ACCESS_TRANSFER_WRITE_BIT,
2181 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002182 false);
2183
egdaniel17b89252016-04-05 07:23:38 -07002184 srcImage->setImageLayout(this,
2185 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002186 VK_ACCESS_TRANSFER_READ_BIT,
2187 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002188 false);
2189
2190 // Flip rect if necessary
2191 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002192 srcVkRect.fLeft = srcRect.fLeft;
2193 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002194 SkIRect dstRect;
2195 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002196 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002197
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002198 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002199 srcVkRect.fTop = src->height() - srcRect.fBottom;
2200 srcVkRect.fBottom = src->height() - srcRect.fTop;
2201 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002202 srcVkRect.fTop = srcRect.fTop;
2203 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002204 }
2205
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002206 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002207 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2208 } else {
2209 dstRect.fTop = dstPoint.fY;
2210 }
2211 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2212
2213 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2214 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002215 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002216 using std::swap;
2217 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002218 }
2219
2220 VkImageBlit blitRegion;
2221 memset(&blitRegion, 0, sizeof(VkImageBlit));
2222 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2223 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002224 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002225 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2226 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002227 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002228
2229 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002230 *srcImage,
2231 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002232 1,
2233 &blitRegion,
2234 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002235
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002236 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002237 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002238}
2239
Brian Salomon1fabd512018-02-09 09:54:25 -05002240void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2241 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2242 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002243 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002244 SkIRect srcRect = origSrcRect;
2245 SkIPoint dstPoint = origDstPoint;
2246 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2247 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2248 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2249 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2250 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2251 }
2252 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002253 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2254 srcRect.width(), srcRect.height());
2255 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002256}
2257
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002258bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2259 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002260 const SkIRect& srcRect, const SkIPoint& dstPoint,
2261 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002262#ifdef SK_DEBUG
2263 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2264 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2265 }
2266 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2267 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2268 }
2269#endif
2270
Greg Daniel25af6712018-04-25 10:44:38 -04002271 GrPixelConfig dstConfig = dst->config();
2272 GrPixelConfig srcConfig = src->config();
2273
2274 int dstSampleCnt = get_surface_sample_cnt(dst);
2275 int srcSampleCnt = get_surface_sample_cnt(src);
2276
egdaniel17b89252016-04-05 07:23:38 -07002277 GrVkImage* dstImage;
2278 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002279 GrRenderTarget* dstRT = dst->asRenderTarget();
2280 if (dstRT) {
2281 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002282 if (vkRT->wrapsSecondaryCommandBuffer()) {
2283 return false;
2284 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002285 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2286 } else {
2287 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002288 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002289 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002290 GrRenderTarget* srcRT = src->asRenderTarget();
2291 if (srcRT) {
2292 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2293 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002294 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002295 SkASSERT(src->asTexture());
2296 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002297 }
2298
Greg Daniela51e93c2019-03-25 12:30:45 -04002299 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2300 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2301
2302 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2303 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2304 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2305 return true;
2306 }
2307
2308 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2309 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2310 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2311 dstPoint, canDiscardOutsideDstRect));
2312 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2313 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2314 return true;
2315 }
2316
2317 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2318 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002319 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2320 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002321 return true;
2322 }
2323
Greg Daniel25af6712018-04-25 10:44:38 -04002324 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002325 dstHasYcbcr, srcConfig, srcSampleCnt,
2326 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002327 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2328 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002329 return true;
2330 }
2331
Greg Daniel164a9f02016-02-22 09:56:40 -05002332 return false;
2333}
2334
Brian Salomona6948702018-06-01 15:33:20 -04002335bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2336 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002337 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002338 return false;
2339 }
2340
egdaniel66933552016-08-24 07:22:19 -07002341 GrVkImage* image = nullptr;
2342 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2343 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002344 // Reading from render targets that wrap a secondary command buffer is not allowed since
2345 // it would require us to know the VkImage, which we don't have, as well as need us to
2346 // stop and start the VkRenderPass which we don't have access to.
2347 if (rt->wrapsSecondaryCommandBuffer()) {
2348 return false;
2349 }
egdaniel66933552016-08-24 07:22:19 -07002350 // resolve the render target if necessary
2351 switch (rt->getResolveType()) {
2352 case GrVkRenderTarget::kCantResolve_ResolveType:
2353 return false;
2354 case GrVkRenderTarget::kAutoResolves_ResolveType:
2355 break;
2356 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002357 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002358 break;
2359 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002360 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002361 }
2362 image = rt;
2363 } else {
2364 image = static_cast<GrVkTexture*>(surface->asTexture());
2365 }
2366
2367 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002368 return false;
2369 }
2370
Greg Daniel475eb702018-09-28 14:16:50 -04002371 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2372 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2373 // image and then do the read pixels from that.
2374 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002375 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2376 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002377
2378 // Make a new surface that is RGBA to copy the RGB surface into.
2379 GrSurfaceDesc surfDesc;
2380 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2381 surfDesc.fWidth = width;
2382 surfDesc.fHeight = height;
2383 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2384 surfDesc.fSampleCnt = 1;
2385
2386 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2387 VK_IMAGE_USAGE_SAMPLED_BIT |
2388 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2389 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2390
2391 GrVkImage::ImageDesc imageDesc;
2392 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2393 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2394 imageDesc.fWidth = width;
2395 imageDesc.fHeight = height;
2396 imageDesc.fLevels = 1;
2397 imageDesc.fSamples = 1;
2398 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2399 imageDesc.fUsageFlags = usageFlags;
2400 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2401
2402 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2403 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2404 if (!copySurface) {
2405 return false;
2406 }
2407
2408 int srcSampleCount = 0;
2409 if (rt) {
2410 srcSampleCount = rt->numColorSamples();
2411 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002412 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel5c7b5412019-05-10 11:39:55 -04002413 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, false, false,
2414 surface->config(), srcSampleCount, image->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002415 srcHasYcbcr) &&
2416 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2417 surface->config(), SkToBool(surface->asTexture()),
2418 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002419 return false;
2420 }
2421 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
Greg Daniel5c7b5412019-05-10 11:39:55 -04002422 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniel475eb702018-09-28 14:16:50 -04002423 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2424 srcRect, SkIPoint::Make(0,0))) {
2425 return false;
2426 }
2427 top = 0;
2428 left = 0;
2429 dstColorType = GrColorType::kRGBA_8888;
2430 image = copySurface.get();
2431 }
2432
Greg Daniel164a9f02016-02-22 09:56:40 -05002433 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002434 image->setImageLayout(this,
2435 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2436 VK_ACCESS_TRANSFER_READ_BIT,
2437 VK_PIPELINE_STAGE_TRANSFER_BIT,
2438 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002439
Brian Salomonc320b152018-02-20 14:05:36 -05002440 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002441 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002442
Greg Daniel164a9f02016-02-22 09:56:40 -05002443 VkBufferImageCopy region;
2444 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002445
2446 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2447 if (copyFromOrigin) {
2448 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002449 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002450 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002451 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002452 region.imageOffset = offset;
2453 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2454 }
2455
2456 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002457 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002458 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002459 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002460 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002461 kStream_GrAccessPattern)
2462 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002463
2464 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002465 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002466 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002467 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2468 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002469
2470 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002471 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002472 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002473 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002474 1,
2475 &region);
2476
2477 // make sure the copy to buffer has finished
2478 transferBuffer->addMemoryBarrier(this,
2479 VK_ACCESS_TRANSFER_WRITE_BIT,
2480 VK_ACCESS_HOST_READ_BIT,
2481 VK_PIPELINE_STAGE_TRANSFER_BIT,
2482 VK_PIPELINE_STAGE_HOST_BIT,
2483 false);
2484
2485 // We need to submit the current command buffer to the Queue and make sure it finishes before
2486 // we can copy the data out of the buffer.
2487 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002488 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002489 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002490 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002491
egdaniel6fa0a912016-09-12 11:51:29 -07002492 if (copyFromOrigin) {
2493 uint32_t skipRows = region.imageExtent.height - height;
2494 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2495 }
2496
Brian Salomona6948702018-06-01 15:33:20 -04002497 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002498
2499 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002500 return true;
2501}
egdaniel066df7c2016-06-08 14:02:27 -07002502
egdaniel27bb2842016-07-07 11:58:35 -07002503// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2504// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2505// the the entire attachment. Similar requirements for the y and height components.
2506void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2507 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2508 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002509 if ((0 != granularity.width && 1 != granularity.width)) {
2510 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2511 int rightAdj = srcBounds.fRight % granularity.width;
2512 if (rightAdj != 0) {
2513 rightAdj = granularity.width - rightAdj;
2514 }
2515 dstBounds->fRight = srcBounds.fRight + rightAdj;
2516 if (dstBounds->fRight > maxWidth) {
2517 dstBounds->fRight = maxWidth;
2518 dstBounds->fLeft = 0;
2519 } else {
2520 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2521 }
egdaniel27bb2842016-07-07 11:58:35 -07002522 } else {
egdanield5797b32016-09-20 12:57:45 -07002523 dstBounds->fLeft = srcBounds.fLeft;
2524 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002525 }
2526
2527 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002528 if ((0 != granularity.height && 1 != granularity.height)) {
2529 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2530 int bottomAdj = srcBounds.fBottom % granularity.height;
2531 if (bottomAdj != 0) {
2532 bottomAdj = granularity.height - bottomAdj;
2533 }
2534 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2535 if (dstBounds->fBottom > maxHeight) {
2536 dstBounds->fBottom = maxHeight;
2537 dstBounds->fTop = 0;
2538 } else {
2539 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2540 }
egdaniel27bb2842016-07-07 11:58:35 -07002541 } else {
egdanield5797b32016-09-20 12:57:45 -07002542 dstBounds->fTop = srcBounds.fTop;
2543 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002544 }
2545}
2546
Greg Daniel22bc8652017-03-22 15:45:43 -04002547void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002548 const GrVkRenderPass* renderPass,
2549 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002550 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002551 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002552 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002553 const SkIRect* pBounds = &bounds;
2554 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002555 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002556 flippedBounds = bounds;
2557 flippedBounds.fTop = target->height() - bounds.fBottom;
2558 flippedBounds.fBottom = target->height() - bounds.fTop;
2559 pBounds = &flippedBounds;
2560 }
2561
egdaniel27bb2842016-07-07 11:58:35 -07002562 // The bounds we use for the render pass should be of the granularity supported
2563 // by the device.
2564 const VkExtent2D& granularity = renderPass->granularity();
2565 SkIRect adjustedBounds;
2566 if ((0 != granularity.width && 1 != granularity.width) ||
2567 (0 != granularity.height && 1 != granularity.height)) {
2568 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2569 target->width(), target->height());
2570 pBounds = &adjustedBounds;
2571 }
2572
Robert Phillips95214472017-08-08 18:00:03 -04002573#ifdef SK_DEBUG
2574 uint32_t index;
2575 bool result = renderPass->colorAttachmentIndex(&index);
2576 SkASSERT(result && 0 == index);
2577 result = renderPass->stencilAttachmentIndex(&index);
2578 if (result) {
2579 SkASSERT(1 == index);
2580 }
2581#endif
2582 VkClearValue clears[2];
2583 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002584 clears[1].depthStencil.depth = 0.0f;
2585 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002586
2587 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002588 for (int i = 0; i < buffers.count(); ++i) {
2589 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2590 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002591 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002592
Brian Salomon1fabd512018-02-09 09:54:25 -05002593 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002594}
egdaniel9cb63402016-06-23 08:37:05 -07002595
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002596void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2597 if (buffer->asRTCommandBuffer()) {
2598 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2599
2600 fCachedRTCommandBuffer->submit();
2601 fCachedRTCommandBuffer->reset();
2602 } else {
2603 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2604
2605 fCachedTexCommandBuffer->submit();
2606 fCachedTexCommandBuffer->reset();
2607 }
2608}
2609
Greg Daniel6be35232017-03-01 17:01:09 -05002610GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002611 VkFenceCreateInfo createInfo;
2612 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2613 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2614 createInfo.pNext = nullptr;
2615 createInfo.flags = 0;
2616 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002617
2618 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2619 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2620
2621 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002622 return (GrFence)fence;
2623}
2624
Greg Daniel6be35232017-03-01 17:01:09 -05002625bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2626 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2627
2628 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002629 return (VK_SUCCESS == result);
2630}
2631
2632void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002633 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2634}
2635
Greg Daniela5cb7812017-06-16 09:45:32 -04002636sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2637 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002638}
2639
Greg Daniel48661b82018-01-22 16:11:35 -05002640sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2641 GrResourceProvider::SemaphoreWrapType wrapType,
2642 GrWrapOwnership ownership) {
2643 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002644}
2645
Greg Daniel858e12c2018-12-06 11:11:37 -05002646void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002647 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2648
Greg Daniel48661b82018-01-22 16:11:35 -05002649 GrVkSemaphore::Resource* resource = vkSem->getResource();
2650 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002651 resource->ref();
2652 fSemaphoresToSignal.push_back(resource);
2653 }
Greg Daniel6be35232017-03-01 17:01:09 -05002654}
2655
Greg Daniel48661b82018-01-22 16:11:35 -05002656void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002657 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2658
Greg Daniel48661b82018-01-22 16:11:35 -05002659 GrVkSemaphore::Resource* resource = vkSem->getResource();
2660 if (resource->shouldWait()) {
2661 resource->ref();
2662 fSemaphoresToWaitOn.push_back(resource);
2663 }
jvanverth84741b32016-09-30 08:39:02 -07002664}
Brian Osman13dddce2017-05-09 13:19:50 -04002665
2666sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2667 SkASSERT(texture);
2668 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2669 vkTexture->setImageLayout(this,
2670 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2671 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002672 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002673 false);
2674 this->submitCommandBuffer(kSkip_SyncQueue);
2675
Greg Danielb3f66542019-05-10 17:11:19 -04002676 // The image layout change serves as a barrier, so no semaphore is needed.
2677 // If we ever decide we need to return a semaphore here, we need to make sure GrVkSemaphore is
2678 // thread safe so that only the first thread that tries to use the semaphore actually submits
2679 // it. This additionally would also require thread safety in command buffer submissions to
2680 // queues in general.
Brian Osman13dddce2017-05-09 13:19:50 -04002681 return nullptr;
2682}
Greg Danielf5d87582017-12-18 14:48:15 -05002683
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002684void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2685 fDrawables.emplace_back(std::move(drawable));
2686}
2687
Greg Daniel7a82edf2018-12-04 10:54:34 -05002688uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2689 const GrBackendFormat& format) {
2690 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2691 SkASSERT(ycbcrInfo);
2692 if (!ycbcrInfo->isValid()) {
2693 return 0;
2694 }
2695
2696 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2697 samplerState, *ycbcrInfo);
2698
2699 return sampler->uniqueID();
2700}
2701
Greg Daniela870b462019-01-08 15:49:46 -05002702void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002703 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002704 this->resourceProvider().storePipelineCacheData();
2705 }
2706}