Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1 | /* |
| 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" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "GrPipeline.h" |
| 15 | #include "GrRenderTargetPriv.h" |
| 16 | #include "GrSurfacePriv.h" |
| 17 | #include "GrTexturePriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
| 19 | #include "GrVkCommandBuffer.h" |
| 20 | #include "GrVkImage.h" |
| 21 | #include "GrVkIndexBuffer.h" |
| 22 | #include "GrVkMemory.h" |
| 23 | #include "GrVkPipeline.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 24 | #include "GrVkPipelineState.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 25 | #include "GrVkRenderPass.h" |
| 26 | #include "GrVkResourceProvider.h" |
| 27 | #include "GrVkTexture.h" |
| 28 | #include "GrVkTextureRenderTarget.h" |
| 29 | #include "GrVkTransferBuffer.h" |
| 30 | #include "GrVkVertexBuffer.h" |
| 31 | |
| 32 | #include "SkConfig8888.h" |
| 33 | |
| 34 | #include "vk/GrVkInterface.h" |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 35 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 36 | |
| 37 | #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) |
| 38 | #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) |
| 39 | #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) |
| 40 | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 41 | #ifdef ENABLE_VK_LAYERS |
| 42 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback( |
| 43 | VkDebugReportFlagsEXT flags, |
| 44 | VkDebugReportObjectTypeEXT objectType, |
| 45 | uint64_t object, |
| 46 | size_t location, |
| 47 | int32_t messageCode, |
| 48 | const char* pLayerPrefix, |
| 49 | const char* pMessage, |
| 50 | void* pUserData) { |
| 51 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
| 52 | SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 53 | } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
| 54 | SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 55 | } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { |
| 56 | SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 57 | } else { |
| 58 | SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 59 | } |
| 60 | return VK_FALSE; |
| 61 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 64 | GrGpu* GrVkGpu::Create(GrBackendContext backendContext, const GrContextOptions& options, |
| 65 | GrContext* context) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 66 | const GrVkBackendContext* vkBackendContext = |
| 67 | reinterpret_cast<const GrVkBackendContext*>(backendContext); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 68 | if (!vkBackendContext) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 69 | vkBackendContext = GrVkBackendContext::Create(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 70 | if (!vkBackendContext) { |
| 71 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 72 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 73 | } else { |
| 74 | vkBackendContext->ref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 75 | } |
| 76 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 77 | return new GrVkGpu(context, options, vkBackendContext); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | //////////////////////////////////////////////////////////////////////////////// |
| 81 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 82 | GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 83 | const GrVkBackendContext* backendCtx) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 84 | : INHERITED(context) |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 85 | , fVkInstance(backendCtx->fInstance) |
| 86 | , fDevice(backendCtx->fDevice) |
| 87 | , fQueue(backendCtx->fQueue) |
| 88 | , fResourceProvider(this) { |
| 89 | fBackendContext.reset(backendCtx); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 90 | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 91 | #ifdef ENABLE_VK_LAYERS |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 92 | if (backendCtx->fExtensions & kEXT_debug_report_GrVkExtensionFlag) { |
| 93 | // Setup callback creation information |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 94 | VkDebugReportCallbackCreateInfoEXT callbackCreateInfo; |
| 95 | callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 96 | callbackCreateInfo.pNext = nullptr; |
| 97 | callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | |
egdaniel | ef0c10c | 2016-04-07 07:51:22 -0700 | [diff] [blame] | 98 | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 99 | //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | |
| 100 | //VK_DEBUG_REPORT_DEBUG_BIT_EXT | |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 101 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 102 | callbackCreateInfo.pfnCallback = &DebugReportCallback; |
| 103 | callbackCreateInfo.pUserData = nullptr; |
| 104 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 105 | // Register the callback |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 106 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateDebugReportCallbackEXT(fVkInstance, |
| 107 | &callbackCreateInfo, nullptr, &fCallback)); |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 108 | } |
| 109 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 110 | |
| 111 | fCompiler = shaderc_compiler_initialize(); |
| 112 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 113 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice, |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 114 | backendCtx->fFeatures, backendCtx->fExtensions)); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 115 | fCaps.reset(SkRef(fVkCaps.get())); |
| 116 | |
| 117 | VK_CALL(GetPhysicalDeviceMemoryProperties(backendCtx->fPhysicalDevice, &fPhysDevMemProps)); |
| 118 | |
| 119 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
| 120 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 121 | nullptr, // pNext |
| 122 | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, // CmdPoolCreateFlags |
| 123 | backendCtx->fQueueFamilyIndex, // queueFamilyIndex |
| 124 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 125 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 126 | &fCmdPool)); |
| 127 | |
| 128 | // must call this after creating the CommandPool |
| 129 | fResourceProvider.init(); |
| 130 | fCurrentCmdBuffer = fResourceProvider.createCommandBuffer(); |
| 131 | SkASSERT(fCurrentCmdBuffer); |
| 132 | fCurrentCmdBuffer->begin(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | GrVkGpu::~GrVkGpu() { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 136 | fCurrentCmdBuffer->end(this); |
| 137 | fCurrentCmdBuffer->unref(this); |
| 138 | |
| 139 | // wait for all commands to finish |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 140 | fResourceProvider.checkCommandBuffers(); |
egdaniel | 2cab66b | 2016-03-21 14:24:14 -0700 | [diff] [blame] | 141 | SkDEBUGCODE(VkResult res =) VK_CALL(QueueWaitIdle(fQueue)); |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 142 | // VK_ERROR_DEVICE_LOST is acceptable when tearing down (see 4.2.4 in spec) |
| 143 | SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 144 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 145 | // must call this just before we destroy the VkDevice |
| 146 | fResourceProvider.destroyResources(); |
| 147 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 148 | VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); |
| 149 | |
| 150 | shaderc_compiler_release(fCompiler); |
| 151 | |
| 152 | #ifdef ENABLE_VK_LAYERS |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 153 | VK_CALL(DestroyDebugReportCallbackEXT(fVkInstance, fCallback, nullptr)); |
| 154 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | void GrVkGpu::submitCommandBuffer(SyncQueue sync) { |
| 160 | SkASSERT(fCurrentCmdBuffer); |
| 161 | fCurrentCmdBuffer->end(this); |
| 162 | |
| 163 | fCurrentCmdBuffer->submitToQueue(this, fQueue, sync); |
| 164 | fResourceProvider.checkCommandBuffers(); |
| 165 | |
| 166 | // Release old command buffer and create a new one |
| 167 | fCurrentCmdBuffer->unref(this); |
| 168 | fCurrentCmdBuffer = fResourceProvider.createCommandBuffer(); |
| 169 | SkASSERT(fCurrentCmdBuffer); |
| 170 | |
| 171 | fCurrentCmdBuffer->begin(this); |
| 172 | } |
| 173 | |
| 174 | /////////////////////////////////////////////////////////////////////////////// |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 175 | GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern, |
| 176 | const void* data) { |
| 177 | GrBuffer* buff; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 178 | switch (type) { |
| 179 | case kVertex_GrBufferType: |
| 180 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 181 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 182 | buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 183 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 184 | case kIndex_GrBufferType: |
| 185 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 186 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 187 | buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 188 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 189 | case kXferCpuToGpu_GrBufferType: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 190 | SkASSERT(kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 191 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 192 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 193 | case kXferGpuToCpu_GrBufferType: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 194 | SkASSERT(kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 195 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 196 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 197 | default: |
| 198 | SkFAIL("Unknown buffer type."); |
| 199 | return nullptr; |
| 200 | } |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 201 | if (data && buff) { |
| 202 | buff->updateData(data, size); |
| 203 | } |
| 204 | return buff; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | //////////////////////////////////////////////////////////////////////////////// |
| 208 | bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
| 209 | GrPixelConfig srcConfig, DrawPreference* drawPreference, |
| 210 | WritePixelTempDrawInfo* tempDrawInfo) { |
| 211 | if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | // Currently we don't handle draws, so if the caller wants/needs to do a draw we need to fail |
| 216 | if (kNoDraw_DrawPreference != *drawPreference) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | if (dstSurface->config() != srcConfig) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 221 | // TODO: This should fall back to drawing or copying to change config of dstSurface to |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 222 | // match that of srcConfig. |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool GrVkGpu::onWritePixels(GrSurface* surface, |
| 230 | int left, int top, int width, int height, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 231 | GrPixelConfig config, |
| 232 | const SkTArray<GrMipLevel>& texels) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 233 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture()); |
| 234 | if (!vkTex) { |
| 235 | return false; |
| 236 | } |
| 237 | |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 238 | // TODO: We're ignoring MIP levels here. |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 239 | if (texels.empty() || !texels.begin()->fPixels) { |
| 240 | return false; |
| 241 | } |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 242 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 243 | // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels. |
| 244 | if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | bool success = false; |
| 249 | if (GrPixelConfigIsCompressed(vkTex->desc().fConfig)) { |
| 250 | // We check that config == desc.fConfig in GrGpu::getWritePixelsInfo() |
| 251 | SkASSERT(config == vkTex->desc().fConfig); |
| 252 | // TODO: add compressed texture support |
| 253 | // delete the following two lines and uncomment the two after that when ready |
| 254 | vkTex->unref(); |
| 255 | return false; |
| 256 | //success = this->uploadCompressedTexData(vkTex->desc(), buffer, false, left, top, width, |
| 257 | // height); |
| 258 | } else { |
| 259 | bool linearTiling = vkTex->isLinearTiled(); |
| 260 | if (linearTiling && VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) { |
| 261 | // Need to change the layout to general in order to perform a host write |
| 262 | VkImageLayout layout = vkTex->currentLayout(); |
| 263 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 264 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_HOST_BIT; |
| 265 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 266 | VkAccessFlags dstAccessMask = VK_ACCESS_HOST_WRITE_BIT; |
| 267 | vkTex->setImageLayout(this, |
| 268 | VK_IMAGE_LAYOUT_GENERAL, |
| 269 | srcAccessMask, |
| 270 | dstAccessMask, |
| 271 | srcStageMask, |
| 272 | dstStageMask, |
| 273 | false); |
| 274 | } |
| 275 | success = this->uploadTexData(vkTex, left, top, width, height, config, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 276 | texels.begin()->fPixels, texels.begin()->fRowBytes); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (success) { |
| 280 | vkTex->texturePriv().dirtyMipMaps(true); |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | bool GrVkGpu::uploadTexData(GrVkTexture* tex, |
| 288 | int left, int top, int width, int height, |
| 289 | GrPixelConfig dataConfig, |
| 290 | const void* data, |
| 291 | size_t rowBytes) { |
| 292 | SkASSERT(data); |
| 293 | |
| 294 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 295 | SkASSERT(!GrPixelConfigIsCompressed(dataConfig)); |
| 296 | |
| 297 | bool linearTiling = tex->isLinearTiled(); |
| 298 | |
| 299 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 300 | |
| 301 | const GrSurfaceDesc& desc = tex->desc(); |
| 302 | |
| 303 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 304 | &width, &height, &data, &rowBytes)) { |
| 305 | return false; |
| 306 | } |
| 307 | size_t trimRowBytes = width * bpp; |
| 308 | |
| 309 | if (linearTiling) { |
| 310 | SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() || |
| 311 | VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout()); |
| 312 | const VkImageSubresource subres = { |
| 313 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 314 | 0, // mipLevel |
| 315 | 0, // arraySlice |
| 316 | }; |
| 317 | VkSubresourceLayout layout; |
| 318 | VkResult err; |
| 319 | |
| 320 | const GrVkInterface* interface = this->vkInterface(); |
| 321 | |
| 322 | GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice, |
| 323 | tex->textureImage(), |
| 324 | &subres, |
| 325 | &layout)); |
| 326 | |
| 327 | int texTop = kBottomLeft_GrSurfaceOrigin == desc.fOrigin ? tex->height() - top - height |
| 328 | : top; |
| 329 | VkDeviceSize offset = texTop*layout.rowPitch + left*bpp; |
| 330 | VkDeviceSize size = height*layout.rowPitch; |
| 331 | void* mapPtr; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 332 | err = GR_VK_CALL(interface, MapMemory(fDevice, tex->textureMemory(), offset, size, 0, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 333 | &mapPtr)); |
| 334 | if (err) { |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) { |
| 339 | // copy into buffer by rows |
| 340 | const char* srcRow = reinterpret_cast<const char*>(data); |
| 341 | char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch; |
| 342 | for (int y = 0; y < height; y++) { |
| 343 | memcpy(dstRow, srcRow, trimRowBytes); |
| 344 | srcRow += rowBytes; |
| 345 | dstRow -= layout.rowPitch; |
| 346 | } |
| 347 | } else { |
| 348 | // If there is no padding on the src (rowBytes) or dst (layout.rowPitch) we can memcpy |
| 349 | if (trimRowBytes == rowBytes && trimRowBytes == layout.rowPitch) { |
| 350 | memcpy(mapPtr, data, trimRowBytes * height); |
| 351 | } else { |
bsalomon | a6b439a | 2016-03-10 12:31:20 -0800 | [diff] [blame] | 352 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, |
| 353 | trimRowBytes, height); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | GR_VK_CALL(interface, UnmapMemory(fDevice, tex->textureMemory())); |
| 358 | } else { |
| 359 | GrVkTransferBuffer* transferBuffer = |
| 360 | GrVkTransferBuffer::Create(this, trimRowBytes * height, GrVkBuffer::kCopyRead_Type); |
| 361 | |
| 362 | void* mapPtr = transferBuffer->map(); |
| 363 | |
| 364 | if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) { |
| 365 | // copy into buffer by rows |
| 366 | const char* srcRow = reinterpret_cast<const char*>(data); |
| 367 | char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*trimRowBytes; |
| 368 | for (int y = 0; y < height; y++) { |
| 369 | memcpy(dstRow, srcRow, trimRowBytes); |
| 370 | srcRow += rowBytes; |
| 371 | dstRow -= trimRowBytes; |
| 372 | } |
| 373 | } else { |
| 374 | // If there is no padding on the src data rows, we can do a single memcpy |
| 375 | if (trimRowBytes == rowBytes) { |
| 376 | memcpy(mapPtr, data, trimRowBytes * height); |
| 377 | } else { |
| 378 | SkRectMemcpy(mapPtr, trimRowBytes, data, rowBytes, trimRowBytes, height); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | transferBuffer->unmap(); |
| 383 | |
| 384 | // make sure the unmap has finished |
| 385 | transferBuffer->addMemoryBarrier(this, |
| 386 | VK_ACCESS_HOST_WRITE_BIT, |
| 387 | VK_ACCESS_TRANSFER_READ_BIT, |
| 388 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
| 389 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 390 | false); |
| 391 | |
| 392 | // Set up copy region |
| 393 | bool flipY = kBottomLeft_GrSurfaceOrigin == tex->origin(); |
| 394 | VkOffset3D offset = { |
| 395 | left, |
| 396 | flipY ? tex->height() - top - height : top, |
| 397 | 0 |
| 398 | }; |
| 399 | |
| 400 | VkBufferImageCopy region; |
| 401 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 402 | region.bufferOffset = 0; |
| 403 | region.bufferRowLength = width; |
| 404 | region.bufferImageHeight = height; |
| 405 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 406 | region.imageOffset = offset; |
| 407 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 408 | |
| 409 | // Change layout of our target so it can be copied to |
| 410 | VkImageLayout layout = tex->currentLayout(); |
| 411 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 412 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 413 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 414 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 415 | tex->setImageLayout(this, |
| 416 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 417 | srcAccessMask, |
| 418 | dstAccessMask, |
| 419 | srcStageMask, |
| 420 | dstStageMask, |
| 421 | false); |
| 422 | |
| 423 | // Copy the buffer to the image |
| 424 | fCurrentCmdBuffer->copyBufferToImage(this, |
| 425 | transferBuffer, |
| 426 | tex, |
| 427 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 428 | 1, |
| 429 | ®ion); |
| 430 | |
| 431 | // Submit the current command buffer to the Queue |
| 432 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 433 | |
| 434 | transferBuffer->unref(); |
| 435 | } |
| 436 | |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | //////////////////////////////////////////////////////////////////////////////// |
| 441 | GrTexture* GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 442 | const SkTArray<GrMipLevel>& texels) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 443 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 444 | |
| 445 | VkFormat pixelFormat; |
| 446 | if (!GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)) { |
| 447 | return nullptr; |
| 448 | } |
| 449 | |
| 450 | if (!fVkCaps->isConfigTexturable(desc.fConfig)) { |
| 451 | return nullptr; |
| 452 | } |
| 453 | |
| 454 | bool linearTiling = false; |
| 455 | if (SkToBool(desc.fFlags & kZeroCopy_GrSurfaceFlag)) { |
| 456 | if (fVkCaps->isConfigTexurableLinearly(desc.fConfig) && |
| 457 | (!renderTarget || fVkCaps->isConfigRenderableLinearly(desc.fConfig, false))) { |
| 458 | linearTiling = true; |
| 459 | } else { |
| 460 | return nullptr; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 465 | if (renderTarget) { |
| 466 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 467 | } |
| 468 | |
| 469 | // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and |
| 470 | // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we |
| 471 | // will be using this texture in some copy or not. Also this assumes, as is the current case, |
| 472 | // that all render targets in vulkan are also texutres. If we change this practice of setting |
| 473 | // both bits, we must make sure to set the destination bit if we are uploading srcData to the |
| 474 | // texture. |
| 475 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 476 | |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 477 | VkFlags memProps = (!texels.empty() && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT : |
| 478 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 479 | |
| 480 | // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is |
| 481 | // requested, this ImageDesc describes the resolved texutre. Therefore we always have samples set |
| 482 | // to 1. |
| 483 | GrVkImage::ImageDesc imageDesc; |
| 484 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 485 | imageDesc.fFormat = pixelFormat; |
| 486 | imageDesc.fWidth = desc.fWidth; |
| 487 | imageDesc.fHeight = desc.fHeight; |
| 488 | imageDesc.fLevels = 1; |
| 489 | imageDesc.fSamples = 1; |
| 490 | imageDesc.fImageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 491 | imageDesc.fUsageFlags = usageFlags; |
| 492 | imageDesc.fMemProps = memProps; |
| 493 | |
| 494 | GrVkTexture* tex; |
| 495 | if (renderTarget) { |
| 496 | tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, desc, lifeCycle, |
| 497 | imageDesc); |
| 498 | } else { |
| 499 | tex = GrVkTexture::CreateNewTexture(this, desc, lifeCycle, imageDesc); |
| 500 | } |
| 501 | |
| 502 | if (!tex) { |
| 503 | return nullptr; |
| 504 | } |
| 505 | |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 506 | // TODO: We're ignoring MIP levels here. |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 507 | if (!texels.empty()) { |
| 508 | SkASSERT(texels.begin()->fPixels); |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 509 | if (!this->uploadTexData(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 510 | texels.begin()->fPixels, texels.begin()->fRowBytes)) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 511 | tex->unref(); |
| 512 | return nullptr; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | return tex; |
| 517 | } |
| 518 | |
| 519 | //////////////////////////////////////////////////////////////////////////////// |
| 520 | |
| 521 | static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin) { |
| 522 | // By default, all textures in Vk use TopLeft |
| 523 | if (kDefault_GrSurfaceOrigin == origin) { |
| 524 | return kTopLeft_GrSurfaceOrigin; |
| 525 | } else { |
| 526 | return origin; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | GrTexture* GrVkGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc, |
| 531 | GrWrapOwnership ownership) { |
| 532 | VkFormat format; |
| 533 | if (!GrPixelConfigToVkFormat(desc.fConfig, &format)) { |
| 534 | return nullptr; |
| 535 | } |
| 536 | |
| 537 | if (0 == desc.fTextureHandle) { |
| 538 | return nullptr; |
| 539 | } |
| 540 | |
| 541 | int maxSize = this->caps()->maxTextureSize(); |
| 542 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 543 | return nullptr; |
| 544 | } |
| 545 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 546 | const GrVkTextureInfo* info = reinterpret_cast<const GrVkTextureInfo*>(desc.fTextureHandle); |
| 547 | if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc) { |
| 548 | return nullptr; |
| 549 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 550 | |
jvanverth | 0fcfb75 | 2016-03-09 09:57:52 -0800 | [diff] [blame] | 551 | GrGpuResource::LifeCycle lifeCycle = (kAdopt_GrWrapOwnership == ownership) |
| 552 | ? GrGpuResource::kAdopted_LifeCycle |
| 553 | : GrGpuResource::kBorrowed_LifeCycle; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 554 | |
| 555 | GrSurfaceDesc surfDesc; |
| 556 | // next line relies on GrBackendTextureDesc's flags matching GrTexture's |
| 557 | surfDesc.fFlags = (GrSurfaceFlags)desc.fFlags; |
| 558 | surfDesc.fWidth = desc.fWidth; |
| 559 | surfDesc.fHeight = desc.fHeight; |
| 560 | surfDesc.fConfig = desc.fConfig; |
| 561 | surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); |
| 562 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 563 | // In GL, Chrome assumes all textures are BottomLeft |
| 564 | // In VK, we don't have this restriction |
| 565 | surfDesc.fOrigin = resolve_origin(desc.fOrigin); |
| 566 | |
| 567 | GrVkTexture* texture = nullptr; |
| 568 | if (renderTarget) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 569 | texture = GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(this, surfDesc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 570 | lifeCycle, format, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 571 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 572 | } else { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 573 | texture = GrVkTexture::CreateWrappedTexture(this, surfDesc, lifeCycle, format, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 574 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 575 | } |
| 576 | if (!texture) { |
| 577 | return nullptr; |
| 578 | } |
| 579 | |
| 580 | return texture; |
| 581 | } |
| 582 | |
| 583 | GrRenderTarget* GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc, |
| 584 | GrWrapOwnership ownership) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 585 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 586 | const GrVkTextureInfo* info = |
| 587 | reinterpret_cast<const GrVkTextureInfo*>(wrapDesc.fRenderTargetHandle); |
| 588 | if (VK_NULL_HANDLE == info->fImage || |
| 589 | (VK_NULL_HANDLE == info->fAlloc && kAdopt_GrWrapOwnership == ownership)) { |
| 590 | return nullptr; |
| 591 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 592 | |
jvanverth | 0fcfb75 | 2016-03-09 09:57:52 -0800 | [diff] [blame] | 593 | GrGpuResource::LifeCycle lifeCycle = (kAdopt_GrWrapOwnership == ownership) |
| 594 | ? GrGpuResource::kAdopted_LifeCycle |
| 595 | : GrGpuResource::kBorrowed_LifeCycle; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 596 | |
| 597 | GrSurfaceDesc desc; |
| 598 | desc.fConfig = wrapDesc.fConfig; |
| 599 | desc.fFlags = kCheckAllocation_GrSurfaceFlag; |
| 600 | desc.fWidth = wrapDesc.fWidth; |
| 601 | desc.fHeight = wrapDesc.fHeight; |
| 602 | desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount()); |
| 603 | |
| 604 | desc.fOrigin = resolve_origin(wrapDesc.fOrigin); |
| 605 | |
| 606 | GrVkRenderTarget* tgt = GrVkRenderTarget::CreateWrappedRenderTarget(this, desc, |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 607 | lifeCycle, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 608 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 609 | if (tgt && wrapDesc.fStencilBits) { |
| 610 | if (!createStencilAttachmentForRenderTarget(tgt, desc.fWidth, desc.fHeight)) { |
| 611 | tgt->unref(); |
| 612 | return nullptr; |
| 613 | } |
| 614 | } |
| 615 | return tgt; |
| 616 | } |
| 617 | |
| 618 | //////////////////////////////////////////////////////////////////////////////// |
| 619 | |
| 620 | void GrVkGpu::bindGeometry(const GrPrimitiveProcessor& primProc, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 621 | const GrNonInstancedMesh& mesh) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 622 | GrVkVertexBuffer* vbuf; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 623 | vbuf = (GrVkVertexBuffer*)mesh.vertexBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 624 | SkASSERT(vbuf); |
| 625 | SkASSERT(!vbuf->isMapped()); |
| 626 | |
| 627 | vbuf->addMemoryBarrier(this, |
| 628 | VK_ACCESS_HOST_WRITE_BIT, |
| 629 | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT, |
| 630 | VK_PIPELINE_STAGE_HOST_BIT, |
| 631 | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, |
| 632 | false); |
| 633 | |
| 634 | fCurrentCmdBuffer->bindVertexBuffer(this, vbuf); |
| 635 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 636 | if (mesh.isIndexed()) { |
| 637 | GrVkIndexBuffer* ibuf = (GrVkIndexBuffer*)mesh.indexBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 638 | SkASSERT(ibuf); |
| 639 | SkASSERT(!ibuf->isMapped()); |
| 640 | |
| 641 | ibuf->addMemoryBarrier(this, |
| 642 | VK_ACCESS_HOST_WRITE_BIT, |
| 643 | VK_ACCESS_INDEX_READ_BIT, |
| 644 | VK_PIPELINE_STAGE_HOST_BIT, |
| 645 | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, |
| 646 | false); |
| 647 | |
| 648 | fCurrentCmdBuffer->bindIndexBuffer(this, ibuf); |
| 649 | } |
| 650 | } |
| 651 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 652 | //////////////////////////////////////////////////////////////////////////////// |
| 653 | |
| 654 | GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 655 | int width, |
| 656 | int height) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 657 | SkASSERT(width >= rt->width()); |
| 658 | SkASSERT(height >= rt->height()); |
| 659 | |
| 660 | int samples = rt->numStencilSamples(); |
| 661 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 662 | const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 663 | |
| 664 | GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this, |
| 665 | GrGpuResource::kCached_LifeCycle, |
| 666 | width, |
| 667 | height, |
| 668 | samples, |
| 669 | sFmt)); |
| 670 | fStats.incStencilAttachmentCreates(); |
| 671 | return stencil; |
| 672 | } |
| 673 | |
| 674 | //////////////////////////////////////////////////////////////////////////////// |
| 675 | |
| 676 | GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h, |
| 677 | GrPixelConfig config) { |
| 678 | |
| 679 | VkFormat pixelFormat; |
| 680 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | bool linearTiling = false; |
| 685 | if (!fVkCaps->isConfigTexturable(config)) { |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | if (fVkCaps->isConfigTexurableLinearly(config)) { |
| 690 | linearTiling = true; |
| 691 | } |
| 692 | |
| 693 | // Currently this is not supported since it requires a copy which has not yet been implemented. |
| 694 | if (srcData && !linearTiling) { |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 699 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 700 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 701 | |
| 702 | VkFlags memProps = (srcData && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT : |
| 703 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 704 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 705 | VkImage image = VK_NULL_HANDLE; |
| 706 | VkDeviceMemory alloc = VK_NULL_HANDLE; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 707 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 708 | VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 709 | VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling) |
| 710 | ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 711 | : VK_IMAGE_LAYOUT_UNDEFINED; |
| 712 | |
| 713 | // Create Image |
| 714 | VkSampleCountFlagBits vkSamples; |
| 715 | if (!GrSampleCountToVkSampleCount(1, &vkSamples)) { |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | const VkImageCreateInfo imageCreateInfo = { |
| 720 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 721 | NULL, // pNext |
| 722 | 0, // VkImageCreateFlags |
| 723 | VK_IMAGE_TYPE_2D, // VkImageType |
| 724 | pixelFormat, // VkFormat |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 725 | { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 726 | 1, // mipLevels |
| 727 | 1, // arrayLayers |
| 728 | vkSamples, // samples |
| 729 | imageTiling, // VkImageTiling |
| 730 | usageFlags, // VkImageUsageFlags |
| 731 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 732 | 0, // queueFamilyCount |
| 733 | 0, // pQueueFamilyIndices |
| 734 | initialLayout // initialLayout |
| 735 | }; |
| 736 | |
| 737 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image)); |
| 738 | |
| 739 | if (!GrVkMemory::AllocAndBindImageMemory(this, image, memProps, &alloc)) { |
| 740 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | if (srcData) { |
| 745 | if (linearTiling) { |
| 746 | const VkImageSubresource subres = { |
| 747 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 748 | 0, // mipLevel |
| 749 | 0, // arraySlice |
| 750 | }; |
| 751 | VkSubresourceLayout layout; |
| 752 | VkResult err; |
| 753 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 754 | VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 755 | |
| 756 | void* mapPtr; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 757 | err = VK_CALL(MapMemory(fDevice, alloc, 0, layout.rowPitch * h, 0, &mapPtr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 758 | if (err) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 759 | VK_CALL(FreeMemory(this->device(), alloc, nullptr)); |
| 760 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | size_t bpp = GrBytesPerPixel(config); |
| 765 | size_t rowCopyBytes = bpp * w; |
| 766 | // If there is no padding on dst (layout.rowPitch) we can do a single memcopy. |
| 767 | // This assumes the srcData comes in with no padding. |
| 768 | if (rowCopyBytes == layout.rowPitch) { |
| 769 | memcpy(mapPtr, srcData, rowCopyBytes * h); |
| 770 | } else { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 771 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), srcData, rowCopyBytes, |
| 772 | rowCopyBytes, h); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 773 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 774 | VK_CALL(UnmapMemory(fDevice, alloc)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 775 | } else { |
| 776 | // TODO: Add support for copying to optimal tiling |
| 777 | SkASSERT(false); |
| 778 | } |
| 779 | } |
| 780 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 781 | GrVkTextureInfo* info = new GrVkTextureInfo; |
| 782 | info->fImage = image; |
| 783 | info->fAlloc = alloc; |
| 784 | info->fImageTiling = imageTiling; |
| 785 | info->fImageLayout = initialLayout; |
| 786 | |
| 787 | return (GrBackendObject)info; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 791 | const GrVkTextureInfo* backend = reinterpret_cast<const GrVkTextureInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 792 | |
| 793 | if (backend && backend->fImage && backend->fAlloc) { |
| 794 | VkMemoryRequirements req; |
| 795 | memset(&req, 0, sizeof(req)); |
| 796 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
| 797 | backend->fImage, |
| 798 | &req)); |
| 799 | // TODO: find a better check |
| 800 | // This will probably fail with a different driver |
| 801 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 802 | } |
| 803 | |
| 804 | return false; |
| 805 | } |
| 806 | |
| 807 | void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 808 | const GrVkTextureInfo* backend = reinterpret_cast<const GrVkTextureInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 809 | |
| 810 | if (backend) { |
| 811 | if (!abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 812 | // something in the command buffer may still be using this, so force submit |
| 813 | this->submitCommandBuffer(kForce_SyncQueue); |
| 814 | |
| 815 | VK_CALL(FreeMemory(this->device(), backend->fAlloc, nullptr)); |
| 816 | VK_CALL(DestroyImage(this->device(), backend->fImage, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 817 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 818 | delete backend; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
| 822 | //////////////////////////////////////////////////////////////////////////////// |
| 823 | |
| 824 | void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 825 | VkPipelineStageFlags dstStageMask, |
| 826 | bool byRegion, |
| 827 | VkMemoryBarrier* barrier) const { |
| 828 | SkASSERT(fCurrentCmdBuffer); |
| 829 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 830 | srcStageMask, |
| 831 | dstStageMask, |
| 832 | byRegion, |
| 833 | GrVkCommandBuffer::kMemory_BarrierType, |
| 834 | barrier); |
| 835 | } |
| 836 | |
| 837 | void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 838 | VkPipelineStageFlags dstStageMask, |
| 839 | bool byRegion, |
| 840 | VkBufferMemoryBarrier* barrier) const { |
| 841 | SkASSERT(fCurrentCmdBuffer); |
| 842 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 843 | srcStageMask, |
| 844 | dstStageMask, |
| 845 | byRegion, |
| 846 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 847 | barrier); |
| 848 | } |
| 849 | |
| 850 | void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 851 | VkPipelineStageFlags dstStageMask, |
| 852 | bool byRegion, |
| 853 | VkImageMemoryBarrier* barrier) const { |
| 854 | SkASSERT(fCurrentCmdBuffer); |
| 855 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 856 | srcStageMask, |
| 857 | dstStageMask, |
| 858 | byRegion, |
| 859 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 860 | barrier); |
| 861 | } |
| 862 | |
| 863 | void GrVkGpu::finishDrawTarget() { |
| 864 | // Submit the current command buffer to the Queue |
| 865 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 866 | } |
| 867 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 868 | void GrVkGpu::clearStencil(GrRenderTarget* target) { |
| 869 | if (nullptr == target) { |
| 870 | return; |
| 871 | } |
| 872 | GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment(); |
| 873 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 874 | |
| 875 | |
| 876 | VkClearDepthStencilValue vkStencilColor; |
| 877 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 878 | |
| 879 | VkImageLayout origDstLayout = vkStencil->currentLayout(); |
| 880 | |
| 881 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 882 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 883 | |
| 884 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 885 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 886 | |
| 887 | vkStencil->setImageLayout(this, |
| 888 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 889 | srcAccessMask, |
| 890 | dstAccessMask, |
| 891 | srcStageMask, |
| 892 | dstStageMask, |
| 893 | false); |
| 894 | |
| 895 | |
| 896 | VkImageSubresourceRange subRange; |
| 897 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 898 | subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 899 | subRange.baseMipLevel = 0; |
| 900 | subRange.levelCount = 1; |
| 901 | subRange.baseArrayLayer = 0; |
| 902 | subRange.layerCount = 1; |
| 903 | |
| 904 | // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a |
| 905 | // draw. Thus we should look into using the load op functions on the render pass to clear out |
| 906 | // the stencil there. |
| 907 | fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange); |
| 908 | } |
| 909 | |
| 910 | void GrVkGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) { |
| 911 | SkASSERT(target); |
| 912 | |
| 913 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
| 914 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
| 915 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)sb; |
| 916 | |
| 917 | // this should only be called internally when we know we have a |
| 918 | // stencil buffer. |
| 919 | SkASSERT(sb); |
| 920 | int stencilBitCount = sb->bits(); |
| 921 | |
| 922 | // The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 923 | // during this clear. Thus we will clear the entire stencil to the desired value. |
| 924 | |
| 925 | VkClearDepthStencilValue vkStencilColor; |
| 926 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 927 | if (insideClip) { |
| 928 | vkStencilColor.stencil = (1 << (stencilBitCount - 1)); |
| 929 | } else { |
| 930 | vkStencilColor.stencil = 0; |
| 931 | } |
| 932 | |
| 933 | VkImageLayout origDstLayout = vkStencil->currentLayout(); |
| 934 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 935 | VkAccessFlags dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 936 | VkPipelineStageFlags srcStageMask = |
| 937 | GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 938 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 939 | vkStencil->setImageLayout(this, |
| 940 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 941 | srcAccessMask, |
| 942 | dstAccessMask, |
| 943 | srcStageMask, |
| 944 | dstStageMask, |
| 945 | false); |
| 946 | |
| 947 | VkClearRect clearRect; |
| 948 | // Flip rect if necessary |
| 949 | SkIRect vkRect = rect; |
| 950 | |
| 951 | if (kBottomLeft_GrSurfaceOrigin == vkRT->origin()) { |
| 952 | vkRect.fTop = vkRT->height() - rect.fBottom; |
| 953 | vkRect.fBottom = vkRT->height() - rect.fTop; |
| 954 | } |
| 955 | |
| 956 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 957 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 958 | |
| 959 | clearRect.baseArrayLayer = 0; |
| 960 | clearRect.layerCount = 1; |
| 961 | |
| 962 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 963 | SkASSERT(renderPass); |
| 964 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 965 | |
| 966 | uint32_t stencilIndex; |
| 967 | SkAssertResult(renderPass->stencilAttachmentIndex(&stencilIndex)); |
| 968 | |
| 969 | VkClearAttachment attachment; |
| 970 | attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 971 | attachment.colorAttachment = 0; // this value shouldn't matter |
| 972 | attachment.clearValue.depthStencil = vkStencilColor; |
| 973 | |
| 974 | fCurrentCmdBuffer->clearAttachments(this, 1, &attachment, 1, &clearRect); |
| 975 | fCurrentCmdBuffer->endRenderPass(this); |
| 976 | |
| 977 | return; |
| 978 | } |
| 979 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 980 | void GrVkGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) { |
| 981 | // parent class should never let us get here with no RT |
| 982 | SkASSERT(target); |
| 983 | |
| 984 | VkClearColorValue vkColor; |
| 985 | GrColorToRGBAFloat(color, vkColor.float32); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 986 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 987 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
| 988 | VkImageLayout origDstLayout = vkRT->currentLayout(); |
| 989 | |
| 990 | if (rect.width() != target->width() || rect.height() != target->height()) { |
| 991 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 992 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 993 | VkPipelineStageFlags srcStageMask = |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 994 | GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 995 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 996 | vkRT->setImageLayout(this, |
| 997 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 998 | srcAccessMask, |
| 999 | dstAccessMask, |
| 1000 | srcStageMask, |
| 1001 | dstStageMask, |
| 1002 | false); |
| 1003 | |
| 1004 | VkClearRect clearRect; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1005 | // Flip rect if necessary |
| 1006 | SkIRect vkRect = rect; |
| 1007 | if (kBottomLeft_GrSurfaceOrigin == vkRT->origin()) { |
| 1008 | vkRect.fTop = vkRT->height() - rect.fBottom; |
| 1009 | vkRect.fBottom = vkRT->height() - rect.fTop; |
| 1010 | } |
| 1011 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 1012 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
jvanverth | e77875a | 2016-03-04 15:16:25 -0800 | [diff] [blame] | 1013 | clearRect.baseArrayLayer = 0; |
| 1014 | clearRect.layerCount = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1015 | |
| 1016 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1017 | SkASSERT(renderPass); |
| 1018 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 1019 | |
| 1020 | uint32_t colorIndex; |
| 1021 | SkAssertResult(renderPass->colorAttachmentIndex(&colorIndex)); |
| 1022 | |
| 1023 | VkClearAttachment attachment; |
| 1024 | attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1025 | attachment.colorAttachment = colorIndex; |
| 1026 | attachment.clearValue.color = vkColor; |
| 1027 | |
| 1028 | fCurrentCmdBuffer->clearAttachments(this, 1, &attachment, 1, &clearRect); |
| 1029 | fCurrentCmdBuffer->endRenderPass(this); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1034 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1035 | |
| 1036 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 1037 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1038 | |
| 1039 | vkRT->setImageLayout(this, |
| 1040 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1041 | srcAccessMask, |
| 1042 | dstAccessMask, |
| 1043 | srcStageMask, |
| 1044 | dstStageMask, |
| 1045 | false); |
| 1046 | |
| 1047 | |
| 1048 | VkImageSubresourceRange subRange; |
| 1049 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1050 | subRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1051 | subRange.baseMipLevel = 0; |
| 1052 | subRange.levelCount = 1; |
| 1053 | subRange.baseArrayLayer = 0; |
| 1054 | subRange.layerCount = 1; |
| 1055 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1056 | // In the future we may not actually be doing this type of clear at all. If we are inside a |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1057 | // render pass or doing a non full clear then we will use CmdClearColorAttachment. The more |
| 1058 | // common use case will be clearing an attachment at the start of a render pass, in which case |
| 1059 | // we will use the clear load ops. |
| 1060 | fCurrentCmdBuffer->clearColorImage(this, |
| 1061 | vkRT, |
| 1062 | &vkColor, |
| 1063 | 1, &subRange); |
| 1064 | } |
| 1065 | |
| 1066 | inline bool can_copy_image(const GrSurface* dst, |
| 1067 | const GrSurface* src, |
| 1068 | const GrVkGpu* gpu) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1069 | // Currently we don't support msaa |
| 1070 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1071 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1072 | return false; |
| 1073 | } |
| 1074 | |
| 1075 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1076 | // as image usage flags. |
| 1077 | if (src->origin() == dst->origin() && |
| 1078 | GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1079 | return true; |
| 1080 | } |
| 1081 | |
| 1082 | // How does msaa play into this? If a VkTexture is multisampled, are we copying the multisampled |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1083 | // or the resolved image here? Im multisampled, Vulkan requires sample counts to be the same. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1084 | |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, |
| 1089 | GrSurface* src, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1090 | GrVkImage* dstImage, |
| 1091 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1092 | const SkIRect& srcRect, |
| 1093 | const SkIPoint& dstPoint) { |
| 1094 | SkASSERT(can_copy_image(dst, src, this)); |
| 1095 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1096 | VkImageLayout origDstLayout = dstImage->currentLayout(); |
| 1097 | VkImageLayout origSrcLayout = srcImage->currentLayout(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1098 | |
| 1099 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1100 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1101 | |
| 1102 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 1103 | // the cache is flushed since it is only being written to. |
| 1104 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 1105 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1106 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1107 | dstImage->setImageLayout(this, |
| 1108 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1109 | srcAccessMask, |
| 1110 | dstAccessMask, |
| 1111 | srcStageMask, |
| 1112 | dstStageMask, |
| 1113 | false); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1114 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1115 | srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origSrcLayout); |
| 1116 | dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1117 | |
| 1118 | srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origSrcLayout); |
| 1119 | dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1120 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1121 | srcImage->setImageLayout(this, |
| 1122 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1123 | srcAccessMask, |
| 1124 | dstAccessMask, |
| 1125 | srcStageMask, |
| 1126 | dstStageMask, |
| 1127 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1128 | |
| 1129 | // Flip rect if necessary |
| 1130 | SkIRect srcVkRect = srcRect; |
| 1131 | int32_t dstY = dstPoint.fY; |
| 1132 | |
| 1133 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1134 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin()); |
| 1135 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1136 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1137 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1138 | } |
| 1139 | |
| 1140 | VkImageCopy copyRegion; |
| 1141 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 1142 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1143 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1144 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1145 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
| 1146 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 0 }; |
| 1147 | |
| 1148 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1149 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1150 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1151 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1152 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1153 | 1, |
| 1154 | ©Region); |
| 1155 | } |
| 1156 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1157 | inline bool can_copy_as_blit(const GrSurface* dst, |
| 1158 | const GrSurface* src, |
| 1159 | const GrVkImage* dstImage, |
| 1160 | const GrVkImage* srcImage, |
| 1161 | const GrVkGpu* gpu) { |
| 1162 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1163 | // as image usage flags. |
| 1164 | const GrVkCaps& caps = gpu->vkCaps(); |
| 1165 | if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) || |
| 1166 | !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) { |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | // We cannot blit images that are multisampled. Will need to figure out if we can blit the |
| 1171 | // resolved msaa though. |
| 1172 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1173 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
| 1177 | return true; |
| 1178 | } |
| 1179 | |
| 1180 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, |
| 1181 | GrSurface* src, |
| 1182 | GrVkImage* dstImage, |
| 1183 | GrVkImage* srcImage, |
| 1184 | const SkIRect& srcRect, |
| 1185 | const SkIPoint& dstPoint) { |
| 1186 | SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this)); |
| 1187 | |
| 1188 | VkImageLayout origDstLayout = dstImage->currentLayout(); |
| 1189 | VkImageLayout origSrcLayout = srcImage->currentLayout(); |
| 1190 | |
| 1191 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1192 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1193 | |
| 1194 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 1195 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1196 | |
| 1197 | dstImage->setImageLayout(this, |
| 1198 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1199 | srcAccessMask, |
| 1200 | dstAccessMask, |
| 1201 | srcStageMask, |
| 1202 | dstStageMask, |
| 1203 | false); |
| 1204 | |
| 1205 | srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origSrcLayout); |
| 1206 | dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1207 | |
| 1208 | srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origSrcLayout); |
| 1209 | dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1210 | |
| 1211 | srcImage->setImageLayout(this, |
| 1212 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1213 | srcAccessMask, |
| 1214 | dstAccessMask, |
| 1215 | srcStageMask, |
| 1216 | dstStageMask, |
| 1217 | false); |
| 1218 | |
| 1219 | // Flip rect if necessary |
| 1220 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1221 | srcVkRect.fLeft = srcRect.fLeft; |
| 1222 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1223 | SkIRect dstRect; |
| 1224 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1225 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1226 | |
| 1227 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1228 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1229 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1230 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1231 | srcVkRect.fTop = srcRect.fTop; |
| 1232 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 1236 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1237 | } else { |
| 1238 | dstRect.fTop = dstPoint.fY; |
| 1239 | } |
| 1240 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 1241 | |
| 1242 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 1243 | // get the correct origintation of the copied data. |
| 1244 | if (src->origin() != dst->origin()) { |
| 1245 | SkTSwap(dstRect.fTop, dstRect.fBottom); |
| 1246 | } |
| 1247 | |
| 1248 | VkImageBlit blitRegion; |
| 1249 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 1250 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1251 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1252 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 0 }; |
| 1253 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1254 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
| 1255 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 0 }; |
| 1256 | |
| 1257 | fCurrentCmdBuffer->blitImage(this, |
| 1258 | srcImage, |
| 1259 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1260 | dstImage, |
| 1261 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1262 | 1, |
| 1263 | &blitRegion, |
| 1264 | VK_FILTER_NEAREST); // We never scale so any filter works here |
| 1265 | } |
| 1266 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1267 | inline bool can_copy_as_draw(const GrSurface* dst, |
| 1268 | const GrSurface* src, |
| 1269 | const GrVkGpu* gpu) { |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
| 1273 | void GrVkGpu::copySurfaceAsDraw(GrSurface* dst, |
| 1274 | GrSurface* src, |
| 1275 | const SkIRect& srcRect, |
| 1276 | const SkIPoint& dstPoint) { |
| 1277 | SkASSERT(false); |
| 1278 | } |
| 1279 | |
| 1280 | bool GrVkGpu::onCopySurface(GrSurface* dst, |
| 1281 | GrSurface* src, |
| 1282 | const SkIRect& srcRect, |
| 1283 | const SkIPoint& dstPoint) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1284 | GrVkImage* dstImage; |
| 1285 | GrVkImage* srcImage; |
| 1286 | if (dst->asTexture()) { |
| 1287 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
| 1288 | } else { |
| 1289 | SkASSERT(dst->asRenderTarget()); |
| 1290 | dstImage = static_cast<GrVkRenderTarget*>(dst->asRenderTarget()); |
| 1291 | } |
| 1292 | if (src->asTexture()) { |
| 1293 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
| 1294 | } else { |
| 1295 | SkASSERT(src->asRenderTarget()); |
| 1296 | srcImage = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
| 1297 | } |
| 1298 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1299 | if (can_copy_image(dst, src, this)) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1300 | this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint); |
| 1301 | return true; |
| 1302 | } |
| 1303 | |
| 1304 | if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) { |
| 1305 | this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1306 | return true; |
| 1307 | } |
| 1308 | |
| 1309 | if (can_copy_as_draw(dst, src, this)) { |
| 1310 | this->copySurfaceAsDraw(dst, src, srcRect, dstPoint); |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | return false; |
| 1315 | } |
| 1316 | |
egdaniel | 37798fb | 2016-04-12 07:31:49 -0700 | [diff] [blame] | 1317 | bool GrVkGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const { |
| 1318 | // Currently we don't support msaa |
| 1319 | if (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1) { |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | // This will support copying the dst as CopyImage since all of our surfaces require transferSrc |
| 1324 | // and transferDst usage flags in Vulkan. |
| 1325 | desc->fOrigin = src->origin(); |
| 1326 | desc->fConfig = src->config(); |
| 1327 | desc->fFlags = kNone_GrSurfaceFlags; |
| 1328 | return true; |
| 1329 | } |
| 1330 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1331 | void GrVkGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&, |
| 1332 | int* effectiveSampleCnt, SkAutoTDeleteArray<SkPoint>*) { |
| 1333 | // TODO: stub. |
| 1334 | SkASSERT(!this->caps()->sampleLocationsSupport()); |
| 1335 | *effectiveSampleCnt = rt->desc().fSampleCnt; |
| 1336 | } |
| 1337 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1338 | bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 1339 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 1340 | ReadPixelTempDrawInfo* tempDrawInfo) { |
| 1341 | // Currently we don't handle draws, so if the caller wants/needs to do a draw we need to fail |
| 1342 | if (kNoDraw_DrawPreference != *drawPreference) { |
| 1343 | return false; |
| 1344 | } |
| 1345 | |
| 1346 | if (srcSurface->config() != readConfig) { |
| 1347 | // TODO: This should fall back to drawing or copying to change config of srcSurface to match |
| 1348 | // that of readConfig. |
| 1349 | return false; |
| 1350 | } |
| 1351 | |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
| 1355 | bool GrVkGpu::onReadPixels(GrSurface* surface, |
| 1356 | int left, int top, int width, int height, |
| 1357 | GrPixelConfig config, |
| 1358 | void* buffer, |
| 1359 | size_t rowBytes) { |
| 1360 | VkFormat pixelFormat; |
| 1361 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1362 | return false; |
| 1363 | } |
| 1364 | |
| 1365 | GrVkTexture* tgt = static_cast<GrVkTexture*>(surface->asTexture()); |
| 1366 | if (!tgt) { |
| 1367 | return false; |
| 1368 | } |
| 1369 | |
| 1370 | // Change layout of our target so it can be used as copy |
| 1371 | VkImageLayout layout = tgt->currentLayout(); |
| 1372 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 1373 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1374 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 1375 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1376 | tgt->setImageLayout(this, |
| 1377 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1378 | srcAccessMask, |
| 1379 | dstAccessMask, |
| 1380 | srcStageMask, |
| 1381 | dstStageMask, |
| 1382 | false); |
| 1383 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1384 | GrVkTransferBuffer* transferBuffer = |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1385 | static_cast<GrVkTransferBuffer*>(this->createBuffer(rowBytes * height, |
| 1386 | kXferGpuToCpu_GrBufferType, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 1387 | kStream_GrAccessPattern)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1388 | |
| 1389 | bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin(); |
| 1390 | VkOffset3D offset = { |
| 1391 | left, |
| 1392 | flipY ? surface->height() - top - height : top, |
| 1393 | 0 |
| 1394 | }; |
| 1395 | |
| 1396 | // Copy the image to a buffer so we can map it to cpu memory |
| 1397 | VkBufferImageCopy region; |
| 1398 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 1399 | region.bufferOffset = 0; |
| 1400 | region.bufferRowLength = 0; // Forces RowLength to be imageExtent.width |
| 1401 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 1402 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1403 | region.imageOffset = offset; |
| 1404 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 1405 | |
| 1406 | fCurrentCmdBuffer->copyImageToBuffer(this, |
| 1407 | tgt, |
| 1408 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1409 | transferBuffer, |
| 1410 | 1, |
| 1411 | ®ion); |
| 1412 | |
| 1413 | // make sure the copy to buffer has finished |
| 1414 | transferBuffer->addMemoryBarrier(this, |
| 1415 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1416 | VK_ACCESS_HOST_READ_BIT, |
| 1417 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1418 | VK_PIPELINE_STAGE_HOST_BIT, |
| 1419 | false); |
| 1420 | |
| 1421 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 1422 | // we can copy the data out of the buffer. |
| 1423 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1424 | |
| 1425 | void* mappedMemory = transferBuffer->map(); |
| 1426 | |
| 1427 | memcpy(buffer, mappedMemory, rowBytes*height); |
| 1428 | |
| 1429 | transferBuffer->unmap(); |
| 1430 | transferBuffer->unref(); |
| 1431 | |
| 1432 | if (flipY) { |
| 1433 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
| 1434 | size_t tightRowBytes = GrBytesPerPixel(config) * width; |
| 1435 | scratch.reset(tightRowBytes); |
| 1436 | void* tmpRow = scratch.get(); |
| 1437 | // flip y in-place by rows |
| 1438 | const int halfY = height >> 1; |
| 1439 | char* top = reinterpret_cast<char*>(buffer); |
| 1440 | char* bottom = top + (height - 1) * rowBytes; |
| 1441 | for (int y = 0; y < halfY; y++) { |
| 1442 | memcpy(tmpRow, top, tightRowBytes); |
| 1443 | memcpy(top, bottom, tightRowBytes); |
| 1444 | memcpy(bottom, tmpRow, tightRowBytes); |
| 1445 | top += rowBytes; |
| 1446 | bottom -= rowBytes; |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | return true; |
| 1451 | } |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1452 | sk_sp<GrVkPipelineState> GrVkGpu::prepareDrawState(const GrPipeline& pipeline, |
| 1453 | const GrPrimitiveProcessor& primProc, |
| 1454 | GrPrimitiveType primitiveType, |
| 1455 | const GrVkRenderPass& renderPass) { |
| 1456 | sk_sp<GrVkPipelineState> pipelineState = |
| 1457 | fResourceProvider.findOrCreateCompatiblePipelineState(pipeline, |
| 1458 | primProc, |
| 1459 | primitiveType, |
| 1460 | renderPass); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1461 | if (!pipelineState) { |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1462 | return pipelineState; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1465 | pipelineState->setData(this, primProc, pipeline); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1466 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1467 | pipelineState->bind(this, fCurrentCmdBuffer); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 1468 | |
| 1469 | GrVkPipeline::SetDynamicState(this, fCurrentCmdBuffer, pipeline); |
| 1470 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1471 | return pipelineState; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | void GrVkGpu::onDraw(const GrPipeline& pipeline, |
| 1475 | const GrPrimitiveProcessor& primProc, |
| 1476 | const GrMesh* meshes, |
| 1477 | int meshCount) { |
| 1478 | if (!meshCount) { |
| 1479 | return; |
| 1480 | } |
| 1481 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1482 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt); |
| 1483 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1484 | SkASSERT(renderPass); |
| 1485 | |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 1486 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 1487 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1488 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1489 | sk_sp<GrVkPipelineState> pipelineState = this->prepareDrawState(pipeline, |
| 1490 | primProc, |
| 1491 | primitiveType, |
| 1492 | *renderPass); |
| 1493 | if (!pipelineState) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1494 | return; |
| 1495 | } |
| 1496 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1497 | // Change layout of our render target so it can be used as the color attachment |
| 1498 | VkImageLayout layout = vkRT->currentLayout(); |
| 1499 | // Our color attachment is purely a destination and won't be read so don't need to flush or |
| 1500 | // invalidate any caches |
| 1501 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 1502 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1503 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 1504 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 1505 | vkRT->setImageLayout(this, |
| 1506 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1507 | srcAccessMask, |
| 1508 | dstAccessMask, |
| 1509 | srcStageMask, |
| 1510 | dstStageMask, |
| 1511 | false); |
| 1512 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1513 | // If we are using a stencil attachment we also need to update its layout |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1514 | if (!pipeline.getStencil().isDisabled()) { |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1515 | GrStencilAttachment* stencil = vkRT->renderTargetPriv().getStencilAttachment(); |
| 1516 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1517 | VkImageLayout origDstLayout = vkStencil->currentLayout(); |
| 1518 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 1519 | VkAccessFlags dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1520 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1521 | VkPipelineStageFlags srcStageMask = |
| 1522 | GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1523 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1524 | vkStencil->setImageLayout(this, |
| 1525 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1526 | srcAccessMask, |
| 1527 | dstAccessMask, |
| 1528 | srcStageMask, |
| 1529 | dstStageMask, |
| 1530 | false); |
| 1531 | } |
| 1532 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1533 | |
| 1534 | for (int i = 0; i < meshCount; ++i) { |
| 1535 | if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*this->caps())) { |
| 1536 | this->xferBarrier(pipeline.getRenderTarget(), barrierType); |
| 1537 | } |
| 1538 | |
| 1539 | const GrMesh& mesh = meshes[i]; |
| 1540 | GrMesh::Iterator iter; |
| 1541 | const GrNonInstancedMesh* nonIdxMesh = iter.init(mesh); |
| 1542 | do { |
| 1543 | if (nonIdxMesh->primitiveType() != primitiveType) { |
| 1544 | // Technically we don't have to call this here (since there is a safety check in |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1545 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 1546 | // pipelineState sits in a cache for a while. |
| 1547 | pipelineState->freeTempResources(this); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1548 | SkDEBUGCODE(pipelineState = nullptr); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1549 | primitiveType = nonIdxMesh->primitiveType(); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1550 | pipelineState = this->prepareDrawState(pipeline, |
| 1551 | primProc, |
| 1552 | primitiveType, |
| 1553 | *renderPass); |
| 1554 | if (!pipelineState) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1555 | return; |
| 1556 | } |
| 1557 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1558 | SkASSERT(pipelineState); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1559 | this->bindGeometry(primProc, *nonIdxMesh); |
| 1560 | |
| 1561 | if (nonIdxMesh->isIndexed()) { |
| 1562 | fCurrentCmdBuffer->drawIndexed(this, |
| 1563 | nonIdxMesh->indexCount(), |
| 1564 | 1, |
| 1565 | nonIdxMesh->startIndex(), |
| 1566 | nonIdxMesh->startVertex(), |
| 1567 | 0); |
| 1568 | } else { |
| 1569 | fCurrentCmdBuffer->draw(this, |
| 1570 | nonIdxMesh->vertexCount(), |
| 1571 | 1, |
| 1572 | nonIdxMesh->startVertex(), |
| 1573 | 0); |
| 1574 | } |
| 1575 | |
| 1576 | fStats.incNumDraws(); |
| 1577 | } while ((nonIdxMesh = iter.next())); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | fCurrentCmdBuffer->endRenderPass(this); |
| 1581 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1582 | // Technically we don't have to call this here (since there is a safety check in |
| 1583 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 1584 | // pipelineState sits in a cache for a while. |
| 1585 | pipelineState->freeTempResources(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1586 | |
| 1587 | #if SWAP_PER_DRAW |
| 1588 | glFlush(); |
| 1589 | #if defined(SK_BUILD_FOR_MAC) |
| 1590 | aglSwapBuffers(aglGetCurrentContext()); |
| 1591 | int set_a_break_pt_here = 9; |
| 1592 | aglSwapBuffers(aglGetCurrentContext()); |
| 1593 | #elif defined(SK_BUILD_FOR_WIN32) |
| 1594 | SwapBuf(); |
| 1595 | int set_a_break_pt_here = 9; |
| 1596 | SwapBuf(); |
| 1597 | #endif |
| 1598 | #endif |
| 1599 | } |