blob: efdca9056ebdfa46aae31f901353dfc971465d13 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrVkGpu.h"
9
Greg Daniela5cb7812017-06-16 09:45:32 -040010#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000011#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrContextOptions.h"
13#include "GrGeometryProcessor.h"
14#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070015#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrPipeline.h"
17#include "GrRenderTargetPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018#include "GrTexturePriv.h"
Greg Daniel81df0412018-05-31 13:13:33 -040019#include "GrVkAMDMemoryAllocator.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050020#include "GrVkCommandBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070021#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050022#include "GrVkImage.h"
23#include "GrVkIndexBuffer.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040024#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050025#include "GrVkMemory.h"
26#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070027#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050028#include "GrVkRenderPass.h"
29#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050030#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050031#include "GrVkTexture.h"
32#include "GrVkTextureRenderTarget.h"
33#include "GrVkTransferBuffer.h"
34#include "GrVkVertexBuffer.h"
Matt Sarett485c4992017-02-14 14:18:27 -050035#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070036#include "SkMipMap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040037#include "SkSLCompiler.h"
38#include "SkTo.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040039
Greg Daniela31f4e52018-08-01 16:48:52 -040040#include "vk/GrVkExtensions.h"
jvanverthfd359ca2016-03-18 11:57:24 -070041#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050042
Ben Wagnerf08d1d02018-06-18 15:11:00 -040043#include <utility>
44
Forrest Reiling44f85712017-03-27 23:22:20 -070045#if !defined(SK_BUILD_FOR_WIN)
46#include <unistd.h>
47#endif // !defined(SK_BUILD_FOR_WIN)
48
Greg Danieldef55462018-08-01 13:40:14 -040049#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
50#include "SkLeanWindows.h"
51#endif
52
Greg Daniel164a9f02016-02-22 09:56:40 -050053#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
54#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
55#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
56
Greg Danielf730c182018-07-02 20:15:37 +000057sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050058 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000059 if (backendContext.fInstance == VK_NULL_HANDLE ||
60 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
61 backendContext.fDevice == VK_NULL_HANDLE ||
62 backendContext.fQueue == VK_NULL_HANDLE) {
63 return nullptr;
64 }
Greg Danield3e65aa2018-08-01 09:19:45 -040065 if (!backendContext.fGetProc) {
66 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040067 }
Greg Danield3e65aa2018-08-01 09:19:45 -040068
Greg Daniel98bffae2018-08-01 13:25:41 -040069 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -040070
Greg Daniel98bffae2018-08-01 13:25:41 -040071 if (backendContext.fVkExtensions) {
72 interface.reset(new GrVkInterface(backendContext.fGetProc,
73 backendContext.fInstance,
74 backendContext.fDevice,
75 backendContext.fVkExtensions));
76 if (!interface->validate(backendContext.fVkExtensions)) {
77 return nullptr;
78 }
79 } else {
80 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
81 // make an empty GrVkExtensions and pass that to the GrVkInterface.
82 GrVkExtensions extensions;
83 interface.reset(new GrVkInterface(backendContext.fGetProc,
84 backendContext.fInstance,
85 backendContext.fDevice,
86 &extensions));
87 if (!interface->validate(&extensions)) {
88 return nullptr;
89 }
Greg Daniel164a9f02016-02-22 09:56:40 -050090 }
91
Greg Danielc8cd45a2018-07-12 10:02:37 -040092 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface));
Greg Daniel164a9f02016-02-22 09:56:40 -050093}
94
95////////////////////////////////////////////////////////////////////////////////
96
halcanary9d524f22016-03-29 09:03:52 -070097GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Danielc8cd45a2018-07-12 10:02:37 -040098 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface)
Brian Salomon384fab42017-12-07 12:33:05 -050099 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400100 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000101 , fMemoryAllocator(backendContext.fMemoryAllocator)
102 , fInstance(backendContext.fInstance)
103 , fDevice(backendContext.fDevice)
104 , fQueue(backendContext.fQueue)
Brian Salomon384fab42017-12-07 12:33:05 -0500105 , fResourceProvider(this)
106 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000107 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700108
Greg Daniel81df0412018-05-31 13:13:33 -0400109 if (!fMemoryAllocator) {
110 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000111 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400112 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400113 }
114
ethannicholasb3058bd2016-07-01 08:22:01 -0700115 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700116
Greg Daniel92aef4b2018-08-02 13:55:49 -0400117 uint32_t instanceVersion = backendContext.fInstanceVersion ? backendContext.fInstanceVersion
118 : backendContext.fMinAPIVersion;
119
Greg Daniel36443602018-08-02 12:51:52 -0400120 if (backendContext.fFeatures & kIgnoreAllFlags_GrVkFeatureFlag) {
121 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel92aef4b2018-08-02 13:55:49 -0400122 backendContext.fDeviceFeatures, instanceVersion));
Greg Daniel36443602018-08-02 12:51:52 -0400123 } else {
124 VkPhysicalDeviceFeatures features;
125 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
126 features.geometryShader = true;
127 }
128 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
129 features.dualSrcBlend = true;
130 }
131 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
132 features.sampleRateShading = true;
133 }
134 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel92aef4b2018-08-02 13:55:49 -0400135 features, instanceVersion));
Greg Daniel36443602018-08-02 12:51:52 -0400136 }
jvanverth633b3562016-03-23 11:01:22 -0700137 fCaps.reset(SkRef(fVkCaps.get()));
138
Greg Danielf730c182018-07-02 20:15:37 +0000139 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
140 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700141
142 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700143 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
144 nullptr, // pNext
145 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
146 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
Greg Danielf730c182018-07-02 20:15:37 +0000147 backendContext.fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700148 };
halcanary9d524f22016-03-29 09:03:52 -0700149 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700150 &fCmdPool));
151
152 // must call this after creating the CommandPool
153 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700154 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700155 SkASSERT(fCurrentCmdBuffer);
156 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500157}
158
Greg Daniel8606cf82017-05-08 16:17:53 -0400159void GrVkGpu::destroyResources() {
160 if (fCurrentCmdBuffer) {
161 fCurrentCmdBuffer->end(this);
162 fCurrentCmdBuffer->unref(this);
163 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500164
165 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700166 fResourceProvider.checkCommandBuffers();
Jim Van Verth09557d72016-11-07 11:10:21 -0500167 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700168
169 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
170 // on the command buffers even though they have completed. This causes an assert to fire when
171 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500172 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700173#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500174 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700175#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500176 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700177#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500178 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700179#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500180 }
egdanielf8c2be32016-06-24 13:18:27 -0700181#endif
182
egdanielbe9d8212016-09-20 08:54:23 -0700183#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400184 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700185#endif
halcanary9d524f22016-03-29 09:03:52 -0700186
Greg Daniel6be35232017-03-01 17:01:09 -0500187 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
188 fSemaphoresToWaitOn[i]->unref(this);
189 }
190 fSemaphoresToWaitOn.reset();
191
Greg Daniela5cb7812017-06-16 09:45:32 -0400192 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
193 fSemaphoresToSignal[i]->unref(this);
194 }
195 fSemaphoresToSignal.reset();
196
197
egdanielbc9b2962016-09-27 08:00:53 -0700198 fCopyManager.destroyResources(this);
199
Jim Van Verth09557d72016-11-07 11:10:21 -0500200 // must call this just before we destroy the command pool and VkDevice
201 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500202
Greg Daniel8606cf82017-05-08 16:17:53 -0400203 if (fCmdPool != VK_NULL_HANDLE) {
204 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
205 }
jvanverth633b3562016-03-23 11:01:22 -0700206
Greg Danielf730c182018-07-02 20:15:37 +0000207 fMemoryAllocator.reset();
208
209 fQueue = VK_NULL_HANDLE;
210 fDevice = VK_NULL_HANDLE;
211 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400212}
213
214GrVkGpu::~GrVkGpu() {
215 if (!fDisconnected) {
216 this->destroyResources();
217 }
218 delete fCompiler;
219}
220
221
222void GrVkGpu::disconnect(DisconnectType type) {
223 INHERITED::disconnect(type);
224 if (!fDisconnected) {
225 if (DisconnectType::kCleanup == type) {
226 this->destroyResources();
227 } else {
228 fCurrentCmdBuffer->unrefAndAbandon();
229 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
230 fSemaphoresToWaitOn[i]->unrefAndAbandon();
231 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400232 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
233 fSemaphoresToSignal[i]->unrefAndAbandon();
234 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400235 fCopyManager.abandonResources();
236
237 // must call this just before we destroy the command pool and VkDevice
238 fResourceProvider.abandonResources();
239 }
240 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400241 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400242 fCurrentCmdBuffer = nullptr;
243 fCmdPool = VK_NULL_HANDLE;
244 fDisconnected = true;
245 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500246}
247
248///////////////////////////////////////////////////////////////////////////////
249
Greg Daniel500d58b2017-08-24 15:59:33 -0400250GrGpuRTCommandBuffer* GrVkGpu::createCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -0400251 GrRenderTarget* rt, GrSurfaceOrigin origin,
Greg Daniel500d58b2017-08-24 15:59:33 -0400252 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
253 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
254 return new GrVkGpuRTCommandBuffer(this, rt, origin, colorInfo, stencilInfo);
255}
256
257GrGpuTextureCommandBuffer* GrVkGpu::createCommandBuffer(GrTexture* texture,
258 GrSurfaceOrigin origin) {
259 return new GrVkGpuTextureCommandBuffer(this, texture, origin);
egdaniel066df7c2016-06-08 14:02:27 -0700260}
261
Greg Daniela5cb7812017-06-16 09:45:32 -0400262void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500263 SkASSERT(fCurrentCmdBuffer);
264 fCurrentCmdBuffer->end(this);
265
Greg Daniela5cb7812017-06-16 09:45:32 -0400266 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500267
268 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
269 fSemaphoresToWaitOn[i]->unref(this);
270 }
271 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400272 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
273 fSemaphoresToSignal[i]->unref(this);
274 }
275 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500276
Greg Daniel164a9f02016-02-22 09:56:40 -0500277 fResourceProvider.checkCommandBuffers();
278
279 // Release old command buffer and create a new one
280 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700281 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500282 SkASSERT(fCurrentCmdBuffer);
283
284 fCurrentCmdBuffer->begin(this);
285}
286
287///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700288GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
289 const void* data) {
290 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700291 switch (type) {
292 case kVertex_GrBufferType:
293 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
294 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700295 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700296 break;
cdalton397536c2016-03-25 12:15:03 -0700297 case kIndex_GrBufferType:
298 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
299 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700300 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700301 break;
cdalton397536c2016-03-25 12:15:03 -0700302 case kXferCpuToGpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400303 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
304 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700305 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700306 break;
cdalton397536c2016-03-25 12:15:03 -0700307 case kXferGpuToCpu_GrBufferType:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400308 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
309 kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700310 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700311 break;
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400312 case kDrawIndirect_GrBufferType:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400313 SK_ABORT("DrawIndirect Buffers not supported in vulkan backend.");
Greg Danielc2dd5ed2017-05-05 13:49:11 -0400314 return nullptr;
cdalton397536c2016-03-25 12:15:03 -0700315 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400316 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700317 return nullptr;
318 }
cdalton1bf3e712016-04-19 10:00:02 -0700319 if (data && buff) {
320 buff->updateData(data, size);
321 }
322 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500323}
324
Brian Salomona9b04b92018-06-01 15:04:28 -0400325bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
326 GrColorType srcColorType, const GrMipLevel texels[],
327 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500328 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
329 if (!vkTex) {
330 return false;
331 }
332
jvanverth900bd4a2016-04-29 13:53:12 -0700333 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400334 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800335 return false;
336 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800337
Greg Daniel164a9f02016-02-22 09:56:40 -0500338 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400339 bool linearTiling = vkTex->isLinearTiled();
340 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400341 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400342 SkDebugf("Can't upload mipmap data to linear tiled texture");
343 return false;
344 }
345 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
346 // Need to change the layout to general in order to perform a host write
347 vkTex->setImageLayout(this,
348 VK_IMAGE_LAYOUT_GENERAL,
349 VK_ACCESS_HOST_WRITE_BIT,
350 VK_PIPELINE_STAGE_HOST_BIT,
351 false);
352 this->submitCommandBuffer(kForce_SyncQueue);
353 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400354 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400355 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500356 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400357 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400358 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
359 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500360 }
egdaniel4583ec52016-06-27 12:57:00 -0700361
jvanverth900bd4a2016-04-29 13:53:12 -0700362 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500363}
364
Brian Salomonc320b152018-02-20 14:05:36 -0500365bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
366 GrColorType bufferColorType, GrBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400367 size_t bufferOffset, size_t rowBytes) {
368 // Vulkan only supports 4-byte aligned offsets
369 if (SkToBool(bufferOffset & 0x2)) {
370 return false;
371 }
372 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
373 if (!vkTex) {
374 return false;
375 }
376 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
377 if (!vkBuffer) {
378 return false;
379 }
380
Greg Daniel660cc992017-06-26 14:55:05 -0400381 SkDEBUGCODE(
382 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
383 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
384 SkASSERT(bounds.contains(subRect));
385 )
Brian Salomonc320b152018-02-20 14:05:36 -0500386 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400387 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500388 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400389 }
390
391 // Set up copy region
392 VkBufferImageCopy region;
393 memset(&region, 0, sizeof(VkBufferImageCopy));
394 region.bufferOffset = bufferOffset;
395 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
396 region.bufferImageHeight = 0;
397 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
398 region.imageOffset = { left, top, 0 };
399 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
400
401 // Change layout of our target so it can be copied to
402 vkTex->setImageLayout(this,
403 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
404 VK_ACCESS_TRANSFER_WRITE_BIT,
405 VK_PIPELINE_STAGE_TRANSFER_BIT,
406 false);
407
408 // Copy the buffer to the image
409 fCurrentCmdBuffer->copyBufferToImage(this,
410 vkBuffer,
411 vkTex,
412 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
413 1,
414 &region);
415
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400416 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400417 return true;
418}
419
Brian Salomon1fabd512018-02-09 09:54:25 -0500420void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
421 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700422 SkASSERT(dst);
423 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
424
egdanielfd016d72016-09-27 12:13:05 -0700425 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
426 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
427 }
428
egdaniel4bcd62e2016-08-31 07:37:31 -0700429 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500430 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
431 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
432 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
433 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
434 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700435
Greg Danielbc26c392017-04-18 13:32:10 -0400436 GrVkImage* dstImage;
437 GrRenderTarget* dstRT = dst->asRenderTarget();
438 if (dstRT) {
439 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400440 dstImage = vkRT;
441 } else {
442 SkASSERT(dst->asTexture());
443 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
444 }
445 dstImage->setImageLayout(this,
446 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
447 VK_ACCESS_TRANSFER_WRITE_BIT,
448 VK_PIPELINE_STAGE_TRANSFER_BIT,
449 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700450
451 src->msaaImage()->setImageLayout(this,
452 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
453 VK_ACCESS_TRANSFER_READ_BIT,
454 VK_PIPELINE_STAGE_TRANSFER_BIT,
455 false);
456
Greg Danielbc26c392017-04-18 13:32:10 -0400457 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700458}
459
Brian Salomon1fabd512018-02-09 09:54:25 -0500460void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700461 if (target->needsResolve()) {
462 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700463 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
464 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500465
egdaniel4bcd62e2016-08-31 07:37:31 -0700466 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700467
Brian Salomon1fabd512018-02-09 09:54:25 -0500468 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700469
470 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500471
472 if (requiresSubmit) {
473 this->submitCommandBuffer(kSkip_SyncQueue);
474 }
egdaniel52ad2512016-08-04 12:50:01 -0700475 }
476}
477
Brian Salomona9b04b92018-06-01 15:04:28 -0400478bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
479 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500480 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700481 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500482
Greg Daniel660cc992017-06-26 14:55:05 -0400483 SkDEBUGCODE(
484 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
485 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
486 SkASSERT(bounds.contains(subRect));
487 )
Brian Salomonc320b152018-02-20 14:05:36 -0500488 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500489 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400490 if (!rowBytes) {
491 rowBytes = trimRowBytes;
492 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500493
jvanverth900bd4a2016-04-29 13:53:12 -0700494 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
495 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
496 const VkImageSubresource subres = {
497 VK_IMAGE_ASPECT_COLOR_BIT,
498 0, // mipLevel
499 0, // arraySlice
500 };
501 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500502
jvanverth900bd4a2016-04-29 13:53:12 -0700503 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500504
jvanverth900bd4a2016-04-29 13:53:12 -0700505 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700506 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700507 &subres,
508 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500509
jvanverth1e305ba2016-06-01 09:39:15 -0700510 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400511 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700512 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400513 SkASSERT(size + offset <= alloc.fSize);
514 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
515 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700516 return false;
517 }
Greg Daniel81df0412018-05-31 13:13:33 -0400518 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700519
Brian Salomona9b04b92018-06-01 15:04:28 -0400520 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
521 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700522
Greg Daniele35a99e2018-03-02 11:44:22 -0500523 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400524 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700525
526 return true;
527}
528
Brian Salomona9b04b92018-06-01 15:04:28 -0400529bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
530 GrColorType dataColorType, const GrMipLevel texels[],
531 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700532 SkASSERT(!tex->isLinearTiled());
533 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400534 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700535 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
536
Greg Danieldd20e912017-04-07 14:42:23 -0400537 // We assume that if the texture has mip levels, we either upload to all the levels or just the
538 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400539 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400540
jvanverth900bd4a2016-04-29 13:53:12 -0700541 if (width == 0 || height == 0) {
542 return false;
543 }
544
Brian Salomond1eaf492017-05-18 10:02:08 -0400545 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500546 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700547
548 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700549 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
550 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400551 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
552
553 if (mipLevelCount) {
554 texelsShallowCopy.reset(mipLevelCount);
555 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
556 }
jvanverth900bd4a2016-04-29 13:53:12 -0700557
Robert Phillips590533f2017-07-11 14:22:35 -0400558 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700559 individualMipOffsets.push_back(0);
560 size_t combinedBufferSize = width * bpp * height;
561 int currentWidth = width;
562 int currentHeight = height;
Greg Daniel55afd6d2017-09-29 09:32:44 -0400563 if (mipLevelCount > 0 && !texelsShallowCopy[0].fPixels) {
564 combinedBufferSize = 0;
565 }
566
Greg Daniel468fd632017-03-22 17:03:45 -0400567 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
568 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
569 SkASSERT((bpp & (bpp - 1)) == 0);
570 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400571 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700572 currentWidth = SkTMax(1, currentWidth/2);
573 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400574
Greg Daniel55afd6d2017-09-29 09:32:44 -0400575 if (texelsShallowCopy[currentMipLevel].fPixels) {
576 const size_t trimmedSize = currentWidth * bpp * currentHeight;
577 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
578 if (alignmentDiff != 0) {
579 combinedBufferSize += alignmentMask - alignmentDiff + 1;
580 }
581 individualMipOffsets.push_back(combinedBufferSize);
582 combinedBufferSize += trimmedSize;
583 } else {
584 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400585 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400586 }
587 if (0 == combinedBufferSize) {
588 // We don't actually have any data to upload so just return success
589 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700590 }
591
592 // allocate buffer to hold our mip data
593 GrVkTransferBuffer* transferBuffer =
594 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel6888c0d2017-08-25 11:55:50 -0400595 if(!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700596 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400597 }
jvanverth900bd4a2016-04-29 13:53:12 -0700598
599 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400600 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700601
jvanverthc578b0632016-05-02 10:58:12 -0700602 currentWidth = width;
603 currentHeight = height;
Greg Daniela1b282b2017-03-28 14:56:46 -0400604 int layerHeight = tex->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400605 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400606 if (texelsShallowCopy[currentMipLevel].fPixels) {
607 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
608 const size_t trimRowBytes = currentWidth * bpp;
609 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
610 ? texelsShallowCopy[currentMipLevel].fRowBytes
611 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700612
Greg Daniel55afd6d2017-09-29 09:32:44 -0400613 // copy data into the buffer, skipping the trailing bytes
614 char* dst = buffer + individualMipOffsets[currentMipLevel];
615 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400616 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400617
618 VkBufferImageCopy& region = regions.push_back();
619 memset(&region, 0, sizeof(VkBufferImageCopy));
620 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
621 region.bufferRowLength = currentWidth;
622 region.bufferImageHeight = currentHeight;
623 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Brian Salomona9b04b92018-06-01 15:04:28 -0400624 region.imageOffset = {left, top, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400625 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700626 }
jvanverthc578b0632016-05-02 10:58:12 -0700627 currentWidth = SkTMax(1, currentWidth/2);
628 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400629 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700630 }
631
jvanverth9d54afc2016-09-20 09:20:03 -0700632 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700633 transferBuffer->unmap();
634
jvanverth900bd4a2016-04-29 13:53:12 -0700635 // Change layout of our target so it can be copied to
jvanverth900bd4a2016-04-29 13:53:12 -0700636 tex->setImageLayout(this,
637 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -0700638 VK_ACCESS_TRANSFER_WRITE_BIT,
639 VK_PIPELINE_STAGE_TRANSFER_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700640 false);
641
642 // Copy the buffer to the image
643 fCurrentCmdBuffer->copyBufferToImage(this,
644 transferBuffer,
645 tex,
646 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
647 regions.count(),
648 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700649 transferBuffer->unref();
Robert Phillips590533f2017-07-11 14:22:35 -0400650 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400651 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400652 }
jvanverth900bd4a2016-04-29 13:53:12 -0700653
Greg Daniel164a9f02016-02-22 09:56:40 -0500654 return true;
655}
656
657////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400658sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500659 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500660 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
661
662 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500663 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700664
Greg Daniel164a9f02016-02-22 09:56:40 -0500665 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
666 if (renderTarget) {
667 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
668 }
669
670 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
671 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
672 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700673 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500674 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
675 // texture.
676 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
677
Greg Daniel164a9f02016-02-22 09:56:40 -0500678 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700679 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500680 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400681 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500682 GrVkImage::ImageDesc imageDesc;
683 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
684 imageDesc.fFormat = pixelFormat;
685 imageDesc.fWidth = desc.fWidth;
686 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400687 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500688 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400689 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500690 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400691 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500692
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400693 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
694 if (mipLevels > 1) {
695 mipMapsStatus = GrMipMapsStatus::kValid;
696 for (int i = 0; i < mipLevels; ++i) {
697 if (!texels[i].fPixels) {
698 mipMapsStatus = GrMipMapsStatus::kDirty;
699 break;
700 }
Greg Daniel834f1202017-10-09 15:06:20 -0400701 }
702 }
703
Robert Phillips67d52cf2017-06-05 13:38:13 -0400704 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500705 if (renderTarget) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700706 tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
Greg Daniel834f1202017-10-09 15:06:20 -0400707 imageDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400708 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500709 } else {
Greg Daniel834f1202017-10-09 15:06:20 -0400710 tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc,
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400711 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500712 }
713
714 if (!tex) {
715 return nullptr;
716 }
717
Brian Salomonc320b152018-02-20 14:05:36 -0500718 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400719 if (mipLevelCount) {
Brian Salomona9b04b92018-06-01 15:04:28 -0400720 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
721 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500722 tex->unref();
723 return nullptr;
724 }
725 }
726
Brian Salomond17b4a62017-05-23 16:53:47 -0400727 if (desc.fFlags & kPerformInitialClear_GrSurfaceFlag) {
728 VkClearColorValue zeroClearColor;
729 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
730 VkImageSubresourceRange range;
731 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
732 range.baseArrayLayer = 0;
733 range.baseMipLevel = 0;
734 range.layerCount = 1;
735 range.levelCount = 1;
736 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
737 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400738 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400739 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400740 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -0500741}
742
743////////////////////////////////////////////////////////////////////////////////
744
Greg Daniel6888c0d2017-08-25 11:55:50 -0400745void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
746 VkDeviceSize dstOffset, VkDeviceSize size) {
747 VkBufferCopy copyRegion;
748 copyRegion.srcOffset = srcOffset;
749 copyRegion.dstOffset = dstOffset;
750 copyRegion.size = size;
751 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
752}
753
jvanverthdb379092016-07-07 11:18:46 -0700754bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
755 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700756 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700757 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700758
759 return true;
760}
761
762////////////////////////////////////////////////////////////////////////////////
763
Greg Daniel5254ccc2017-11-13 11:05:52 -0500764static bool check_backend_texture(const GrBackendTexture& backendTex,
765 GrPixelConfig config) {
Greg Daniel52e16d92018-04-10 09:34:07 -0400766 GrVkImageInfo info;
767 if (!backendTex.getVkImageInfo(&info)) {
Brian Salomond17f6582017-07-19 18:28:58 -0400768 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500769 }
770
Greg Daniel52e16d92018-04-10 09:34:07 -0400771 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -0400772 return false;
jvanverthfd359ca2016-03-18 11:57:24 -0700773 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000774
Greg Daniel52e16d92018-04-10 09:34:07 -0400775 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -0400776 return true;
777}
778
779sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400780 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500781 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400782 return nullptr;
783 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500784
Greg Daniel164a9f02016-02-22 09:56:40 -0500785 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -0400786 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000787 surfDesc.fWidth = backendTex.width();
788 surfDesc.fHeight = backendTex.height();
789 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500790 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500791
Greg Daniel52e16d92018-04-10 09:34:07 -0400792 GrVkImageInfo imageInfo;
793 if (!backendTex.getVkImageInfo(&imageInfo)) {
794 return nullptr;
795 }
796 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
797 SkASSERT(layout);
798 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, imageInfo, std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -0400799}
800
801sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -0400802 int sampleCnt,
803 GrWrapOwnership ownership) {
Greg Daniel5254ccc2017-11-13 11:05:52 -0500804 if (!check_backend_texture(backendTex, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -0400805 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -0500806 }
Brian Salomond17f6582017-07-19 18:28:58 -0400807
808 GrSurfaceDesc surfDesc;
809 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
810 surfDesc.fWidth = backendTex.width();
811 surfDesc.fHeight = backendTex.height();
812 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500813 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -0400814
Greg Daniel52e16d92018-04-10 09:34:07 -0400815 GrVkImageInfo imageInfo;
816 if (!backendTex.getVkImageInfo(&imageInfo)) {
817 return nullptr;
818 }
819 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
820 SkASSERT(layout);
821
Brian Salomond17f6582017-07-19 18:28:58 -0400822 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership,
Greg Daniel52e16d92018-04-10 09:34:07 -0400823 imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500824}
825
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400826sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -0400827 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
828 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
829 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
830 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500831 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -0400832 return nullptr;
833 }
halcanary9d524f22016-03-29 09:03:52 -0700834
Greg Daniel323fbcf2018-04-10 13:46:30 -0400835 GrVkImageInfo info;
836 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000837 return nullptr;
838 }
Greg Daniel323fbcf2018-04-10 13:46:30 -0400839
840 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700841 return nullptr;
842 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500843
Greg Daniel164a9f02016-02-22 09:56:40 -0500844 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -0400845 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -0400846 desc.fWidth = backendRT.width();
847 desc.fHeight = backendRT.height();
848 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500849 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -0500850
Greg Daniel323fbcf2018-04-10 13:46:30 -0400851 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -0400852
Greg Daniel323fbcf2018-04-10 13:46:30 -0400853 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -0400854 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -0500855
856 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
857 SkASSERT(!backendRT.stencilBits());
858 if (tgt) {
859 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -0500860 }
Brian Salomonafdc6b12018-03-09 12:02:32 -0500861
Ben Wagnerff134f22018-04-24 16:29:16 -0400862 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -0500863}
864
Greg Daniel7ef28f32017-04-20 16:41:55 +0000865sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000866 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400867
Greg Daniel52e16d92018-04-10 09:34:07 -0400868 GrVkImageInfo imageInfo;
869 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +0000870 return nullptr;
871 }
Greg Daniel52e16d92018-04-10 09:34:07 -0400872 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -0400873 return nullptr;
874 }
875
876 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000877 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000878 desc.fWidth = tex.width();
879 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400880 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500881 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
882 if (!desc.fSampleCnt) {
883 return nullptr;
884 }
Brian Osman33910292017-04-18 14:38:53 -0400885
Greg Daniel52e16d92018-04-10 09:34:07 -0400886 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
887 SkASSERT(layout);
888
Ben Wagnerff134f22018-04-24 16:29:16 -0400889 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -0400890}
891
Brian Salomon930f9392018-06-20 16:25:26 -0400892bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
893 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -0700894 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -0400895 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700896 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -0400897 return false;
jvanverth62340062016-04-26 08:01:44 -0700898 }
899
jvanverth62340062016-04-26 08:01:44 -0700900 // determine if we can blit to and from this format
901 const GrVkCaps& caps = this->vkCaps();
902 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -0700903 !caps.configCanBeSrcofBlit(tex->config(), false) ||
904 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -0400905 return false;
jvanverth62340062016-04-26 08:01:44 -0700906 }
907
egdanielfd016d72016-09-27 12:13:05 -0700908 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
909 this->submitCommandBuffer(kSkip_SyncQueue);
910 }
911
egdaniel7ac5da82016-07-15 13:41:42 -0700912 int width = tex->width();
913 int height = tex->height();
914 VkImageBlit blitRegion;
915 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -0700916
jvanverth82c05582016-05-03 11:19:01 -0700917 // SkMipMap doesn't include the base level in the level count so we have to add 1
918 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -0400919 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -0700920
Greg Danielda86e282018-06-13 09:41:19 -0400921 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -0400922 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
923 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -0700924
jvanverth50c46c72016-05-06 12:31:28 -0700925 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -0400926 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -0700927 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
928 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -0400929 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
930 nullptr, // pNext
931 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
932 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
933 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
934 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
935 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
936 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
937 vkTex->image(), // image
938 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -0700939 };
940
jvanverth62340062016-04-26 08:01:44 -0700941 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -0700942 uint32_t mipLevel = 1;
943 while (mipLevel < levelCount) {
944 int prevWidth = width;
945 int prevHeight = height;
946 width = SkTMax(1, width / 2);
947 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -0700948
jvanverth50c46c72016-05-06 12:31:28 -0700949 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
950 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
951 false, &imageMemoryBarrier);
952
953 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700954 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700955 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700956 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
957 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700958 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -0700959 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -0400960 vkTex->resource(),
961 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -0500962 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -0400963 vkTex->resource(),
964 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -0500965 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -0700966 1,
967 &blitRegion,
968 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -0700969 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -0700970 }
Greg Daniel31cc7312018-03-05 11:41:06 -0500971 // This barrier logically is not needed, but it changes the final level to the same layout as
972 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
973 // future layout changes easier.
974 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
975 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
976 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -0400977 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
978 return true;
jvanverth62340062016-04-26 08:01:44 -0700979}
980
Greg Daniel164a9f02016-02-22 09:56:40 -0500981////////////////////////////////////////////////////////////////////////////////
982
983GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
984 int width,
985 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500986 SkASSERT(width >= rt->width());
987 SkASSERT(height >= rt->height());
988
989 int samples = rt->numStencilSamples();
990
egdaniel8f1dcaa2016-04-01 10:10:45 -0700991 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -0500992
993 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -0500994 width,
995 height,
996 samples,
997 sFmt));
998 fStats.incStencilAttachmentCreates();
999 return stencil;
1000}
1001
1002////////////////////////////////////////////////////////////////////////////////
1003
Brian Salomon52e943a2018-03-13 09:32:39 -04001004bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
1005 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001006 VkDeviceSize size = dstRowBytes * h;
1007 VkDeviceSize offset = bufferOffset;
1008 SkASSERT(size + offset <= alloc.fSize);
1009 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1010 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001011 return false;
1012 }
Greg Daniel81df0412018-05-31 13:13:33 -04001013 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001014
Greg Daniel20ece3a2017-03-28 10:24:43 -04001015 if (srcData) {
1016 // If there is no padding on dst we can do a single memcopy.
1017 // This assumes the srcData comes in with no padding.
1018 SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes),
1019 srcData, srcRowBytes, srcRowBytes, h);
1020 } else {
1021 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1022 // with some data.
1023 if (srcRowBytes == static_cast<size_t>(dstRowBytes)) {
1024 memset(mapPtr, 0, srcRowBytes * h);
1025 } else {
1026 for (int i = 0; i < h; ++i) {
1027 memset(mapPtr, 0, srcRowBytes);
1028 mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes));
1029 }
1030 }
1031 }
Greg Daniel81df0412018-05-31 13:13:33 -04001032 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1033 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001034 return true;
1035}
1036
Brian Salomonf865b052018-03-09 09:01:53 -05001037#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001038bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1039 bool renderable, GrMipMapped mipMapped, const void* srcData,
1040 GrVkImageInfo* info) {
1041 SkASSERT(texturable || renderable);
1042 if (!texturable) {
1043 SkASSERT(GrMipMapped::kNo == mipMapped);
1044 SkASSERT(!srcData);
1045 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001046 VkFormat pixelFormat;
1047 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001048 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001049 }
1050
Brian Salomon52e943a2018-03-13 09:32:39 -04001051 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1052 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001053 }
1054
Brian Salomon52e943a2018-03-13 09:32:39 -04001055 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1056 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001057 }
1058
1059 // Currently we don't support uploading pixel data when mipped.
1060 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001061 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001062 }
1063
Brian Salomon52e943a2018-03-13 09:32:39 -04001064 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001065 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1066 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001067 if (texturable) {
1068 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1069 }
1070 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001071 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1072 }
1073
1074 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001075 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001076 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001077
1078 // Create Image
1079 VkSampleCountFlagBits vkSamples;
1080 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001081 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001082 }
1083
1084 // Figure out the number of mip levels.
1085 uint32_t mipLevels = 1;
1086 if (GrMipMapped::kYes == mipMapped) {
1087 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1088 }
1089
1090 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001091 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1092 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001093 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001094 VK_IMAGE_TYPE_2D, // VkImageType
1095 pixelFormat, // VkFormat
1096 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1097 mipLevels, // mipLevels
1098 1, // arrayLayers
1099 vkSamples, // samples
1100 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1101 usageFlags, // VkImageUsageFlags
1102 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1103 0, // queueFamilyCount
1104 0, // pQueueFamilyIndices
1105 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001106 };
1107
Brian Salomon52e943a2018-03-13 09:32:39 -04001108 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1109 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001110
Brian Salomonde9f5462018-03-07 14:23:58 -05001111 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001112 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001113 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001114 }
1115
1116 // We need to declare these early so that we can delete them at the end outside of the if block.
Greg Daniel8385a8a2018-02-26 13:29:37 -05001117 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001118 VkBuffer buffer = VK_NULL_HANDLE;
1119
1120 VkResult err;
1121 const VkCommandBufferAllocateInfo cmdInfo = {
1122 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1123 nullptr, // pNext
1124 fCmdPool, // commandPool
1125 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1126 1 // bufferCount
1127 };
1128
1129 VkCommandBuffer cmdBuffer;
1130 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1131 if (err) {
1132 GrVkMemory::FreeImageMemory(this, false, alloc);
1133 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001134 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001135 }
1136
1137 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1138 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1139 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1140 cmdBufferBeginInfo.pNext = nullptr;
1141 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1142 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1143
1144 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1145 SkASSERT(!err);
1146
1147 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001148 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001149
Brian Salomonde9f5462018-03-07 14:23:58 -05001150 SkTArray<size_t> individualMipOffsets(mipLevels);
1151 individualMipOffsets.push_back(0);
1152 size_t combinedBufferSize = w * bpp * h;
1153 int currentWidth = w;
1154 int currentHeight = h;
1155 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1156 // config. This works with the assumption that the bytes in pixel config is always a power
1157 // of 2.
1158 SkASSERT((bpp & (bpp - 1)) == 0);
1159 const size_t alignmentMask = 0x3 | (bpp - 1);
1160 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1161 currentWidth = SkTMax(1, currentWidth / 2);
1162 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001163
Brian Salomonde9f5462018-03-07 14:23:58 -05001164 const size_t trimmedSize = currentWidth * bpp * currentHeight;
1165 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1166 if (alignmentDiff != 0) {
1167 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001168 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001169 individualMipOffsets.push_back(combinedBufferSize);
1170 combinedBufferSize += trimmedSize;
1171 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001172
Brian Salomonde9f5462018-03-07 14:23:58 -05001173 VkBufferCreateInfo bufInfo;
1174 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1175 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1176 bufInfo.flags = 0;
1177 bufInfo.size = combinedBufferSize;
1178 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1179 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1180 bufInfo.queueFamilyIndexCount = 0;
1181 bufInfo.pQueueFamilyIndices = nullptr;
1182 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001183
Brian Salomonde9f5462018-03-07 14:23:58 -05001184 if (err) {
1185 GrVkMemory::FreeImageMemory(this, false, alloc);
1186 VK_CALL(DestroyImage(fDevice, image, nullptr));
1187 VK_CALL(EndCommandBuffer(cmdBuffer));
1188 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001189 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001190 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001191
Brian Salomonde9f5462018-03-07 14:23:58 -05001192 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1193 &bufferAlloc)) {
1194 GrVkMemory::FreeImageMemory(this, false, alloc);
1195 VK_CALL(DestroyImage(fDevice, image, nullptr));
1196 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1197 VK_CALL(EndCommandBuffer(cmdBuffer));
1198 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001199 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001200 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001201
Brian Salomonde9f5462018-03-07 14:23:58 -05001202 currentWidth = w;
1203 currentHeight = h;
1204 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1205 SkASSERT(0 == currentMipLevel || !srcData);
1206 size_t currentRowBytes = bpp * currentWidth;
1207 size_t bufferOffset = individualMipOffsets[currentMipLevel];
1208 if (!copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1209 currentRowBytes, currentHeight)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001210 GrVkMemory::FreeImageMemory(this, false, alloc);
1211 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001212 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001213 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1214 VK_CALL(EndCommandBuffer(cmdBuffer));
1215 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001216 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001217 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001218 currentWidth = SkTMax(1, currentWidth / 2);
1219 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001220 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001221
1222 // Set image layout and add barrier
1223 VkImageMemoryBarrier barrier;
1224 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1225 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1226 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001227 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001228 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1229 barrier.oldLayout = initialLayout;
1230 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1231 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1232 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1233 barrier.image = image;
1234 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1235
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001236 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001237 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1238 &barrier));
1239 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1240
1241 SkTArray<VkBufferImageCopy> regions(mipLevels);
1242
1243 currentWidth = w;
1244 currentHeight = h;
1245 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1246 // Submit copy command
1247 VkBufferImageCopy& region = regions.push_back();
1248 memset(&region, 0, sizeof(VkBufferImageCopy));
1249 region.bufferOffset = individualMipOffsets[currentMipLevel];
1250 region.bufferRowLength = currentWidth;
1251 region.bufferImageHeight = currentHeight;
1252 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1253 region.imageOffset = {0, 0, 0};
1254 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1255 currentWidth = SkTMax(1, currentWidth / 2);
1256 currentHeight = SkTMax(1, currentHeight / 2);
1257 }
1258
1259 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1260 regions.begin()));
1261
Brian Salomon52e943a2018-03-13 09:32:39 -04001262 if (texturable) {
1263 // Change Image layout to shader read since if we use this texture as a borrowed textures
1264 // within Ganesh we require that its layout be set to that
1265 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1266 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1267 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001268 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001269 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1270 barrier.oldLayout = initialLayout;
1271 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1272 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1273 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1274 barrier.image = image;
1275 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001276 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001277 GrVkImage::LayoutToPipelineStageFlags(initialLayout),
Brian Salomon52e943a2018-03-13 09:32:39 -04001278 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1279 0,
1280 0, nullptr,
1281 0, nullptr,
1282 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001283 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001284 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001285
1286 // End CommandBuffer
1287 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1288 SkASSERT(!err);
1289
1290 // Create Fence for queue
1291 VkFence fence;
1292 VkFenceCreateInfo fenceInfo;
1293 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1294 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1295
1296 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1297 SkASSERT(!err);
1298
1299 VkSubmitInfo submitInfo;
1300 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1301 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1302 submitInfo.pNext = nullptr;
1303 submitInfo.waitSemaphoreCount = 0;
1304 submitInfo.pWaitSemaphores = nullptr;
1305 submitInfo.pWaitDstStageMask = 0;
1306 submitInfo.commandBufferCount = 1;
1307 submitInfo.pCommandBuffers = &cmdBuffer;
1308 submitInfo.signalSemaphoreCount = 0;
1309 submitInfo.pSignalSemaphores = nullptr;
1310 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1311 SkASSERT(!err);
1312
1313 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1314 if (VK_TIMEOUT == err) {
1315 GrVkMemory::FreeImageMemory(this, false, alloc);
1316 VK_CALL(DestroyImage(fDevice, image, nullptr));
1317 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1318 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1319 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1320 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1321 SkDebugf("Fence failed to signal: %d\n", err);
1322 SK_ABORT("failing");
1323 }
1324 SkASSERT(!err);
1325
1326 // Clean up transfer resources
1327 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1328 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1329 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1330 }
1331 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1332 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1333
Brian Salomon52e943a2018-03-13 09:32:39 -04001334 info->fImage = image;
1335 info->fAlloc = alloc;
1336 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1337 info->fImageLayout = initialLayout;
1338 info->fFormat = pixelFormat;
1339 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001340
Brian Salomon52e943a2018-03-13 09:32:39 -04001341 return true;
1342}
1343
1344GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
1345 GrPixelConfig config, bool isRenderTarget,
1346 GrMipMapped mipMapped) {
Brian Salomon8a375832018-03-14 10:21:40 -04001347 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001348
1349 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1350 return GrBackendTexture();
1351 }
1352
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001353 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001354 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
1355 &info)) {
1356 return {};
1357 }
Greg Daniel108bb232018-07-03 16:18:29 -04001358 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1359 // Lots of tests don't go through Skia's public interface which will set the config so for
1360 // testing we make sure we set a config here.
1361 beTex.setPixelConfig(config);
1362 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001363}
1364
1365bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
1366 SkASSERT(kVulkan_GrBackend == tex.fBackend);
1367
Greg Daniel52e16d92018-04-10 09:34:07 -04001368 GrVkImageInfo backend;
1369 if (!tex.getVkImageInfo(&backend)) {
1370 return false;
1371 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001372
Greg Daniel52e16d92018-04-10 09:34:07 -04001373 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001374 VkMemoryRequirements req;
1375 memset(&req, 0, sizeof(req));
1376 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001377 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001378 &req));
1379 // TODO: find a better check
1380 // This will probably fail with a different driver
1381 return (req.size > 0) && (req.size <= 8192 * 8192);
1382 }
1383
1384 return false;
1385}
1386
Brian Salomon26102cb2018-03-09 09:33:19 -05001387void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
1388 SkASSERT(kVulkan_GrBackend == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001389
Greg Daniel52e16d92018-04-10 09:34:07 -04001390 GrVkImageInfo info;
1391 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001392 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001393 }
1394}
1395
Brian Salomon52e943a2018-03-13 09:32:39 -04001396GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct,
1397 GrSRGBEncoded srgbEncoded) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001398 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1399 return GrBackendRenderTarget();
1400 }
1401
Brian Salomon8a375832018-03-14 10:21:40 -04001402 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001403 GrVkImageInfo info;
1404 auto config = GrColorTypeToPixelConfig(ct, srgbEncoded);
1405 if (kUnknown_GrPixelConfig == config) {
1406 return {};
1407 }
1408 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr,
1409 &info)) {
1410 return {};
1411 }
Greg Daniel108bb232018-07-03 16:18:29 -04001412 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1413 // Lots of tests don't go through Skia's public interface which will set the config so for
1414 // testing we make sure we set a config here.
1415 beRT.setPixelConfig(config);
1416 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001417}
1418
Brian Salomon52e943a2018-03-13 09:32:39 -04001419void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
1420 SkASSERT(kVulkan_GrBackend == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001421
Greg Daniel323fbcf2018-04-10 13:46:30 -04001422 GrVkImageInfo info;
1423 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001424 // something in the command buffer may still be using this, so force submit
1425 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001426 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001427 }
1428}
Brian Salomonf865b052018-03-09 09:01:53 -05001429
Greg Daniel26b50a42018-03-08 09:49:58 -05001430void GrVkGpu::testingOnly_flushGpuAndSync() {
1431 this->submitCommandBuffer(kForce_SyncQueue);
1432}
Brian Salomonf865b052018-03-09 09:01:53 -05001433#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001434
Greg Daniel164a9f02016-02-22 09:56:40 -05001435////////////////////////////////////////////////////////////////////////////////
1436
1437void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1438 VkPipelineStageFlags dstStageMask,
1439 bool byRegion,
1440 VkMemoryBarrier* barrier) const {
1441 SkASSERT(fCurrentCmdBuffer);
1442 fCurrentCmdBuffer->pipelineBarrier(this,
1443 srcStageMask,
1444 dstStageMask,
1445 byRegion,
1446 GrVkCommandBuffer::kMemory_BarrierType,
1447 barrier);
1448}
1449
1450void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1451 VkPipelineStageFlags dstStageMask,
1452 bool byRegion,
1453 VkBufferMemoryBarrier* barrier) const {
1454 SkASSERT(fCurrentCmdBuffer);
1455 fCurrentCmdBuffer->pipelineBarrier(this,
1456 srcStageMask,
1457 dstStageMask,
1458 byRegion,
1459 GrVkCommandBuffer::kBufferMemory_BarrierType,
1460 barrier);
1461}
1462
1463void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1464 VkPipelineStageFlags dstStageMask,
1465 bool byRegion,
1466 VkImageMemoryBarrier* barrier) const {
1467 SkASSERT(fCurrentCmdBuffer);
1468 fCurrentCmdBuffer->pipelineBarrier(this,
1469 srcStageMask,
1470 dstStageMask,
1471 byRegion,
1472 GrVkCommandBuffer::kImageMemory_BarrierType,
1473 barrier);
1474}
1475
Greg Daniel51316782017-08-02 15:10:09 +00001476void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1477 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1478 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001479 this->submitCommandBuffer(kSkip_SyncQueue);
1480}
1481
Robert Phillips95214472017-08-08 18:00:03 -04001482void GrVkGpu::clearStencil(GrRenderTarget* target, int clearValue) {
1483 if (!target) {
egdaniel3d5d9ac2016-03-01 12:56:15 -08001484 return;
1485 }
1486 GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
1487 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
1488
1489
1490 VkClearDepthStencilValue vkStencilColor;
Robert Phillips95214472017-08-08 18:00:03 -04001491 vkStencilColor.depth = 0.0f;
1492 vkStencilColor.stencil = clearValue;
egdaniel3d5d9ac2016-03-01 12:56:15 -08001493
egdaniel3d5d9ac2016-03-01 12:56:15 -08001494 vkStencil->setImageLayout(this,
1495 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001496 VK_ACCESS_TRANSFER_WRITE_BIT,
1497 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel3d5d9ac2016-03-01 12:56:15 -08001498 false);
1499
egdaniel3d5d9ac2016-03-01 12:56:15 -08001500 VkImageSubresourceRange subRange;
1501 memset(&subRange, 0, sizeof(VkImageSubresourceRange));
1502 subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1503 subRange.baseMipLevel = 0;
1504 subRange.levelCount = 1;
1505 subRange.baseArrayLayer = 0;
1506 subRange.layerCount = 1;
1507
1508 // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
1509 // draw. Thus we should look into using the load op functions on the render pass to clear out
1510 // the stencil there.
1511 fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
1512}
1513
Greg Daniel25af6712018-04-25 10:44:38 -04001514static int get_surface_sample_cnt(GrSurface* surf) {
1515 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1516 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001517 }
Greg Daniel25af6712018-04-25 10:44:38 -04001518 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001519}
1520
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001521void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1522 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001523 GrVkImage* dstImage,
1524 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001525 const SkIRect& srcRect,
1526 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001527#ifdef SK_DEBUG
1528 int dstSampleCnt = get_surface_sample_cnt(dst);
1529 int srcSampleCnt = get_surface_sample_cnt(src);
1530 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1531 src->config(), srcSampleCnt, srcOrigin));
1532
1533#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001534
Greg Daniel164a9f02016-02-22 09:56:40 -05001535 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1536 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001537 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001538 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1539 VK_ACCESS_TRANSFER_WRITE_BIT,
1540 VK_PIPELINE_STAGE_TRANSFER_BIT,
1541 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001542
egdaniel17b89252016-04-05 07:23:38 -07001543 srcImage->setImageLayout(this,
1544 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001545 VK_ACCESS_TRANSFER_READ_BIT,
1546 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001547 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001548
1549 // Flip rect if necessary
1550 SkIRect srcVkRect = srcRect;
1551 int32_t dstY = dstPoint.fY;
1552
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001553 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1554 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001555 srcVkRect.fTop = src->height() - srcRect.fBottom;
1556 srcVkRect.fBottom = src->height() - srcRect.fTop;
1557 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1558 }
1559
1560 VkImageCopy copyRegion;
1561 memset(&copyRegion, 0, sizeof(VkImageCopy));
1562 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1563 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1564 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1565 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001566 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001567
1568 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001569 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001570 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001571 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001572 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1573 1,
1574 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001575
1576 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1577 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001578 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001579}
1580
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001581void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1582 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001583 GrVkImage* dstImage,
1584 GrVkImage* srcImage,
1585 const SkIRect& srcRect,
1586 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001587#ifdef SK_DEBUG
1588 int dstSampleCnt = get_surface_sample_cnt(dst);
1589 int srcSampleCnt = get_surface_sample_cnt(src);
1590 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1591 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001592
Greg Daniel25af6712018-04-25 10:44:38 -04001593#endif
egdaniel17b89252016-04-05 07:23:38 -07001594 dstImage->setImageLayout(this,
1595 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001596 VK_ACCESS_TRANSFER_WRITE_BIT,
1597 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001598 false);
1599
egdaniel17b89252016-04-05 07:23:38 -07001600 srcImage->setImageLayout(this,
1601 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001602 VK_ACCESS_TRANSFER_READ_BIT,
1603 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001604 false);
1605
1606 // Flip rect if necessary
1607 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001608 srcVkRect.fLeft = srcRect.fLeft;
1609 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001610 SkIRect dstRect;
1611 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001612 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001613
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001614 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001615 srcVkRect.fTop = src->height() - srcRect.fBottom;
1616 srcVkRect.fBottom = src->height() - srcRect.fTop;
1617 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001618 srcVkRect.fTop = srcRect.fTop;
1619 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001620 }
1621
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001622 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001623 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1624 } else {
1625 dstRect.fTop = dstPoint.fY;
1626 }
1627 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1628
1629 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1630 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001631 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001632 using std::swap;
1633 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001634 }
1635
1636 VkImageBlit blitRegion;
1637 memset(&blitRegion, 0, sizeof(VkImageBlit));
1638 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1639 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001640 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001641 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1642 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001643 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001644
1645 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001646 *srcImage,
1647 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001648 1,
1649 &blitRegion,
1650 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001651
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001652 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001653 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001654}
1655
Brian Salomon1fabd512018-02-09 09:54:25 -05001656void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1657 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1658 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001659 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001660 SkIRect srcRect = origSrcRect;
1661 SkIPoint dstPoint = origDstPoint;
1662 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1663 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1664 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1665 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1666 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1667 }
1668 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001669 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1670 srcRect.width(), srcRect.height());
1671 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001672}
1673
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001674bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1675 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001676 const SkIRect& srcRect, const SkIPoint& dstPoint,
1677 bool canDiscardOutsideDstRect) {
Greg Daniel25af6712018-04-25 10:44:38 -04001678 GrPixelConfig dstConfig = dst->config();
1679 GrPixelConfig srcConfig = src->config();
1680
1681 int dstSampleCnt = get_surface_sample_cnt(dst);
1682 int srcSampleCnt = get_surface_sample_cnt(src);
1683
1684 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1685 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001686 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001687 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001688 }
1689
egdanielfd016d72016-09-27 12:13:05 -07001690 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1691 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
1692 }
1693
Greg Daniel25af6712018-04-25 10:44:38 -04001694 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1695 srcConfig, SkToBool(src->asTexture()))) {
1696 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1697 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001698 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1699 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001700 return true;
1701 }
1702
egdaniel17b89252016-04-05 07:23:38 -07001703 GrVkImage* dstImage;
1704 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001705 GrRenderTarget* dstRT = dst->asRenderTarget();
1706 if (dstRT) {
1707 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1708 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1709 } else {
1710 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001711 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001712 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001713 GrRenderTarget* srcRT = src->asRenderTarget();
1714 if (srcRT) {
1715 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1716 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001717 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001718 SkASSERT(src->asTexture());
1719 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001720 }
1721
Greg Daniel25af6712018-04-25 10:44:38 -04001722 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
1723 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001724 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1725 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07001726 return true;
1727 }
1728
Greg Daniel25af6712018-04-25 10:44:38 -04001729 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
1730 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001731 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
1732 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001733 return true;
1734 }
1735
Greg Daniel164a9f02016-02-22 09:56:40 -05001736 return false;
1737}
1738
Brian Salomona6948702018-06-01 15:33:20 -04001739bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
1740 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05001741 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001742 return false;
1743 }
1744
egdaniel66933552016-08-24 07:22:19 -07001745 GrVkImage* image = nullptr;
1746 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1747 if (rt) {
1748 // resolve the render target if necessary
1749 switch (rt->getResolveType()) {
1750 case GrVkRenderTarget::kCantResolve_ResolveType:
1751 return false;
1752 case GrVkRenderTarget::kAutoResolves_ResolveType:
1753 break;
1754 case GrVkRenderTarget::kCanResolve_ResolveType:
Brian Salomon1fabd512018-02-09 09:54:25 -05001755 this->internalResolveRenderTarget(rt, false);
egdaniel66933552016-08-24 07:22:19 -07001756 break;
1757 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04001758 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07001759 }
1760 image = rt;
1761 } else {
1762 image = static_cast<GrVkTexture*>(surface->asTexture());
1763 }
1764
1765 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001766 return false;
1767 }
1768
1769 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001770 image->setImageLayout(this,
1771 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1772 VK_ACCESS_TRANSFER_READ_BIT,
1773 VK_PIPELINE_STAGE_TRANSFER_BIT,
1774 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001775
Brian Salomonc320b152018-02-20 14:05:36 -05001776 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07001777 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05001778
Greg Daniel164a9f02016-02-22 09:56:40 -05001779 VkBufferImageCopy region;
1780 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001781
1782 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1783 if (copyFromOrigin) {
1784 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04001785 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07001786 } else {
Brian Salomona6948702018-06-01 15:33:20 -04001787 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07001788 region.imageOffset = offset;
1789 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1790 }
1791
1792 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04001793 size_t imageRows = region.imageExtent.height;
egdaniel6fa0a912016-09-12 11:51:29 -07001794 GrVkTransferBuffer* transferBuffer =
Greg Daniel3cdfa092018-02-26 16:14:10 -05001795 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
egdaniel6fa0a912016-09-12 11:51:29 -07001796 kXferGpuToCpu_GrBufferType,
1797 kStream_GrAccessPattern));
1798
1799 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001800 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001801 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001802 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1803 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001804
1805 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001806 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001807 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1808 transferBuffer,
1809 1,
1810 &region);
1811
1812 // make sure the copy to buffer has finished
1813 transferBuffer->addMemoryBarrier(this,
1814 VK_ACCESS_TRANSFER_WRITE_BIT,
1815 VK_ACCESS_HOST_READ_BIT,
1816 VK_PIPELINE_STAGE_TRANSFER_BIT,
1817 VK_PIPELINE_STAGE_HOST_BIT,
1818 false);
1819
1820 // We need to submit the current command buffer to the Queue and make sure it finishes before
1821 // we can copy the data out of the buffer.
1822 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00001823 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05001824 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04001825 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05001826
egdaniel6fa0a912016-09-12 11:51:29 -07001827 if (copyFromOrigin) {
1828 uint32_t skipRows = region.imageExtent.height - height;
1829 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1830 }
1831
Brian Salomona6948702018-06-01 15:33:20 -04001832 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05001833
1834 transferBuffer->unmap();
1835 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001836 return true;
1837}
egdaniel066df7c2016-06-08 14:02:27 -07001838
egdaniel27bb2842016-07-07 11:58:35 -07001839// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1840// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1841// the the entire attachment. Similar requirements for the y and height components.
1842void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1843 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1844 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001845 if ((0 != granularity.width && 1 != granularity.width)) {
1846 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1847 int rightAdj = srcBounds.fRight % granularity.width;
1848 if (rightAdj != 0) {
1849 rightAdj = granularity.width - rightAdj;
1850 }
1851 dstBounds->fRight = srcBounds.fRight + rightAdj;
1852 if (dstBounds->fRight > maxWidth) {
1853 dstBounds->fRight = maxWidth;
1854 dstBounds->fLeft = 0;
1855 } else {
1856 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1857 }
egdaniel27bb2842016-07-07 11:58:35 -07001858 } else {
egdanield5797b32016-09-20 12:57:45 -07001859 dstBounds->fLeft = srcBounds.fLeft;
1860 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07001861 }
1862
1863 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07001864 if ((0 != granularity.height && 1 != granularity.height)) {
1865 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
1866 int bottomAdj = srcBounds.fBottom % granularity.height;
1867 if (bottomAdj != 0) {
1868 bottomAdj = granularity.height - bottomAdj;
1869 }
1870 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
1871 if (dstBounds->fBottom > maxHeight) {
1872 dstBounds->fBottom = maxHeight;
1873 dstBounds->fTop = 0;
1874 } else {
1875 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
1876 }
egdaniel27bb2842016-07-07 11:58:35 -07001877 } else {
egdanield5797b32016-09-20 12:57:45 -07001878 dstBounds->fTop = srcBounds.fTop;
1879 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07001880 }
1881}
1882
Greg Daniel22bc8652017-03-22 15:45:43 -04001883void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07001884 const GrVkRenderPass* renderPass,
1885 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001886 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07001887 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07001888 const SkIRect* pBounds = &bounds;
1889 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001890 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07001891 flippedBounds = bounds;
1892 flippedBounds.fTop = target->height() - bounds.fBottom;
1893 flippedBounds.fBottom = target->height() - bounds.fTop;
1894 pBounds = &flippedBounds;
1895 }
1896
egdaniel27bb2842016-07-07 11:58:35 -07001897 // The bounds we use for the render pass should be of the granularity supported
1898 // by the device.
1899 const VkExtent2D& granularity = renderPass->granularity();
1900 SkIRect adjustedBounds;
1901 if ((0 != granularity.width && 1 != granularity.width) ||
1902 (0 != granularity.height && 1 != granularity.height)) {
1903 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
1904 target->width(), target->height());
1905 pBounds = &adjustedBounds;
1906 }
1907
Robert Phillips95214472017-08-08 18:00:03 -04001908#ifdef SK_DEBUG
1909 uint32_t index;
1910 bool result = renderPass->colorAttachmentIndex(&index);
1911 SkASSERT(result && 0 == index);
1912 result = renderPass->stencilAttachmentIndex(&index);
1913 if (result) {
1914 SkASSERT(1 == index);
1915 }
1916#endif
1917 VkClearValue clears[2];
1918 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04001919 clears[1].depthStencil.depth = 0.0f;
1920 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04001921
1922 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04001923 for (int i = 0; i < buffers.count(); ++i) {
1924 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
1925 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001926 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07001927
Brian Salomon1fabd512018-02-09 09:54:25 -05001928 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05001929}
egdaniel9cb63402016-06-23 08:37:05 -07001930
Greg Daniel6be35232017-03-01 17:01:09 -05001931GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07001932 VkFenceCreateInfo createInfo;
1933 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
1934 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1935 createInfo.pNext = nullptr;
1936 createInfo.flags = 0;
1937 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05001938
1939 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
1940 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
1941
1942 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07001943 return (GrFence)fence;
1944}
1945
Greg Daniel6be35232017-03-01 17:01:09 -05001946bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
1947 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
1948
1949 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07001950 return (VK_SUCCESS == result);
1951}
1952
1953void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05001954 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
1955}
1956
Greg Daniela5cb7812017-06-16 09:45:32 -04001957sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
1958 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05001959}
1960
Greg Daniel48661b82018-01-22 16:11:35 -05001961sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
1962 GrResourceProvider::SemaphoreWrapType wrapType,
1963 GrWrapOwnership ownership) {
1964 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04001965}
1966
Greg Daniel48661b82018-01-22 16:11:35 -05001967void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) {
Greg Daniel6be35232017-03-01 17:01:09 -05001968 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
1969
Greg Daniel48661b82018-01-22 16:11:35 -05001970 GrVkSemaphore::Resource* resource = vkSem->getResource();
1971 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05001972 resource->ref();
1973 fSemaphoresToSignal.push_back(resource);
1974 }
Greg Daniela5cb7812017-06-16 09:45:32 -04001975
1976 if (flush) {
1977 this->submitCommandBuffer(kSkip_SyncQueue);
1978 }
Greg Daniel6be35232017-03-01 17:01:09 -05001979}
1980
Greg Daniel48661b82018-01-22 16:11:35 -05001981void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05001982 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
1983
Greg Daniel48661b82018-01-22 16:11:35 -05001984 GrVkSemaphore::Resource* resource = vkSem->getResource();
1985 if (resource->shouldWait()) {
1986 resource->ref();
1987 fSemaphoresToWaitOn.push_back(resource);
1988 }
jvanverth84741b32016-09-30 08:39:02 -07001989}
Brian Osman13dddce2017-05-09 13:19:50 -04001990
1991sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
1992 SkASSERT(texture);
1993 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
1994 vkTexture->setImageLayout(this,
1995 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1996 VK_ACCESS_SHADER_READ_BIT,
1997 VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
1998 false);
1999 this->submitCommandBuffer(kSkip_SyncQueue);
2000
2001 // The image layout change serves as a barrier, so no semaphore is needed
2002 return nullptr;
2003}
Greg Danielf5d87582017-12-18 14:48:15 -05002004