blob: 724a50de54d65ccdc1436667620c5b14391317d0 [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
10#include "GrContextOptions.h"
11#include "GrGeometryProcessor.h"
12#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070013#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrPipeline.h"
15#include "GrRenderTargetPriv.h"
16#include "GrSurfacePriv.h"
17#include "GrTexturePriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018
19#include "GrVkCommandBuffer.h"
egdaniel066df7c2016-06-08 14:02:27 -070020#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "GrVkImage.h"
22#include "GrVkIndexBuffer.h"
23#include "GrVkMemory.h"
24#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070025#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050026#include "GrVkRenderPass.h"
27#include "GrVkResourceProvider.h"
28#include "GrVkTexture.h"
29#include "GrVkTextureRenderTarget.h"
30#include "GrVkTransferBuffer.h"
31#include "GrVkVertexBuffer.h"
32
33#include "SkConfig8888.h"
jvanverth900bd4a2016-04-29 13:53:12 -070034#include "SkMipMap.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050035
36#include "vk/GrVkInterface.h"
jvanverthfd359ca2016-03-18 11:57:24 -070037#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050038
ethannicholasb3058bd2016-07-01 08:22:01 -070039#if USE_SKSL
40#include "SkSLCompiler.h"
41#endif
42
Greg Daniel164a9f02016-02-22 09:56:40 -050043#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
44#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
45#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
46
egdaniel735109c2016-07-27 08:03:57 -070047#ifdef SK_ENABLE_VK_LAYERS
jvanverthd2497f32016-03-18 12:39:05 -070048VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback(
49 VkDebugReportFlagsEXT flags,
50 VkDebugReportObjectTypeEXT objectType,
51 uint64_t object,
52 size_t location,
53 int32_t messageCode,
54 const char* pLayerPrefix,
55 const char* pMessage,
56 void* pUserData) {
57 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
58 SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
59 } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) {
60 SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
61 } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
62 SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
63 } else {
64 SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage);
65 }
66 return VK_FALSE;
67}
jvanverthd2497f32016-03-18 12:39:05 -070068#endif
69
jvanverth633b3562016-03-23 11:01:22 -070070GrGpu* GrVkGpu::Create(GrBackendContext backendContext, const GrContextOptions& options,
71 GrContext* context) {
bsalomondc0fcd42016-04-11 14:21:33 -070072 const GrVkBackendContext* vkBackendContext =
73 reinterpret_cast<const GrVkBackendContext*>(backendContext);
jvanverth633b3562016-03-23 11:01:22 -070074 if (!vkBackendContext) {
bsalomondc0fcd42016-04-11 14:21:33 -070075 vkBackendContext = GrVkBackendContext::Create();
jvanverth633b3562016-03-23 11:01:22 -070076 if (!vkBackendContext) {
77 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -050078 }
jvanverth633b3562016-03-23 11:01:22 -070079 } else {
80 vkBackendContext->ref();
Greg Daniel164a9f02016-02-22 09:56:40 -050081 }
82
jvanverth633b3562016-03-23 11:01:22 -070083 return new GrVkGpu(context, options, vkBackendContext);
Greg Daniel164a9f02016-02-22 09:56:40 -050084}
85
86////////////////////////////////////////////////////////////////////////////////
87
halcanary9d524f22016-03-29 09:03:52 -070088GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
jvanverth633b3562016-03-23 11:01:22 -070089 const GrVkBackendContext* backendCtx)
Greg Daniel164a9f02016-02-22 09:56:40 -050090 : INHERITED(context)
jvanverth633b3562016-03-23 11:01:22 -070091 , fDevice(backendCtx->fDevice)
92 , fQueue(backendCtx->fQueue)
93 , fResourceProvider(this) {
94 fBackendContext.reset(backendCtx);
Greg Daniel164a9f02016-02-22 09:56:40 -050095
egdaniel735109c2016-07-27 08:03:57 -070096#ifdef SK_ENABLE_VK_LAYERS
brianosman419ca642016-05-04 08:19:44 -070097 fCallback = VK_NULL_HANDLE;
jvanverthfd7bd452016-03-25 06:29:52 -070098 if (backendCtx->fExtensions & kEXT_debug_report_GrVkExtensionFlag) {
99 // Setup callback creation information
jvanverthd2497f32016-03-18 12:39:05 -0700100 VkDebugReportCallbackCreateInfoEXT callbackCreateInfo;
101 callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
102 callbackCreateInfo.pNext = nullptr;
103 callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT |
egdanielef0c10c2016-04-07 07:51:22 -0700104 VK_DEBUG_REPORT_WARNING_BIT_EXT |
jvanverthd2497f32016-03-18 12:39:05 -0700105 //VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
106 //VK_DEBUG_REPORT_DEBUG_BIT_EXT |
egdanielb4aa3622016-04-06 13:47:08 -0700107 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
jvanverthd2497f32016-03-18 12:39:05 -0700108 callbackCreateInfo.pfnCallback = &DebugReportCallback;
109 callbackCreateInfo.pUserData = nullptr;
110
jvanverthfd7bd452016-03-25 06:29:52 -0700111 // Register the callback
jvanvertha00980e2016-05-02 13:24:48 -0700112 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateDebugReportCallbackEXT(
113 backendCtx->fInstance, &callbackCreateInfo, nullptr, &fCallback));
jvanverthd2497f32016-03-18 12:39:05 -0700114 }
115#endif
jvanverth633b3562016-03-23 11:01:22 -0700116
ethannicholasb3058bd2016-07-01 08:22:01 -0700117#if USE_SKSL
118 fCompiler = new SkSL::Compiler();
119#else
jvanverth633b3562016-03-23 11:01:22 -0700120 fCompiler = shaderc_compiler_initialize();
ethannicholasb3058bd2016-07-01 08:22:01 -0700121#endif
jvanverth633b3562016-03-23 11:01:22 -0700122
jvanverthfd7bd452016-03-25 06:29:52 -0700123 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice,
egdanielc5ec1402016-03-28 12:14:42 -0700124 backendCtx->fFeatures, backendCtx->fExtensions));
jvanverth633b3562016-03-23 11:01:22 -0700125 fCaps.reset(SkRef(fVkCaps.get()));
126
127 VK_CALL(GetPhysicalDeviceMemoryProperties(backendCtx->fPhysicalDevice, &fPhysDevMemProps));
128
129 const VkCommandPoolCreateInfo cmdPoolInfo = {
jvanverth7ec92412016-07-06 09:24:57 -0700130 VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType
131 nullptr, // pNext
132 VK_COMMAND_POOL_CREATE_TRANSIENT_BIT |
133 VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags
134 backendCtx->fGraphicsQueueIndex, // queueFamilyIndex
jvanverth633b3562016-03-23 11:01:22 -0700135 };
halcanary9d524f22016-03-29 09:03:52 -0700136 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr,
jvanverth633b3562016-03-23 11:01:22 -0700137 &fCmdPool));
138
139 // must call this after creating the CommandPool
140 fResourceProvider.init();
jvanverth7ec92412016-07-06 09:24:57 -0700141 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
jvanverth633b3562016-03-23 11:01:22 -0700142 SkASSERT(fCurrentCmdBuffer);
143 fCurrentCmdBuffer->begin(this);
jvanverth6b6ffc42016-06-13 14:28:07 -0700144
145 // set up our heaps
146 fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
egdaniel05dceab2016-06-22 07:45:50 -0700147 // We want the OptimalImage_Heap to use a SubAlloc_strategy but it occasionally causes the
148 // device to run out of memory. Most likely this is caused by fragmentation in the device heap
149 // and we can't allocate more. Until we get a fix moving this to SingleAlloc.
150 fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 64*1024*1024));
jvanverth6b6ffc42016-06-13 14:28:07 -0700151 fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024));
152 fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
153 fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
jvanverth4c6e47a2016-07-22 10:34:52 -0700154 fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024));
jvanverth6b6ffc42016-06-13 14:28:07 -0700155 fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0));
156 fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024));
Greg Daniel164a9f02016-02-22 09:56:40 -0500157}
158
159GrVkGpu::~GrVkGpu() {
Greg Daniel164a9f02016-02-22 09:56:40 -0500160 fCurrentCmdBuffer->end(this);
161 fCurrentCmdBuffer->unref(this);
162
163 // wait for all commands to finish
jvanverthddf98352016-03-21 11:46:00 -0700164 fResourceProvider.checkCommandBuffers();
jvanvertha00980e2016-05-02 13:24:48 -0700165 SkDEBUGCODE(VkResult res = ) VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700166
167 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
168 // on the command buffers even though they have completed. This causes an assert to fire when
169 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
170 // sleep to make sure the fence singals.
171#ifdef SK_DEBUG
172#if defined(SK_BUILD_FOR_WIN)
173 Sleep(10); // In milliseconds
174#else
175 // Uncomment if above bug happens on non windows build.
176 // sleep(1); // In seconds
177#endif
178#endif
179
jvanverthddf98352016-03-21 11:46:00 -0700180 // VK_ERROR_DEVICE_LOST is acceptable when tearing down (see 4.2.4 in spec)
181 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
halcanary9d524f22016-03-29 09:03:52 -0700182
Greg Daniel164a9f02016-02-22 09:56:40 -0500183 // must call this just before we destroy the VkDevice
184 fResourceProvider.destroyResources();
185
jvanverth633b3562016-03-23 11:01:22 -0700186 VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr));
187
ethannicholasb3058bd2016-07-01 08:22:01 -0700188#if USE_SKSL
189 delete fCompiler;
190#else
jvanverth633b3562016-03-23 11:01:22 -0700191 shaderc_compiler_release(fCompiler);
ethannicholasb3058bd2016-07-01 08:22:01 -0700192#endif
jvanverth633b3562016-03-23 11:01:22 -0700193
egdaniel735109c2016-07-27 08:03:57 -0700194#ifdef SK_ENABLE_VK_LAYERS
jvanvertha00980e2016-05-02 13:24:48 -0700195 if (fCallback) {
196 VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr));
brianosman419ca642016-05-04 08:19:44 -0700197 fCallback = VK_NULL_HANDLE;
jvanvertha00980e2016-05-02 13:24:48 -0700198 }
jvanverthd2497f32016-03-18 12:39:05 -0700199#endif
Greg Daniel164a9f02016-02-22 09:56:40 -0500200}
201
202///////////////////////////////////////////////////////////////////////////////
203
egdaniel9cb63402016-06-23 08:37:05 -0700204GrGpuCommandBuffer* GrVkGpu::createCommandBuffer(
205 GrRenderTarget* target,
206 const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo,
207 const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) {
208 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target);
209 return new GrVkGpuCommandBuffer(this, vkRT, colorInfo, stencilInfo);
egdaniel066df7c2016-06-08 14:02:27 -0700210}
211
Greg Daniel164a9f02016-02-22 09:56:40 -0500212void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
213 SkASSERT(fCurrentCmdBuffer);
214 fCurrentCmdBuffer->end(this);
215
216 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync);
217 fResourceProvider.checkCommandBuffers();
218
219 // Release old command buffer and create a new one
220 fCurrentCmdBuffer->unref(this);
jvanverth7ec92412016-07-06 09:24:57 -0700221 fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500222 SkASSERT(fCurrentCmdBuffer);
223
224 fCurrentCmdBuffer->begin(this);
225}
226
227///////////////////////////////////////////////////////////////////////////////
cdalton1bf3e712016-04-19 10:00:02 -0700228GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern,
229 const void* data) {
230 GrBuffer* buff;
cdalton397536c2016-03-25 12:15:03 -0700231 switch (type) {
232 case kVertex_GrBufferType:
233 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
234 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700235 buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700236 break;
cdalton397536c2016-03-25 12:15:03 -0700237 case kIndex_GrBufferType:
238 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
239 kStatic_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700240 buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700241 break;
cdalton397536c2016-03-25 12:15:03 -0700242 case kXferCpuToGpu_GrBufferType:
jvanverthc3d706f2016-04-20 10:33:27 -0700243 SkASSERT(kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700244 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700245 break;
cdalton397536c2016-03-25 12:15:03 -0700246 case kXferGpuToCpu_GrBufferType:
jvanverthc3d706f2016-04-20 10:33:27 -0700247 SkASSERT(kStream_GrAccessPattern == accessPattern);
cdalton1bf3e712016-04-19 10:00:02 -0700248 buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700249 break;
cdalton397536c2016-03-25 12:15:03 -0700250 default:
251 SkFAIL("Unknown buffer type.");
252 return nullptr;
253 }
cdalton1bf3e712016-04-19 10:00:02 -0700254 if (data && buff) {
255 buff->updateData(data, size);
256 }
257 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500258}
259
260////////////////////////////////////////////////////////////////////////////////
261bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
262 GrPixelConfig srcConfig, DrawPreference* drawPreference,
263 WritePixelTempDrawInfo* tempDrawInfo) {
264 if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) {
265 return false;
266 }
267
egdaniel4583ec52016-06-27 12:57:00 -0700268 GrRenderTarget* renderTarget = dstSurface->asRenderTarget();
269
270 // Start off assuming no swizzling
271 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
272 tempDrawInfo->fWriteConfig = srcConfig;
273
274 // These settings we will always want if a temp draw is performed. Initially set the config
275 // to srcConfig, though that may be modified if we decide to do a R/B swap
276 tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
277 tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig;
278 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
279 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
280 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
281 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin;
282
egdanield66110f2016-06-28 13:38:26 -0700283 if (dstSurface->config() == srcConfig) {
284 return true;
285 }
286
egdaniel4583ec52016-06-27 12:57:00 -0700287 if (renderTarget && this->vkCaps().isConfigRenderable(renderTarget->config(), false)) {
288 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
289
290 bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config();
291
292 if (!this->vkCaps().isConfigTexturable(srcConfig) && configsAreRBSwaps) {
293 if (!this->vkCaps().isConfigTexturable(dstSurface->config())) {
294 return false;
295 }
296 tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config();
297 tempDrawInfo->fSwizzle = GrSwizzle::BGRA();
298 tempDrawInfo->fWriteConfig = dstSurface->config();
299 }
300 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -0500301 }
302
egdaniel4583ec52016-06-27 12:57:00 -0700303 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -0500304}
305
306bool GrVkGpu::onWritePixels(GrSurface* surface,
307 int left, int top, int width, int height,
bsalomona1e6b3b2016-03-02 10:58:23 -0800308 GrPixelConfig config,
309 const SkTArray<GrMipLevel>& texels) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500310 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
311 if (!vkTex) {
312 return false;
313 }
314
jvanverth900bd4a2016-04-29 13:53:12 -0700315 // Make sure we have at least the base level
jvanverth03509ea2016-03-02 13:19:47 -0800316 if (texels.empty() || !texels.begin()->fPixels) {
317 return false;
318 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800319
Greg Daniel164a9f02016-02-22 09:56:40 -0500320 // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels.
321 if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) {
322 return false;
323 }
324
325 bool success = false;
326 if (GrPixelConfigIsCompressed(vkTex->desc().fConfig)) {
327 // We check that config == desc.fConfig in GrGpu::getWritePixelsInfo()
328 SkASSERT(config == vkTex->desc().fConfig);
329 // TODO: add compressed texture support
330 // delete the following two lines and uncomment the two after that when ready
331 vkTex->unref();
332 return false;
333 //success = this->uploadCompressedTexData(vkTex->desc(), buffer, false, left, top, width,
334 // height);
335 } else {
336 bool linearTiling = vkTex->isLinearTiled();
jvanverth900bd4a2016-04-29 13:53:12 -0700337 if (linearTiling) {
338 if (texels.count() > 1) {
339 SkDebugf("Can't upload mipmap data to linear tiled texture");
340 return false;
341 }
342 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
343 // Need to change the layout to general in order to perform a host write
jvanverth900bd4a2016-04-29 13:53:12 -0700344 vkTex->setImageLayout(this,
345 VK_IMAGE_LAYOUT_GENERAL,
jvanverth50c46c72016-05-06 12:31:28 -0700346 VK_ACCESS_HOST_WRITE_BIT,
347 VK_PIPELINE_STAGE_HOST_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700348 false);
egdanielbdf88112016-05-03 07:25:56 -0700349 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth900bd4a2016-04-29 13:53:12 -0700350 }
351 success = this->uploadTexDataLinear(vkTex, left, top, width, height, config,
352 texels.begin()->fPixels, texels.begin()->fRowBytes);
353 } else {
jvanverthc578b0632016-05-02 10:58:12 -0700354 int newMipLevels = texels.count();
jvanverth82c05582016-05-03 11:19:01 -0700355 int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1;
356 if (newMipLevels != currentMipLevels) {
jvanverthc578b0632016-05-02 10:58:12 -0700357 if (!vkTex->reallocForMipmap(this, newMipLevels)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700358 return false;
359 }
360 }
361 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, config, texels);
Greg Daniel164a9f02016-02-22 09:56:40 -0500362 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500363 }
egdaniel4583ec52016-06-27 12:57:00 -0700364
jvanverth900bd4a2016-04-29 13:53:12 -0700365 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500366}
367
jvanverth900bd4a2016-04-29 13:53:12 -0700368bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex,
369 int left, int top, int width, int height,
370 GrPixelConfig dataConfig,
371 const void* data,
372 size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500373 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700374 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500375
376 // If we're uploading compressed data then we should be using uploadCompressedTexData
377 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
378
Greg Daniel164a9f02016-02-22 09:56:40 -0500379 size_t bpp = GrBytesPerPixel(dataConfig);
380
381 const GrSurfaceDesc& desc = tex->desc();
382
383 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
384 &width, &height, &data, &rowBytes)) {
385 return false;
386 }
387 size_t trimRowBytes = width * bpp;
388
jvanverth900bd4a2016-04-29 13:53:12 -0700389 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
390 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
391 const VkImageSubresource subres = {
392 VK_IMAGE_ASPECT_COLOR_BIT,
393 0, // mipLevel
394 0, // arraySlice
395 };
396 VkSubresourceLayout layout;
397 VkResult err;
Greg Daniel164a9f02016-02-22 09:56:40 -0500398
jvanverth900bd4a2016-04-29 13:53:12 -0700399 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500400
jvanverth900bd4a2016-04-29 13:53:12 -0700401 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700402 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700403 &subres,
404 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500405
jvanverth900bd4a2016-04-29 13:53:12 -0700406 int texTop = kBottomLeft_GrSurfaceOrigin == desc.fOrigin ? tex->height() - top - height : top;
jvanverth1e305ba2016-06-01 09:39:15 -0700407 const GrVkAlloc& alloc = tex->alloc();
408 VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700409 VkDeviceSize size = height*layout.rowPitch;
410 void* mapPtr;
jvanverth1e305ba2016-06-01 09:39:15 -0700411 err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr));
jvanverth900bd4a2016-04-29 13:53:12 -0700412 if (err) {
413 return false;
414 }
415
416 if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) {
417 // copy into buffer by rows
418 const char* srcRow = reinterpret_cast<const char*>(data);
419 char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch;
420 for (int y = 0; y < height; y++) {
421 memcpy(dstRow, srcRow, trimRowBytes);
422 srcRow += rowBytes;
423 dstRow -= layout.rowPitch;
424 }
425 } else {
426 // If there is no padding on the src (rowBytes) or dst (layout.rowPitch) we can memcpy
427 if (trimRowBytes == rowBytes && trimRowBytes == layout.rowPitch) {
428 memcpy(mapPtr, data, trimRowBytes * height);
429 } else {
egdaniel88e8aef2016-06-27 14:34:55 -0700430 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
431 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700432 }
433 }
434
jvanverth1e305ba2016-06-01 09:39:15 -0700435 GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory));
jvanverth900bd4a2016-04-29 13:53:12 -0700436
437 return true;
438}
439
440bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex,
jvanvertha584de92016-06-30 09:10:52 -0700441 int left, int top, int width, int height,
442 GrPixelConfig dataConfig,
443 const SkTArray<GrMipLevel>& texels) {
jvanverth900bd4a2016-04-29 13:53:12 -0700444 SkASSERT(!tex->isLinearTiled());
445 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
446 SkASSERT(1 == texels.count() ||
447 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
448
449 // If we're uploading compressed data then we should be using uploadCompressedTexData
450 SkASSERT(!GrPixelConfigIsCompressed(dataConfig));
451
452 if (width == 0 || height == 0) {
453 return false;
454 }
455
456 const GrSurfaceDesc& desc = tex->desc();
457 SkASSERT(this->caps()->isConfigTexturable(desc.fConfig));
458 size_t bpp = GrBytesPerPixel(dataConfig);
459
460 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700461 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
462 // Because of this we need to make a non-const shallow copy of texels.
463 SkTArray<GrMipLevel> texelsShallowCopy(texels);
jvanverth900bd4a2016-04-29 13:53:12 -0700464
jvanverthc578b0632016-05-02 10:58:12 -0700465 for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0;
466 currentMipLevel--) {
467 SkASSERT(texelsShallowCopy[currentMipLevel].fPixels);
Greg Daniel164a9f02016-02-22 09:56:40 -0500468 }
469
jvanverth900bd4a2016-04-29 13:53:12 -0700470 // Determine whether we need to flip when we copy into the buffer
jvanverthc578b0632016-05-02 10:58:12 -0700471 bool flipY = (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty());
jvanverth900bd4a2016-04-29 13:53:12 -0700472
jvanverthc578b0632016-05-02 10:58:12 -0700473 // adjust any params (left, top, currentWidth, currentHeight
jvanverth900bd4a2016-04-29 13:53:12 -0700474 // find the combined size of all the mip levels and the relative offset of
475 // each into the collective buffer
jvanverthc578b0632016-05-02 10:58:12 -0700476 // Do the first level separately because we may need to adjust width and height
477 // (for the non-mipped case).
478 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
479 &width,
480 &height,
481 &texelsShallowCopy[0].fPixels,
482 &texelsShallowCopy[0].fRowBytes)) {
483 return false;
484 }
485 SkTArray<size_t> individualMipOffsets(texelsShallowCopy.count());
486 individualMipOffsets.push_back(0);
487 size_t combinedBufferSize = width * bpp * height;
488 int currentWidth = width;
489 int currentHeight = height;
490 for (int currentMipLevel = 1; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
491 currentWidth = SkTMax(1, currentWidth/2);
492 currentHeight = SkTMax(1, currentHeight/2);
493 if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top,
494 &currentWidth,
495 &currentHeight,
496 &texelsShallowCopy[currentMipLevel].fPixels,
497 &texelsShallowCopy[currentMipLevel].fRowBytes)) {
498 return false;
499 }
jvanverth900bd4a2016-04-29 13:53:12 -0700500 const size_t trimmedSize = currentWidth * bpp * currentHeight;
501 individualMipOffsets.push_back(combinedBufferSize);
502 combinedBufferSize += trimmedSize;
503 }
504
505 // allocate buffer to hold our mip data
506 GrVkTransferBuffer* transferBuffer =
507 GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
508
509 char* buffer = (char*) transferBuffer->map();
jvanverthc578b0632016-05-02 10:58:12 -0700510 SkTArray<VkBufferImageCopy> regions(texelsShallowCopy.count());
jvanverth900bd4a2016-04-29 13:53:12 -0700511
jvanverthc578b0632016-05-02 10:58:12 -0700512 currentWidth = width;
513 currentHeight = height;
514 for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) {
jvanverth900bd4a2016-04-29 13:53:12 -0700515 const size_t trimRowBytes = currentWidth * bpp;
jvanverthc578b0632016-05-02 10:58:12 -0700516 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700517
518 // copy data into the buffer, skipping the trailing bytes
519 char* dst = buffer + individualMipOffsets[currentMipLevel];
jvanverthc578b0632016-05-02 10:58:12 -0700520 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
jvanverth900bd4a2016-04-29 13:53:12 -0700521 if (flipY) {
522 src += (currentHeight - 1) * rowBytes;
523 for (int y = 0; y < currentHeight; y++) {
524 memcpy(dst, src, trimRowBytes);
525 src -= rowBytes;
526 dst += trimRowBytes;
527 }
528 } else if (trimRowBytes == rowBytes) {
529 memcpy(dst, src, trimRowBytes * currentHeight);
530 } else {
531 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
532 }
533
534 VkBufferImageCopy& region = regions.push_back();
535 memset(&region, 0, sizeof(VkBufferImageCopy));
jvanverthdb379092016-07-07 11:18:46 -0700536 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
jvanverth900bd4a2016-04-29 13:53:12 -0700537 region.bufferRowLength = currentWidth;
538 region.bufferImageHeight = currentHeight;
bsalomoncf942c42016-04-29 18:30:06 -0700539 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
jvanverthc578b0632016-05-02 10:58:12 -0700540 region.imageOffset = { left, flipY ? tex->height() - top - currentHeight : top, 0 };
jvanverth900bd4a2016-04-29 13:53:12 -0700541 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
egdaniel4583ec52016-06-27 12:57:00 -0700542
jvanverthc578b0632016-05-02 10:58:12 -0700543 currentWidth = SkTMax(1, currentWidth/2);
544 currentHeight = SkTMax(1, currentHeight/2);
jvanverth900bd4a2016-04-29 13:53:12 -0700545 }
546
547 transferBuffer->unmap();
548
549 // make sure the unmap has finished
550 transferBuffer->addMemoryBarrier(this,
551 VK_ACCESS_HOST_WRITE_BIT,
552 VK_ACCESS_TRANSFER_READ_BIT,
553 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
554 VK_PIPELINE_STAGE_TRANSFER_BIT,
555 false);
556
557 // Change layout of our target so it can be copied to
jvanverth900bd4a2016-04-29 13:53:12 -0700558 tex->setImageLayout(this,
559 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -0700560 VK_ACCESS_TRANSFER_WRITE_BIT,
561 VK_PIPELINE_STAGE_TRANSFER_BIT,
jvanverth900bd4a2016-04-29 13:53:12 -0700562 false);
563
564 // Copy the buffer to the image
565 fCurrentCmdBuffer->copyBufferToImage(this,
566 transferBuffer,
567 tex,
568 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
569 regions.count(),
570 regions.begin());
jvanverth900bd4a2016-04-29 13:53:12 -0700571 transferBuffer->unref();
572
Greg Daniel164a9f02016-02-22 09:56:40 -0500573 return true;
574}
575
576////////////////////////////////////////////////////////////////////////////////
kkinnunen2e6055b2016-04-22 01:48:29 -0700577GrTexture* GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
bsalomona1e6b3b2016-03-02 10:58:23 -0800578 const SkTArray<GrMipLevel>& texels) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500579 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
580
581 VkFormat pixelFormat;
582 if (!GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)) {
583 return nullptr;
584 }
585
586 if (!fVkCaps->isConfigTexturable(desc.fConfig)) {
587 return nullptr;
588 }
589
egdaniel0a3a7f72016-06-24 09:22:31 -0700590 if (renderTarget && !fVkCaps->isConfigRenderable(desc.fConfig, false)) {
591 return nullptr;
592 }
593
Greg Daniel164a9f02016-02-22 09:56:40 -0500594 bool linearTiling = false;
595 if (SkToBool(desc.fFlags & kZeroCopy_GrSurfaceFlag)) {
jvanverth900bd4a2016-04-29 13:53:12 -0700596 // we can't have a linear texture with a mipmap
597 if (texels.count() > 1) {
598 SkDebugf("Trying to create linear tiled texture with mipmap");
599 return nullptr;
600 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500601 if (fVkCaps->isConfigTexurableLinearly(desc.fConfig) &&
602 (!renderTarget || fVkCaps->isConfigRenderableLinearly(desc.fConfig, false))) {
603 linearTiling = true;
604 } else {
605 return nullptr;
606 }
607 }
608
609 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
610 if (renderTarget) {
611 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
612 }
613
614 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
615 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
616 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700617 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500618 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
619 // texture.
620 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
621
bsalomona1e6b3b2016-03-02 10:58:23 -0800622 VkFlags memProps = (!texels.empty() && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT :
623 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500624
625 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700626 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500627 // to 1.
jvanverthc578b0632016-05-02 10:58:12 -0700628 int mipLevels = texels.empty() ? 1 : texels.count();
Greg Daniel164a9f02016-02-22 09:56:40 -0500629 GrVkImage::ImageDesc imageDesc;
630 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
631 imageDesc.fFormat = pixelFormat;
632 imageDesc.fWidth = desc.fWidth;
633 imageDesc.fHeight = desc.fHeight;
jvanverthc578b0632016-05-02 10:58:12 -0700634 imageDesc.fLevels = linearTiling ? 1 : mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500635 imageDesc.fSamples = 1;
636 imageDesc.fImageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
637 imageDesc.fUsageFlags = usageFlags;
638 imageDesc.fMemProps = memProps;
639
640 GrVkTexture* tex;
641 if (renderTarget) {
kkinnunen2e6055b2016-04-22 01:48:29 -0700642 tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc,
Greg Daniel164a9f02016-02-22 09:56:40 -0500643 imageDesc);
644 } else {
kkinnunen2e6055b2016-04-22 01:48:29 -0700645 tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc);
Greg Daniel164a9f02016-02-22 09:56:40 -0500646 }
647
648 if (!tex) {
649 return nullptr;
650 }
651
bsalomone699d0c2016-03-09 06:25:15 -0800652 if (!texels.empty()) {
653 SkASSERT(texels.begin()->fPixels);
jvanverth900bd4a2016-04-29 13:53:12 -0700654 bool success;
655 if (linearTiling) {
656 success = this->uploadTexDataLinear(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
657 texels.begin()->fPixels, texels.begin()->fRowBytes);
658 } else {
659 success = this->uploadTexDataOptimal(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
660 texels);
661 }
662 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500663 tex->unref();
664 return nullptr;
665 }
666 }
667
668 return tex;
669}
670
671////////////////////////////////////////////////////////////////////////////////
672
jvanverthdb379092016-07-07 11:18:46 -0700673bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
674 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -0700675
676 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -0700677 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -0700678
679 return true;
680}
681
682////////////////////////////////////////////////////////////////////////////////
683
Greg Daniel164a9f02016-02-22 09:56:40 -0500684static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin) {
685 // By default, all textures in Vk use TopLeft
686 if (kDefault_GrSurfaceOrigin == origin) {
687 return kTopLeft_GrSurfaceOrigin;
688 } else {
689 return origin;
690 }
691}
692
693GrTexture* GrVkGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc,
694 GrWrapOwnership ownership) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500695 if (0 == desc.fTextureHandle) {
696 return nullptr;
697 }
698
699 int maxSize = this->caps()->maxTextureSize();
700 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
701 return nullptr;
702 }
703
egdanielb2df0c22016-05-13 11:30:37 -0700704 const GrVkImageInfo* info = reinterpret_cast<const GrVkImageInfo*>(desc.fTextureHandle);
jvanverth1e305ba2016-06-01 09:39:15 -0700705 if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) {
jvanverthfd359ca2016-03-18 11:57:24 -0700706 return nullptr;
707 }
egdanielb2df0c22016-05-13 11:30:37 -0700708#ifdef SK_DEBUG
709 VkFormat format;
710 if (!GrPixelConfigToVkFormat(desc.fConfig, &format)) {
711 return nullptr;
712 }
713 SkASSERT(format == info->fFormat);
714#endif
Greg Daniel164a9f02016-02-22 09:56:40 -0500715
Greg Daniel164a9f02016-02-22 09:56:40 -0500716 GrSurfaceDesc surfDesc;
717 // next line relies on GrBackendTextureDesc's flags matching GrTexture's
718 surfDesc.fFlags = (GrSurfaceFlags)desc.fFlags;
719 surfDesc.fWidth = desc.fWidth;
720 surfDesc.fHeight = desc.fHeight;
721 surfDesc.fConfig = desc.fConfig;
722 surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
723 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
724 // In GL, Chrome assumes all textures are BottomLeft
725 // In VK, we don't have this restriction
726 surfDesc.fOrigin = resolve_origin(desc.fOrigin);
727
728 GrVkTexture* texture = nullptr;
729 if (renderTarget) {
halcanary9d524f22016-03-29 09:03:52 -0700730 texture = GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(this, surfDesc,
egdanielb2df0c22016-05-13 11:30:37 -0700731 ownership, info);
Greg Daniel164a9f02016-02-22 09:56:40 -0500732 } else {
egdanielb2df0c22016-05-13 11:30:37 -0700733 texture = GrVkTexture::CreateWrappedTexture(this, surfDesc, ownership, info);
Greg Daniel164a9f02016-02-22 09:56:40 -0500734 }
735 if (!texture) {
736 return nullptr;
737 }
738
739 return texture;
740}
741
742GrRenderTarget* GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc,
743 GrWrapOwnership ownership) {
halcanary9d524f22016-03-29 09:03:52 -0700744
egdanielb2df0c22016-05-13 11:30:37 -0700745 const GrVkImageInfo* info =
746 reinterpret_cast<const GrVkImageInfo*>(wrapDesc.fRenderTargetHandle);
jvanverthfd359ca2016-03-18 11:57:24 -0700747 if (VK_NULL_HANDLE == info->fImage ||
jvanverth1e305ba2016-06-01 09:39:15 -0700748 (VK_NULL_HANDLE == info->fAlloc.fMemory && kAdopt_GrWrapOwnership == ownership)) {
jvanverthfd359ca2016-03-18 11:57:24 -0700749 return nullptr;
750 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500751
Greg Daniel164a9f02016-02-22 09:56:40 -0500752 GrSurfaceDesc desc;
753 desc.fConfig = wrapDesc.fConfig;
754 desc.fFlags = kCheckAllocation_GrSurfaceFlag;
755 desc.fWidth = wrapDesc.fWidth;
756 desc.fHeight = wrapDesc.fHeight;
757 desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount());
758
759 desc.fOrigin = resolve_origin(wrapDesc.fOrigin);
760
761 GrVkRenderTarget* tgt = GrVkRenderTarget::CreateWrappedRenderTarget(this, desc,
kkinnunen2e6055b2016-04-22 01:48:29 -0700762 ownership,
jvanverthfd359ca2016-03-18 11:57:24 -0700763 info);
Greg Daniel164a9f02016-02-22 09:56:40 -0500764 if (tgt && wrapDesc.fStencilBits) {
765 if (!createStencilAttachmentForRenderTarget(tgt, desc.fWidth, desc.fHeight)) {
766 tgt->unref();
767 return nullptr;
768 }
769 }
770 return tgt;
771}
772
egdaniel50ead532016-07-13 14:23:26 -0700773void GrVkGpu::generateMipmap(GrVkTexture* tex) {
jvanverth900bd4a2016-04-29 13:53:12 -0700774 // don't do anything for linearly tiled textures (can't have mipmaps)
jvanverth62340062016-04-26 08:01:44 -0700775 if (tex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -0700776 SkDebugf("Trying to create mipmap for linear tiled texture");
jvanverth62340062016-04-26 08:01:44 -0700777 return;
778 }
779
egdaniel7ac5da82016-07-15 13:41:42 -0700780 // We currently don't support generating mipmaps for images that are multisampled.
781 // TODO: Add support for mipmapping the resolve target of a multisampled image
jvanverth62340062016-04-26 08:01:44 -0700782 if (tex->asRenderTarget() && tex->asRenderTarget()->numColorSamples() > 1) {
783 return;
784 }
785
786 // determine if we can blit to and from this format
787 const GrVkCaps& caps = this->vkCaps();
788 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -0700789 !caps.configCanBeSrcofBlit(tex->config(), false) ||
790 !caps.mipMapSupport()) {
jvanverth62340062016-04-26 08:01:44 -0700791 return;
792 }
793
egdaniel7ac5da82016-07-15 13:41:42 -0700794 int width = tex->width();
795 int height = tex->height();
796 VkImageBlit blitRegion;
797 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -0700798
jvanverth82c05582016-05-03 11:19:01 -0700799 // SkMipMap doesn't include the base level in the level count so we have to add 1
800 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
egdaniel7ac5da82016-07-15 13:41:42 -0700801 if (levelCount != tex->mipLevels()) {
802 const GrVkResource* oldResource = tex->resource();
803 oldResource->ref();
804 // grab handle to the original image resource
805 VkImage oldImage = tex->image();
806
807 // change the original image's layout so we can copy from it
808 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
809 VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
810
811 if (!tex->reallocForMipmap(this, levelCount)) {
812 oldResource->unref(this);
813 return;
814 }
815 // change the new image's layout so we can blit to it
816 tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
817 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
818
819 // Blit original image to top level of new image
820 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
821 blitRegion.srcOffsets[0] = { 0, 0, 0 };
822 blitRegion.srcOffsets[1] = { width, height, 1 };
823 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
824 blitRegion.dstOffsets[0] = { 0, 0, 0 };
825 blitRegion.dstOffsets[1] = { width, height, 1 };
826
827 fCurrentCmdBuffer->blitImage(this,
828 oldResource,
829 oldImage,
830 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
831 tex->resource(),
832 tex->image(),
833 VK_IMAGE_LAYOUT_GENERAL,
834 1,
835 &blitRegion,
836 VK_FILTER_LINEAR);
837
jvanverth62340062016-04-26 08:01:44 -0700838 oldResource->unref(this);
egdaniel7ac5da82016-07-15 13:41:42 -0700839 } else {
840 // change layout of the layers so we can write to them.
841 tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL,
842 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -0700843 }
844
jvanverth50c46c72016-05-06 12:31:28 -0700845 // setup memory barrier
egdanielb2df0c22016-05-13 11:30:37 -0700846 SkASSERT(GrVkFormatToPixelConfig(tex->imageFormat(), nullptr));
jvanverth50c46c72016-05-06 12:31:28 -0700847 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
848 VkImageMemoryBarrier imageMemoryBarrier = {
849 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
850 NULL, // pNext
egdaniel7ac5da82016-07-15 13:41:42 -0700851 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
852 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
jvanverth50c46c72016-05-06 12:31:28 -0700853 VK_IMAGE_LAYOUT_GENERAL, // oldLayout
854 VK_IMAGE_LAYOUT_GENERAL, // newLayout
855 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
856 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
egdanielb2df0c22016-05-13 11:30:37 -0700857 tex->image(), // image
jvanverth50c46c72016-05-06 12:31:28 -0700858 { aspectFlags, 0, 1, 0, 1 } // subresourceRange
859 };
860
jvanverth62340062016-04-26 08:01:44 -0700861 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -0700862 uint32_t mipLevel = 1;
863 while (mipLevel < levelCount) {
864 int prevWidth = width;
865 int prevHeight = height;
866 width = SkTMax(1, width / 2);
867 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -0700868
jvanverth50c46c72016-05-06 12:31:28 -0700869 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
870 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
871 false, &imageMemoryBarrier);
872
873 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700874 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700875 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -0700876 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
877 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -0700878 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -0700879 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -0700880 *tex,
881 *tex,
jvanverth62340062016-04-26 08:01:44 -0700882 1,
883 &blitRegion,
884 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -0700885 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -0700886 }
jvanverth62340062016-04-26 08:01:44 -0700887}
888
Greg Daniel164a9f02016-02-22 09:56:40 -0500889////////////////////////////////////////////////////////////////////////////////
890
891GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
892 int width,
893 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500894 SkASSERT(width >= rt->width());
895 SkASSERT(height >= rt->height());
896
897 int samples = rt->numStencilSamples();
898
egdaniel8f1dcaa2016-04-01 10:10:45 -0700899 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -0500900
901 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -0500902 width,
903 height,
904 samples,
905 sFmt));
906 fStats.incStencilAttachmentCreates();
907 return stencil;
908}
909
910////////////////////////////////////////////////////////////////////////////////
911
912GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -0700913 GrPixelConfig config,
914 bool isRenderTarget) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500915
916 VkFormat pixelFormat;
917 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
918 return 0;
919 }
920
921 bool linearTiling = false;
922 if (!fVkCaps->isConfigTexturable(config)) {
923 return 0;
924 }
925
egdaniel0a3a7f72016-06-24 09:22:31 -0700926 if (isRenderTarget && !fVkCaps->isConfigRenderable(config, false)) {
927 return 0;
928 }
929
930 if (fVkCaps->isConfigTexurableLinearly(config) &&
931 (!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false))) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500932 linearTiling = true;
933 }
934
935 // Currently this is not supported since it requires a copy which has not yet been implemented.
936 if (srcData && !linearTiling) {
937 return 0;
938 }
939
940 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
941 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
942 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
egdaniel0a3a7f72016-06-24 09:22:31 -0700943 if (isRenderTarget) {
944 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
945 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500946
jvanverthfd359ca2016-03-18 11:57:24 -0700947 VkImage image = VK_NULL_HANDLE;
jvanverth6b6ffc42016-06-13 14:28:07 -0700948 GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0 };
Greg Daniel164a9f02016-02-22 09:56:40 -0500949
jvanverthfd359ca2016-03-18 11:57:24 -0700950 VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
951 VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling)
952 ? VK_IMAGE_LAYOUT_PREINITIALIZED
953 : VK_IMAGE_LAYOUT_UNDEFINED;
954
955 // Create Image
956 VkSampleCountFlagBits vkSamples;
957 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
958 return 0;
959 }
960
961 const VkImageCreateInfo imageCreateInfo = {
962 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
963 NULL, // pNext
964 0, // VkImageCreateFlags
965 VK_IMAGE_TYPE_2D, // VkImageType
966 pixelFormat, // VkFormat
ethannicholas384b5e92016-03-25 11:04:06 -0700967 { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D
jvanverthfd359ca2016-03-18 11:57:24 -0700968 1, // mipLevels
969 1, // arrayLayers
970 vkSamples, // samples
971 imageTiling, // VkImageTiling
972 usageFlags, // VkImageUsageFlags
973 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
974 0, // queueFamilyCount
975 0, // pQueueFamilyIndices
976 initialLayout // initialLayout
977 };
978
979 GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
980
jvanverth6b6ffc42016-06-13 14:28:07 -0700981 if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) {
jvanverthfd359ca2016-03-18 11:57:24 -0700982 VK_CALL(DestroyImage(this->device(), image, nullptr));
Greg Daniel164a9f02016-02-22 09:56:40 -0500983 return 0;
984 }
985
986 if (srcData) {
987 if (linearTiling) {
988 const VkImageSubresource subres = {
989 VK_IMAGE_ASPECT_COLOR_BIT,
990 0, // mipLevel
991 0, // arraySlice
992 };
993 VkSubresourceLayout layout;
994 VkResult err;
995
jvanverthfd359ca2016-03-18 11:57:24 -0700996 VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500997
998 void* mapPtr;
jvanverth1e305ba2016-06-01 09:39:15 -0700999 err = VK_CALL(MapMemory(fDevice, alloc.fMemory, alloc.fOffset, layout.rowPitch * h,
1000 0, &mapPtr));
Greg Daniel164a9f02016-02-22 09:56:40 -05001001 if (err) {
jvanverth6b6ffc42016-06-13 14:28:07 -07001002 GrVkMemory::FreeImageMemory(this, linearTiling, alloc);
jvanverthfd359ca2016-03-18 11:57:24 -07001003 VK_CALL(DestroyImage(this->device(), image, nullptr));
Greg Daniel164a9f02016-02-22 09:56:40 -05001004 return 0;
1005 }
1006
1007 size_t bpp = GrBytesPerPixel(config);
1008 size_t rowCopyBytes = bpp * w;
1009 // If there is no padding on dst (layout.rowPitch) we can do a single memcopy.
1010 // This assumes the srcData comes in with no padding.
1011 if (rowCopyBytes == layout.rowPitch) {
1012 memcpy(mapPtr, srcData, rowCopyBytes * h);
1013 } else {
jvanverthfd359ca2016-03-18 11:57:24 -07001014 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), srcData, rowCopyBytes,
1015 rowCopyBytes, h);
Greg Daniel164a9f02016-02-22 09:56:40 -05001016 }
jvanverth1e305ba2016-06-01 09:39:15 -07001017 VK_CALL(UnmapMemory(fDevice, alloc.fMemory));
Greg Daniel164a9f02016-02-22 09:56:40 -05001018 } else {
1019 // TODO: Add support for copying to optimal tiling
1020 SkASSERT(false);
1021 }
1022 }
1023
egdanielb2df0c22016-05-13 11:30:37 -07001024 GrVkImageInfo* info = new GrVkImageInfo;
jvanverthfd359ca2016-03-18 11:57:24 -07001025 info->fImage = image;
1026 info->fAlloc = alloc;
1027 info->fImageTiling = imageTiling;
1028 info->fImageLayout = initialLayout;
egdaniel58a8d922016-04-21 08:03:10 -07001029 info->fFormat = pixelFormat;
jvanverth2af0f1b2016-05-03 10:36:49 -07001030 info->fLevelCount = 1;
jvanverthfd359ca2016-03-18 11:57:24 -07001031
1032 return (GrBackendObject)info;
Greg Daniel164a9f02016-02-22 09:56:40 -05001033}
1034
1035bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const {
egdanielb2df0c22016-05-13 11:30:37 -07001036 const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id);
Greg Daniel164a9f02016-02-22 09:56:40 -05001037
jvanverth1e305ba2016-06-01 09:39:15 -07001038 if (backend && backend->fImage && backend->fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001039 VkMemoryRequirements req;
1040 memset(&req, 0, sizeof(req));
1041 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
1042 backend->fImage,
1043 &req));
1044 // TODO: find a better check
1045 // This will probably fail with a different driver
1046 return (req.size > 0) && (req.size <= 8192 * 8192);
1047 }
1048
1049 return false;
1050}
1051
1052void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) {
jvanverth6b6ffc42016-06-13 14:28:07 -07001053 GrVkImageInfo* backend = reinterpret_cast<GrVkImageInfo*>(id);
Greg Daniel164a9f02016-02-22 09:56:40 -05001054 if (backend) {
1055 if (!abandon) {
jvanverthfd359ca2016-03-18 11:57:24 -07001056 // something in the command buffer may still be using this, so force submit
1057 this->submitCommandBuffer(kForce_SyncQueue);
jvanverth6b6ffc42016-06-13 14:28:07 -07001058 GrVkImage::DestroyImageInfo(this, backend);
Greg Daniel164a9f02016-02-22 09:56:40 -05001059 }
jvanverthfd359ca2016-03-18 11:57:24 -07001060 delete backend;
Greg Daniel164a9f02016-02-22 09:56:40 -05001061 }
1062}
1063
1064////////////////////////////////////////////////////////////////////////////////
1065
1066void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1067 VkPipelineStageFlags dstStageMask,
1068 bool byRegion,
1069 VkMemoryBarrier* barrier) const {
1070 SkASSERT(fCurrentCmdBuffer);
1071 fCurrentCmdBuffer->pipelineBarrier(this,
1072 srcStageMask,
1073 dstStageMask,
1074 byRegion,
1075 GrVkCommandBuffer::kMemory_BarrierType,
1076 barrier);
1077}
1078
1079void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1080 VkPipelineStageFlags dstStageMask,
1081 bool byRegion,
1082 VkBufferMemoryBarrier* barrier) const {
1083 SkASSERT(fCurrentCmdBuffer);
1084 fCurrentCmdBuffer->pipelineBarrier(this,
1085 srcStageMask,
1086 dstStageMask,
1087 byRegion,
1088 GrVkCommandBuffer::kBufferMemory_BarrierType,
1089 barrier);
1090}
1091
1092void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1093 VkPipelineStageFlags dstStageMask,
1094 bool byRegion,
1095 VkImageMemoryBarrier* barrier) const {
1096 SkASSERT(fCurrentCmdBuffer);
1097 fCurrentCmdBuffer->pipelineBarrier(this,
1098 srcStageMask,
1099 dstStageMask,
1100 byRegion,
1101 GrVkCommandBuffer::kImageMemory_BarrierType,
1102 barrier);
1103}
1104
1105void GrVkGpu::finishDrawTarget() {
1106 // Submit the current command buffer to the Queue
1107 this->submitCommandBuffer(kSkip_SyncQueue);
1108}
1109
egdaniel3d5d9ac2016-03-01 12:56:15 -08001110void GrVkGpu::clearStencil(GrRenderTarget* target) {
1111 if (nullptr == target) {
1112 return;
1113 }
1114 GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment();
1115 GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil;
1116
1117
1118 VkClearDepthStencilValue vkStencilColor;
1119 memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue));
1120
egdaniel3d5d9ac2016-03-01 12:56:15 -08001121 vkStencil->setImageLayout(this,
1122 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001123 VK_ACCESS_TRANSFER_WRITE_BIT,
1124 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel3d5d9ac2016-03-01 12:56:15 -08001125 false);
1126
egdaniel3d5d9ac2016-03-01 12:56:15 -08001127 VkImageSubresourceRange subRange;
1128 memset(&subRange, 0, sizeof(VkImageSubresourceRange));
1129 subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1130 subRange.baseMipLevel = 0;
1131 subRange.levelCount = 1;
1132 subRange.baseArrayLayer = 0;
1133 subRange.layerCount = 1;
1134
1135 // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a
1136 // draw. Thus we should look into using the load op functions on the render pass to clear out
1137 // the stencil there.
1138 fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange);
1139}
1140
Greg Daniel164a9f02016-02-22 09:56:40 -05001141inline bool can_copy_image(const GrSurface* dst,
1142 const GrSurface* src,
1143 const GrVkGpu* gpu) {
egdaniel17b89252016-04-05 07:23:38 -07001144 // Currently we don't support msaa
1145 if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) ||
1146 (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) {
1147 return false;
1148 }
1149
1150 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
1151 // as image usage flags.
1152 if (src->origin() == dst->origin() &&
1153 GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001154 return true;
1155 }
1156
1157 // How does msaa play into this? If a VkTexture is multisampled, are we copying the multisampled
egdaniel17b89252016-04-05 07:23:38 -07001158 // or the resolved image here? Im multisampled, Vulkan requires sample counts to be the same.
Greg Daniel164a9f02016-02-22 09:56:40 -05001159
1160 return false;
1161}
1162
1163void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst,
1164 GrSurface* src,
egdaniel17b89252016-04-05 07:23:38 -07001165 GrVkImage* dstImage,
1166 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001167 const SkIRect& srcRect,
1168 const SkIPoint& dstPoint) {
1169 SkASSERT(can_copy_image(dst, src, this));
1170
Greg Daniel164a9f02016-02-22 09:56:40 -05001171 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1172 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001173 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001174 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1175 VK_ACCESS_TRANSFER_WRITE_BIT,
1176 VK_PIPELINE_STAGE_TRANSFER_BIT,
1177 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001178
egdaniel17b89252016-04-05 07:23:38 -07001179 srcImage->setImageLayout(this,
1180 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001181 VK_ACCESS_TRANSFER_READ_BIT,
1182 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001183 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001184
1185 // Flip rect if necessary
1186 SkIRect srcVkRect = srcRect;
1187 int32_t dstY = dstPoint.fY;
1188
1189 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
1190 SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin());
1191 srcVkRect.fTop = src->height() - srcRect.fBottom;
1192 srcVkRect.fBottom = src->height() - srcRect.fTop;
1193 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1194 }
1195
1196 VkImageCopy copyRegion;
1197 memset(&copyRegion, 0, sizeof(VkImageCopy));
1198 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1199 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1200 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1201 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001202 // The depth value of the extent is ignored according the vulkan spec for 2D images. However, on
1203 // at least the nexus 5X it seems to be checking it. Thus as a working around we must have the
1204 // depth value be 1.
1205 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001206
1207 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001208 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001209 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001210 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001211 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1212 1,
1213 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001214
1215 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1216 srcRect.width(), srcRect.height());
1217 this->didWriteToSurface(dst, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001218}
1219
egdaniel17b89252016-04-05 07:23:38 -07001220inline bool can_copy_as_blit(const GrSurface* dst,
1221 const GrSurface* src,
1222 const GrVkImage* dstImage,
1223 const GrVkImage* srcImage,
1224 const GrVkGpu* gpu) {
1225 // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src
1226 // as image usage flags.
1227 const GrVkCaps& caps = gpu->vkCaps();
1228 if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) ||
1229 !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) {
1230 return false;
1231 }
1232
1233 // We cannot blit images that are multisampled. Will need to figure out if we can blit the
1234 // resolved msaa though.
1235 if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) ||
1236 (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) {
1237 return false;
1238 }
1239
1240 return true;
1241}
1242
1243void GrVkGpu::copySurfaceAsBlit(GrSurface* dst,
1244 GrSurface* src,
1245 GrVkImage* dstImage,
1246 GrVkImage* srcImage,
1247 const SkIRect& srcRect,
1248 const SkIPoint& dstPoint) {
1249 SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this));
1250
egdaniel17b89252016-04-05 07:23:38 -07001251 dstImage->setImageLayout(this,
1252 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001253 VK_ACCESS_TRANSFER_WRITE_BIT,
1254 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001255 false);
1256
egdaniel17b89252016-04-05 07:23:38 -07001257 srcImage->setImageLayout(this,
1258 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001259 VK_ACCESS_TRANSFER_READ_BIT,
1260 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001261 false);
1262
1263 // Flip rect if necessary
1264 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001265 srcVkRect.fLeft = srcRect.fLeft;
1266 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001267 SkIRect dstRect;
1268 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001269 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001270
1271 if (kBottomLeft_GrSurfaceOrigin == src->origin()) {
1272 srcVkRect.fTop = src->height() - srcRect.fBottom;
1273 srcVkRect.fBottom = src->height() - srcRect.fTop;
1274 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001275 srcVkRect.fTop = srcRect.fTop;
1276 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001277 }
1278
1279 if (kBottomLeft_GrSurfaceOrigin == dst->origin()) {
1280 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1281 } else {
1282 dstRect.fTop = dstPoint.fY;
1283 }
1284 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1285
1286 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1287 // get the correct origintation of the copied data.
1288 if (src->origin() != dst->origin()) {
1289 SkTSwap(dstRect.fTop, dstRect.fBottom);
1290 }
1291
1292 VkImageBlit blitRegion;
1293 memset(&blitRegion, 0, sizeof(VkImageBlit));
1294 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1295 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1296 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 0 };
1297 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1298 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
1299 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 0 };
1300
1301 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001302 *srcImage,
1303 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001304 1,
1305 &blitRegion,
1306 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001307
1308 this->didWriteToSurface(dst, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001309}
1310
Greg Daniel164a9f02016-02-22 09:56:40 -05001311inline bool can_copy_as_draw(const GrSurface* dst,
1312 const GrSurface* src,
1313 const GrVkGpu* gpu) {
1314 return false;
1315}
1316
1317void GrVkGpu::copySurfaceAsDraw(GrSurface* dst,
1318 GrSurface* src,
1319 const SkIRect& srcRect,
1320 const SkIPoint& dstPoint) {
1321 SkASSERT(false);
1322}
1323
1324bool GrVkGpu::onCopySurface(GrSurface* dst,
1325 GrSurface* src,
1326 const SkIRect& srcRect,
1327 const SkIPoint& dstPoint) {
egdaniel17b89252016-04-05 07:23:38 -07001328 GrVkImage* dstImage;
1329 GrVkImage* srcImage;
1330 if (dst->asTexture()) {
1331 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
1332 } else {
1333 SkASSERT(dst->asRenderTarget());
1334 dstImage = static_cast<GrVkRenderTarget*>(dst->asRenderTarget());
1335 }
1336 if (src->asTexture()) {
1337 srcImage = static_cast<GrVkTexture*>(src->asTexture());
1338 } else {
1339 SkASSERT(src->asRenderTarget());
1340 srcImage = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
1341 }
1342
Greg Daniel164a9f02016-02-22 09:56:40 -05001343 if (can_copy_image(dst, src, this)) {
egdaniel17b89252016-04-05 07:23:38 -07001344 this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint);
1345 return true;
1346 }
1347
1348 if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) {
1349 this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05001350 return true;
1351 }
1352
1353 if (can_copy_as_draw(dst, src, this)) {
1354 this->copySurfaceAsDraw(dst, src, srcRect, dstPoint);
1355 return true;
1356 }
1357
1358 return false;
1359}
1360
egdaniel37798fb2016-04-12 07:31:49 -07001361bool GrVkGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const {
1362 // Currently we don't support msaa
1363 if (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1) {
1364 return false;
1365 }
1366
1367 // This will support copying the dst as CopyImage since all of our surfaces require transferSrc
1368 // and transferDst usage flags in Vulkan.
1369 desc->fOrigin = src->origin();
1370 desc->fConfig = src->config();
1371 desc->fFlags = kNone_GrSurfaceFlags;
1372 return true;
1373}
1374
cdalton28f45b92016-03-07 13:58:26 -08001375void GrVkGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&,
csmartdalton0d28e572016-07-06 09:59:43 -07001376 int* effectiveSampleCnt, SamplePattern*) {
cdalton28f45b92016-03-07 13:58:26 -08001377 // TODO: stub.
1378 SkASSERT(!this->caps()->sampleLocationsSupport());
1379 *effectiveSampleCnt = rt->desc().fSampleCnt;
1380}
1381
Greg Daniel164a9f02016-02-22 09:56:40 -05001382bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
1383 GrPixelConfig readConfig, DrawPreference* drawPreference,
1384 ReadPixelTempDrawInfo* tempDrawInfo) {
egdaniel88e8aef2016-06-27 14:34:55 -07001385 // These settings we will always want if a temp draw is performed.
1386 tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1387 tempDrawInfo->fTempSurfaceDesc.fWidth = width;
1388 tempDrawInfo->fTempSurfaceDesc.fHeight = height;
1389 tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0;
1390 tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL.
bsalomonb117ff12016-07-19 07:24:40 -07001391 tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox;
egdaniel88e8aef2016-06-27 14:34:55 -07001392
1393 // For now assume no swizzling, we may change that below.
1394 tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
1395
1396 // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read
1397 // from will be srcConfig and we will read readConfig pixels from it.
1398 // Not that if we require a draw and return a non-renderable format for the temp surface the
1399 // base class will fail for us.
1400 tempDrawInfo->fTempSurfaceDesc.fConfig = srcSurface->config();
1401 tempDrawInfo->fReadConfig = readConfig;
1402
egdaniel4583ec52016-06-27 12:57:00 -07001403 if (srcSurface->config() == readConfig) {
1404 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -05001405 }
1406
egdaniel4583ec52016-06-27 12:57:00 -07001407 if (this->vkCaps().isConfigRenderable(readConfig, false)) {
1408 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
1409 tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig;
1410 tempDrawInfo->fReadConfig = readConfig;
1411 return true;
Greg Daniel164a9f02016-02-22 09:56:40 -05001412 }
1413
egdaniel4583ec52016-06-27 12:57:00 -07001414 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001415}
1416
1417bool GrVkGpu::onReadPixels(GrSurface* surface,
1418 int left, int top, int width, int height,
1419 GrPixelConfig config,
1420 void* buffer,
1421 size_t rowBytes) {
1422 VkFormat pixelFormat;
1423 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
1424 return false;
1425 }
1426
1427 GrVkTexture* tgt = static_cast<GrVkTexture*>(surface->asTexture());
1428 if (!tgt) {
1429 return false;
1430 }
1431
1432 // Change layout of our target so it can be used as copy
Greg Daniel164a9f02016-02-22 09:56:40 -05001433 tgt->setImageLayout(this,
1434 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001435 VK_ACCESS_TRANSFER_READ_BIT,
1436 VK_PIPELINE_STAGE_TRANSFER_BIT,
Greg Daniel164a9f02016-02-22 09:56:40 -05001437 false);
1438
halcanary9d524f22016-03-29 09:03:52 -07001439 GrVkTransferBuffer* transferBuffer =
cdaltone2e71c22016-04-07 18:13:29 -07001440 static_cast<GrVkTransferBuffer*>(this->createBuffer(rowBytes * height,
1441 kXferGpuToCpu_GrBufferType,
cdalton397536c2016-03-25 12:15:03 -07001442 kStream_GrAccessPattern));
Greg Daniel164a9f02016-02-22 09:56:40 -05001443
1444 bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin();
1445 VkOffset3D offset = {
1446 left,
1447 flipY ? surface->height() - top - height : top,
1448 0
1449 };
1450
1451 // Copy the image to a buffer so we can map it to cpu memory
1452 VkBufferImageCopy region;
1453 memset(&region, 0, sizeof(VkBufferImageCopy));
jvanverthdb379092016-07-07 11:18:46 -07001454 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07001455 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05001456 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
1457 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1458 region.imageOffset = offset;
1459 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
1460
1461 fCurrentCmdBuffer->copyImageToBuffer(this,
1462 tgt,
1463 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1464 transferBuffer,
1465 1,
1466 &region);
1467
1468 // make sure the copy to buffer has finished
1469 transferBuffer->addMemoryBarrier(this,
1470 VK_ACCESS_TRANSFER_WRITE_BIT,
1471 VK_ACCESS_HOST_READ_BIT,
1472 VK_PIPELINE_STAGE_TRANSFER_BIT,
1473 VK_PIPELINE_STAGE_HOST_BIT,
1474 false);
1475
1476 // We need to submit the current command buffer to the Queue and make sure it finishes before
1477 // we can copy the data out of the buffer.
1478 this->submitCommandBuffer(kForce_SyncQueue);
1479
1480 void* mappedMemory = transferBuffer->map();
1481
egdaniel88e8aef2016-06-27 14:34:55 -07001482 size_t tightRowBytes = GrBytesPerPixel(config) * width;
1483 if (flipY) {
1484 const char* srcRow = reinterpret_cast<const char*>(mappedMemory);
1485 char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes;
1486 for (int y = 0; y < height; y++) {
1487 memcpy(dstRow, srcRow, tightRowBytes);
1488 srcRow += tightRowBytes;
1489 dstRow -= rowBytes;
1490 }
1491 } else {
1492 if (tightRowBytes == rowBytes) {
1493 memcpy(buffer, mappedMemory, rowBytes*height);
1494 } else {
1495 SkRectMemcpy(buffer, rowBytes, mappedMemory, tightRowBytes, tightRowBytes, height);
1496 }
1497 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001498
1499 transferBuffer->unmap();
1500 transferBuffer->unref();
1501
Greg Daniel164a9f02016-02-22 09:56:40 -05001502 return true;
1503}
egdaniel066df7c2016-06-08 14:02:27 -07001504
egdaniel27bb2842016-07-07 11:58:35 -07001505// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
1506// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
1507// the the entire attachment. Similar requirements for the y and height components.
1508void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
1509 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
1510 // Adjust Width
1511 // Start with the right side of rect so we know if we end up going pass the maxWidth.
1512 int rightAdj = srcBounds.fRight % granularity.width;
1513 if (rightAdj != 0) {
1514 rightAdj = granularity.width - rightAdj;
1515 }
1516 dstBounds->fRight = srcBounds.fRight + rightAdj;
1517 if (dstBounds->fRight > maxWidth) {
1518 dstBounds->fRight = maxWidth;
1519 dstBounds->fLeft = 0;
1520 } else {
1521 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
1522 }
1523
1524 // Adjust height
1525 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
1526 int bottomAdj = srcBounds.fBottom % granularity.height;
1527 if (bottomAdj != 0) {
1528 bottomAdj = granularity.height - bottomAdj;
1529 }
1530 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
1531 if (dstBounds->fBottom > maxHeight) {
1532 dstBounds->fBottom = maxHeight;
1533 dstBounds->fTop = 0;
1534 } else {
1535 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
1536 }
1537}
1538
jvanverth7ec92412016-07-06 09:24:57 -07001539void GrVkGpu::submitSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer,
egdaniel9cb63402016-06-23 08:37:05 -07001540 const GrVkRenderPass* renderPass,
1541 const VkClearValue* colorClear,
1542 GrVkRenderTarget* target,
1543 const SkIRect& bounds) {
egdaniele7d1b242016-07-01 08:06:45 -07001544 const SkIRect* pBounds = &bounds;
1545 SkIRect flippedBounds;
1546 if (kBottomLeft_GrSurfaceOrigin == target->origin()) {
1547 flippedBounds = bounds;
1548 flippedBounds.fTop = target->height() - bounds.fBottom;
1549 flippedBounds.fBottom = target->height() - bounds.fTop;
1550 pBounds = &flippedBounds;
1551 }
1552
egdaniel27bb2842016-07-07 11:58:35 -07001553 // The bounds we use for the render pass should be of the granularity supported
1554 // by the device.
1555 const VkExtent2D& granularity = renderPass->granularity();
1556 SkIRect adjustedBounds;
1557 if ((0 != granularity.width && 1 != granularity.width) ||
1558 (0 != granularity.height && 1 != granularity.height)) {
1559 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
1560 target->width(), target->height());
1561 pBounds = &adjustedBounds;
1562 }
1563
egdaniel9cb63402016-06-23 08:37:05 -07001564 // Currently it is fine for us to always pass in 1 for the clear count even if no attachment
1565 // uses it. In the current state, we also only use the LOAD_OP_CLEAR for the color attachment
1566 // which is always at the first attachment.
egdaniele7d1b242016-07-01 08:06:45 -07001567 fCurrentCmdBuffer->beginRenderPass(this, renderPass, 1, colorClear, *target, *pBounds, true);
egdaniel066df7c2016-06-08 14:02:27 -07001568 fCurrentCmdBuffer->executeCommands(this, buffer);
Greg Daniel164a9f02016-02-22 09:56:40 -05001569 fCurrentCmdBuffer->endRenderPass(this);
Greg Daniel164a9f02016-02-22 09:56:40 -05001570}
egdaniel9cb63402016-06-23 08:37:05 -07001571