blob: 1eec506a4248a69d82a4a18127cfe0baeb4c075b [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 Daniel7ef28f32017-04-20 16:41:55 +000010#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050011#include "GrContextOptions.h"
12#include "GrGeometryProcessor.h"
13#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070014#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050015#include "GrPipeline.h"
16#include "GrRenderTargetPriv.h"
17#include "GrSurfacePriv.h"
18#include "GrTexturePriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019
20#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"
24#include "GrVkMemory.h"
25#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070026#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050027#include "GrVkRenderPass.h"
28#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050029#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050030#include "GrVkTexture.h"
31#include "GrVkTextureRenderTarget.h"
32#include "GrVkTransferBuffer.h"
33#include "GrVkVertexBuffer.h"
34
Matt Sarett485c4992017-02-14 14:18:27 -050035#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070036#include "SkMipMap.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050037
38#include "vk/GrVkInterface.h"
jvanverthfd359ca2016-03-18 11:57:24 -070039#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050040
ethannicholasb3058bd2016-07-01 08:22:01 -070041#include "SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070042
Forrest Reiling44f85712017-03-27 23:22:20 -070043#if !defined(SK_BUILD_FOR_WIN)
44#include <unistd.h>
45#endif // !defined(SK_BUILD_FOR_WIN)
46
Greg Daniel164a9f02016-02-22 09:56:40 -050047#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
48#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
49#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
50
egdaniel735109c2016-07-27 08:03:57 -070051#ifdef SK_ENABLE_VK_LAYERS
jvanverthd2497f32016-03-18 12:39:05 -070052VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(
53 VkDebugReportFlagsEXT flags,
54 VkDebugReportObjectTypeEXT objectType,
55 uint64_t object,
56 size_t location,
57 int32_t messageCode,
58 const char* pLayerPrefix,
59 const char* pMessage,
60 void* pUserData) {
61 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
62 SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
Jim Van Verthce3fe232016-11-11 09:41:04 -080063 return VK_TRUE; // skip further layers
jvanverthd2497f32016-03-18 12:39:05 -070064 } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
65 SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
66 } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
67 SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
68 } else {
69 SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
70 }
71 return VK_FALSE;
72}
jvanverthd2497f32016-03-18 12:39:05 -070073#endif
74
jvanverth633b3562016-03-23 11:01:22 -070075GrGpu* GrVkGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
76 GrContext* context) {
bsalomondc0fcd42016-04-11 14:21:33 -070077 const GrVkBackendContext* vkBackendContext =
78 reinterpret_cast<const GrVkBackendContext*>(backendContext);
jvanverth633b3562016-03-23 11:01:22 -070079 if (!vkBackendContext) {
Brian Salomonc1889822017-04-24 16:49:05 -040080 return nullptr;
jvanverth633b3562016-03-23 11:01:22 -070081 } else {
82 vkBackendContext->ref();
Greg Daniel164a9f02016-02-22 09:56:40 -050083 }
84
Greg Danielfe2965a2016-10-11 16:31:05 -040085 if (!vkBackendContext->fInterface->validate(vkBackendContext->fExtensions)) {
86 return nullptr;
87 }
88
jvanverth633b3562016-03-23 11:01:22 -070089 return new GrVkGpu(context, options, vkBackendContext);
Greg Daniel164a9f02016-02-22 09:56:40 -050090}
91
92////////////////////////////////////////////////////////////////////////////////
93
halcanary9d524f22016-03-29 09:03:52 -070094GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
jvanverth633b3562016-03-23 11:01:22 -070095 const GrVkBackendContext* backendCtx)
Greg Daniel164a9f02016-02-22 09:56:40 -050096 : INHERITED(context)
jvanverth633b3562016-03-23 11:01:22 -070097 , fDevice(backendCtx->fDevice)
98 , fQueue(backendCtx->fQueue)
99 , fResourceProvider(this) {
100 fBackendContext.reset(backendCtx);
Greg Daniel164a9f02016-02-22 09:56:40 -0500101
egdaniel735109c2016-07-27 08:03:57 -0700102#ifdef SK_ENABLE_VK_LAYERS
brianosman419ca642016-05-04 08:19:44 -0700103 fCallback = VK_NULL_HANDLE;
jvanverthfd7bd452016-03-25 06:29:52 -0700104 if (backendCtx->fExtensions & kEXT_debug_report_GrVkExtensionFlag) {
105 // Setup callback creation information
jvanverthd2497f32016-03-18 12:39:05 -0700106 VkDebugReportCallbackCreateInfoEXT callbackCreateInfo;
107 callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
108 callbackCreateInfo.pNext = nullptr;
109 callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
egdanielef0c10c2016-04-07 07:51:22 -0700110 VK_DEBUG_REPORT_WARNING_BIT_EXT |
jvanverthd2497f32016-03-18 12:39:05 -0700111 //VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
112 //VK_DEBUG_REPORT_DEBUG_BIT_EXT |
egdanielb4aa3622016-04-06 13:47:08 -0700113 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
jvanverthd2497f32016-03-18 12:39:05 -0700114 callbackCreateInfo.pfnCallback = &DebugReportCallback;
115 callbackCreateInfo.pUserData = nullptr;
116
jvanverthfd7bd452016-03-25 06:29:52 -0700117 // Register the callback
jvanvertha00980e2016-05-02 13:24:48 -0700118 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateDebugReportCallbackEXT(
119 backendCtx->fInstance, &callbackCreateInfo, nullptr, &fCallback));
jvanverthd2497f32016-03-18 12:39:05 -0700120 }
121#endif
jvanverth633b3562016-03-23 11:01:22 -0700122
ethannicholasb3058bd2016-07-01 08:22:01 -0700123 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700124
jvanverthfd7bd452016-03-25 06:29:52 -0700125 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice,
egdanielc5ec1402016-03-28 12:14:42 -0700126 backendCtx->fFeatures, backendCtx->fExtensions));
jvanverth633b3562016-03-23 11:01:22 -0700127 fCaps.reset(SkRef(fVkCaps.get()));
128
129 VK_CALL(GetPhysicalDeviceMemoryProperties(backendCtx->fPhysicalDevice, &fPhysDevMemProps));
130
131 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700132 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
133 nullptr, // pNext
134 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
135 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
136 backendCtx->fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700137 };
halcanary9d524f22016-03-29 09:03:52 -0700138 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700139 &fCmdPool));
140
141 // must call this after creating the CommandPool
142 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700143 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700144 SkASSERT(fCurrentCmdBuffer);
145 fCurrentCmdBuffer->begin(this);
jvanverth6b6ffc42016-06-13 14:28:07 -0700146
147 // set up our heaps
148 fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
Greg Daniel4f7d97c2017-04-26 15:10:00 -0400149 fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 64*1024*1024));
jvanverth6b6ffc42016-06-13 14:28:07 -0700150 fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024));
151 fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
152 fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
jvanverth4c6e47a2016-07-22 10:34:52 -0700153 fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024));
jvanverth6b6ffc42016-06-13 14:28:07 -0700154 fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
155 fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
Greg Daniel164a9f02016-02-22 09:56:40 -0500156}
157
158GrVkGpu::~GrVkGpu() {
Greg Daniel164a9f02016-02-22 09:56:40 -0500159 fCurrentCmdBuffer->end(this);
160 fCurrentCmdBuffer->unref(this);
161
162 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700163 fResourceProvider.checkCommandBuffers();
Jim Van Verth09557d72016-11-07 11:10:21 -0500164 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700165
166 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
167 // on the command buffers even though they have completed. This causes an assert to fire when
168 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500169 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700170#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500171 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700172#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500173 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700174#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500175 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700176#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500177 }
egdanielf8c2be32016-06-24 13:18:27 -0700178#endif
179
egdanielbe9d8212016-09-20 08:54:23 -0700180#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400181 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700182#endif
halcanary9d524f22016-03-29 09:03:52 -0700183
Greg Daniel6be35232017-03-01 17:01:09 -0500184 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
185 fSemaphoresToWaitOn[i]->unref(this);
186 }
187 fSemaphoresToWaitOn.reset();
188
egdanielbc9b2962016-09-27 08:00:53 -0700189 fCopyManager.destroyResources(this);
190
Jim Van Verth09557d72016-11-07 11:10:21 -0500191 // must call this just before we destroy the command pool and VkDevice
192 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500193
jvanverth633b3562016-03-23 11:01:22 -0700194 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
195
ethannicholasb3058bd2016-07-01 08:22:01 -0700196 delete fCompiler;
jvanverth633b3562016-03-23 11:01:22 -0700197
egdaniel735109c2016-07-27 08:03:57 -0700198#ifdef SK_ENABLE_VK_LAYERS
jvanvertha00980e2016-05-02 13:24:48 -0700199 if (fCallback) {
200 VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr));
brianosman419ca642016-05-04 08:19:44 -0700201 fCallback = VK_NULL_HANDLE;
jvanvertha00980e2016-05-02 13:24:48 -0700202 }
jvanverthd2497f32016-03-18 12:39:05 -0700203#endif
Greg Daniel164a9f02016-02-22 09:56:40 -0500204}
205
206///////////////////////////////////////////////////////////////////////////////
207
egdaniel9cb63402016-06-23 08:37:05 -0700208GrGpuCommandBuffer* GrVkGpu::createCommandBuffer(
egdaniel9cb63402016-06-23 08:37:05 -0700209 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
210 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
Brian Salomonc293a292016-11-30 13:38:32 -0500211 return new GrVkGpuCommandBuffer(this, colorInfo, stencilInfo);
egdaniel066df7c2016-06-08 14:02:27 -0700212}
213
Greg Daniel6be35232017-03-01 17:01:09 -0500214void GrVkGpu::submitCommandBuffer(SyncQueue sync,
215 const GrVkSemaphore::Resource* signalSemaphore) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500216 SkASSERT(fCurrentCmdBuffer);
217 fCurrentCmdBuffer->end(this);
218
Greg Daniel6be35232017-03-01 17:01:09 -0500219 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, signalSemaphore, fSemaphoresToWaitOn);
220
221 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
222 fSemaphoresToWaitOn[i]->unref(this);
223 }
224 fSemaphoresToWaitOn.reset();
225
Greg Daniel164a9f02016-02-22 09:56:40 -0500226 fResourceProvider.checkCommandBuffers();
227
228 // Release old command buffer and create a new one
229 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700230 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500231 SkASSERT(fCurrentCmdBuffer);
232
233 fCurrentCmdBuffer->begin(this);
234}
235
236///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700237GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
238 const void* data) {
239 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700240 switch (type) {
241 case kVertex_GrBufferType:
242 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
243 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700244 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700245 break;
cdalton397536c2016-03-25 12:15:03 -0700246 case kIndex_GrBufferType:
247 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
248 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700249 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700250 break;
cdalton397536c2016-03-25 12:15:03 -0700251 case kXferCpuToGpu_GrBufferType:
jvanverthc3d706f2016-04-20 10:33:27 -0700252 SkASSERT(kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700253 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700254 break;
cdalton397536c2016-03-25 12:15:03 -0700255 case kXferGpuToCpu_GrBufferType:
jvanverthc3d706f2016-04-20 10:33:27 -0700256 SkASSERT(kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700257 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700258 break;
cdalton397536c2016-03-25 12:15:03 -0700259 default:
260 SkFAIL("Unknown buffer type.");
261 return nullptr;
262 }
cdalton1bf3e712016-04-19 10:00:02 -0700263 if (data && buff) {
264 buff->updateData(data, size);
265 }
266 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500267}
268
269////////////////////////////////////////////////////////////////////////////////
270bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
271 GrPixelConfig srcConfig, DrawPreference* drawPreference,
272 WritePixelTempDrawInfo* tempDrawInfo) {
Brian Osmand0be1ef2017-01-11 16:57:15 -0500273 if (GrPixelConfigIsCompressed(dstSurface->config())) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500274 return false;
275 }
276
egdaniel4583ec52016-06-27 12:57:00 -0700277 GrRenderTarget* renderTarget = dstSurface->asRenderTarget();
278
279 // Start off assuming no swizzling
280 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
281 tempDrawInfo->fWriteConfig = srcConfig;
282
283 // These settings we will always want if a temp draw is performed. Initially set the config
284 // to srcConfig, though that may be modified if we decide to do a R/B swap
285 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
286 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
287 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
288 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
289 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
290 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
291
egdanield66110f2016-06-28 13:38:26 -0700292 if (dstSurface->config() == srcConfig) {
Brian Osman33910292017-04-18 14:38:53 -0400293 // We only support writing pixels to textures. Forcing a draw lets us write to pure RTs.
294 if (!dstSurface->asTexture()) {
295 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
296 }
297 // If the dst is MSAA, we have to draw, or we'll just be writing to the resolve target.
298 if (renderTarget && renderTarget->numColorSamples() > 1) {
299 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
egdaniel4583ec52016-06-27 12:57:00 -0700300 }
301 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -0500302 }
303
Brian Osman33910292017-04-18 14:38:53 -0400304 // Any config change requires a draw
305 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
306
307 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
308
309 if (!this->vkCaps().isConfigTexturable(srcConfig) && configsAreRBSwaps) {
310 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
311 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
312 tempDrawInfo->fWriteConfig = dstSurface->config();
313 }
314 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -0500315}
316
317bool GrVkGpu::onWritePixels(GrSurface* surface,
318 int left, int top, int width, int height,
bsalomona1e6b3b2016-03-02 10:58:23 -0800319 GrPixelConfig config,
320 const SkTArray<GrMipLevel>& texels) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500321 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
322 if (!vkTex) {
323 return false;
324 }
325
jvanverth900bd4a2016-04-29 13:53:12 -0700326 // Make sure we have at least the base level
jvanverth03509ea2016-03-02 13:19:47 -0800327 if (texels.empty() || !texels.begin()->fPixels) {
328 return false;
329 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800330
Greg Daniel164a9f02016-02-22 09:56:40 -0500331 // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels.
332 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
333 return false;
334 }
335
336 bool success = false;
337 if (GrPixelConfigIsCompressed(vkTex->desc().fConfig)) {
338 // We check that config == desc.fConfig in GrGpu::getWritePixelsInfo()
339 SkASSERT(config == vkTex->desc().fConfig);
340 // TODO: add compressed texture support
341 // delete the following two lines and uncomment the two after that when ready
342 vkTex->unref();
343 return false;
344 //success = this->uploadCompressedTexData(vkTex->desc(), buffer, false, left, top, width,
345 // height);
346 } else {
347 bool linearTiling = vkTex->isLinearTiled();
jvanverth900bd4a2016-04-29 13:53:12 -0700348 if (linearTiling) {
349 if (texels.count() > 1) {
350 SkDebugf("Can't upload mipmap data to linear tiled texture");
351 return false;
352 }
353 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
354 // Need to change the layout to general in order to perform a host write
jvanverth900bd4a2016-04-29 13:53:12 -0700355 vkTex->setImageLayout(this,
356 VK_IMAGE_LAYOUT_GENERAL,
jvanverth50c46c72016-05-06 12:31:28 -0700357 VK_ACCESS_HOST_WRITE_BIT,
358 VK_PIPELINE_STAGE_HOST_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700359 false);
egdanielbdf88112016-05-03 07:25:56 -0700360 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth900bd4a2016-04-29 13:53:12 -0700361 }
362 success = this->uploadTexDataLinear(vkTex, left, top, width, height, config,
363 texels.begin()->fPixels, texels.begin()->fRowBytes);
364 } else {
jvanverthc578b0632016-05-02 10:58:12 -0700365 int newMipLevels = texels.count();
jvanverth82c05582016-05-03 11:19:01 -0700366 int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1;
Greg Danieldd20e912017-04-07 14:42:23 -0400367 if (newMipLevels > currentMipLevels) {
jvanverthc578b0632016-05-02 10:58:12 -0700368 if (!vkTex->reallocForMipmap(this, newMipLevels)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700369 return false;
370 }
371 }
372 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, config, texels);
Greg Daniel164a9f02016-02-22 09:56:40 -0500373 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500374 }
egdaniel4583ec52016-06-27 12:57:00 -0700375
jvanverth900bd4a2016-04-29 13:53:12 -0700376 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500377}
378
Greg Danielbc26c392017-04-18 13:32:10 -0400379void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
egdaniel4bcd62e2016-08-31 07:37:31 -0700380 const SkIPoint& dstPoint) {
381 SkASSERT(dst);
382 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
383
egdanielfd016d72016-09-27 12:13:05 -0700384 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
385 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
386 }
387
egdaniel4bcd62e2016-08-31 07:37:31 -0700388 // Flip rect if necessary
389 SkIRect srcVkRect = srcRect;
390 int32_t dstY = dstPoint.fY;
391
392 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
393 SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin());
394 srcVkRect.fTop = src->height() - srcRect.fBottom;
395 srcVkRect.fBottom = src->height() - srcRect.fTop;
396 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
397 }
398
399 VkImageResolve resolveInfo;
400 resolveInfo.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
401 resolveInfo.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
402 resolveInfo.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
403 resolveInfo.dstOffset = { dstPoint.fX, dstY, 0 };
egdaniel4bcd62e2016-08-31 07:37:31 -0700404 resolveInfo.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
405
Greg Danielbc26c392017-04-18 13:32:10 -0400406 GrVkImage* dstImage;
407 GrRenderTarget* dstRT = dst->asRenderTarget();
408 if (dstRT) {
409 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400410 dstImage = vkRT;
411 } else {
412 SkASSERT(dst->asTexture());
413 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
414 }
415 dstImage->setImageLayout(this,
416 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
417 VK_ACCESS_TRANSFER_WRITE_BIT,
418 VK_PIPELINE_STAGE_TRANSFER_BIT,
419 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700420
421 src->msaaImage()->setImageLayout(this,
422 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
423 VK_ACCESS_TRANSFER_READ_BIT,
424 VK_PIPELINE_STAGE_TRANSFER_BIT,
425 false);
426
Greg Danielbc26c392017-04-18 13:32:10 -0400427 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700428}
429
Greg Daniel69d49922017-02-23 09:44:02 -0500430void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700431 if (target->needsResolve()) {
432 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700433 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
434 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500435
egdaniel4bcd62e2016-08-31 07:37:31 -0700436 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700437
Greg Danielbc26c392017-04-18 13:32:10 -0400438 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700439
440 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500441
442 if (requiresSubmit) {
443 this->submitCommandBuffer(kSkip_SyncQueue);
444 }
egdaniel52ad2512016-08-04 12:50:01 -0700445 }
446}
447
jvanverth900bd4a2016-04-29 13:53:12 -0700448bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex,
449 int left, int top, int width, int height,
450 GrPixelConfig dataConfig,
451 const void* data,
452 size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500453 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700454 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500455
456 // If we're uploading compressed data then we should be using uploadCompressedTexData
457 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
458
Greg Daniel164a9f02016-02-22 09:56:40 -0500459 size_t bpp = GrBytesPerPixel(dataConfig);
460
461 const GrSurfaceDesc& desc = tex->desc();
462
463 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
464 &width, &height, &data, &rowBytes)) {
465 return false;
466 }
467 size_t trimRowBytes = width * bpp;
468
jvanverth900bd4a2016-04-29 13:53:12 -0700469 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
470 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
471 const VkImageSubresource subres = {
472 VK_IMAGE_ASPECT_COLOR_BIT,
473 0, // mipLevel
474 0, // arraySlice
475 };
476 VkSubresourceLayout layout;
477 VkResult err;
Greg Daniel164a9f02016-02-22 09:56:40 -0500478
jvanverth900bd4a2016-04-29 13:53:12 -0700479 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500480
jvanverth900bd4a2016-04-29 13:53:12 -0700481 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700482 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700483 &subres,
484 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500485
jvanverth900bd4a2016-04-29 13:53:12 -0700486 int texTop = kBottomLeft_GrSurfaceOrigin == desc.fOrigin ? tex->height() - top - height : top;
jvanverth1e305ba2016-06-01 09:39:15 -0700487 const GrVkAlloc& alloc = tex->alloc();
488 VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700489 VkDeviceSize size = height*layout.rowPitch;
490 void* mapPtr;
jvanverth1e305ba2016-06-01 09:39:15 -0700491 err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr));
jvanverth900bd4a2016-04-29 13:53:12 -0700492 if (err) {
493 return false;
494 }
495
496 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
497 // copy into buffer by rows
498 const char* srcRow = reinterpret_cast<const char*>(data);
499 char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch;
500 for (int y = 0; y < height; y++) {
501 memcpy(dstRow, srcRow, trimRowBytes);
502 srcRow += rowBytes;
503 dstRow -= layout.rowPitch;
504 }
505 } else {
Matt Sarettcf3525a2017-01-24 12:43:41 -0500506 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
507 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700508 }
509
jvanverth9d54afc2016-09-20 09:20:03 -0700510 GrVkMemory::FlushMappedAlloc(this, alloc);
jvanverth1e305ba2016-06-01 09:39:15 -0700511 GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory));
jvanverth900bd4a2016-04-29 13:53:12 -0700512
513 return true;
514}
515
516bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex,
jvanvertha584de92016-06-30 09:10:52 -0700517 int left, int top, int width, int height,
518 GrPixelConfig dataConfig,
519 const SkTArray<GrMipLevel>& texels) {
jvanverth900bd4a2016-04-29 13:53:12 -0700520 SkASSERT(!tex->isLinearTiled());
521 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
522 SkASSERT(1 == texels.count() ||
523 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
524
Greg Danieldd20e912017-04-07 14:42:23 -0400525 // We assume that if the texture has mip levels, we either upload to all the levels or just the
526 // first.
527 SkASSERT(1 == texels.count() || texels.count() == (tex->texturePriv().maxMipMapLevel() + 1));
528
jvanverth900bd4a2016-04-29 13:53:12 -0700529 // If we're uploading compressed data then we should be using uploadCompressedTexData
530 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
531
532 if (width == 0 || height == 0) {
533 return false;
534 }
535
536 const GrSurfaceDesc& desc = tex->desc();
537 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
538 size_t bpp = GrBytesPerPixel(dataConfig);
539
540 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700541 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
542 // Because of this we need to make a non-const shallow copy of texels.
543 SkTArray<GrMipLevel> texelsShallowCopy(texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700544
jvanverthc578b0632016-05-02 10:58:12 -0700545 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
546 currentMipLevel--) {
547 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels);
Greg Daniel164a9f02016-02-22 09:56:40 -0500548 }
549
jvanverth900bd4a2016-04-29 13:53:12 -0700550 // Determine whether we need to flip when we copy into the buffer
jvanverthc578b0632016-05-02 10:58:12 -0700551 bool flipY = (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty());
jvanverth900bd4a2016-04-29 13:53:12 -0700552
jvanverthc578b0632016-05-02 10:58:12 -0700553 // adjust any params (left, top, currentWidth, currentHeight
jvanverth900bd4a2016-04-29 13:53:12 -0700554 // find the combined size of all the mip levels and the relative offset of
555 // each into the collective buffer
jvanverthc578b0632016-05-02 10:58:12 -0700556 // Do the first level separately because we may need to adjust width and height
557 // (for the non-mipped case).
558 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
559 &width,
560 &height,
561 &texelsShallowCopy[0].fPixels,
562 &texelsShallowCopy[0].fRowBytes)) {
563 return false;
564 }
565 SkTArray<size_t> individualMipOffsets(texelsShallowCopy.count());
566 individualMipOffsets.push_back(0);
567 size_t combinedBufferSize = width * bpp * height;
568 int currentWidth = width;
569 int currentHeight = height;
Greg Daniel468fd632017-03-22 17:03:45 -0400570 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
571 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
572 SkASSERT((bpp & (bpp - 1)) == 0);
573 const size_t alignmentMask = 0x3 | (bpp - 1);
jvanverthc578b0632016-05-02 10:58:12 -0700574 for (int currentMipLevel = 1; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
575 currentWidth = SkTMax(1, currentWidth/2);
576 currentHeight = SkTMax(1, currentHeight/2);
577 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
578 &currentWidth,
579 &currentHeight,
580 &texelsShallowCopy[currentMipLevel].fPixels,
581 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
582 return false;
583 }
jvanverth900bd4a2016-04-29 13:53:12 -0700584 const size_t trimmedSize = currentWidth * bpp * currentHeight;
Greg Daniel468fd632017-03-22 17:03:45 -0400585 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
586 if (alignmentDiff != 0) {
587 combinedBufferSize += alignmentMask - alignmentDiff + 1;
588 }
jvanverth900bd4a2016-04-29 13:53:12 -0700589 individualMipOffsets.push_back(combinedBufferSize);
590 combinedBufferSize += trimmedSize;
591 }
592
593 // allocate buffer to hold our mip data
594 GrVkTransferBuffer* transferBuffer =
595 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
596
597 char* buffer = (char*) transferBuffer->map();
jvanverthc578b0632016-05-02 10:58:12 -0700598 SkTArray<VkBufferImageCopy> regions(texelsShallowCopy.count());
jvanverth900bd4a2016-04-29 13:53:12 -0700599
jvanverthc578b0632016-05-02 10:58:12 -0700600 currentWidth = width;
601 currentHeight = height;
Greg Daniela1b282b2017-03-28 14:56:46 -0400602 int layerHeight = tex->height();
jvanverthc578b0632016-05-02 10:58:12 -0700603 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
Greg Daniela1b282b2017-03-28 14:56:46 -0400604 SkASSERT(1 == texelsShallowCopy.count() || currentHeight == layerHeight);
jvanverth900bd4a2016-04-29 13:53:12 -0700605 const size_t trimRowBytes = currentWidth * bpp;
jvanverthc578b0632016-05-02 10:58:12 -0700606 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700607
608 // copy data into the buffer, skipping the trailing bytes
609 char* dst = buffer + individualMipOffsets[currentMipLevel];
jvanverthc578b0632016-05-02 10:58:12 -0700610 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
jvanverth900bd4a2016-04-29 13:53:12 -0700611 if (flipY) {
612 src += (currentHeight - 1) * rowBytes;
613 for (int y = 0; y < currentHeight; y++) {
614 memcpy(dst, src, trimRowBytes);
615 src -= rowBytes;
616 dst += trimRowBytes;
617 }
jvanverth900bd4a2016-04-29 13:53:12 -0700618 } else {
619 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
620 }
621
622 VkBufferImageCopy& region = regions.push_back();
623 memset(&region, 0, sizeof(VkBufferImageCopy));
jvanverthdb379092016-07-07 11:18:46 -0700624 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
jvanverth900bd4a2016-04-29 13:53:12 -0700625 region.bufferRowLength = currentWidth;
626 region.bufferImageHeight = currentHeight;
bsalomoncf942c42016-04-29 18:30:06 -0700627 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniela1b282b2017-03-28 14:56:46 -0400628 region.imageOffset = { left, flipY ? layerHeight - top - currentHeight : top, 0 };
jvanverth900bd4a2016-04-29 13:53:12 -0700629 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
egdaniel4583ec52016-06-27 12:57:00 -0700630
jvanverthc578b0632016-05-02 10:58:12 -0700631 currentWidth = SkTMax(1, currentWidth/2);
632 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400633 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700634 }
635
jvanverth9d54afc2016-09-20 09:20:03 -0700636 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700637 transferBuffer->unmap();
638
jvanverth900bd4a2016-04-29 13:53:12 -0700639 // Change layout of our target so it can be copied to
jvanverth900bd4a2016-04-29 13:53:12 -0700640 tex->setImageLayout(this,
641 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -0700642 VK_ACCESS_TRANSFER_WRITE_BIT,
643 VK_PIPELINE_STAGE_TRANSFER_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700644 false);
645
646 // Copy the buffer to the image
647 fCurrentCmdBuffer->copyBufferToImage(this,
648 transferBuffer,
649 tex,
650 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
651 regions.count(),
652 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700653 transferBuffer->unref();
Greg Danieldd20e912017-04-07 14:42:23 -0400654 if (1 == texelsShallowCopy.count()) {
655 tex->texturePriv().dirtyMipMaps(true);
656 }
jvanverth900bd4a2016-04-29 13:53:12 -0700657
Greg Daniel164a9f02016-02-22 09:56:40 -0500658 return true;
659}
660
661////////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -0700662GrTexture* GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
bsalomona1e6b3b2016-03-02 10:58:23 -0800663 const SkTArray<GrMipLevel>& texels) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500664 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
665
666 VkFormat pixelFormat;
667 if (!GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)) {
668 return nullptr;
669 }
670
671 if (!fVkCaps->isConfigTexturable(desc.fConfig)) {
672 return nullptr;
673 }
674
egdaniel0a3a7f72016-06-24 09:22:31 -0700675 if (renderTarget && !fVkCaps->isConfigRenderable(desc.fConfig, false)) {
676 return nullptr;
677 }
678
Greg Daniel164a9f02016-02-22 09:56:40 -0500679 bool linearTiling = false;
680 if (SkToBool(desc.fFlags & kZeroCopy_GrSurfaceFlag)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700681 // we can't have a linear texture with a mipmap
682 if (texels.count() > 1) {
683 SkDebugf("Trying to create linear tiled texture with mipmap");
684 return nullptr;
685 }
egdaniela95d46b2016-08-15 08:06:29 -0700686 if (fVkCaps->isConfigTexturableLinearly(desc.fConfig) &&
Greg Daniel164a9f02016-02-22 09:56:40 -0500687 (!renderTarget || fVkCaps->isConfigRenderableLinearly(desc.fConfig, false))) {
688 linearTiling = true;
689 } else {
690 return nullptr;
691 }
692 }
693
694 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
695 if (renderTarget) {
696 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
697 }
698
699 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
700 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
701 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700702 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500703 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
704 // texture.
705 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
706
bsalomona1e6b3b2016-03-02 10:58:23 -0800707 VkFlags memProps = (!texels.empty() && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT :
708 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500709
710 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700711 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500712 // to 1.
jvanverthc578b0632016-05-02 10:58:12 -0700713 int mipLevels = texels.empty() ? 1 : texels.count();
Greg Daniel164a9f02016-02-22 09:56:40 -0500714 GrVkImage::ImageDesc imageDesc;
715 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
716 imageDesc.fFormat = pixelFormat;
717 imageDesc.fWidth = desc.fWidth;
718 imageDesc.fHeight = desc.fHeight;
jvanverthc578b0632016-05-02 10:58:12 -0700719 imageDesc.fLevels = linearTiling ? 1 : mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500720 imageDesc.fSamples = 1;
721 imageDesc.fImageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
722 imageDesc.fUsageFlags = usageFlags;
723 imageDesc.fMemProps = memProps;
724
725 GrVkTexture* tex;
726 if (renderTarget) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700727 tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
Greg Daniel164a9f02016-02-22 09:56:40 -0500728 imageDesc);
729 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -0700730 tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc);
Greg Daniel164a9f02016-02-22 09:56:40 -0500731 }
732
733 if (!tex) {
734 return nullptr;
735 }
736
bsalomone699d0c2016-03-09 06:25:15 -0800737 if (!texels.empty()) {
738 SkASSERT(texels.begin()->fPixels);
jvanverth900bd4a2016-04-29 13:53:12 -0700739 bool success;
740 if (linearTiling) {
741 success = this->uploadTexDataLinear(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
742 texels.begin()->fPixels, texels.begin()->fRowBytes);
743 } else {
744 success = this->uploadTexDataOptimal(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
745 texels);
746 }
747 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500748 tex->unref();
749 return nullptr;
750 }
751 }
752
753 return tex;
754}
755
756////////////////////////////////////////////////////////////////////////////////
757
jvanverthdb379092016-07-07 11:18:46 -0700758bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
759 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700760
761 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700762 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700763
764 return true;
765}
766
767////////////////////////////////////////////////////////////////////////////////
768
Greg Daniel164a9f02016-02-22 09:56:40 -0500769static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin) {
770 // By default, all textures in Vk use TopLeft
771 if (kDefault_GrSurfaceOrigin == origin) {
772 return kTopLeft_GrSurfaceOrigin;
773 } else {
774 return origin;
775 }
776}
777
Greg Daniel7ef28f32017-04-20 16:41:55 +0000778sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
779 GrSurfaceOrigin origin,
780 GrBackendTextureFlags flags,
781 int sampleCnt,
bungeman6bd52842016-10-27 09:30:08 -0700782 GrWrapOwnership ownership) {
Greg Daniel7ef28f32017-04-20 16:41:55 +0000783 const GrVkImageInfo* info = backendTex.getVkImageInfo();
784 if (!info) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500785 return nullptr;
786 }
787
788 int maxSize = this->caps()->maxTextureSize();
Greg Daniel7ef28f32017-04-20 16:41:55 +0000789 if (backendTex.width() > maxSize || backendTex.height() > maxSize) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500790 return nullptr;
791 }
792
jvanverth1e305ba2016-06-01 09:39:15 -0700793 if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) {
jvanverthfd359ca2016-03-18 11:57:24 -0700794 return nullptr;
795 }
Greg Daniel7ef28f32017-04-20 16:41:55 +0000796
797 SkASSERT(backendTex.config() == GrVkFormatToPixelConfig(info->fFormat));
Greg Daniel164a9f02016-02-22 09:56:40 -0500798
Greg Daniel164a9f02016-02-22 09:56:40 -0500799 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000800 // next line relies on GrBackendTextureFlags matching GrTexture's
801 surfDesc.fFlags = (GrSurfaceFlags)flags;
802 surfDesc.fWidth = backendTex.width();
803 surfDesc.fHeight = backendTex.height();
804 surfDesc.fConfig = backendTex.config();
805 surfDesc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
806 bool renderTarget = SkToBool(flags & kRenderTarget_GrBackendTextureFlag);
Brian Osman766fcbb2017-03-13 09:33:09 -0400807 SkASSERT(!renderTarget || kAdoptAndCache_GrWrapOwnership != ownership); // Not supported
Greg Daniel164a9f02016-02-22 09:56:40 -0500808 // In GL, Chrome assumes all textures are BottomLeft
809 // In VK, we don't have this restriction
Greg Daniel7ef28f32017-04-20 16:41:55 +0000810 surfDesc.fOrigin = resolve_origin(origin);
Greg Daniel164a9f02016-02-22 09:56:40 -0500811
bungeman6bd52842016-10-27 09:30:08 -0700812 if (!renderTarget) {
813 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, info);
Greg Daniel164a9f02016-02-22 09:56:40 -0500814 }
bungeman6bd52842016-10-27 09:30:08 -0700815 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(this, surfDesc, ownership, info);
Greg Daniel164a9f02016-02-22 09:56:40 -0500816}
817
Greg Danielfdd77da2017-04-26 12:19:14 -0400818sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
819 GrSurfaceOrigin origin){
Greg Daniele79b4732017-04-20 14:07:46 -0400820 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
821 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
822 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
823 // creating and owning the MSAA images.
Greg Danielfdd77da2017-04-26 12:19:14 -0400824 if (backendRT.sampleCnt()) {
Greg Daniele79b4732017-04-20 14:07:46 -0400825 return nullptr;
826 }
halcanary9d524f22016-03-29 09:03:52 -0700827
Greg Danielfdd77da2017-04-26 12:19:14 -0400828 const GrVkImageInfo* info = backendRT.getVkImageInfo();
829 if (!info) {
830 return nullptr;
831 }
Brian Osman0b791f52017-03-10 08:30:22 -0500832 if (VK_NULL_HANDLE == info->fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -0700833 return nullptr;
834 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500835
Greg Daniel164a9f02016-02-22 09:56:40 -0500836 GrSurfaceDesc desc;
Greg Danielfdd77da2017-04-26 12:19:14 -0400837 desc.fConfig = backendRT.config();
Robert Phillips3667b492016-11-15 14:46:09 -0500838 desc.fFlags = kCheckAllocation_GrSurfaceFlag | kRenderTarget_GrSurfaceFlag;
Greg Danielfdd77da2017-04-26 12:19:14 -0400839 desc.fWidth = backendRT.width();
840 desc.fHeight = backendRT.height();
Greg Daniele79b4732017-04-20 14:07:46 -0400841 desc.fSampleCnt = 0;
Greg Daniel164a9f02016-02-22 09:56:40 -0500842
Greg Danielfdd77da2017-04-26 12:19:14 -0400843 SkASSERT(kDefault_GrSurfaceOrigin != origin);
844 desc.fOrigin = origin;
Greg Daniel164a9f02016-02-22 09:56:40 -0500845
Brian Osman0b791f52017-03-10 08:30:22 -0500846 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info);
Greg Danielfdd77da2017-04-26 12:19:14 -0400847 if (tgt && backendRT.stencilBits()) {
bungeman6bd52842016-10-27 09:30:08 -0700848 if (!createStencilAttachmentForRenderTarget(tgt.get(), desc.fWidth, desc.fHeight)) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500849 return nullptr;
850 }
851 }
852 return tgt;
853}
854
Greg Daniel7ef28f32017-04-20 16:41:55 +0000855sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
856 GrSurfaceOrigin origin,
857 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -0400858
Greg Daniel7ef28f32017-04-20 16:41:55 +0000859 const GrVkImageInfo* info = tex.getVkImageInfo();
Greg Danielfdd77da2017-04-26 12:19:14 -0400860 if (!info) {
861 return nullptr;
862 }
Brian Osman33910292017-04-18 14:38:53 -0400863 if (VK_NULL_HANDLE == info->fImage) {
864 return nullptr;
865 }
866
867 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000868 desc.fFlags = kRenderTarget_GrSurfaceFlag;
869 desc.fConfig = tex.config();
870 desc.fWidth = tex.width();
871 desc.fHeight = tex.height();
872 desc.fSampleCnt = SkTMin(sampleCnt, this->caps()->maxSampleCount());
Brian Osman33910292017-04-18 14:38:53 -0400873
Greg Daniel7ef28f32017-04-20 16:41:55 +0000874 desc.fOrigin = resolve_origin(origin);
Brian Osman33910292017-04-18 14:38:53 -0400875
876 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info);
877 return tgt;
878}
879
egdaniel50ead532016-07-13 14:23:26 -0700880void GrVkGpu::generateMipmap(GrVkTexture* tex) {
jvanverth900bd4a2016-04-29 13:53:12 -0700881 // don't do anything for linearly tiled textures (can't have mipmaps)
jvanverth62340062016-04-26 08:01:44 -0700882 if (tex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700883 SkDebugf("Trying to create mipmap for linear tiled texture");
jvanverth62340062016-04-26 08:01:44 -0700884 return;
885 }
886
jvanverth62340062016-04-26 08:01:44 -0700887 // determine if we can blit to and from this format
888 const GrVkCaps& caps = this->vkCaps();
889 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -0700890 !caps.configCanBeSrcofBlit(tex->config(), false) ||
891 !caps.mipMapSupport()) {
jvanverth62340062016-04-26 08:01:44 -0700892 return;
893 }
894
egdanielfd016d72016-09-27 12:13:05 -0700895 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
896 this->submitCommandBuffer(kSkip_SyncQueue);
897 }
898
egdaniel66933552016-08-24 07:22:19 -0700899 // We may need to resolve the texture first if it is also a render target
900 GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(tex->asRenderTarget());
901 if (texRT) {
Greg Daniel69d49922017-02-23 09:44:02 -0500902 this->internalResolveRenderTarget(texRT, false);
egdaniel66933552016-08-24 07:22:19 -0700903 }
904
egdaniel7ac5da82016-07-15 13:41:42 -0700905 int width = tex->width();
906 int height = tex->height();
907 VkImageBlit blitRegion;
908 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -0700909
jvanverth82c05582016-05-03 11:19:01 -0700910 // SkMipMap doesn't include the base level in the level count so we have to add 1
911 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
egdaniel7ac5da82016-07-15 13:41:42 -0700912 if (levelCount != tex->mipLevels()) {
913 const GrVkResource* oldResource = tex->resource();
914 oldResource->ref();
915 // grab handle to the original image resource
916 VkImage oldImage = tex->image();
917
918 // change the original image's layout so we can copy from it
919 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
920 VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
921
922 if (!tex->reallocForMipmap(this, levelCount)) {
923 oldResource->unref(this);
924 return;
925 }
926 // change the new image's layout so we can blit to it
927 tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
928 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
929
930 // Blit original image to top level of new image
931 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
932 blitRegion.srcOffsets[0] = { 0, 0, 0 };
933 blitRegion.srcOffsets[1] = { width, height, 1 };
934 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
935 blitRegion.dstOffsets[0] = { 0, 0, 0 };
936 blitRegion.dstOffsets[1] = { width, height, 1 };
937
938 fCurrentCmdBuffer->blitImage(this,
939 oldResource,
940 oldImage,
941 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
942 tex->resource(),
943 tex->image(),
944 VK_IMAGE_LAYOUT_GENERAL,
945 1,
946 &blitRegion,
947 VK_FILTER_LINEAR);
948
jvanverth62340062016-04-26 08:01:44 -0700949 oldResource->unref(this);
egdaniel7ac5da82016-07-15 13:41:42 -0700950 } else {
951 // change layout of the layers so we can write to them.
952 tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
953 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -0700954 }
955
jvanverth50c46c72016-05-06 12:31:28 -0700956 // setup memory barrier
Greg Daniel94403452017-04-18 15:52:36 -0400957 SkASSERT(kUnknown_GrPixelConfig != GrVkFormatToPixelConfig(tex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -0700958 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
959 VkImageMemoryBarrier imageMemoryBarrier = {
960 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
961 NULL, // pNext
egdaniel7ac5da82016-07-15 13:41:42 -0700962 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
963 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
jvanverth50c46c72016-05-06 12:31:28 -0700964 VK_IMAGE_LAYOUT_GENERAL, // oldLayout
965 VK_IMAGE_LAYOUT_GENERAL, // newLayout
966 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
967 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
egdanielb2df0c22016-05-13 11:30:37 -0700968 tex->image(), // image
jvanverth50c46c72016-05-06 12:31:28 -0700969 { aspectFlags, 0, 1, 0, 1 } // subresourceRange
970 };
971
jvanverth62340062016-04-26 08:01:44 -0700972 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -0700973 uint32_t mipLevel = 1;
974 while (mipLevel < levelCount) {
975 int prevWidth = width;
976 int prevHeight = height;
977 width = SkTMax(1, width / 2);
978 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -0700979
jvanverth50c46c72016-05-06 12:31:28 -0700980 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
981 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
982 false, &imageMemoryBarrier);
983
984 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700985 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700986 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700987 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
988 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700989 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -0700990 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -0700991 *tex,
992 *tex,
jvanverth62340062016-04-26 08:01:44 -0700993 1,
994 &blitRegion,
995 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -0700996 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -0700997 }
jvanverth62340062016-04-26 08:01:44 -0700998}
999
Greg Daniel164a9f02016-02-22 09:56:40 -05001000////////////////////////////////////////////////////////////////////////////////
1001
1002GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1003 int width,
1004 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001005 SkASSERT(width >= rt->width());
1006 SkASSERT(height >= rt->height());
1007
1008 int samples = rt->numStencilSamples();
1009
egdaniel8f1dcaa2016-04-01 10:10:45 -07001010 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001011
1012 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001013 width,
1014 height,
1015 samples,
1016 sFmt));
1017 fStats.incStencilAttachmentCreates();
1018 return stencil;
1019}
1020
1021////////////////////////////////////////////////////////////////////////////////
1022
jvanverth9d54afc2016-09-20 09:20:03 -07001023bool copy_testing_data(GrVkGpu* gpu, void* srcData, const GrVkAlloc& alloc,
egdaniel3602d4f2016-08-12 11:58:53 -07001024 size_t srcRowBytes, size_t dstRowBytes, int h) {
1025 void* mapPtr;
1026 VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(),
jvanverth9d54afc2016-09-20 09:20:03 -07001027 alloc.fMemory,
1028 alloc.fOffset,
egdaniel3602d4f2016-08-12 11:58:53 -07001029 dstRowBytes * h,
1030 0,
1031 &mapPtr));
1032 if (err) {
1033 return false;
1034 }
1035
Greg Daniel20ece3a2017-03-28 10:24:43 -04001036 if (srcData) {
1037 // If there is no padding on dst we can do a single memcopy.
1038 // This assumes the srcData comes in with no padding.
1039 SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes),
1040 srcData, srcRowBytes, srcRowBytes, h);
1041 } else {
1042 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1043 // with some data.
1044 if (srcRowBytes == static_cast<size_t>(dstRowBytes)) {
1045 memset(mapPtr, 0, srcRowBytes * h);
1046 } else {
1047 for (int i = 0; i < h; ++i) {
1048 memset(mapPtr, 0, srcRowBytes);
1049 mapPtr = SkTAddOffset<void>(mapPtr, static_cast<size_t>(dstRowBytes));
1050 }
1051 }
1052 }
jvanverth9d54afc2016-09-20 09:20:03 -07001053 GrVkMemory::FlushMappedAlloc(gpu, alloc);
1054 GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory));
egdaniel3602d4f2016-08-12 11:58:53 -07001055 return true;
1056}
1057
Greg Daniel164a9f02016-02-22 09:56:40 -05001058GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -07001059 GrPixelConfig config,
1060 bool isRenderTarget) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001061
1062 VkFormat pixelFormat;
1063 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
1064 return 0;
1065 }
1066
1067 bool linearTiling = false;
1068 if (!fVkCaps->isConfigTexturable(config)) {
1069 return 0;
1070 }
1071
egdaniel0a3a7f72016-06-24 09:22:31 -07001072 if (isRenderTarget && !fVkCaps->isConfigRenderable(config, false)) {
1073 return 0;
1074 }
1075
egdaniela95d46b2016-08-15 08:06:29 -07001076 if (fVkCaps->isConfigTexturableLinearly(config) &&
egdaniel0a3a7f72016-06-24 09:22:31 -07001077 (!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false))) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001078 linearTiling = true;
1079 }
1080
Greg Daniel164a9f02016-02-22 09:56:40 -05001081 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1082 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1083 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
egdaniel0a3a7f72016-06-24 09:22:31 -07001084 if (isRenderTarget) {
1085 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1086 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001087
jvanverthfd359ca2016-03-18 11:57:24 -07001088 VkImage image = VK_NULL_HANDLE;
jvanverth9d54afc2016-09-20 09:20:03 -07001089 GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0, 0 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001090
jvanverthfd359ca2016-03-18 11:57:24 -07001091 VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
1092 VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling)
1093 ? VK_IMAGE_LAYOUT_PREINITIALIZED
1094 : VK_IMAGE_LAYOUT_UNDEFINED;
1095
1096 // Create Image
1097 VkSampleCountFlagBits vkSamples;
1098 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
1099 return 0;
1100 }
1101
1102 const VkImageCreateInfo imageCreateInfo = {
1103 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1104 NULL, // pNext
1105 0, // VkImageCreateFlags
1106 VK_IMAGE_TYPE_2D, // VkImageType
1107 pixelFormat, // VkFormat
ethannicholas384b5e92016-03-25 11:04:06 -07001108 { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D
jvanverthfd359ca2016-03-18 11:57:24 -07001109 1, // mipLevels
1110 1, // arrayLayers
1111 vkSamples, // samples
1112 imageTiling, // VkImageTiling
1113 usageFlags, // VkImageUsageFlags
1114 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1115 0, // queueFamilyCount
1116 0, // pQueueFamilyIndices
1117 initialLayout // initialLayout
1118 };
1119
1120 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
1121
jvanverth6b6ffc42016-06-13 14:28:07 -07001122 if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001123 VK_CALL(DestroyImage(this->device(), image, nullptr));
Greg Daniel164a9f02016-02-22 09:56:40 -05001124 return 0;
1125 }
1126
Greg Daniel20ece3a2017-03-28 10:24:43 -04001127 size_t bpp = GrBytesPerPixel(config);
1128 size_t rowCopyBytes = bpp * w;
1129 if (linearTiling) {
1130 const VkImageSubresource subres = {
1131 VK_IMAGE_ASPECT_COLOR_BIT,
1132 0, // mipLevel
1133 0, // arraySlice
1134 };
1135 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -05001136
Greg Daniel20ece3a2017-03-28 10:24:43 -04001137 VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001138
Greg Daniel20ece3a2017-03-28 10:24:43 -04001139 if (!copy_testing_data(this, srcData, alloc, rowCopyBytes,
1140 static_cast<size_t>(layout.rowPitch), h)) {
1141 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1142 VK_CALL(DestroyImage(fDevice, image, nullptr));
1143 return 0;
1144 }
1145 } else {
1146 SkASSERT(w && h);
egdaniel3602d4f2016-08-12 11:58:53 -07001147
Greg Daniel20ece3a2017-03-28 10:24:43 -04001148 VkBuffer buffer;
1149 VkBufferCreateInfo bufInfo;
1150 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1151 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1152 bufInfo.flags = 0;
1153 bufInfo.size = rowCopyBytes * h;
1154 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1155 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1156 bufInfo.queueFamilyIndexCount = 0;
1157 bufInfo.pQueueFamilyIndices = nullptr;
1158 VkResult err;
1159 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
egdaniel3602d4f2016-08-12 11:58:53 -07001160
Greg Daniel20ece3a2017-03-28 10:24:43 -04001161 if (err) {
1162 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1163 VK_CALL(DestroyImage(fDevice, image, nullptr));
1164 return 0;
1165 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001166
Greg Daniel20ece3a2017-03-28 10:24:43 -04001167 GrVkAlloc bufferAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
1168 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type,
1169 true, &bufferAlloc)) {
1170 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1171 VK_CALL(DestroyImage(fDevice, image, nullptr));
1172 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1173 return 0;
1174 }
egdaniel3602d4f2016-08-12 11:58:53 -07001175
Greg Daniel20ece3a2017-03-28 10:24:43 -04001176 if (!copy_testing_data(this, srcData, bufferAlloc, rowCopyBytes, rowCopyBytes, h)) {
1177 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1178 VK_CALL(DestroyImage(fDevice, image, nullptr));
1179 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1180 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1181 return 0;
1182 }
egdaniel3602d4f2016-08-12 11:58:53 -07001183
Greg Daniel20ece3a2017-03-28 10:24:43 -04001184 const VkCommandBufferAllocateInfo cmdInfo = {
1185 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1186 NULL, // pNext
1187 fCmdPool, // commandPool
1188 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1189 1 // bufferCount
1190 };
egdaniel3602d4f2016-08-12 11:58:53 -07001191
Greg Daniel20ece3a2017-03-28 10:24:43 -04001192 VkCommandBuffer cmdBuffer;
1193 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1194 if (err) {
1195 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1196 VK_CALL(DestroyImage(fDevice, image, nullptr));
1197 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1198 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1199 return 0;
1200 }
egdaniel3602d4f2016-08-12 11:58:53 -07001201
Greg Daniel20ece3a2017-03-28 10:24:43 -04001202 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1203 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1204 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1205 cmdBufferBeginInfo.pNext = nullptr;
1206 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1207 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
egdaniel3602d4f2016-08-12 11:58:53 -07001208
Greg Daniel20ece3a2017-03-28 10:24:43 -04001209 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1210 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001211
Greg Daniel20ece3a2017-03-28 10:24:43 -04001212 // Set image layout and add barrier
1213 VkImageMemoryBarrier barrier;
1214 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1215 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1216 barrier.pNext = nullptr;
1217 barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout);
1218 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1219 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1220 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1221 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1222 barrier.image = image;
1223 barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0 , 1};
egdaniel3602d4f2016-08-12 11:58:53 -07001224
Greg Daniel20ece3a2017-03-28 10:24:43 -04001225 VK_CALL(CmdPipelineBarrier(cmdBuffer,
1226 GrVkMemory::LayoutToPipelineStageFlags(initialLayout),
1227 VK_PIPELINE_STAGE_TRANSFER_BIT,
1228 0,
1229 0, nullptr,
1230 0, nullptr,
1231 1, &barrier));
1232 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
egdaniel3602d4f2016-08-12 11:58:53 -07001233
Greg Daniel20ece3a2017-03-28 10:24:43 -04001234 // Submit copy command
1235 VkBufferImageCopy region;
1236 memset(&region, 0, sizeof(VkBufferImageCopy));
1237 region.bufferOffset = 0;
1238 region.bufferRowLength = w;
1239 region.bufferImageHeight = h;
1240 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1241 region.imageOffset = { 0, 0, 0 };
1242 region.imageExtent = { (uint32_t)w, (uint32_t)h, 1 };
egdaniel3602d4f2016-08-12 11:58:53 -07001243
Greg Daniel20ece3a2017-03-28 10:24:43 -04001244 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, 1, &region));
egdaniel3602d4f2016-08-12 11:58:53 -07001245
Greg Daniel20ece3a2017-03-28 10:24:43 -04001246 // End CommandBuffer
1247 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1248 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001249
Greg Daniel20ece3a2017-03-28 10:24:43 -04001250 // Create Fence for queue
1251 VkFence fence;
1252 VkFenceCreateInfo fenceInfo;
1253 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1254 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
egdaniel3602d4f2016-08-12 11:58:53 -07001255
Greg Daniel20ece3a2017-03-28 10:24:43 -04001256 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1257 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001258
Greg Daniel20ece3a2017-03-28 10:24:43 -04001259 VkSubmitInfo submitInfo;
1260 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1261 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1262 submitInfo.pNext = nullptr;
1263 submitInfo.waitSemaphoreCount = 0;
1264 submitInfo.pWaitSemaphores = nullptr;
1265 submitInfo.pWaitDstStageMask = 0;
1266 submitInfo.commandBufferCount = 1;
1267 submitInfo.pCommandBuffers = &cmdBuffer;
1268 submitInfo.signalSemaphoreCount = 0;
1269 submitInfo.pSignalSemaphores = nullptr;
1270 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1271 SkASSERT(!err);
egdaniel3602d4f2016-08-12 11:58:53 -07001272
Greg Daniel20ece3a2017-03-28 10:24:43 -04001273 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1274 if (VK_TIMEOUT == err) {
1275 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
1276 VK_CALL(DestroyImage(fDevice, image, nullptr));
egdaniel3602d4f2016-08-12 11:58:53 -07001277 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1278 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1279 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1280 VK_CALL(DestroyFence(fDevice, fence, nullptr));
Greg Daniel20ece3a2017-03-28 10:24:43 -04001281 SkDebugf("Fence failed to signal: %d\n", err);
1282 SkFAIL("failing");
Greg Daniel164a9f02016-02-22 09:56:40 -05001283 }
Greg Daniel20ece3a2017-03-28 10:24:43 -04001284 SkASSERT(!err);
1285
1286 // Clean up transfer resources
1287 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1288 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1289 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer));
1290 VK_CALL(DestroyFence(fDevice, fence, nullptr));
Greg Daniel164a9f02016-02-22 09:56:40 -05001291 }
1292
egdanielb2df0c22016-05-13 11:30:37 -07001293 GrVkImageInfo* info = new GrVkImageInfo;
jvanverthfd359ca2016-03-18 11:57:24 -07001294 info->fImage = image;
1295 info->fAlloc = alloc;
1296 info->fImageTiling = imageTiling;
1297 info->fImageLayout = initialLayout;
egdaniel58a8d922016-04-21 08:03:10 -07001298 info->fFormat = pixelFormat;
jvanverth2af0f1b2016-05-03 10:36:49 -07001299 info->fLevelCount = 1;
jvanverthfd359ca2016-03-18 11:57:24 -07001300
1301 return (GrBackendObject)info;
Greg Daniel164a9f02016-02-22 09:56:40 -05001302}
1303
1304bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
egdanielb2df0c22016-05-13 11:30:37 -07001305 const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id);
Greg Daniel164a9f02016-02-22 09:56:40 -05001306
jvanverth1e305ba2016-06-01 09:39:15 -07001307 if (backend && backend->fImage && backend->fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001308 VkMemoryRequirements req;
1309 memset(&req, 0, sizeof(req));
1310 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
1311 backend->fImage,
1312 &req));
1313 // TODO: find a better check
1314 // This will probably fail with a different driver
1315 return (req.size > 0) && (req.size <= 8192 * 8192);
1316 }
1317
1318 return false;
1319}
1320
1321void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) {
jvanverth6b6ffc42016-06-13 14:28:07 -07001322 GrVkImageInfo* backend = reinterpret_cast<GrVkImageInfo*>(id);
Greg Daniel164a9f02016-02-22 09:56:40 -05001323 if (backend) {
1324 if (!abandon) {
jvanverthfd359ca2016-03-18 11:57:24 -07001325 // something in the command buffer may still be using this, so force submit
1326 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth6b6ffc42016-06-13 14:28:07 -07001327 GrVkImage::DestroyImageInfo(this, backend);
Greg Daniel164a9f02016-02-22 09:56:40 -05001328 }
jvanverthfd359ca2016-03-18 11:57:24 -07001329 delete backend;
Greg Daniel164a9f02016-02-22 09:56:40 -05001330 }
1331}
1332
1333////////////////////////////////////////////////////////////////////////////////
1334
1335void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1336 VkPipelineStageFlags dstStageMask,
1337 bool byRegion,
1338 VkMemoryBarrier* barrier) const {
1339 SkASSERT(fCurrentCmdBuffer);
1340 fCurrentCmdBuffer->pipelineBarrier(this,
1341 srcStageMask,
1342 dstStageMask,
1343 byRegion,
1344 GrVkCommandBuffer::kMemory_BarrierType,
1345 barrier);
1346}
1347
1348void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1349 VkPipelineStageFlags dstStageMask,
1350 bool byRegion,
1351 VkBufferMemoryBarrier* barrier) const {
1352 SkASSERT(fCurrentCmdBuffer);
1353 fCurrentCmdBuffer->pipelineBarrier(this,
1354 srcStageMask,
1355 dstStageMask,
1356 byRegion,
1357 GrVkCommandBuffer::kBufferMemory_BarrierType,
1358 barrier);
1359}
1360
1361void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1362 VkPipelineStageFlags dstStageMask,
1363 bool byRegion,
1364 VkImageMemoryBarrier* barrier) const {
1365 SkASSERT(fCurrentCmdBuffer);
1366 fCurrentCmdBuffer->pipelineBarrier(this,
1367 srcStageMask,
1368 dstStageMask,
1369 byRegion,
1370 GrVkCommandBuffer::kImageMemory_BarrierType,
1371 barrier);
1372}
1373
Robert Phillipsf2361d22016-10-25 14:20:06 -04001374void GrVkGpu::finishOpList() {
Greg Daniel164a9f02016-02-22 09:56:40 -05001375 // Submit the current command buffer to the Queue
1376 this->submitCommandBuffer(kSkip_SyncQueue);
1377}
1378
egdaniel3d5d9ac2016-03-01 12:56:15 -08001379void GrVkGpu::clearStencil(GrRenderTarget* target) {
1380 if (nullptr == target) {
1381 return;
1382 }
1383 GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
1384 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
1385
1386
1387 VkClearDepthStencilValue vkStencilColor;
1388 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
1389
egdaniel3d5d9ac2016-03-01 12:56:15 -08001390 vkStencil->setImageLayout(this,
1391 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001392 VK_ACCESS_TRANSFER_WRITE_BIT,
1393 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel3d5d9ac2016-03-01 12:56:15 -08001394 false);
1395
egdaniel3d5d9ac2016-03-01 12:56:15 -08001396 VkImageSubresourceRange subRange;
1397 memset(&subRange, 0, sizeof(VkImageSubresourceRange));
1398 subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1399 subRange.baseMipLevel = 0;
1400 subRange.levelCount = 1;
1401 subRange.baseArrayLayer = 0;
1402 subRange.layerCount = 1;
1403
1404 // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
1405 // draw. Thus we should look into using the load op functions on the render pass to clear out
1406 // the stencil there.
1407 fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
1408}
1409
Greg Daniel164a9f02016-02-22 09:56:40 -05001410inline bool can_copy_image(const GrSurface* dst,
1411 const GrSurface* src,
1412 const GrVkGpu* gpu) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001413 const GrRenderTarget* dstRT = dst->asRenderTarget();
1414 const GrRenderTarget* srcRT = src->asRenderTarget();
1415 if (dstRT && srcRT) {
1416 if (srcRT->numColorSamples() != dstRT->numColorSamples()) {
1417 return false;
1418 }
1419 } else if (dstRT) {
1420 if (dstRT->numColorSamples() > 1) {
1421 return false;
1422 }
1423 } else if (srcRT) {
1424 if (srcRT->numColorSamples() > 1) {
1425 return false;
1426 }
egdaniel17b89252016-04-05 07:23:38 -07001427 }
1428
Michael Jurka3251ed82017-04-05 09:52:55 -07001429 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
egdaniel17b89252016-04-05 07:23:38 -07001430 // as image usage flags.
1431 if (src->origin() == dst->origin() &&
1432 GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001433 return true;
1434 }
1435
Greg Daniel164a9f02016-02-22 09:56:40 -05001436 return false;
1437}
1438
1439void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst,
1440 GrSurface* src,
egdaniel17b89252016-04-05 07:23:38 -07001441 GrVkImage* dstImage,
1442 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001443 const SkIRect& srcRect,
1444 const SkIPoint& dstPoint) {
1445 SkASSERT(can_copy_image(dst, src, this));
1446
Greg Daniel164a9f02016-02-22 09:56:40 -05001447 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1448 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001449 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001450 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1451 VK_ACCESS_TRANSFER_WRITE_BIT,
1452 VK_PIPELINE_STAGE_TRANSFER_BIT,
1453 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001454
egdaniel17b89252016-04-05 07:23:38 -07001455 srcImage->setImageLayout(this,
1456 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001457 VK_ACCESS_TRANSFER_READ_BIT,
1458 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001459 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001460
1461 // Flip rect if necessary
1462 SkIRect srcVkRect = srcRect;
1463 int32_t dstY = dstPoint.fY;
1464
1465 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
1466 SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin());
1467 srcVkRect.fTop = src->height() - srcRect.fBottom;
1468 srcVkRect.fBottom = src->height() - srcRect.fTop;
1469 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1470 }
1471
1472 VkImageCopy copyRegion;
1473 memset(&copyRegion, 0, sizeof(VkImageCopy));
1474 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1475 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1476 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1477 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001478 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001479
1480 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001481 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001482 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001483 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001484 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1485 1,
1486 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001487
1488 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1489 srcRect.width(), srcRect.height());
1490 this->didWriteToSurface(dst, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001491}
1492
egdaniel17b89252016-04-05 07:23:38 -07001493inline bool can_copy_as_blit(const GrSurface* dst,
1494 const GrSurface* src,
1495 const GrVkImage* dstImage,
1496 const GrVkImage* srcImage,
1497 const GrVkGpu* gpu) {
egdaniel66933552016-08-24 07:22:19 -07001498 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
egdaniel17b89252016-04-05 07:23:38 -07001499 // as image usage flags.
1500 const GrVkCaps& caps = gpu->vkCaps();
1501 if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) ||
1502 !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) {
1503 return false;
1504 }
1505
1506 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
1507 // resolved msaa though.
1508 if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) ||
1509 (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) {
1510 return false;
1511 }
1512
1513 return true;
1514}
1515
1516void GrVkGpu::copySurfaceAsBlit(GrSurface* dst,
1517 GrSurface* src,
1518 GrVkImage* dstImage,
1519 GrVkImage* srcImage,
1520 const SkIRect& srcRect,
1521 const SkIPoint& dstPoint) {
1522 SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this));
1523
egdaniel17b89252016-04-05 07:23:38 -07001524 dstImage->setImageLayout(this,
1525 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001526 VK_ACCESS_TRANSFER_WRITE_BIT,
1527 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001528 false);
1529
egdaniel17b89252016-04-05 07:23:38 -07001530 srcImage->setImageLayout(this,
1531 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001532 VK_ACCESS_TRANSFER_READ_BIT,
1533 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001534 false);
1535
1536 // Flip rect if necessary
1537 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001538 srcVkRect.fLeft = srcRect.fLeft;
1539 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001540 SkIRect dstRect;
1541 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001542 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001543
1544 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
1545 srcVkRect.fTop = src->height() - srcRect.fBottom;
1546 srcVkRect.fBottom = src->height() - srcRect.fTop;
1547 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001548 srcVkRect.fTop = srcRect.fTop;
1549 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001550 }
1551
1552 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
1553 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1554 } else {
1555 dstRect.fTop = dstPoint.fY;
1556 }
1557 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1558
1559 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1560 // get the correct origintation of the copied data.
1561 if (src->origin() != dst->origin()) {
1562 SkTSwap(dstRect.fTop, dstRect.fBottom);
1563 }
1564
1565 VkImageBlit blitRegion;
1566 memset(&blitRegion, 0, sizeof(VkImageBlit));
1567 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1568 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001569 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001570 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1571 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001572 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001573
1574 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001575 *srcImage,
1576 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001577 1,
1578 &blitRegion,
1579 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001580
1581 this->didWriteToSurface(dst, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001582}
1583
egdaniel4bcd62e2016-08-31 07:37:31 -07001584inline bool can_copy_as_resolve(const GrSurface* dst,
1585 const GrSurface* src,
1586 const GrVkGpu* gpu) {
1587 // Our src must be a multisampled render target
1588 if (!src->asRenderTarget() || src->asRenderTarget()->numColorSamples() <= 1) {
1589 return false;
1590 }
1591
Greg Daniel7d6f6662017-04-18 16:40:35 -04001592 // The dst must not be a multisampled render target, expect in the case where the dst is the
1593 // resolve texture connected to the msaa src. We check for this in case we are copying a part of
1594 // a surface to a different region in the same surface.
1595 if (dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1 && dst != src) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001596 return false;
1597 }
1598
1599 // Surfaces must have the same origin.
1600 if (src->origin() != dst->origin()) {
1601 return false;
1602 }
1603
1604 return true;
1605}
1606
1607void GrVkGpu::copySurfaceAsResolve(GrSurface* dst,
1608 GrSurface* src,
1609 const SkIRect& srcRect,
1610 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001611 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Greg Danielbc26c392017-04-18 13:32:10 -04001612 this->resolveImage(dst, srcRT, srcRect, dstPoint);
egdaniel4bcd62e2016-08-31 07:37:31 -07001613}
1614
Greg Daniel164a9f02016-02-22 09:56:40 -05001615bool GrVkGpu::onCopySurface(GrSurface* dst,
1616 GrSurface* src,
1617 const SkIRect& srcRect,
1618 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001619 if (can_copy_as_resolve(dst, src, this)) {
1620 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001621 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001622 }
1623
egdanielfd016d72016-09-27 12:13:05 -07001624 if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) {
1625 this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue);
1626 }
1627
egdanielbc9b2962016-09-27 08:00:53 -07001628 if (fCopyManager.copySurfaceAsDraw(this, dst, src, srcRect, dstPoint)) {
1629 return true;
1630 }
1631
egdaniel17b89252016-04-05 07:23:38 -07001632 GrVkImage* dstImage;
1633 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001634 GrRenderTarget* dstRT = dst->asRenderTarget();
1635 if (dstRT) {
1636 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
1637 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1638 } else {
1639 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001640 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001641 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001642 GrRenderTarget* srcRT = src->asRenderTarget();
1643 if (srcRT) {
1644 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
1645 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07001646 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07001647 SkASSERT(src->asTexture());
1648 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07001649 }
1650
Greg Daniel164a9f02016-02-22 09:56:40 -05001651 if (can_copy_image(dst, src, this)) {
egdaniel17b89252016-04-05 07:23:38 -07001652 this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint);
1653 return true;
1654 }
1655
1656 if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) {
1657 this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001658 return true;
1659 }
1660
Greg Daniel164a9f02016-02-22 09:56:40 -05001661 return false;
1662}
1663
csmartdaltonc25c5d72016-11-01 07:03:59 -07001664void GrVkGpu::onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&,
1665 int* effectiveSampleCnt, SamplePattern*) {
cdalton28f45b92016-03-07 13:58:26 -08001666 // TODO: stub.
1667 SkASSERT(!this->caps()->sampleLocationsSupport());
1668 *effectiveSampleCnt = rt->desc().fSampleCnt;
1669}
1670
Greg Daniel164a9f02016-02-22 09:56:40 -05001671bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
1672 GrPixelConfig readConfig, DrawPreference* drawPreference,
1673 ReadPixelTempDrawInfo* tempDrawInfo) {
egdaniel88e8aef2016-06-27 14:34:55 -07001674 // These settings we will always want if a temp draw is performed.
1675 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1676 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
1677 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
1678 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
1679 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07001680 tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox;
egdaniel88e8aef2016-06-27 14:34:55 -07001681
1682 // For now assume no swizzling, we may change that below.
1683 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
1684
1685 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
1686 // from will be srcConfig and we will read readConfig pixels from it.
Brian Osman33910292017-04-18 14:38:53 -04001687 // Note that if we require a draw and return a non-renderable format for the temp surface the
egdaniel88e8aef2016-06-27 14:34:55 -07001688 // base class will fail for us.
1689 tempDrawInfo->fTempSurfaceDesc.fConfig = srcSurface->config();
1690 tempDrawInfo->fReadConfig = readConfig;
1691
egdaniel4583ec52016-06-27 12:57:00 -07001692 if (srcSurface->config() == readConfig) {
1693 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -05001694 }
1695
Brian Osman33910292017-04-18 14:38:53 -04001696 // Any config change requires a draw
1697 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1698 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
1699 tempDrawInfo->fReadConfig = readConfig;
Greg Daniel164a9f02016-02-22 09:56:40 -05001700
Brian Osman33910292017-04-18 14:38:53 -04001701 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -05001702}
1703
1704bool GrVkGpu::onReadPixels(GrSurface* surface,
1705 int left, int top, int width, int height,
1706 GrPixelConfig config,
1707 void* buffer,
1708 size_t rowBytes) {
1709 VkFormat pixelFormat;
1710 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
1711 return false;
1712 }
1713
egdaniel66933552016-08-24 07:22:19 -07001714 GrVkImage* image = nullptr;
1715 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
1716 if (rt) {
1717 // resolve the render target if necessary
1718 switch (rt->getResolveType()) {
1719 case GrVkRenderTarget::kCantResolve_ResolveType:
1720 return false;
1721 case GrVkRenderTarget::kAutoResolves_ResolveType:
1722 break;
1723 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel69d49922017-02-23 09:44:02 -05001724 this->internalResolveRenderTarget(rt, false);
egdaniel66933552016-08-24 07:22:19 -07001725 break;
1726 default:
1727 SkFAIL("Unknown resolve type");
1728 }
1729 image = rt;
1730 } else {
1731 image = static_cast<GrVkTexture*>(surface->asTexture());
1732 }
1733
1734 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001735 return false;
1736 }
1737
1738 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07001739 image->setImageLayout(this,
1740 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1741 VK_ACCESS_TRANSFER_READ_BIT,
1742 VK_PIPELINE_STAGE_TRANSFER_BIT,
1743 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001744
egdaniel6fa0a912016-09-12 11:51:29 -07001745 size_t bpp = GrBytesPerPixel(config);
1746 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05001747 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
Greg Daniel164a9f02016-02-22 09:56:40 -05001748
Greg Daniel164a9f02016-02-22 09:56:40 -05001749 VkBufferImageCopy region;
1750 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07001751
1752 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
1753 if (copyFromOrigin) {
1754 region.imageOffset = { 0, 0, 0 };
1755 region.imageExtent = { (uint32_t)(left + width),
1756 (uint32_t)(flipY ? surface->height() - top : top + height),
1757 1
1758 };
1759 } else {
1760 VkOffset3D offset = {
1761 left,
1762 flipY ? surface->height() - top - height : top,
1763 0
1764 };
1765 region.imageOffset = offset;
1766 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1767 }
1768
1769 size_t transBufferRowBytes = bpp * region.imageExtent.width;
1770 GrVkTransferBuffer* transferBuffer =
1771 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * height,
1772 kXferGpuToCpu_GrBufferType,
1773 kStream_GrAccessPattern));
1774
1775 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07001776 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001777 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001778 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1779 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001780
1781 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07001782 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05001783 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1784 transferBuffer,
1785 1,
1786 &region);
1787
1788 // make sure the copy to buffer has finished
1789 transferBuffer->addMemoryBarrier(this,
1790 VK_ACCESS_TRANSFER_WRITE_BIT,
1791 VK_ACCESS_HOST_READ_BIT,
1792 VK_PIPELINE_STAGE_TRANSFER_BIT,
1793 VK_PIPELINE_STAGE_HOST_BIT,
1794 false);
1795
1796 // We need to submit the current command buffer to the Queue and make sure it finishes before
1797 // we can copy the data out of the buffer.
1798 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth9d54afc2016-09-20 09:20:03 -07001799 GrVkMemory::InvalidateMappedAlloc(this, transferBuffer->alloc());
Greg Daniel164a9f02016-02-22 09:56:40 -05001800 void* mappedMemory = transferBuffer->map();
1801
egdaniel6fa0a912016-09-12 11:51:29 -07001802 if (copyFromOrigin) {
1803 uint32_t skipRows = region.imageExtent.height - height;
1804 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
1805 }
1806
egdaniel88e8aef2016-06-27 14:34:55 -07001807 if (flipY) {
1808 const char* srcRow = reinterpret_cast<const char*>(mappedMemory);
1809 char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes;
1810 for (int y = 0; y < height; y++) {
1811 memcpy(dstRow, srcRow, tightRowBytes);
egdaniel6fa0a912016-09-12 11:51:29 -07001812 srcRow += transBufferRowBytes;
egdaniel88e8aef2016-06-27 14:34:55 -07001813 dstRow -= rowBytes;
1814 }
1815 } else {
Matt Sarettcf3525a2017-01-24 12:43:41 -05001816 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
egdaniel88e8aef2016-06-27 14:34:55 -07001817 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001818
1819 transferBuffer->unmap();
1820 transferBuffer->unref();
Greg Daniel164a9f02016-02-22 09:56:40 -05001821 return true;
1822}
egdaniel066df7c2016-06-08 14:02:27 -07001823
egdaniel27bb2842016-07-07 11:58:35 -07001824// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1825// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1826// the the entire attachment. Similar requirements for the y and height components.
1827void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1828 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1829 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07001830 if ((0 != granularity.width && 1 != granularity.width)) {
1831 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1832 int rightAdj = srcBounds.fRight % granularity.width;
1833 if (rightAdj != 0) {
1834 rightAdj = granularity.width - rightAdj;
1835 }
1836 dstBounds->fRight = srcBounds.fRight + rightAdj;
1837 if (dstBounds->fRight > maxWidth) {
1838 dstBounds->fRight = maxWidth;
1839 dstBounds->fLeft = 0;
1840 } else {
1841 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1842 }
egdaniel27bb2842016-07-07 11:58:35 -07001843 } else {
egdanield5797b32016-09-20 12:57:45 -07001844 dstBounds->fLeft = srcBounds.fLeft;
1845 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07001846 }
1847
1848 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07001849 if ((0 != granularity.height && 1 != granularity.height)) {
1850 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
1851 int bottomAdj = srcBounds.fBottom % granularity.height;
1852 if (bottomAdj != 0) {
1853 bottomAdj = granularity.height - bottomAdj;
1854 }
1855 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
1856 if (dstBounds->fBottom > maxHeight) {
1857 dstBounds->fBottom = maxHeight;
1858 dstBounds->fTop = 0;
1859 } else {
1860 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
1861 }
egdaniel27bb2842016-07-07 11:58:35 -07001862 } else {
egdanield5797b32016-09-20 12:57:45 -07001863 dstBounds->fTop = srcBounds.fTop;
1864 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07001865 }
1866}
1867
Greg Daniel22bc8652017-03-22 15:45:43 -04001868void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07001869 const GrVkRenderPass* renderPass,
1870 const VkClearValue* colorClear,
1871 GrVkRenderTarget* target,
1872 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07001873 const SkIRect* pBounds = &bounds;
1874 SkIRect flippedBounds;
1875 if (kBottomLeft_GrSurfaceOrigin == target->origin()) {
1876 flippedBounds = bounds;
1877 flippedBounds.fTop = target->height() - bounds.fBottom;
1878 flippedBounds.fBottom = target->height() - bounds.fTop;
1879 pBounds = &flippedBounds;
1880 }
1881
egdaniel27bb2842016-07-07 11:58:35 -07001882 // The bounds we use for the render pass should be of the granularity supported
1883 // by the device.
1884 const VkExtent2D& granularity = renderPass->granularity();
1885 SkIRect adjustedBounds;
1886 if ((0 != granularity.width && 1 != granularity.width) ||
1887 (0 != granularity.height && 1 != granularity.height)) {
1888 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
1889 target->width(), target->height());
1890 pBounds = &adjustedBounds;
1891 }
1892
Greg Daniel77a86f82017-01-23 11:04:45 -05001893 fCurrentCmdBuffer->beginRenderPass(this, renderPass, colorClear, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04001894 for (int i = 0; i < buffers.count(); ++i) {
1895 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
1896 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001897 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07001898
egdanielce3bfb12016-08-26 11:05:13 -07001899 this->didWriteToSurface(target, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05001900}
egdaniel9cb63402016-06-23 08:37:05 -07001901
Greg Daniel6be35232017-03-01 17:01:09 -05001902GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07001903 VkFenceCreateInfo createInfo;
1904 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
1905 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1906 createInfo.pNext = nullptr;
1907 createInfo.flags = 0;
1908 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05001909
1910 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
1911 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
1912
1913 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07001914 return (GrFence)fence;
1915}
1916
Greg Daniel6be35232017-03-01 17:01:09 -05001917bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
1918 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
1919
1920 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07001921 return (VK_SUCCESS == result);
1922}
1923
1924void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05001925 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
1926}
1927
1928sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore() {
1929 return GrVkSemaphore::Make(this);
1930}
1931
1932void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
1933 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
1934
1935 this->submitCommandBuffer(kSkip_SyncQueue, vkSem->getResource());
1936}
1937
1938void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
1939 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
1940
1941 const GrVkSemaphore::Resource* resource = vkSem->getResource();
1942 resource->ref();
1943 fSemaphoresToWaitOn.push_back(resource);
jvanverth84741b32016-09-30 08:39:02 -07001944}
1945
Brian Osman2c2bc112017-02-28 10:02:49 -05001946void GrVkGpu::flush() {
1947 // We submit the command buffer to the queue whenever Ganesh is flushed, so nothing is needed
1948}