blob: 2f7cbe5551a15f4bdbf931edf953d47bfbd66049 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrVkGpu.h"
9
Greg Daniela5cb7812017-06-16 09:45:32 -040010#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrContextOptions.h"
13#include "GrGeometryProcessor.h"
14#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070015#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrPipeline.h"
17#include "GrRenderTargetPriv.h"
18#include "GrSurfacePriv.h"
19#include "GrTexturePriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050020
21#include "GrVkCommandBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070022#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050023#include "GrVkImage.h"
24#include "GrVkIndexBuffer.h"
25#include "GrVkMemory.h"
26#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070027#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050028#include "GrVkRenderPass.h"
29#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050030#include "GrVkSemaphore.h"
Greg Danielc2dd5ed2017-05-05 13:49:11 -040031#include "GrVkTexelBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050032#include "GrVkTexture.h"
33#include "GrVkTextureRenderTarget.h"
34#include "GrVkTransferBuffer.h"
35#include "GrVkVertexBuffer.h"
36
Matt Sarett485c4992017-02-14 14:18:27 -050037#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070038#include "SkMipMap.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050039
40#include "vk/GrVkInterface.h"
jvanverthfd359ca2016-03-18 11:57:24 -070041#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050042
ethannicholasb3058bd2016-07-01 08:22:01 -070043#include "SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070044
Forrest Reiling44f85712017-03-27 23:22:20 -070045#if !defined(SK_BUILD_FOR_WIN)
46#include <unistd.h>
47#endif // !defined(SK_BUILD_FOR_WIN)
48
Greg Daniel164a9f02016-02-22 09:56:40 -050049#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
50#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
51#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
52
egdaniel735109c2016-07-27 08:03:57 -070053#ifdef SK_ENABLE_VK_LAYERS
jvanverthd2497f32016-03-18 12:39:05 -070054VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(
55 VkDebugReportFlagsEXT flags,
56 VkDebugReportObjectTypeEXT objectType,
57 uint64_t object,
58 size_t location,
59 int32_t messageCode,
60 const char* pLayerPrefix,
61 const char* pMessage,
62 void* pUserData) {
63 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
64 SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
Jim Van Verthce3fe232016-11-11 09:41:04 -080065 return VK_TRUE; // skip further layers
jvanverthd2497f32016-03-18 12:39:05 -070066 } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
67 SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
68 } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
69 SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
70 } else {
71 SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
72 }
73 return VK_FALSE;
74}
jvanverthd2497f32016-03-18 12:39:05 -070075#endif
76
jvanverth633b3562016-03-23 11:01:22 -070077GrGpu* GrVkGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
78 GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -040079 return Create(reinterpret_cast<const GrVkBackendContext*>(backendContext), options, context);
80}
81
82GrGpu* GrVkGpu::Create(const GrVkBackendContext* backendContext, const GrContextOptions& options,
83 GrContext* context) {
84 if (!backendContext) {
Brian Salomonc1889822017-04-24 16:49:05 -040085 return nullptr;
jvanverth633b3562016-03-23 11:01:22 -070086 } else {
Greg Daniel02611d92017-07-25 10:05:01 -040087 backendContext->ref();
Greg Daniel164a9f02016-02-22 09:56:40 -050088 }
89
Greg Daniel02611d92017-07-25 10:05:01 -040090 if (!backendContext->fInterface->validate(backendContext->fExtensions)) {
Greg Danielfe2965a2016-10-11 16:31:05 -040091 return nullptr;
92 }
93
Greg Daniel02611d92017-07-25 10:05:01 -040094 return new GrVkGpu(context, options, backendContext);
Greg Daniel164a9f02016-02-22 09:56:40 -050095}
96
97////////////////////////////////////////////////////////////////////////////////
98
halcanary9d524f22016-03-29 09:03:52 -070099GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
jvanverth633b3562016-03-23 11:01:22 -0700100 const GrVkBackendContext* backendCtx)
Greg Daniel164a9f02016-02-22 09:56:40 -0500101 : INHERITED(context)
jvanverth633b3562016-03-23 11:01:22 -0700102 , fDevice(backendCtx->fDevice)
103 , fQueue(backendCtx->fQueue)
Greg Daniel8606cf82017-05-08 16:17:53 -0400104 , fResourceProvider(this)
105 , fDisconnected(false) {
jvanverth633b3562016-03-23 11:01:22 -0700106 fBackendContext.reset(backendCtx);
Greg Daniel164a9f02016-02-22 09:56:40 -0500107
egdaniel735109c2016-07-27 08:03:57 -0700108#ifdef SK_ENABLE_VK_LAYERS
brianosman419ca642016-05-04 08:19:44 -0700109 fCallback = VK_NULL_HANDLE;
jvanverthfd7bd452016-03-25 06:29:52 -0700110 if (backendCtx->fExtensions & kEXT_debug_report_GrVkExtensionFlag) {
111 // Setup callback creation information
jvanverthd2497f32016-03-18 12:39:05 -0700112 VkDebugReportCallbackCreateInfoEXT callbackCreateInfo;
113 callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
114 callbackCreateInfo.pNext = nullptr;
115 callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
egdanielef0c10c2016-04-07 07:51:22 -0700116 VK_DEBUG_REPORT_WARNING_BIT_EXT |
jvanverthd2497f32016-03-18 12:39:05 -0700117 //VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
118 //VK_DEBUG_REPORT_DEBUG_BIT_EXT |
egdanielb4aa3622016-04-06 13:47:08 -0700119 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
jvanverthd2497f32016-03-18 12:39:05 -0700120 callbackCreateInfo.pfnCallback = &DebugReportCallback;
121 callbackCreateInfo.pUserData = nullptr;
122
jvanverthfd7bd452016-03-25 06:29:52 -0700123 // Register the callback
jvanvertha00980e2016-05-02 13:24:48 -0700124 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateDebugReportCallbackEXT(
125 backendCtx->fInstance, &callbackCreateInfo, nullptr, &fCallback));
jvanverthd2497f32016-03-18 12:39:05 -0700126 }
127#endif
jvanverth633b3562016-03-23 11:01:22 -0700128
ethannicholasb3058bd2016-07-01 08:22:01 -0700129 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700130
jvanverthfd7bd452016-03-25 06:29:52 -0700131 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice,
egdanielc5ec1402016-03-28 12:14:42 -0700132 backendCtx->fFeatures, backendCtx->fExtensions));
jvanverth633b3562016-03-23 11:01:22 -0700133 fCaps.reset(SkRef(fVkCaps.get()));
134
135 VK_CALL(GetPhysicalDeviceMemoryProperties(backendCtx->fPhysicalDevice, &fPhysDevMemProps));
136
137 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700138 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
139 nullptr, // pNext
140 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
141 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
142 backendCtx->fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700143 };
halcanary9d524f22016-03-29 09:03:52 -0700144 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700145 &fCmdPool));
146
147 // must call this after creating the CommandPool
148 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700149 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700150 SkASSERT(fCurrentCmdBuffer);
151 fCurrentCmdBuffer->begin(this);
jvanverth6b6ffc42016-06-13 14:28:07 -0700152
153 // set up our heaps
154 fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
Greg Daniel4f7d97c2017-04-26 15:10:00 -0400155 fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 64*1024*1024));
jvanverth6b6ffc42016-06-13 14:28:07 -0700156 fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024));
157 fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
158 fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
jvanverth4c6e47a2016-07-22 10:34:52 -0700159 fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024));
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400160 fHeaps[kTexelBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
jvanverth6b6ffc42016-06-13 14:28:07 -0700161 fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
162 fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
Greg Daniel164a9f02016-02-22 09:56:40 -0500163}
164
Greg Daniel8606cf82017-05-08 16:17:53 -0400165void GrVkGpu::destroyResources() {
166 if (fCurrentCmdBuffer) {
167 fCurrentCmdBuffer->end(this);
168 fCurrentCmdBuffer->unref(this);
169 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500170
171 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700172 fResourceProvider.checkCommandBuffers();
Jim Van Verth09557d72016-11-07 11:10:21 -0500173 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700174
175 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
176 // on the command buffers even though they have completed. This causes an assert to fire when
177 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500178 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700179#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500180 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700181#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500182 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700183#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500184 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700185#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500186 }
egdanielf8c2be32016-06-24 13:18:27 -0700187#endif
188
egdanielbe9d8212016-09-20 08:54:23 -0700189#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400190 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700191#endif
halcanary9d524f22016-03-29 09:03:52 -0700192
Greg Daniel6be35232017-03-01 17:01:09 -0500193 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
194 fSemaphoresToWaitOn[i]->unref(this);
195 }
196 fSemaphoresToWaitOn.reset();
197
Greg Daniela5cb7812017-06-16 09:45:32 -0400198 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
199 fSemaphoresToSignal[i]->unref(this);
200 }
201 fSemaphoresToSignal.reset();
202
203
egdanielbc9b2962016-09-27 08:00:53 -0700204 fCopyManager.destroyResources(this);
205
Jim Van Verth09557d72016-11-07 11:10:21 -0500206 // must call this just before we destroy the command pool and VkDevice
207 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
Greg Daniel8606cf82017-05-08 16:17:53 -0400209 if (fCmdPool != VK_NULL_HANDLE) {
210 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
211 }
jvanverth633b3562016-03-23 11:01:22 -0700212
egdaniel735109c2016-07-27 08:03:57 -0700213#ifdef SK_ENABLE_VK_LAYERS
jvanvertha00980e2016-05-02 13:24:48 -0700214 if (fCallback) {
215 VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr));
jvanvertha00980e2016-05-02 13:24:48 -0700216 }
jvanverthd2497f32016-03-18 12:39:05 -0700217#endif
Greg Daniel8606cf82017-05-08 16:17:53 -0400218
219}
220
221GrVkGpu::~GrVkGpu() {
222 if (!fDisconnected) {
223 this->destroyResources();
224 }
225 delete fCompiler;
226}
227
228
229void GrVkGpu::disconnect(DisconnectType type) {
230 INHERITED::disconnect(type);
231 if (!fDisconnected) {
232 if (DisconnectType::kCleanup == type) {
233 this->destroyResources();
234 } else {
235 fCurrentCmdBuffer->unrefAndAbandon();
236 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
237 fSemaphoresToWaitOn[i]->unrefAndAbandon();
238 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400239 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
240 fSemaphoresToSignal[i]->unrefAndAbandon();
241 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400242 fCopyManager.abandonResources();
243
244 // must call this just before we destroy the command pool and VkDevice
245 fResourceProvider.abandonResources();
246 }
247 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400248 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400249#ifdef SK_ENABLE_VK_LAYERS
250 fCallback = VK_NULL_HANDLE;
251#endif
252 fCurrentCmdBuffer = nullptr;
253 fCmdPool = VK_NULL_HANDLE;
254 fDisconnected = true;
255 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500256}
257
258///////////////////////////////////////////////////////////////////////////////
259
egdaniel9cb63402016-06-23 08:37:05 -0700260GrGpuCommandBuffer* GrVkGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -0700261 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
262 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -0500263 return new GrVkGpuCommandBuffer(this, colorInfo, stencilInfo);
egdaniel066df7c2016-06-08 14:02:27 -0700264}
265
Greg Daniela5cb7812017-06-16 09:45:32 -0400266void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500267 SkASSERT(fCurrentCmdBuffer);
268 fCurrentCmdBuffer->end(this);
269
Greg Daniela5cb7812017-06-16 09:45:32 -0400270 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500271
272 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
273 fSemaphoresToWaitOn[i]->unref(this);
274 }
275 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400276 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
277 fSemaphoresToSignal[i]->unref(this);
278 }
279 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500280
Greg Daniel164a9f02016-02-22 09:56:40 -0500281 fResourceProvider.checkCommandBuffers();
282
283 // Release old command buffer and create a new one
284 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700285 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500286 SkASSERT(fCurrentCmdBuffer);
287
288 fCurrentCmdBuffer->begin(this);
289}
290
291///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700292GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
293 const void* data) {
294 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700295 switch (type) {
296 case kVertex_GrBufferType:
297 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
298 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700299 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700300 break;
cdalton397536c2016-03-25 12:15:03 -0700301 case kIndex_GrBufferType:
302 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
303 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700304 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700305 break;
cdalton397536c2016-03-25 12:15:03 -0700306 case kXferCpuToGpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400307 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
308 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700309 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700310 break;
cdalton397536c2016-03-25 12:15:03 -0700311 case kXferGpuToCpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400312 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
313 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700314 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700315 break;
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400316 case kTexel_GrBufferType:
Chris Dalton6b65b982017-07-06 11:04:00 -0600317 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
318 kStatic_GrAccessPattern == accessPattern);
319 buff = GrVkTexelBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400320 break;
321 case kDrawIndirect_GrBufferType:
322 SkFAIL("DrawIndirect Buffers not supported in vulkan backend.");
323 return nullptr;
cdalton397536c2016-03-25 12:15:03 -0700324 default:
325 SkFAIL("Unknown buffer type.");
326 return nullptr;
327 }
cdalton1bf3e712016-04-19 10:00:02 -0700328 if (data && buff) {
329 buff->updateData(data, size);
330 }
331 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500332}
333
334////////////////////////////////////////////////////////////////////////////////
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400335bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin,
336 int width, int height,
Greg Daniel164a9f02016-02-22 09:56:40 -0500337 GrPixelConfig srcConfig, DrawPreference* drawPreference,
338 WritePixelTempDrawInfo* tempDrawInfo) {
egdaniel4583ec52016-06-27 12:57:00 -0700339 GrRenderTarget* renderTarget = dstSurface->asRenderTarget();
340
341 // Start off assuming no swizzling
342 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
343 tempDrawInfo->fWriteConfig = srcConfig;
344
345 // These settings we will always want if a temp draw is performed. Initially set the config
346 // to srcConfig, though that may be modified if we decide to do a R/B swap
347 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
348 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
349 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
350 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
351 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
352 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
353
egdanield66110f2016-06-28 13:38:26 -0700354 if (dstSurface->config() == srcConfig) {
Brian Osman33910292017-04-18 14:38:53 -0400355 // We only support writing pixels to textures. Forcing a draw lets us write to pure RTs.
356 if (!dstSurface->asTexture()) {
357 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
358 }
359 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
360 if (renderTarget && renderTarget->numColorSamples() > 1) {
361 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
egdaniel4583ec52016-06-27 12:57:00 -0700362 }
363 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -0500364 }
365
Brian Osman33910292017-04-18 14:38:53 -0400366 // Any config change requires a draw
367 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
368
369 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
370
371 if (!this->vkCaps().isConfigTexturable(srcConfig) && configsAreRBSwaps) {
372 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
373 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
374 tempDrawInfo->fWriteConfig = dstSurface->config();
375 }
376 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -0500377}
378
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400379bool GrVkGpu::onWritePixels(GrSurface* surface, GrSurfaceOrigin origin,
Greg Daniel164a9f02016-02-22 09:56:40 -0500380 int left, int top, int width, int height,
bsalomona1e6b3b2016-03-02 10:58:23 -0800381 GrPixelConfig config,
Robert Phillips590533f2017-07-11 14:22:35 -0400382 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500383 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
384 if (!vkTex) {
385 return false;
386 }
387
jvanverth900bd4a2016-04-29 13:53:12 -0700388 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400389 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800390 return false;
391 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800392
Greg Daniel164a9f02016-02-22 09:56:40 -0500393 // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels.
394 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
395 return false;
396 }
397
398 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400399 bool linearTiling = vkTex->isLinearTiled();
400 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400401 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400402 SkDebugf("Can't upload mipmap data to linear tiled texture");
403 return false;
404 }
405 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
406 // Need to change the layout to general in order to perform a host write
407 vkTex->setImageLayout(this,
408 VK_IMAGE_LAYOUT_GENERAL,
409 VK_ACCESS_HOST_WRITE_BIT,
410 VK_PIPELINE_STAGE_HOST_BIT,
411 false);
412 this->submitCommandBuffer(kForce_SyncQueue);
413 }
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400414 success = this->uploadTexDataLinear(vkTex, origin, left, top, width, height, config,
Robert Phillips590533f2017-07-11 14:22:35 -0400415 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500416 } else {
Robert Phillips92de6312017-05-23 07:43:48 -0400417 int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1;
Robert Phillips590533f2017-07-11 14:22:35 -0400418 if (mipLevelCount > currentMipLevels) {
419 if (!vkTex->reallocForMipmap(this, mipLevelCount)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700420 return false;
421 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500422 }
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400423 success = this->uploadTexDataOptimal(vkTex, origin, left, top, width, height, config,
Robert Phillips590533f2017-07-11 14:22:35 -0400424 texels, mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500425 }
egdaniel4583ec52016-06-27 12:57:00 -0700426
jvanverth900bd4a2016-04-29 13:53:12 -0700427 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500428}
429
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400430bool GrVkGpu::onTransferPixels(GrTexture* texture,
431 int left, int top, int width, int height,
432 GrPixelConfig config, GrBuffer* transferBuffer,
433 size_t bufferOffset, size_t rowBytes) {
434 // Vulkan only supports 4-byte aligned offsets
435 if (SkToBool(bufferOffset & 0x2)) {
436 return false;
437 }
438 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
439 if (!vkTex) {
440 return false;
441 }
442 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
443 if (!vkBuffer) {
444 return false;
445 }
446
447 // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels.
448 if (GrPixelConfigIsSRGB(texture->config()) != GrPixelConfigIsSRGB(config)) {
449 return false;
450 }
451
Greg Daniel660cc992017-06-26 14:55:05 -0400452 SkDEBUGCODE(
453 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
454 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
455 SkASSERT(bounds.contains(subRect));
456 )
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400457 size_t bpp = GrBytesPerPixel(config);
458 if (rowBytes == 0) {
459 rowBytes = bpp*width;
460 }
461
462 // Set up copy region
463 VkBufferImageCopy region;
464 memset(&region, 0, sizeof(VkBufferImageCopy));
465 region.bufferOffset = bufferOffset;
466 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
467 region.bufferImageHeight = 0;
468 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
469 region.imageOffset = { left, top, 0 };
470 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
471
472 // Change layout of our target so it can be copied to
473 vkTex->setImageLayout(this,
474 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
475 VK_ACCESS_TRANSFER_WRITE_BIT,
476 VK_PIPELINE_STAGE_TRANSFER_BIT,
477 false);
478
479 // Copy the buffer to the image
480 fCurrentCmdBuffer->copyBufferToImage(this,
481 vkBuffer,
482 vkTex,
483 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
484 1,
485 &region);
486
487 vkTex->texturePriv().dirtyMipMaps(true);
488 return true;
489}
490
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400491void GrVkGpu::resolveImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
492 GrVkRenderTarget* src, GrSurfaceOrigin srcOrigin,
493 const SkIRect& srcRect, const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700494 SkASSERT(dst);
495 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
496
egdanielfd016d72016-09-27 12:13:05 -0700497 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
498 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
499 }
500
egdaniel4bcd62e2016-08-31 07:37:31 -0700501 // Flip rect if necessary
502 SkIRect srcVkRect = srcRect;
503 int32_t dstY = dstPoint.fY;
504
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400505 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
506 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
egdaniel4bcd62e2016-08-31 07:37:31 -0700507 srcVkRect.fTop = src->height() - srcRect.fBottom;
508 srcVkRect.fBottom = src->height() - srcRect.fTop;
509 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
510 }
511
512 VkImageResolve resolveInfo;
513 resolveInfo.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
514 resolveInfo.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
515 resolveInfo.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
516 resolveInfo.dstOffset = { dstPoint.fX, dstY, 0 };
egdaniel4bcd62e2016-08-31 07:37:31 -0700517 resolveInfo.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
518
Greg Danielbc26c392017-04-18 13:32:10 -0400519 GrVkImage* dstImage;
520 GrRenderTarget* dstRT = dst->asRenderTarget();
521 if (dstRT) {
522 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400523 dstImage = vkRT;
524 } else {
525 SkASSERT(dst->asTexture());
526 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
527 }
528 dstImage->setImageLayout(this,
529 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
530 VK_ACCESS_TRANSFER_WRITE_BIT,
531 VK_PIPELINE_STAGE_TRANSFER_BIT,
532 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700533
534 src->msaaImage()->setImageLayout(this,
535 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
536 VK_ACCESS_TRANSFER_READ_BIT,
537 VK_PIPELINE_STAGE_TRANSFER_BIT,
538 false);
539
Greg Danielbc26c392017-04-18 13:32:10 -0400540 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700541}
542
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400543void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, GrSurfaceOrigin origin,
544 bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700545 if (target->needsResolve()) {
546 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700547 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
548 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500549
egdaniel4bcd62e2016-08-31 07:37:31 -0700550 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700551
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400552 this->resolveImage(target, origin, rt, origin, srcRect,
553 SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700554
555 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500556
557 if (requiresSubmit) {
558 this->submitCommandBuffer(kSkip_SyncQueue);
559 }
egdaniel52ad2512016-08-04 12:50:01 -0700560 }
561}
562
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400563bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, GrSurfaceOrigin texOrigin,
jvanverth900bd4a2016-04-29 13:53:12 -0700564 int left, int top, int width, int height,
565 GrPixelConfig dataConfig,
566 const void* data,
567 size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500568 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700569 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500570
Greg Daniel660cc992017-06-26 14:55:05 -0400571 SkDEBUGCODE(
572 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
573 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
574 SkASSERT(bounds.contains(subRect));
575 )
Greg Daniel164a9f02016-02-22 09:56:40 -0500576 size_t bpp = GrBytesPerPixel(dataConfig);
Greg Daniel164a9f02016-02-22 09:56:40 -0500577 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400578 if (!rowBytes) {
579 rowBytes = trimRowBytes;
580 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500581
jvanverth900bd4a2016-04-29 13:53:12 -0700582 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
583 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
584 const VkImageSubresource subres = {
585 VK_IMAGE_ASPECT_COLOR_BIT,
586 0, // mipLevel
587 0, // arraySlice
588 };
589 VkSubresourceLayout layout;
590 VkResult err;
Greg Daniel164a9f02016-02-22 09:56:40 -0500591
jvanverth900bd4a2016-04-29 13:53:12 -0700592 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500593
jvanverth900bd4a2016-04-29 13:53:12 -0700594 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700595 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700596 &subres,
597 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500598
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400599 int texTop = kBottomLeft_GrSurfaceOrigin == texOrigin ? tex->height() - top - height : top;
jvanverth1e305ba2016-06-01 09:39:15 -0700600 const GrVkAlloc& alloc = tex->alloc();
601 VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700602 VkDeviceSize size = height*layout.rowPitch;
603 void* mapPtr;
jvanverth1e305ba2016-06-01 09:39:15 -0700604 err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr));
jvanverth900bd4a2016-04-29 13:53:12 -0700605 if (err) {
606 return false;
607 }
608
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400609 if (kBottomLeft_GrSurfaceOrigin == texOrigin) {
jvanverth900bd4a2016-04-29 13:53:12 -0700610 // copy into buffer by rows
611 const char* srcRow = reinterpret_cast<const char*>(data);
612 char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch;
613 for (int y = 0; y < height; y++) {
614 memcpy(dstRow, srcRow, trimRowBytes);
615 srcRow += rowBytes;
616 dstRow -= layout.rowPitch;
617 }
618 } else {
Matt Sarettcf3525a2017-01-24 12:43:41 -0500619 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
620 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700621 }
622
jvanverth9d54afc2016-09-20 09:20:03 -0700623 GrVkMemory::FlushMappedAlloc(this, alloc);
jvanverth1e305ba2016-06-01 09:39:15 -0700624 GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory));
jvanverth900bd4a2016-04-29 13:53:12 -0700625
626 return true;
627}
628
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400629bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, GrSurfaceOrigin texOrigin,
jvanvertha584de92016-06-30 09:10:52 -0700630 int left, int top, int width, int height,
631 GrPixelConfig dataConfig,
Robert Phillips590533f2017-07-11 14:22:35 -0400632 const GrMipLevel texels[], int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700633 SkASSERT(!tex->isLinearTiled());
634 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400635 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700636 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
637
Greg Danieldd20e912017-04-07 14:42:23 -0400638 // We assume that if the texture has mip levels, we either upload to all the levels or just the
639 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400640 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400641
jvanverth900bd4a2016-04-29 13:53:12 -0700642 if (width == 0 || height == 0) {
643 return false;
644 }
645
Brian Salomond1eaf492017-05-18 10:02:08 -0400646 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
jvanverth900bd4a2016-04-29 13:53:12 -0700647 size_t bpp = GrBytesPerPixel(dataConfig);
648
649 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700650 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
651 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400652 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
653
654 if (mipLevelCount) {
655 texelsShallowCopy.reset(mipLevelCount);
656 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
657 }
jvanverth900bd4a2016-04-29 13:53:12 -0700658
Robert Phillips590533f2017-07-11 14:22:35 -0400659 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; ++currentMipLevel) {
jvanverthc578b0632016-05-02 10:58:12 -0700660 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels);
Greg Daniel164a9f02016-02-22 09:56:40 -0500661 }
662
jvanverth900bd4a2016-04-29 13:53:12 -0700663 // Determine whether we need to flip when we copy into the buffer
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400664 bool flipY = (kBottomLeft_GrSurfaceOrigin == texOrigin && mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700665
Robert Phillips590533f2017-07-11 14:22:35 -0400666 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700667 individualMipOffsets.push_back(0);
668 size_t combinedBufferSize = width * bpp * height;
669 int currentWidth = width;
670 int currentHeight = height;
Greg Daniel468fd632017-03-22 17:03:45 -0400671 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
672 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
673 SkASSERT((bpp & (bpp - 1)) == 0);
674 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400675 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700676 currentWidth = SkTMax(1, currentWidth/2);
677 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400678
jvanverth900bd4a2016-04-29 13:53:12 -0700679 const size_t trimmedSize = currentWidth * bpp * currentHeight;
Greg Daniel468fd632017-03-22 17:03:45 -0400680 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
681 if (alignmentDiff != 0) {
682 combinedBufferSize += alignmentMask - alignmentDiff + 1;
683 }
jvanverth900bd4a2016-04-29 13:53:12 -0700684 individualMipOffsets.push_back(combinedBufferSize);
685 combinedBufferSize += trimmedSize;
686 }
687
688 // allocate buffer to hold our mip data
689 GrVkTransferBuffer* transferBuffer =
690 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Forrest Reilingc04f8452017-04-26 19:26:12 -0700691 if(!transferBuffer)
692 return false;
jvanverth900bd4a2016-04-29 13:53:12 -0700693
694 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400695 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700696
jvanverthc578b0632016-05-02 10:58:12 -0700697 currentWidth = width;
698 currentHeight = height;
Greg Daniela1b282b2017-03-28 14:56:46 -0400699 int layerHeight = tex->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400700 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
701 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
jvanverth900bd4a2016-04-29 13:53:12 -0700702 const size_t trimRowBytes = currentWidth * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400703 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes ?
704 texelsShallowCopy[currentMipLevel].fRowBytes :
705 trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700706
707 // copy data into the buffer, skipping the trailing bytes
708 char* dst = buffer + individualMipOffsets[currentMipLevel];
jvanverthc578b0632016-05-02 10:58:12 -0700709 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
jvanverth900bd4a2016-04-29 13:53:12 -0700710 if (flipY) {
711 src += (currentHeight - 1) * rowBytes;
712 for (int y = 0; y < currentHeight; y++) {
713 memcpy(dst, src, trimRowBytes);
714 src -= rowBytes;
715 dst += trimRowBytes;
716 }
jvanverth900bd4a2016-04-29 13:53:12 -0700717 } else {
718 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
719 }
720
721 VkBufferImageCopy& region = regions.push_back();
722 memset(&region, 0, sizeof(VkBufferImageCopy));
jvanverthdb379092016-07-07 11:18:46 -0700723 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
jvanverth900bd4a2016-04-29 13:53:12 -0700724 region.bufferRowLength = currentWidth;
725 region.bufferImageHeight = currentHeight;
bsalomoncf942c42016-04-29 18:30:06 -0700726 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniela1b282b2017-03-28 14:56:46 -0400727 region.imageOffset = { left, flipY ? layerHeight - top - currentHeight : top, 0 };
jvanverth900bd4a2016-04-29 13:53:12 -0700728 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
egdaniel4583ec52016-06-27 12:57:00 -0700729
jvanverthc578b0632016-05-02 10:58:12 -0700730 currentWidth = SkTMax(1, currentWidth/2);
731 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400732 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700733 }
734
jvanverth9d54afc2016-09-20 09:20:03 -0700735 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700736 transferBuffer->unmap();
737
jvanverth900bd4a2016-04-29 13:53:12 -0700738 // Change layout of our target so it can be copied to
jvanverth900bd4a2016-04-29 13:53:12 -0700739 tex->setImageLayout(this,
740 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -0700741 VK_ACCESS_TRANSFER_WRITE_BIT,
742 VK_PIPELINE_STAGE_TRANSFER_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700743 false);
744
745 // Copy the buffer to the image
746 fCurrentCmdBuffer->copyBufferToImage(this,
747 transferBuffer,
748 tex,
749 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
750 regions.count(),
751 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700752 transferBuffer->unref();
Robert Phillips590533f2017-07-11 14:22:35 -0400753 if (1 == mipLevelCount) {
Greg Danieldd20e912017-04-07 14:42:23 -0400754 tex->texturePriv().dirtyMipMaps(true);
755 }
jvanverth900bd4a2016-04-29 13:53:12 -0700756
Greg Daniel164a9f02016-02-22 09:56:40 -0500757 return true;
758}
759
760////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400761sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -0400762 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500763 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
764
765 VkFormat pixelFormat;
766 if (!GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)) {
767 return nullptr;
768 }
769
770 if (!fVkCaps->isConfigTexturable(desc.fConfig)) {
771 return nullptr;
772 }
773
egdaniel0a3a7f72016-06-24 09:22:31 -0700774 if (renderTarget && !fVkCaps->isConfigRenderable(desc.fConfig, false)) {
775 return nullptr;
776 }
777
Greg Daniel164a9f02016-02-22 09:56:40 -0500778 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
779 if (renderTarget) {
780 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
781 }
782
783 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
784 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
785 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700786 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500787 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
788 // texture.
789 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
790
Greg Daniel164a9f02016-02-22 09:56:40 -0500791 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700792 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500793 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400794 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500795 GrVkImage::ImageDesc imageDesc;
796 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
797 imageDesc.fFormat = pixelFormat;
798 imageDesc.fWidth = desc.fWidth;
799 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400800 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500801 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400802 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500803 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400804 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500805
Robert Phillips67d52cf2017-06-05 13:38:13 -0400806 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500807 if (renderTarget) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700808 tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
Greg Daniel164a9f02016-02-22 09:56:40 -0500809 imageDesc);
810 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -0700811 tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc);
Greg Daniel164a9f02016-02-22 09:56:40 -0500812 }
813
814 if (!tex) {
815 return nullptr;
816 }
817
Robert Phillips590533f2017-07-11 14:22:35 -0400818 if (mipLevelCount) {
819 SkASSERT(texels[0].fPixels);
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400820 if (!this->uploadTexDataOptimal(tex.get(), desc.fOrigin, 0, 0, desc.fWidth, desc.fHeight,
821 desc.fConfig, texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500822 tex->unref();
823 return nullptr;
824 }
825 }
826
Brian Salomond17b4a62017-05-23 16:53:47 -0400827 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
828 VkClearColorValue zeroClearColor;
829 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
830 VkImageSubresourceRange range;
831 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
832 range.baseArrayLayer = 0;
833 range.baseMipLevel = 0;
834 range.layerCount = 1;
835 range.levelCount = 1;
836 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
837 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400838 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400839 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500840 return tex;
841}
842
843////////////////////////////////////////////////////////////////////////////////
844
jvanverthdb379092016-07-07 11:18:46 -0700845bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
846 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700847
848 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700849 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700850
851 return true;
852}
853
854////////////////////////////////////////////////////////////////////////////////
855
Brian Salomond17f6582017-07-19 18:28:58 -0400856static bool check_backend_texture(const GrBackendTexture& backendTex) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000857 const GrVkImageInfo* info = backendTex.getVkImageInfo();
858 if (!info) {
Brian Salomond17f6582017-07-19 18:28:58 -0400859 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500860 }
861
jvanverth1e305ba2016-06-01 09:39:15 -0700862 if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -0400863 return false;
jvanverthfd359ca2016-03-18 11:57:24 -0700864 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000865
866 SkASSERT(backendTex.config() == GrVkFormatToPixelConfig(info->fFormat));
Brian Salomond17f6582017-07-19 18:28:58 -0400867 return true;
868}
869
870sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400871 GrWrapOwnership ownership) {
872 if (!check_backend_texture(backendTex)) {
873 return nullptr;
874 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500875
Greg Daniel164a9f02016-02-22 09:56:40 -0500876 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400877 surfDesc.fFlags = kNone_GrSurfaceFlags;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400878 surfDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // Not actually used in the following
Greg Daniel7ef28f32017-04-20 16:41:55 +0000879 surfDesc.fWidth = backendTex.width();
880 surfDesc.fHeight = backendTex.height();
881 surfDesc.fConfig = backendTex.config();
Brian Salomond17f6582017-07-19 18:28:58 -0400882 surfDesc.fSampleCnt = 0;
Greg Daniel164a9f02016-02-22 09:56:40 -0500883
Brian Salomond17f6582017-07-19 18:28:58 -0400884 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, backendTex.getVkImageInfo());
885}
886
887sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400888 int sampleCnt,
889 GrWrapOwnership ownership) {
890 if (!check_backend_texture(backendTex)) {
891 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500892 }
Brian Salomond17f6582017-07-19 18:28:58 -0400893
894 GrSurfaceDesc surfDesc;
895 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400896 surfDesc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following
Brian Salomond17f6582017-07-19 18:28:58 -0400897 surfDesc.fWidth = backendTex.width();
898 surfDesc.fHeight = backendTex.height();
899 surfDesc.fConfig = backendTex.config();
900 surfDesc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -0400901
902 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
903 backendTex.getVkImageInfo());
Greg Daniel164a9f02016-02-22 09:56:40 -0500904}
905
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400906sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -0400907 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
908 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
909 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
910 // creating and owning the MSAA images.
Greg Danielbcf612b2017-05-01 13:50:58 +0000911 if (backendRT.sampleCnt()) {
Greg Daniele79b4732017-04-20 14:07:46 -0400912 return nullptr;
913 }
halcanary9d524f22016-03-29 09:03:52 -0700914
Greg Danielbcf612b2017-05-01 13:50:58 +0000915 const GrVkImageInfo* info = backendRT.getVkImageInfo();
916 if (!info) {
917 return nullptr;
918 }
Brian Osman0b791f52017-03-10 08:30:22 -0500919 if (VK_NULL_HANDLE == info->fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700920 return nullptr;
921 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500922
Greg Daniel164a9f02016-02-22 09:56:40 -0500923 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400924 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400925 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following
Robert Phillips16d8ec62017-07-27 16:16:25 -0400926 desc.fWidth = backendRT.width();
927 desc.fHeight = backendRT.height();
928 desc.fConfig = backendRT.config();
929 desc.fSampleCnt = 0;
Greg Daniel164a9f02016-02-22 09:56:40 -0500930
Brian Osman0b791f52017-03-10 08:30:22 -0500931 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info);
Greg Danielbcf612b2017-05-01 13:50:58 +0000932 if (tgt && backendRT.stencilBits()) {
bungeman6bd52842016-10-27 09:30:08 -0700933 if (!createStencilAttachmentForRenderTarget(tgt.get(), desc.fWidth, desc.fHeight)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500934 return nullptr;
935 }
936 }
937 return tgt;
938}
939
Greg Daniel7ef28f32017-04-20 16:41:55 +0000940sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000941 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400942
Greg Daniel7ef28f32017-04-20 16:41:55 +0000943 const GrVkImageInfo* info = tex.getVkImageInfo();
Greg Danielbcf612b2017-05-01 13:50:58 +0000944 if (!info) {
945 return nullptr;
946 }
Brian Osman33910292017-04-18 14:38:53 -0400947 if (VK_NULL_HANDLE == info->fImage) {
948 return nullptr;
949 }
950
951 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000952 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400953 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; // Not actually used in the following
Greg Daniel7ef28f32017-04-20 16:41:55 +0000954 desc.fWidth = tex.width();
955 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400956 desc.fConfig = tex.config();
Greg Daniel81e7bf82017-07-19 14:47:42 -0400957 desc.fSampleCnt = this->caps()->getSampleCount(sampleCnt, tex.config());
Brian Osman33910292017-04-18 14:38:53 -0400958
Brian Osman33910292017-04-18 14:38:53 -0400959 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info);
960 return tgt;
961}
962
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400963void GrVkGpu::generateMipmap(GrVkTexture* tex, GrSurfaceOrigin texOrigin) {
jvanverth900bd4a2016-04-29 13:53:12 -0700964 // don't do anything for linearly tiled textures (can't have mipmaps)
jvanverth62340062016-04-26 08:01:44 -0700965 if (tex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700966 SkDebugf("Trying to create mipmap for linear tiled texture");
jvanverth62340062016-04-26 08:01:44 -0700967 return;
968 }
969
jvanverth62340062016-04-26 08:01:44 -0700970 // determine if we can blit to and from this format
971 const GrVkCaps& caps = this->vkCaps();
972 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -0700973 !caps.configCanBeSrcofBlit(tex->config(), false) ||
974 !caps.mipMapSupport()) {
jvanverth62340062016-04-26 08:01:44 -0700975 return;
976 }
977
egdanielfd016d72016-09-27 12:13:05 -0700978 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
979 this->submitCommandBuffer(kSkip_SyncQueue);
980 }
981
egdaniel66933552016-08-24 07:22:19 -0700982 // We may need to resolve the texture first if it is also a render target
983 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(tex->asRenderTarget());
984 if (texRT) {
Robert Phillipsdf0e09f2017-07-28 11:56:47 -0400985 this->internalResolveRenderTarget(texRT, texOrigin, false);
egdaniel66933552016-08-24 07:22:19 -0700986 }
987
egdaniel7ac5da82016-07-15 13:41:42 -0700988 int width = tex->width();
989 int height = tex->height();
990 VkImageBlit blitRegion;
991 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -0700992
jvanverth82c05582016-05-03 11:19:01 -0700993 // SkMipMap doesn't include the base level in the level count so we have to add 1
994 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
egdaniel7ac5da82016-07-15 13:41:42 -0700995 if (levelCount != tex->mipLevels()) {
996 const GrVkResource* oldResource = tex->resource();
997 oldResource->ref();
998 // grab handle to the original image resource
999 VkImage oldImage = tex->image();
1000
1001 // change the original image's layout so we can copy from it
1002 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1003 VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
1004
1005 if (!tex->reallocForMipmap(this, levelCount)) {
1006 oldResource->unref(this);
1007 return;
1008 }
1009 // change the new image's layout so we can blit to it
1010 tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
1011 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
1012
1013 // Blit original image to top level of new image
1014 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1015 blitRegion.srcOffsets[0] = { 0, 0, 0 };
1016 blitRegion.srcOffsets[1] = { width, height, 1 };
1017 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1018 blitRegion.dstOffsets[0] = { 0, 0, 0 };
1019 blitRegion.dstOffsets[1] = { width, height, 1 };
1020
1021 fCurrentCmdBuffer->blitImage(this,
1022 oldResource,
1023 oldImage,
1024 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1025 tex->resource(),
1026 tex->image(),
1027 VK_IMAGE_LAYOUT_GENERAL,
1028 1,
1029 &blitRegion,
1030 VK_FILTER_LINEAR);
1031
jvanverth62340062016-04-26 08:01:44 -07001032 oldResource->unref(this);
egdaniel7ac5da82016-07-15 13:41:42 -07001033 } else {
1034 // change layout of the layers so we can write to them.
1035 tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
1036 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001037 }
1038
jvanverth50c46c72016-05-06 12:31:28 -07001039 // setup memory barrier
Greg Daniel94403452017-04-18 15:52:36 -04001040 SkASSERT(kUnknown_GrPixelConfig != GrVkFormatToPixelConfig(tex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001041 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1042 VkImageMemoryBarrier imageMemoryBarrier = {
1043 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1044 NULL, // pNext
egdaniel7ac5da82016-07-15 13:41:42 -07001045 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1046 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
jvanverth50c46c72016-05-06 12:31:28 -07001047 VK_IMAGE_LAYOUT_GENERAL, // oldLayout
1048 VK_IMAGE_LAYOUT_GENERAL, // newLayout
1049 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1050 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
egdanielb2df0c22016-05-13 11:30:37 -07001051 tex->image(), // image
jvanverth50c46c72016-05-06 12:31:28 -07001052 { aspectFlags, 0, 1, 0, 1 } // subresourceRange
1053 };
1054
jvanverth62340062016-04-26 08:01:44 -07001055 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001056 uint32_t mipLevel = 1;
1057 while (mipLevel < levelCount) {
1058 int prevWidth = width;
1059 int prevHeight = height;
1060 width = SkTMax(1, width / 2);
1061 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001062
jvanverth50c46c72016-05-06 12:31:28 -07001063 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1064 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1065 false, &imageMemoryBarrier);
1066
1067 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001068 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001069 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001070 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1071 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001072 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001073 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001074 *tex,
1075 *tex,
jvanverth62340062016-04-26 08:01:44 -07001076 1,
1077 &blitRegion,
1078 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001079 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001080 }
jvanverth62340062016-04-26 08:01:44 -07001081}
1082
Greg Daniel164a9f02016-02-22 09:56:40 -05001083////////////////////////////////////////////////////////////////////////////////
1084
1085GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1086 int width,
1087 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001088 SkASSERT(width >= rt->width());
1089 SkASSERT(height >= rt->height());
1090
1091 int samples = rt->numStencilSamples();
1092
egdaniel8f1dcaa2016-04-01 10:10:45 -07001093 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001094
1095 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001096 width,
1097 height,
1098 samples,
1099 sFmt));
1100 fStats.incStencilAttachmentCreates();
1101 return stencil;
1102}
1103
1104////////////////////////////////////////////////////////////////////////////////
1105
jvanverth9d54afc2016-09-20 09:20:03 -07001106bool copy_testing_data(GrVkGpu* gpu, void* srcData, const GrVkAlloc& alloc,
egdaniel3602d4f2016-08-12 11:58:53 -07001107 size_t srcRowBytes, size_t dstRowBytes, int h) {
1108 void* mapPtr;
1109 VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(),
jvanverth9d54afc2016-09-20 09:20:03 -07001110 alloc.fMemory,
1111 alloc.fOffset,
egdaniel3602d4f2016-08-12 11:58:53 -07001112 dstRowBytes * h,
1113 0,
1114 &mapPtr));
1115 if (err) {
1116 return false;
1117 }
1118
Greg Daniel20ece3a2017-03-28 10:24:43 -04001119 if (srcData) {
1120 // If there is no padding on dst we can do a single memcopy.
1121 // This assumes the srcData comes in with no padding.
1122 SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes),
1123 srcData, srcRowBytes, srcRowBytes, h);
1124 } else {
1125 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1126 // with some data.
1127 if (srcRowBytes == static_cast<size_t>(dstRowBytes)) {
1128 memset(mapPtr, 0, srcRowBytes * h);
1129 } else {
1130 for (int i = 0; i < h; ++i) {
1131 memset(mapPtr, 0, srcRowBytes);
1132 mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes));
1133 }
1134 }
1135 }
jvanverth9d54afc2016-09-20 09:20:03 -07001136 GrVkMemory::FlushMappedAlloc(gpu, alloc);
1137 GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory));
egdaniel3602d4f2016-08-12 11:58:53 -07001138 return true;
1139}
1140
Greg Daniel164a9f02016-02-22 09:56:40 -05001141GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07001142 GrPixelConfig config,
1143 bool isRenderTarget) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001144
1145 VkFormat pixelFormat;
1146 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
1147 return 0;
1148 }
1149
1150 bool linearTiling = false;
1151 if (!fVkCaps->isConfigTexturable(config)) {
1152 return 0;
1153 }
1154
egdaniel0a3a7f72016-06-24 09:22:31 -07001155 if (isRenderTarget && !fVkCaps->isConfigRenderable(config, false)) {
1156 return 0;
1157 }
1158
egdaniela95d46b2016-08-15 08:06:29 -07001159 if (fVkCaps->isConfigTexturableLinearly(config) &&
egdaniel0a3a7f72016-06-24 09:22:31 -07001160 (!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false))) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001161 linearTiling = true;
1162 }
1163
Greg Daniel164a9f02016-02-22 09:56:40 -05001164 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1165 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1166 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
egdaniel0a3a7f72016-06-24 09:22:31 -07001167 if (isRenderTarget) {
1168 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1169 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001170
jvanverthfd359ca2016-03-18 11:57:24 -07001171 VkImage image = VK_NULL_HANDLE;
jvanverth9d54afc2016-09-20 09:20:03 -07001172 GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0, 0 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001173
jvanverthfd359ca2016-03-18 11:57:24 -07001174 VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
1175 VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling)
1176 ? VK_IMAGE_LAYOUT_PREINITIALIZED
1177 : VK_IMAGE_LAYOUT_UNDEFINED;
1178
1179 // Create Image
1180 VkSampleCountFlagBits vkSamples;
1181 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
1182 return 0;
1183 }
1184
1185 const VkImageCreateInfo imageCreateInfo = {
1186 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1187 NULL, // pNext
1188 0, // VkImageCreateFlags
1189 VK_IMAGE_TYPE_2D, // VkImageType
1190 pixelFormat, // VkFormat
ethannicholas384b5e92016-03-25 11:04:06 -07001191 { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D
jvanverthfd359ca2016-03-18 11:57:24 -07001192 1, // mipLevels
1193 1, // arrayLayers
1194 vkSamples, // samples
1195 imageTiling, // VkImageTiling
1196 usageFlags, // VkImageUsageFlags
1197 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1198 0, // queueFamilyCount
1199 0, // pQueueFamilyIndices
1200 initialLayout // initialLayout
1201 };
1202
1203 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
1204
jvanverth6b6ffc42016-06-13 14:28:07 -07001205 if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001206 VK_CALL(DestroyImage(this->device(), image, nullptr));
Greg Daniel164a9f02016-02-22 09:56:40 -05001207 return 0;
1208 }
1209
Greg Daniel20ece3a2017-03-28 10:24:43 -04001210 size_t bpp = GrBytesPerPixel(config);
1211 size_t rowCopyBytes = bpp * w;
1212 if (linearTiling) {
1213 const VkImageSubresource subres = {
1214 VK_IMAGE_ASPECT_COLOR_BIT,
1215 0, // mipLevel
1216 0, // arraySlice
1217 };
1218 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -05001219
Greg Daniel20ece3a2017-03-28 10:24:43 -04001220 VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001221
Greg Daniel20ece3a2017-03-28 10:24:43 -04001222 if (!copy_testing_data(this, srcData, alloc, rowCopyBytes,
1223 static_cast<size_t>(layout.rowPitch), h)) {
1224 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1225 VK_CALL(DestroyImage(fDevice, image, nullptr));
1226 return 0;
1227 }
1228 } else {
1229 SkASSERT(w && h);
egdaniel3602d4f2016-08-12 11:58:53 -07001230
Greg Daniel20ece3a2017-03-28 10:24:43 -04001231 VkBuffer buffer;
1232 VkBufferCreateInfo bufInfo;
1233 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1234 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1235 bufInfo.flags = 0;
1236 bufInfo.size = rowCopyBytes * h;
1237 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1238 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1239 bufInfo.queueFamilyIndexCount = 0;
1240 bufInfo.pQueueFamilyIndices = nullptr;
1241 VkResult err;
1242 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
egdaniel3602d4f2016-08-12 11:58:53 -07001243
Greg Daniel20ece3a2017-03-28 10:24:43 -04001244 if (err) {
1245 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1246 VK_CALL(DestroyImage(fDevice, image, nullptr));
1247 return 0;
1248 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001249
Greg Daniel20ece3a2017-03-28 10:24:43 -04001250 GrVkAlloc bufferAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
1251 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type,
1252 true, &bufferAlloc)) {
1253 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1254 VK_CALL(DestroyImage(fDevice, image, nullptr));
1255 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1256 return 0;
1257 }
egdaniel3602d4f2016-08-12 11:58:53 -07001258
Greg Daniel20ece3a2017-03-28 10:24:43 -04001259 if (!copy_testing_data(this, srcData, bufferAlloc, rowCopyBytes, rowCopyBytes, h)) {
1260 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1261 VK_CALL(DestroyImage(fDevice, image, nullptr));
1262 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1263 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1264 return 0;
1265 }
egdaniel3602d4f2016-08-12 11:58:53 -07001266
Greg Daniel20ece3a2017-03-28 10:24:43 -04001267 const VkCommandBufferAllocateInfo cmdInfo = {
1268 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1269 NULL, // pNext
1270 fCmdPool, // commandPool
1271 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1272 1 // bufferCount
1273 };
egdaniel3602d4f2016-08-12 11:58:53 -07001274
Greg Daniel20ece3a2017-03-28 10:24:43 -04001275 VkCommandBuffer cmdBuffer;
1276 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1277 if (err) {
1278 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1279 VK_CALL(DestroyImage(fDevice, image, nullptr));
1280 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1281 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1282 return 0;
1283 }
egdaniel3602d4f2016-08-12 11:58:53 -07001284
Greg Daniel20ece3a2017-03-28 10:24:43 -04001285 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1286 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1287 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1288 cmdBufferBeginInfo.pNext = nullptr;
1289 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1290 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
egdaniel3602d4f2016-08-12 11:58:53 -07001291
Greg Daniel20ece3a2017-03-28 10:24:43 -04001292 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1293 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001294
Greg Daniel20ece3a2017-03-28 10:24:43 -04001295 // Set image layout and add barrier
1296 VkImageMemoryBarrier barrier;
1297 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1298 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1299 barrier.pNext = nullptr;
1300 barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout);
1301 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1302 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1303 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1304 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1305 barrier.image = image;
1306 barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0 , 1};
egdaniel3602d4f2016-08-12 11:58:53 -07001307
Greg Daniel20ece3a2017-03-28 10:24:43 -04001308 VK_CALL(CmdPipelineBarrier(cmdBuffer,
1309 GrVkMemory::LayoutToPipelineStageFlags(initialLayout),
1310 VK_PIPELINE_STAGE_TRANSFER_BIT,
1311 0,
1312 0, nullptr,
1313 0, nullptr,
1314 1, &barrier));
1315 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
egdaniel3602d4f2016-08-12 11:58:53 -07001316
Greg Daniel20ece3a2017-03-28 10:24:43 -04001317 // Submit copy command
1318 VkBufferImageCopy region;
1319 memset(&region, 0, sizeof(VkBufferImageCopy));
1320 region.bufferOffset = 0;
1321 region.bufferRowLength = w;
1322 region.bufferImageHeight = h;
1323 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1324 region.imageOffset = { 0, 0, 0 };
1325 region.imageExtent = { (uint32_t)w, (uint32_t)h, 1 };
egdaniel3602d4f2016-08-12 11:58:53 -07001326
Greg Daniel20ece3a2017-03-28 10:24:43 -04001327 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, 1, &region));
egdaniel3602d4f2016-08-12 11:58:53 -07001328
Greg Daniel20ece3a2017-03-28 10:24:43 -04001329 // End CommandBuffer
1330 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1331 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001332
Greg Daniel20ece3a2017-03-28 10:24:43 -04001333 // Create Fence for queue
1334 VkFence fence;
1335 VkFenceCreateInfo fenceInfo;
1336 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1337 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
egdaniel3602d4f2016-08-12 11:58:53 -07001338
Greg Daniel20ece3a2017-03-28 10:24:43 -04001339 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1340 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001341
Greg Daniel20ece3a2017-03-28 10:24:43 -04001342 VkSubmitInfo submitInfo;
1343 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1344 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1345 submitInfo.pNext = nullptr;
1346 submitInfo.waitSemaphoreCount = 0;
1347 submitInfo.pWaitSemaphores = nullptr;
1348 submitInfo.pWaitDstStageMask = 0;
1349 submitInfo.commandBufferCount = 1;
1350 submitInfo.pCommandBuffers = &cmdBuffer;
1351 submitInfo.signalSemaphoreCount = 0;
1352 submitInfo.pSignalSemaphores = nullptr;
1353 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1354 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001355
Greg Daniel20ece3a2017-03-28 10:24:43 -04001356 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1357 if (VK_TIMEOUT == err) {
1358 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1359 VK_CALL(DestroyImage(fDevice, image, nullptr));
egdaniel3602d4f2016-08-12 11:58:53 -07001360 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1361 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1362 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1363 VK_CALL(DestroyFence(fDevice, fence, nullptr));
Greg Daniel20ece3a2017-03-28 10:24:43 -04001364 SkDebugf("Fence failed to signal: %d\n", err);
1365 SkFAIL("failing");
Greg Daniel164a9f02016-02-22 09:56:40 -05001366 }
Greg Daniel20ece3a2017-03-28 10:24:43 -04001367 SkASSERT(!err);
1368
1369 // Clean up transfer resources
1370 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1371 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1372 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1373 VK_CALL(DestroyFence(fDevice, fence, nullptr));
Greg Daniel164a9f02016-02-22 09:56:40 -05001374 }
1375
egdanielb2df0c22016-05-13 11:30:37 -07001376 GrVkImageInfo* info = new GrVkImageInfo;
jvanverthfd359ca2016-03-18 11:57:24 -07001377 info->fImage = image;
1378 info->fAlloc = alloc;
1379 info->fImageTiling = imageTiling;
1380 info->fImageLayout = initialLayout;
egdaniel58a8d922016-04-21 08:03:10 -07001381 info->fFormat = pixelFormat;
jvanverth2af0f1b2016-05-03 10:36:49 -07001382 info->fLevelCount = 1;
jvanverthfd359ca2016-03-18 11:57:24 -07001383
1384 return (GrBackendObject)info;
Greg Daniel164a9f02016-02-22 09:56:40 -05001385}
1386
1387bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
egdanielb2df0c22016-05-13 11:30:37 -07001388 const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id);
Greg Daniel164a9f02016-02-22 09:56:40 -05001389
jvanverth1e305ba2016-06-01 09:39:15 -07001390 if (backend && backend->fImage && backend->fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001391 VkMemoryRequirements req;
1392 memset(&req, 0, sizeof(req));
1393 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
1394 backend->fImage,
1395 &req));
1396 // TODO: find a better check
1397 // This will probably fail with a different driver
1398 return (req.size > 0) && (req.size <= 8192 * 8192);
1399 }
1400
1401 return false;
1402}
1403
1404void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) {
jvanverth6b6ffc42016-06-13 14:28:07 -07001405 GrVkImageInfo* backend = reinterpret_cast<GrVkImageInfo*>(id);
Greg Daniel164a9f02016-02-22 09:56:40 -05001406 if (backend) {
1407 if (!abandon) {
jvanverthfd359ca2016-03-18 11:57:24 -07001408 // something in the command buffer may still be using this, so force submit
1409 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth6b6ffc42016-06-13 14:28:07 -07001410 GrVkImage::DestroyImageInfo(this, backend);
Greg Daniel164a9f02016-02-22 09:56:40 -05001411 }
jvanverthfd359ca2016-03-18 11:57:24 -07001412 delete backend;
Greg Daniel164a9f02016-02-22 09:56:40 -05001413 }
1414}
1415
1416////////////////////////////////////////////////////////////////////////////////
1417
1418void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1419 VkPipelineStageFlags dstStageMask,
1420 bool byRegion,
1421 VkMemoryBarrier* barrier) const {
1422 SkASSERT(fCurrentCmdBuffer);
1423 fCurrentCmdBuffer->pipelineBarrier(this,
1424 srcStageMask,
1425 dstStageMask,
1426 byRegion,
1427 GrVkCommandBuffer::kMemory_BarrierType,
1428 barrier);
1429}
1430
1431void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1432 VkPipelineStageFlags dstStageMask,
1433 bool byRegion,
1434 VkBufferMemoryBarrier* barrier) const {
1435 SkASSERT(fCurrentCmdBuffer);
1436 fCurrentCmdBuffer->pipelineBarrier(this,
1437 srcStageMask,
1438 dstStageMask,
1439 byRegion,
1440 GrVkCommandBuffer::kBufferMemory_BarrierType,
1441 barrier);
1442}
1443
1444void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1445 VkPipelineStageFlags dstStageMask,
1446 bool byRegion,
1447 VkImageMemoryBarrier* barrier) const {
1448 SkASSERT(fCurrentCmdBuffer);
1449 fCurrentCmdBuffer->pipelineBarrier(this,
1450 srcStageMask,
1451 dstStageMask,
1452 byRegion,
1453 GrVkCommandBuffer::kImageMemory_BarrierType,
1454 barrier);
1455}
1456
Mike Reed8724b462017-07-22 17:33:48 +00001457void GrVkGpu::finishFlush() {
1458 // Submit the current command buffer to the Queue
Greg Daniel164a9f02016-02-22 09:56:40 -05001459 this->submitCommandBuffer(kSkip_SyncQueue);
1460}
1461
egdaniel3d5d9ac2016-03-01 12:56:15 -08001462void GrVkGpu::clearStencil(GrRenderTarget* target) {
1463 if (nullptr == target) {
1464 return;
1465 }
1466 GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
1467 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
1468
1469
1470 VkClearDepthStencilValue vkStencilColor;
1471 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
1472
egdaniel3d5d9ac2016-03-01 12:56:15 -08001473 vkStencil->setImageLayout(this,
1474 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001475 VK_ACCESS_TRANSFER_WRITE_BIT,
1476 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel3d5d9ac2016-03-01 12:56:15 -08001477 false);
1478
egdaniel3d5d9ac2016-03-01 12:56:15 -08001479 VkImageSubresourceRange subRange;
1480 memset(&subRange, 0, sizeof(VkImageSubresourceRange));
1481 subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1482 subRange.baseMipLevel = 0;
1483 subRange.levelCount = 1;
1484 subRange.baseArrayLayer = 0;
1485 subRange.layerCount = 1;
1486
1487 // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
1488 // draw. Thus we should look into using the load op functions on the render pass to clear out
1489 // the stencil there.
1490 fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
1491}
1492
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001493inline bool can_copy_image(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
1494 const GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel164a9f02016-02-22 09:56:40 -05001495 const GrVkGpu* gpu) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001496 const GrRenderTarget* dstRT = dst->asRenderTarget();
1497 const GrRenderTarget* srcRT = src->asRenderTarget();
1498 if (dstRT && srcRT) {
1499 if (srcRT->numColorSamples() != dstRT->numColorSamples()) {
1500 return false;
1501 }
1502 } else if (dstRT) {
1503 if (dstRT->numColorSamples() > 1) {
1504 return false;
1505 }
1506 } else if (srcRT) {
1507 if (srcRT->numColorSamples() > 1) {
1508 return false;
1509 }
egdaniel17b89252016-04-05 07:23:38 -07001510 }
1511
Michael Jurka3251ed82017-04-05 09:52:55 -07001512 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
egdaniel17b89252016-04-05 07:23:38 -07001513 // as image usage flags.
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001514 if (srcOrigin == dstOrigin &&
egdaniel17b89252016-04-05 07:23:38 -07001515 GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001516 return true;
1517 }
1518
Greg Daniel164a9f02016-02-22 09:56:40 -05001519 return false;
1520}
1521
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001522void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1523 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001524 GrVkImage* dstImage,
1525 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001526 const SkIRect& srcRect,
1527 const SkIPoint& dstPoint) {
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001528 SkASSERT(can_copy_image(dst, dstOrigin, src, srcOrigin, this));
Greg Daniel164a9f02016-02-22 09:56:40 -05001529
Greg Daniel164a9f02016-02-22 09:56:40 -05001530 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1531 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001532 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001533 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1534 VK_ACCESS_TRANSFER_WRITE_BIT,
1535 VK_PIPELINE_STAGE_TRANSFER_BIT,
1536 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001537
egdaniel17b89252016-04-05 07:23:38 -07001538 srcImage->setImageLayout(this,
1539 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001540 VK_ACCESS_TRANSFER_READ_BIT,
1541 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001542 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001543
1544 // Flip rect if necessary
1545 SkIRect srcVkRect = srcRect;
1546 int32_t dstY = dstPoint.fY;
1547
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001548 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1549 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001550 srcVkRect.fTop = src->height() - srcRect.fBottom;
1551 srcVkRect.fBottom = src->height() - srcRect.fTop;
1552 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1553 }
1554
1555 VkImageCopy copyRegion;
1556 memset(&copyRegion, 0, sizeof(VkImageCopy));
1557 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1558 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1559 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1560 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001561 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001562
1563 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001564 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001565 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001566 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001567 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1568 1,
1569 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001570
1571 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1572 srcRect.width(), srcRect.height());
1573 this->didWriteToSurface(dst, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001574}
1575
egdaniel17b89252016-04-05 07:23:38 -07001576inline bool can_copy_as_blit(const GrSurface* dst,
1577 const GrSurface* src,
1578 const GrVkImage* dstImage,
1579 const GrVkImage* srcImage,
1580 const GrVkGpu* gpu) {
egdaniel66933552016-08-24 07:22:19 -07001581 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
egdaniel17b89252016-04-05 07:23:38 -07001582 // as image usage flags.
1583 const GrVkCaps& caps = gpu->vkCaps();
1584 if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) ||
1585 !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) {
1586 return false;
1587 }
1588
1589 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
1590 // resolved msaa though.
1591 if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) ||
1592 (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) {
1593 return false;
1594 }
1595
1596 return true;
1597}
1598
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001599void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1600 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001601 GrVkImage* dstImage,
1602 GrVkImage* srcImage,
1603 const SkIRect& srcRect,
1604 const SkIPoint& dstPoint) {
1605 SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this));
1606
egdaniel17b89252016-04-05 07:23:38 -07001607 dstImage->setImageLayout(this,
1608 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001609 VK_ACCESS_TRANSFER_WRITE_BIT,
1610 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001611 false);
1612
egdaniel17b89252016-04-05 07:23:38 -07001613 srcImage->setImageLayout(this,
1614 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001615 VK_ACCESS_TRANSFER_READ_BIT,
1616 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001617 false);
1618
1619 // Flip rect if necessary
1620 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001621 srcVkRect.fLeft = srcRect.fLeft;
1622 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001623 SkIRect dstRect;
1624 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001625 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001626
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001627 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001628 srcVkRect.fTop = src->height() - srcRect.fBottom;
1629 srcVkRect.fBottom = src->height() - srcRect.fTop;
1630 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001631 srcVkRect.fTop = srcRect.fTop;
1632 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001633 }
1634
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001635 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001636 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1637 } else {
1638 dstRect.fTop = dstPoint.fY;
1639 }
1640 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1641
1642 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1643 // get the correct origintation of the copied data.
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001644 if (srcOrigin != dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001645 SkTSwap(dstRect.fTop, dstRect.fBottom);
1646 }
1647
1648 VkImageBlit blitRegion;
1649 memset(&blitRegion, 0, sizeof(VkImageBlit));
1650 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1651 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001652 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001653 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1654 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001655 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001656
1657 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001658 *srcImage,
1659 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001660 1,
1661 &blitRegion,
1662 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001663
1664 this->didWriteToSurface(dst, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001665}
1666
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001667inline bool can_copy_as_resolve(const GrSurface* dst, GrSurfaceOrigin dstOrigin,
1668 const GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel4bcd62e2016-08-31 07:37:31 -07001669 const GrVkGpu* gpu) {
1670 // Our src must be a multisampled render target
1671 if (!src->asRenderTarget() || src->asRenderTarget()->numColorSamples() <= 1) {
1672 return false;
1673 }
1674
Greg Daniel7d6f6662017-04-18 16:40:35 -04001675 // The dst must not be a multisampled render target, expect in the case where the dst is the
1676 // resolve texture connected to the msaa src. We check for this in case we are copying a part of
1677 // a surface to a different region in the same surface.
1678 if (dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1 && dst != src) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001679 return false;
1680 }
1681
1682 // Surfaces must have the same origin.
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001683 if (srcOrigin != dstOrigin) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001684 return false;
1685 }
1686
1687 return true;
1688}
1689
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001690void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1691 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel4bcd62e2016-08-31 07:37:31 -07001692 const SkIRect& srcRect,
1693 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001694 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001695 this->resolveImage(dst, dstOrigin, srcRT, srcOrigin, srcRect, dstPoint);
egdaniel4bcd62e2016-08-31 07:37:31 -07001696}
1697
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001698bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1699 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel164a9f02016-02-22 09:56:40 -05001700 const SkIRect& srcRect,
1701 const SkIPoint& dstPoint) {
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001702 if (can_copy_as_resolve(dst, dstOrigin, src, srcOrigin, this)) {
1703 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001704 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001705 }
1706
egdanielfd016d72016-09-27 12:13:05 -07001707 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1708 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
1709 }
1710
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001711 if (fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect, dstPoint)) {
egdanielbc9b2962016-09-27 08:00:53 -07001712 return true;
1713 }
1714
egdaniel17b89252016-04-05 07:23:38 -07001715 GrVkImage* dstImage;
1716 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001717 GrRenderTarget* dstRT = dst->asRenderTarget();
1718 if (dstRT) {
1719 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1720 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1721 } else {
1722 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001723 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001724 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001725 GrRenderTarget* srcRT = src->asRenderTarget();
1726 if (srcRT) {
1727 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1728 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001729 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001730 SkASSERT(src->asTexture());
1731 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001732 }
1733
Brian Osman13dddce2017-05-09 13:19:50 -04001734 // For borrowed textures, we *only* want to copy using draws (to avoid layout changes)
1735 if (srcImage->isBorrowed()) {
1736 return false;
1737 }
1738
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001739 if (can_copy_image(dst, dstOrigin, src, srcOrigin, this)) {
1740 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1741 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07001742 return true;
1743 }
1744
1745 if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) {
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001746 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1747 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001748 return true;
1749 }
1750
Greg Daniel164a9f02016-02-22 09:56:40 -05001751 return false;
1752}
1753
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001754void GrVkGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, GrSurfaceOrigin, const GrStencilSettings&,
csmartdaltonc25c5d72016-11-01 07:03:59 -07001755 int* effectiveSampleCnt, SamplePattern*) {
cdalton28f45b92016-03-07 13:58:26 -08001756 // TODO: stub.
1757 SkASSERT(!this->caps()->sampleLocationsSupport());
Brian Salomon154ce912017-05-17 10:40:02 -04001758 *effectiveSampleCnt = rt->numStencilSamples();
cdalton28f45b92016-03-07 13:58:26 -08001759}
1760
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001761bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin,
1762 int width, int height, size_t rowBytes,
Greg Daniel164a9f02016-02-22 09:56:40 -05001763 GrPixelConfig readConfig, DrawPreference* drawPreference,
1764 ReadPixelTempDrawInfo* tempDrawInfo) {
egdaniel88e8aef2016-06-27 14:34:55 -07001765 // These settings we will always want if a temp draw is performed.
1766 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1767 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
1768 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
1769 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
1770 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07001771 tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox;
egdaniel88e8aef2016-06-27 14:34:55 -07001772
1773 // For now assume no swizzling, we may change that below.
1774 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
1775
1776 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
1777 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04001778 // Note that if we require a draw and return a non-renderable format for the temp surface the
egdaniel88e8aef2016-06-27 14:34:55 -07001779 // base class will fail for us.
1780 tempDrawInfo->fTempSurfaceDesc.fConfig = srcSurface->config();
1781 tempDrawInfo->fReadConfig = readConfig;
1782
egdaniel4583ec52016-06-27 12:57:00 -07001783 if (srcSurface->config() == readConfig) {
1784 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -05001785 }
1786
Brian Osman33910292017-04-18 14:38:53 -04001787 // Any config change requires a draw
1788 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1789 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
1790 tempDrawInfo->fReadConfig = readConfig;
Greg Daniel164a9f02016-02-22 09:56:40 -05001791
Brian Osman33910292017-04-18 14:38:53 -04001792 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -05001793}
1794
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001795bool GrVkGpu::onReadPixels(GrSurface* surface, GrSurfaceOrigin origin,
Greg Daniel164a9f02016-02-22 09:56:40 -05001796 int left, int top, int width, int height,
1797 GrPixelConfig config,
1798 void* buffer,
1799 size_t rowBytes) {
1800 VkFormat pixelFormat;
1801 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
1802 return false;
1803 }
1804
egdaniel66933552016-08-24 07:22:19 -07001805 GrVkImage* image = nullptr;
1806 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1807 if (rt) {
1808 // resolve the render target if necessary
1809 switch (rt->getResolveType()) {
1810 case GrVkRenderTarget::kCantResolve_ResolveType:
1811 return false;
1812 case GrVkRenderTarget::kAutoResolves_ResolveType:
1813 break;
1814 case GrVkRenderTarget::kCanResolve_ResolveType:
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001815 this->internalResolveRenderTarget(rt, origin, false);
egdaniel66933552016-08-24 07:22:19 -07001816 break;
1817 default:
1818 SkFAIL("Unknown resolve type");
1819 }
1820 image = rt;
1821 } else {
1822 image = static_cast<GrVkTexture*>(surface->asTexture());
1823 }
1824
1825 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001826 return false;
1827 }
1828
1829 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001830 image->setImageLayout(this,
1831 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1832 VK_ACCESS_TRANSFER_READ_BIT,
1833 VK_PIPELINE_STAGE_TRANSFER_BIT,
1834 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001835
egdaniel6fa0a912016-09-12 11:51:29 -07001836 size_t bpp = GrBytesPerPixel(config);
1837 size_t tightRowBytes = bpp * width;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001838 bool flipY = kBottomLeft_GrSurfaceOrigin == origin;
Greg Daniel164a9f02016-02-22 09:56:40 -05001839
Greg Daniel164a9f02016-02-22 09:56:40 -05001840 VkBufferImageCopy region;
1841 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001842
1843 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1844 if (copyFromOrigin) {
1845 region.imageOffset = { 0, 0, 0 };
1846 region.imageExtent = { (uint32_t)(left + width),
1847 (uint32_t)(flipY ? surface->height() - top : top + height),
1848 1
1849 };
1850 } else {
1851 VkOffset3D offset = {
1852 left,
1853 flipY ? surface->height() - top - height : top,
1854 0
1855 };
1856 region.imageOffset = offset;
1857 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1858 }
1859
1860 size_t transBufferRowBytes = bpp * region.imageExtent.width;
1861 GrVkTransferBuffer* transferBuffer =
1862 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * height,
1863 kXferGpuToCpu_GrBufferType,
1864 kStream_GrAccessPattern));
1865
1866 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001867 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001868 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001869 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1870 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001871
1872 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001873 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001874 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1875 transferBuffer,
1876 1,
1877 &region);
1878
1879 // make sure the copy to buffer has finished
1880 transferBuffer->addMemoryBarrier(this,
1881 VK_ACCESS_TRANSFER_WRITE_BIT,
1882 VK_ACCESS_HOST_READ_BIT,
1883 VK_PIPELINE_STAGE_TRANSFER_BIT,
1884 VK_PIPELINE_STAGE_HOST_BIT,
1885 false);
1886
1887 // We need to submit the current command buffer to the Queue and make sure it finishes before
1888 // we can copy the data out of the buffer.
1889 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth9d54afc2016-09-20 09:20:03 -07001890 GrVkMemory::InvalidateMappedAlloc(this, transferBuffer->alloc());
Greg Daniel164a9f02016-02-22 09:56:40 -05001891 void* mappedMemory = transferBuffer->map();
1892
egdaniel6fa0a912016-09-12 11:51:29 -07001893 if (copyFromOrigin) {
1894 uint32_t skipRows = region.imageExtent.height - height;
1895 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1896 }
1897
egdaniel88e8aef2016-06-27 14:34:55 -07001898 if (flipY) {
1899 const char* srcRow = reinterpret_cast<const char*>(mappedMemory);
1900 char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes;
1901 for (int y = 0; y < height; y++) {
1902 memcpy(dstRow, srcRow, tightRowBytes);
egdaniel6fa0a912016-09-12 11:51:29 -07001903 srcRow += transBufferRowBytes;
egdaniel88e8aef2016-06-27 14:34:55 -07001904 dstRow -= rowBytes;
1905 }
1906 } else {
Matt Sarettcf3525a2017-01-24 12:43:41 -05001907 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
egdaniel88e8aef2016-06-27 14:34:55 -07001908 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001909
1910 transferBuffer->unmap();
1911 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001912 return true;
1913}
egdaniel066df7c2016-06-08 14:02:27 -07001914
egdaniel27bb2842016-07-07 11:58:35 -07001915// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1916// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1917// the the entire attachment. Similar requirements for the y and height components.
1918void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1919 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1920 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001921 if ((0 != granularity.width && 1 != granularity.width)) {
1922 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1923 int rightAdj = srcBounds.fRight % granularity.width;
1924 if (rightAdj != 0) {
1925 rightAdj = granularity.width - rightAdj;
1926 }
1927 dstBounds->fRight = srcBounds.fRight + rightAdj;
1928 if (dstBounds->fRight > maxWidth) {
1929 dstBounds->fRight = maxWidth;
1930 dstBounds->fLeft = 0;
1931 } else {
1932 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1933 }
egdaniel27bb2842016-07-07 11:58:35 -07001934 } else {
egdanield5797b32016-09-20 12:57:45 -07001935 dstBounds->fLeft = srcBounds.fLeft;
1936 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07001937 }
1938
1939 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07001940 if ((0 != granularity.height && 1 != granularity.height)) {
1941 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
1942 int bottomAdj = srcBounds.fBottom % granularity.height;
1943 if (bottomAdj != 0) {
1944 bottomAdj = granularity.height - bottomAdj;
1945 }
1946 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
1947 if (dstBounds->fBottom > maxHeight) {
1948 dstBounds->fBottom = maxHeight;
1949 dstBounds->fTop = 0;
1950 } else {
1951 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
1952 }
egdaniel27bb2842016-07-07 11:58:35 -07001953 } else {
egdanield5797b32016-09-20 12:57:45 -07001954 dstBounds->fTop = srcBounds.fTop;
1955 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07001956 }
1957}
1958
Greg Daniel22bc8652017-03-22 15:45:43 -04001959void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07001960 const GrVkRenderPass* renderPass,
1961 const VkClearValue* colorClear,
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001962 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07001963 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07001964 const SkIRect* pBounds = &bounds;
1965 SkIRect flippedBounds;
Robert Phillipsdf0e09f2017-07-28 11:56:47 -04001966 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07001967 flippedBounds = bounds;
1968 flippedBounds.fTop = target->height() - bounds.fBottom;
1969 flippedBounds.fBottom = target->height() - bounds.fTop;
1970 pBounds = &flippedBounds;
1971 }
1972
egdaniel27bb2842016-07-07 11:58:35 -07001973 // The bounds we use for the render pass should be of the granularity supported
1974 // by the device.
1975 const VkExtent2D& granularity = renderPass->granularity();
1976 SkIRect adjustedBounds;
1977 if ((0 != granularity.width && 1 != granularity.width) ||
1978 (0 != granularity.height && 1 != granularity.height)) {
1979 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
1980 target->width(), target->height());
1981 pBounds = &adjustedBounds;
1982 }
1983
Greg Daniel77a86f82017-01-23 11:04:45 -05001984 fCurrentCmdBuffer->beginRenderPass(this, renderPass, colorClear, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04001985 for (int i = 0; i < buffers.count(); ++i) {
1986 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
1987 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001988 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07001989
egdanielce3bfb12016-08-26 11:05:13 -07001990 this->didWriteToSurface(target, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05001991}
egdaniel9cb63402016-06-23 08:37:05 -07001992
Greg Daniel6be35232017-03-01 17:01:09 -05001993GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07001994 VkFenceCreateInfo createInfo;
1995 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
1996 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1997 createInfo.pNext = nullptr;
1998 createInfo.flags = 0;
1999 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002000
2001 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2002 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2003
2004 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002005 return (GrFence)fence;
2006}
2007
Greg Daniel6be35232017-03-01 17:01:09 -05002008bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2009 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2010
2011 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002012 return (VK_SUCCESS == result);
2013}
2014
2015void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002016 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2017}
2018
Greg Daniela5cb7812017-06-16 09:45:32 -04002019sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2020 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002021}
2022
Greg Daniela5cb7812017-06-16 09:45:32 -04002023sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2024 GrWrapOwnership ownership) {
2025 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), ownership);
2026}
2027
2028void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05002029 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2030
Greg Daniela5cb7812017-06-16 09:45:32 -04002031 const GrVkSemaphore::Resource* resource = vkSem->getResource();
2032 resource->ref();
2033 fSemaphoresToSignal.push_back(resource);
2034
2035 if (flush) {
2036 this->submitCommandBuffer(kSkip_SyncQueue);
2037 }
Greg Daniel6be35232017-03-01 17:01:09 -05002038}
2039
2040void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
2041 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2042
2043 const GrVkSemaphore::Resource* resource = vkSem->getResource();
2044 resource->ref();
2045 fSemaphoresToWaitOn.push_back(resource);
jvanverth84741b32016-09-30 08:39:02 -07002046}
Brian Osman13dddce2017-05-09 13:19:50 -04002047
2048sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2049 SkASSERT(texture);
2050 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2051 vkTexture->setImageLayout(this,
2052 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2053 VK_ACCESS_SHADER_READ_BIT,
2054 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
2055 false);
2056 this->submitCommandBuffer(kSkip_SyncQueue);
2057
2058 // The image layout change serves as a barrier, so no semaphore is needed
2059 return nullptr;
2060}