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 |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 123 | backendCtx->fGraphicsQueueIndex, // queueFamilyIndex |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 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 | //////////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 441 | GrTexture* GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
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) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 496 | tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 497 | imageDesc); |
| 498 | } else { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 499 | tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 551 | GrSurfaceDesc surfDesc; |
| 552 | // next line relies on GrBackendTextureDesc's flags matching GrTexture's |
| 553 | surfDesc.fFlags = (GrSurfaceFlags)desc.fFlags; |
| 554 | surfDesc.fWidth = desc.fWidth; |
| 555 | surfDesc.fHeight = desc.fHeight; |
| 556 | surfDesc.fConfig = desc.fConfig; |
| 557 | surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); |
| 558 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 559 | // In GL, Chrome assumes all textures are BottomLeft |
| 560 | // In VK, we don't have this restriction |
| 561 | surfDesc.fOrigin = resolve_origin(desc.fOrigin); |
| 562 | |
| 563 | GrVkTexture* texture = nullptr; |
| 564 | if (renderTarget) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 565 | texture = GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(this, surfDesc, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 566 | ownership, format, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 567 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 568 | } else { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 569 | texture = GrVkTexture::CreateWrappedTexture(this, surfDesc, ownership, format, info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 570 | } |
| 571 | if (!texture) { |
| 572 | return nullptr; |
| 573 | } |
| 574 | |
| 575 | return texture; |
| 576 | } |
| 577 | |
| 578 | GrRenderTarget* GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc, |
| 579 | GrWrapOwnership ownership) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 580 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 581 | const GrVkTextureInfo* info = |
| 582 | reinterpret_cast<const GrVkTextureInfo*>(wrapDesc.fRenderTargetHandle); |
| 583 | if (VK_NULL_HANDLE == info->fImage || |
| 584 | (VK_NULL_HANDLE == info->fAlloc && kAdopt_GrWrapOwnership == ownership)) { |
| 585 | return nullptr; |
| 586 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 587 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 588 | GrSurfaceDesc desc; |
| 589 | desc.fConfig = wrapDesc.fConfig; |
| 590 | desc.fFlags = kCheckAllocation_GrSurfaceFlag; |
| 591 | desc.fWidth = wrapDesc.fWidth; |
| 592 | desc.fHeight = wrapDesc.fHeight; |
| 593 | desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount()); |
| 594 | |
| 595 | desc.fOrigin = resolve_origin(wrapDesc.fOrigin); |
| 596 | |
| 597 | GrVkRenderTarget* tgt = GrVkRenderTarget::CreateWrappedRenderTarget(this, desc, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame^] | 598 | ownership, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 599 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 600 | if (tgt && wrapDesc.fStencilBits) { |
| 601 | if (!createStencilAttachmentForRenderTarget(tgt, desc.fWidth, desc.fHeight)) { |
| 602 | tgt->unref(); |
| 603 | return nullptr; |
| 604 | } |
| 605 | } |
| 606 | return tgt; |
| 607 | } |
| 608 | |
| 609 | //////////////////////////////////////////////////////////////////////////////// |
| 610 | |
| 611 | void GrVkGpu::bindGeometry(const GrPrimitiveProcessor& primProc, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 612 | const GrNonInstancedMesh& mesh) { |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 613 | // There is no need to put any memory barriers to make sure host writes have finished here. |
| 614 | // When a command buffer is submitted to a queue, there is an implicit memory barrier that |
| 615 | // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of |
| 616 | // an active RenderPass. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 617 | GrVkVertexBuffer* vbuf; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 618 | vbuf = (GrVkVertexBuffer*)mesh.vertexBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 619 | SkASSERT(vbuf); |
| 620 | SkASSERT(!vbuf->isMapped()); |
| 621 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 622 | fCurrentCmdBuffer->bindVertexBuffer(this, vbuf); |
| 623 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 624 | if (mesh.isIndexed()) { |
| 625 | GrVkIndexBuffer* ibuf = (GrVkIndexBuffer*)mesh.indexBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 626 | SkASSERT(ibuf); |
| 627 | SkASSERT(!ibuf->isMapped()); |
| 628 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 629 | fCurrentCmdBuffer->bindIndexBuffer(this, ibuf); |
| 630 | } |
| 631 | } |
| 632 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 633 | //////////////////////////////////////////////////////////////////////////////// |
| 634 | |
| 635 | GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 636 | int width, |
| 637 | int height) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 638 | SkASSERT(width >= rt->width()); |
| 639 | SkASSERT(height >= rt->height()); |
| 640 | |
| 641 | int samples = rt->numStencilSamples(); |
| 642 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 643 | const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 644 | |
| 645 | GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 646 | width, |
| 647 | height, |
| 648 | samples, |
| 649 | sFmt)); |
| 650 | fStats.incStencilAttachmentCreates(); |
| 651 | return stencil; |
| 652 | } |
| 653 | |
| 654 | //////////////////////////////////////////////////////////////////////////////// |
| 655 | |
| 656 | GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h, |
| 657 | GrPixelConfig config) { |
| 658 | |
| 659 | VkFormat pixelFormat; |
| 660 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | bool linearTiling = false; |
| 665 | if (!fVkCaps->isConfigTexturable(config)) { |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | if (fVkCaps->isConfigTexurableLinearly(config)) { |
| 670 | linearTiling = true; |
| 671 | } |
| 672 | |
| 673 | // Currently this is not supported since it requires a copy which has not yet been implemented. |
| 674 | if (srcData && !linearTiling) { |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 679 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 680 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 681 | |
| 682 | VkFlags memProps = (srcData && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT : |
| 683 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 684 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 685 | VkImage image = VK_NULL_HANDLE; |
| 686 | VkDeviceMemory alloc = VK_NULL_HANDLE; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 687 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 688 | VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 689 | VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling) |
| 690 | ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 691 | : VK_IMAGE_LAYOUT_UNDEFINED; |
| 692 | |
| 693 | // Create Image |
| 694 | VkSampleCountFlagBits vkSamples; |
| 695 | if (!GrSampleCountToVkSampleCount(1, &vkSamples)) { |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | const VkImageCreateInfo imageCreateInfo = { |
| 700 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 701 | NULL, // pNext |
| 702 | 0, // VkImageCreateFlags |
| 703 | VK_IMAGE_TYPE_2D, // VkImageType |
| 704 | pixelFormat, // VkFormat |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 705 | { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 706 | 1, // mipLevels |
| 707 | 1, // arrayLayers |
| 708 | vkSamples, // samples |
| 709 | imageTiling, // VkImageTiling |
| 710 | usageFlags, // VkImageUsageFlags |
| 711 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 712 | 0, // queueFamilyCount |
| 713 | 0, // pQueueFamilyIndices |
| 714 | initialLayout // initialLayout |
| 715 | }; |
| 716 | |
| 717 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image)); |
| 718 | |
| 719 | if (!GrVkMemory::AllocAndBindImageMemory(this, image, memProps, &alloc)) { |
| 720 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | if (srcData) { |
| 725 | if (linearTiling) { |
| 726 | const VkImageSubresource subres = { |
| 727 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 728 | 0, // mipLevel |
| 729 | 0, // arraySlice |
| 730 | }; |
| 731 | VkSubresourceLayout layout; |
| 732 | VkResult err; |
| 733 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 734 | VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 735 | |
| 736 | void* mapPtr; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 737 | err = VK_CALL(MapMemory(fDevice, alloc, 0, layout.rowPitch * h, 0, &mapPtr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 738 | if (err) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 739 | VK_CALL(FreeMemory(this->device(), alloc, nullptr)); |
| 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 | size_t bpp = GrBytesPerPixel(config); |
| 745 | size_t rowCopyBytes = bpp * w; |
| 746 | // If there is no padding on dst (layout.rowPitch) we can do a single memcopy. |
| 747 | // This assumes the srcData comes in with no padding. |
| 748 | if (rowCopyBytes == layout.rowPitch) { |
| 749 | memcpy(mapPtr, srcData, rowCopyBytes * h); |
| 750 | } else { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 751 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), srcData, rowCopyBytes, |
| 752 | rowCopyBytes, h); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 753 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 754 | VK_CALL(UnmapMemory(fDevice, alloc)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 755 | } else { |
| 756 | // TODO: Add support for copying to optimal tiling |
| 757 | SkASSERT(false); |
| 758 | } |
| 759 | } |
| 760 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 761 | GrVkTextureInfo* info = new GrVkTextureInfo; |
| 762 | info->fImage = image; |
| 763 | info->fAlloc = alloc; |
| 764 | info->fImageTiling = imageTiling; |
| 765 | info->fImageLayout = initialLayout; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 766 | info->fFormat = pixelFormat; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 767 | |
| 768 | return (GrBackendObject)info; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 772 | const GrVkTextureInfo* backend = reinterpret_cast<const GrVkTextureInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 773 | |
| 774 | if (backend && backend->fImage && backend->fAlloc) { |
| 775 | VkMemoryRequirements req; |
| 776 | memset(&req, 0, sizeof(req)); |
| 777 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
| 778 | backend->fImage, |
| 779 | &req)); |
| 780 | // TODO: find a better check |
| 781 | // This will probably fail with a different driver |
| 782 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 783 | } |
| 784 | |
| 785 | return false; |
| 786 | } |
| 787 | |
| 788 | void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 789 | const GrVkTextureInfo* backend = reinterpret_cast<const GrVkTextureInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 790 | |
| 791 | if (backend) { |
| 792 | if (!abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 793 | // something in the command buffer may still be using this, so force submit |
| 794 | this->submitCommandBuffer(kForce_SyncQueue); |
| 795 | |
| 796 | VK_CALL(FreeMemory(this->device(), backend->fAlloc, nullptr)); |
| 797 | VK_CALL(DestroyImage(this->device(), backend->fImage, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 798 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 799 | delete backend; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
| 803 | //////////////////////////////////////////////////////////////////////////////// |
| 804 | |
| 805 | void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 806 | VkPipelineStageFlags dstStageMask, |
| 807 | bool byRegion, |
| 808 | VkMemoryBarrier* barrier) const { |
| 809 | SkASSERT(fCurrentCmdBuffer); |
| 810 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 811 | srcStageMask, |
| 812 | dstStageMask, |
| 813 | byRegion, |
| 814 | GrVkCommandBuffer::kMemory_BarrierType, |
| 815 | barrier); |
| 816 | } |
| 817 | |
| 818 | void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 819 | VkPipelineStageFlags dstStageMask, |
| 820 | bool byRegion, |
| 821 | VkBufferMemoryBarrier* barrier) const { |
| 822 | SkASSERT(fCurrentCmdBuffer); |
| 823 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 824 | srcStageMask, |
| 825 | dstStageMask, |
| 826 | byRegion, |
| 827 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 828 | barrier); |
| 829 | } |
| 830 | |
| 831 | void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 832 | VkPipelineStageFlags dstStageMask, |
| 833 | bool byRegion, |
| 834 | VkImageMemoryBarrier* barrier) const { |
| 835 | SkASSERT(fCurrentCmdBuffer); |
| 836 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 837 | srcStageMask, |
| 838 | dstStageMask, |
| 839 | byRegion, |
| 840 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 841 | barrier); |
| 842 | } |
| 843 | |
| 844 | void GrVkGpu::finishDrawTarget() { |
| 845 | // Submit the current command buffer to the Queue |
| 846 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 847 | } |
| 848 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 849 | void GrVkGpu::clearStencil(GrRenderTarget* target) { |
| 850 | if (nullptr == target) { |
| 851 | return; |
| 852 | } |
| 853 | GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment(); |
| 854 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 855 | |
| 856 | |
| 857 | VkClearDepthStencilValue vkStencilColor; |
| 858 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 859 | |
| 860 | VkImageLayout origDstLayout = vkStencil->currentLayout(); |
| 861 | |
| 862 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 863 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 864 | |
| 865 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 866 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 867 | |
| 868 | vkStencil->setImageLayout(this, |
| 869 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 870 | srcAccessMask, |
| 871 | dstAccessMask, |
| 872 | srcStageMask, |
| 873 | dstStageMask, |
| 874 | false); |
| 875 | |
| 876 | |
| 877 | VkImageSubresourceRange subRange; |
| 878 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 879 | subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 880 | subRange.baseMipLevel = 0; |
| 881 | subRange.levelCount = 1; |
| 882 | subRange.baseArrayLayer = 0; |
| 883 | subRange.layerCount = 1; |
| 884 | |
| 885 | // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a |
| 886 | // draw. Thus we should look into using the load op functions on the render pass to clear out |
| 887 | // the stencil there. |
| 888 | fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange); |
| 889 | } |
| 890 | |
| 891 | void GrVkGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) { |
| 892 | SkASSERT(target); |
| 893 | |
| 894 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
| 895 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
| 896 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)sb; |
| 897 | |
| 898 | // this should only be called internally when we know we have a |
| 899 | // stencil buffer. |
| 900 | SkASSERT(sb); |
| 901 | int stencilBitCount = sb->bits(); |
| 902 | |
| 903 | // The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 904 | // during this clear. Thus we will clear the entire stencil to the desired value. |
| 905 | |
| 906 | VkClearDepthStencilValue vkStencilColor; |
| 907 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 908 | if (insideClip) { |
| 909 | vkStencilColor.stencil = (1 << (stencilBitCount - 1)); |
| 910 | } else { |
| 911 | vkStencilColor.stencil = 0; |
| 912 | } |
| 913 | |
| 914 | VkImageLayout origDstLayout = vkStencil->currentLayout(); |
| 915 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 916 | VkAccessFlags dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 917 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 918 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 919 | vkStencil->setImageLayout(this, |
| 920 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 921 | srcAccessMask, |
| 922 | dstAccessMask, |
| 923 | srcStageMask, |
| 924 | dstStageMask, |
| 925 | false); |
| 926 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 927 | // Change layout of our render target so it can be used as the color attachment. This is what |
| 928 | // the render pass expects when it begins. |
| 929 | VkImageLayout layout = vkRT->currentLayout(); |
| 930 | srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 931 | dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 932 | srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 933 | dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 934 | vkRT->setImageLayout(this, |
| 935 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 936 | srcAccessMask, |
| 937 | dstAccessMask, |
| 938 | srcStageMask, |
| 939 | dstStageMask, |
| 940 | false); |
| 941 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 942 | VkClearRect clearRect; |
| 943 | // Flip rect if necessary |
| 944 | SkIRect vkRect = rect; |
| 945 | |
| 946 | if (kBottomLeft_GrSurfaceOrigin == vkRT->origin()) { |
| 947 | vkRect.fTop = vkRT->height() - rect.fBottom; |
| 948 | vkRect.fBottom = vkRT->height() - rect.fTop; |
| 949 | } |
| 950 | |
| 951 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 952 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 953 | |
| 954 | clearRect.baseArrayLayer = 0; |
| 955 | clearRect.layerCount = 1; |
| 956 | |
| 957 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 958 | SkASSERT(renderPass); |
| 959 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 960 | |
| 961 | uint32_t stencilIndex; |
| 962 | SkAssertResult(renderPass->stencilAttachmentIndex(&stencilIndex)); |
| 963 | |
| 964 | VkClearAttachment attachment; |
| 965 | attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 966 | attachment.colorAttachment = 0; // this value shouldn't matter |
| 967 | attachment.clearValue.depthStencil = vkStencilColor; |
| 968 | |
| 969 | fCurrentCmdBuffer->clearAttachments(this, 1, &attachment, 1, &clearRect); |
| 970 | fCurrentCmdBuffer->endRenderPass(this); |
| 971 | |
| 972 | return; |
| 973 | } |
| 974 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 975 | void GrVkGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) { |
| 976 | // parent class should never let us get here with no RT |
| 977 | SkASSERT(target); |
| 978 | |
| 979 | VkClearColorValue vkColor; |
| 980 | GrColorToRGBAFloat(color, vkColor.float32); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 981 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 982 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
| 983 | VkImageLayout origDstLayout = vkRT->currentLayout(); |
| 984 | |
| 985 | if (rect.width() != target->width() || rect.height() != target->height()) { |
| 986 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 987 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 988 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 989 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 990 | vkRT->setImageLayout(this, |
| 991 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 992 | srcAccessMask, |
| 993 | dstAccessMask, |
| 994 | srcStageMask, |
| 995 | dstStageMask, |
| 996 | false); |
| 997 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 998 | // If we are using a stencil attachment we also need to change its layout to what the render |
| 999 | // pass is expecting. |
| 1000 | if (GrStencilAttachment* stencil = vkRT->renderTargetPriv().getStencilAttachment()) { |
| 1001 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1002 | origDstLayout = vkStencil->currentLayout(); |
| 1003 | srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 1004 | dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 1005 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; |
| 1006 | srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1007 | dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1008 | vkStencil->setImageLayout(this, |
| 1009 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1010 | srcAccessMask, |
| 1011 | dstAccessMask, |
| 1012 | srcStageMask, |
| 1013 | dstStageMask, |
| 1014 | false); |
| 1015 | } |
| 1016 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1017 | VkClearRect clearRect; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1018 | // Flip rect if necessary |
| 1019 | SkIRect vkRect = rect; |
| 1020 | if (kBottomLeft_GrSurfaceOrigin == vkRT->origin()) { |
| 1021 | vkRect.fTop = vkRT->height() - rect.fBottom; |
| 1022 | vkRect.fBottom = vkRT->height() - rect.fTop; |
| 1023 | } |
| 1024 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 1025 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
jvanverth | e77875a | 2016-03-04 15:16:25 -0800 | [diff] [blame] | 1026 | clearRect.baseArrayLayer = 0; |
| 1027 | clearRect.layerCount = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1028 | |
| 1029 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1030 | SkASSERT(renderPass); |
| 1031 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 1032 | |
| 1033 | uint32_t colorIndex; |
| 1034 | SkAssertResult(renderPass->colorAttachmentIndex(&colorIndex)); |
| 1035 | |
| 1036 | VkClearAttachment attachment; |
| 1037 | attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1038 | attachment.colorAttachment = colorIndex; |
| 1039 | attachment.clearValue.color = vkColor; |
| 1040 | |
| 1041 | fCurrentCmdBuffer->clearAttachments(this, 1, &attachment, 1, &clearRect); |
| 1042 | fCurrentCmdBuffer->endRenderPass(this); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1047 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1048 | |
| 1049 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 1050 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1051 | |
| 1052 | vkRT->setImageLayout(this, |
| 1053 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1054 | srcAccessMask, |
| 1055 | dstAccessMask, |
| 1056 | srcStageMask, |
| 1057 | dstStageMask, |
| 1058 | false); |
| 1059 | |
| 1060 | |
| 1061 | VkImageSubresourceRange subRange; |
| 1062 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1063 | subRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1064 | subRange.baseMipLevel = 0; |
| 1065 | subRange.levelCount = 1; |
| 1066 | subRange.baseArrayLayer = 0; |
| 1067 | subRange.layerCount = 1; |
| 1068 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1069 | // 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] | 1070 | // render pass or doing a non full clear then we will use CmdClearColorAttachment. The more |
| 1071 | // common use case will be clearing an attachment at the start of a render pass, in which case |
| 1072 | // we will use the clear load ops. |
| 1073 | fCurrentCmdBuffer->clearColorImage(this, |
| 1074 | vkRT, |
| 1075 | &vkColor, |
| 1076 | 1, &subRange); |
| 1077 | } |
| 1078 | |
| 1079 | inline bool can_copy_image(const GrSurface* dst, |
| 1080 | const GrSurface* src, |
| 1081 | const GrVkGpu* gpu) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1082 | // Currently we don't support msaa |
| 1083 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1084 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1085 | return false; |
| 1086 | } |
| 1087 | |
| 1088 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1089 | // as image usage flags. |
| 1090 | if (src->origin() == dst->origin() && |
| 1091 | GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
| 1094 | |
| 1095 | // 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] | 1096 | // 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] | 1097 | |
| 1098 | return false; |
| 1099 | } |
| 1100 | |
| 1101 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, |
| 1102 | GrSurface* src, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1103 | GrVkImage* dstImage, |
| 1104 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1105 | const SkIRect& srcRect, |
| 1106 | const SkIPoint& dstPoint) { |
| 1107 | SkASSERT(can_copy_image(dst, src, this)); |
| 1108 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1109 | VkImageLayout origDstLayout = dstImage->currentLayout(); |
| 1110 | VkImageLayout origSrcLayout = srcImage->currentLayout(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1111 | |
| 1112 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1113 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1114 | |
| 1115 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 1116 | // the cache is flushed since it is only being written to. |
| 1117 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 1118 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1119 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1120 | dstImage->setImageLayout(this, |
| 1121 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1122 | srcAccessMask, |
| 1123 | dstAccessMask, |
| 1124 | srcStageMask, |
| 1125 | dstStageMask, |
| 1126 | false); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1127 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1128 | srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origSrcLayout); |
| 1129 | dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1130 | |
| 1131 | srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origSrcLayout); |
| 1132 | dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1133 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1134 | srcImage->setImageLayout(this, |
| 1135 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1136 | srcAccessMask, |
| 1137 | dstAccessMask, |
| 1138 | srcStageMask, |
| 1139 | dstStageMask, |
| 1140 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1141 | |
| 1142 | // Flip rect if necessary |
| 1143 | SkIRect srcVkRect = srcRect; |
| 1144 | int32_t dstY = dstPoint.fY; |
| 1145 | |
| 1146 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1147 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin()); |
| 1148 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1149 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1150 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1151 | } |
| 1152 | |
| 1153 | VkImageCopy copyRegion; |
| 1154 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 1155 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1156 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1157 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1158 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
| 1159 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 0 }; |
| 1160 | |
| 1161 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1162 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1163 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1164 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1165 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1166 | 1, |
| 1167 | ©Region); |
| 1168 | } |
| 1169 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1170 | inline bool can_copy_as_blit(const GrSurface* dst, |
| 1171 | const GrSurface* src, |
| 1172 | const GrVkImage* dstImage, |
| 1173 | const GrVkImage* srcImage, |
| 1174 | const GrVkGpu* gpu) { |
| 1175 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1176 | // as image usage flags. |
| 1177 | const GrVkCaps& caps = gpu->vkCaps(); |
| 1178 | if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) || |
| 1179 | !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) { |
| 1180 | return false; |
| 1181 | } |
| 1182 | |
| 1183 | // We cannot blit images that are multisampled. Will need to figure out if we can blit the |
| 1184 | // resolved msaa though. |
| 1185 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1186 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
| 1193 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, |
| 1194 | GrSurface* src, |
| 1195 | GrVkImage* dstImage, |
| 1196 | GrVkImage* srcImage, |
| 1197 | const SkIRect& srcRect, |
| 1198 | const SkIPoint& dstPoint) { |
| 1199 | SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this)); |
| 1200 | |
| 1201 | VkImageLayout origDstLayout = dstImage->currentLayout(); |
| 1202 | VkImageLayout origSrcLayout = srcImage->currentLayout(); |
| 1203 | |
| 1204 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1205 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1206 | |
| 1207 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout);; |
| 1208 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1209 | |
| 1210 | dstImage->setImageLayout(this, |
| 1211 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1212 | srcAccessMask, |
| 1213 | dstAccessMask, |
| 1214 | srcStageMask, |
| 1215 | dstStageMask, |
| 1216 | false); |
| 1217 | |
| 1218 | srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(origSrcLayout); |
| 1219 | dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1220 | |
| 1221 | srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origSrcLayout); |
| 1222 | dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1223 | |
| 1224 | srcImage->setImageLayout(this, |
| 1225 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1226 | srcAccessMask, |
| 1227 | dstAccessMask, |
| 1228 | srcStageMask, |
| 1229 | dstStageMask, |
| 1230 | false); |
| 1231 | |
| 1232 | // Flip rect if necessary |
| 1233 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1234 | srcVkRect.fLeft = srcRect.fLeft; |
| 1235 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1236 | SkIRect dstRect; |
| 1237 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1238 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1241 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1242 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1243 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1244 | srcVkRect.fTop = srcRect.fTop; |
| 1245 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 1249 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1250 | } else { |
| 1251 | dstRect.fTop = dstPoint.fY; |
| 1252 | } |
| 1253 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 1254 | |
| 1255 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 1256 | // get the correct origintation of the copied data. |
| 1257 | if (src->origin() != dst->origin()) { |
| 1258 | SkTSwap(dstRect.fTop, dstRect.fBottom); |
| 1259 | } |
| 1260 | |
| 1261 | VkImageBlit blitRegion; |
| 1262 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 1263 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1264 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1265 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 0 }; |
| 1266 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1267 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
| 1268 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 0 }; |
| 1269 | |
| 1270 | fCurrentCmdBuffer->blitImage(this, |
| 1271 | srcImage, |
| 1272 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1273 | dstImage, |
| 1274 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1275 | 1, |
| 1276 | &blitRegion, |
| 1277 | VK_FILTER_NEAREST); // We never scale so any filter works here |
| 1278 | } |
| 1279 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1280 | inline bool can_copy_as_draw(const GrSurface* dst, |
| 1281 | const GrSurface* src, |
| 1282 | const GrVkGpu* gpu) { |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
| 1286 | void GrVkGpu::copySurfaceAsDraw(GrSurface* dst, |
| 1287 | GrSurface* src, |
| 1288 | const SkIRect& srcRect, |
| 1289 | const SkIPoint& dstPoint) { |
| 1290 | SkASSERT(false); |
| 1291 | } |
| 1292 | |
| 1293 | bool GrVkGpu::onCopySurface(GrSurface* dst, |
| 1294 | GrSurface* src, |
| 1295 | const SkIRect& srcRect, |
| 1296 | const SkIPoint& dstPoint) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1297 | GrVkImage* dstImage; |
| 1298 | GrVkImage* srcImage; |
| 1299 | if (dst->asTexture()) { |
| 1300 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
| 1301 | } else { |
| 1302 | SkASSERT(dst->asRenderTarget()); |
| 1303 | dstImage = static_cast<GrVkRenderTarget*>(dst->asRenderTarget()); |
| 1304 | } |
| 1305 | if (src->asTexture()) { |
| 1306 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
| 1307 | } else { |
| 1308 | SkASSERT(src->asRenderTarget()); |
| 1309 | srcImage = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
| 1310 | } |
| 1311 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1312 | if (can_copy_image(dst, src, this)) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1313 | this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint); |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) { |
| 1318 | this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1319 | return true; |
| 1320 | } |
| 1321 | |
| 1322 | if (can_copy_as_draw(dst, src, this)) { |
| 1323 | this->copySurfaceAsDraw(dst, src, srcRect, dstPoint); |
| 1324 | return true; |
| 1325 | } |
| 1326 | |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
egdaniel | 37798fb | 2016-04-12 07:31:49 -0700 | [diff] [blame] | 1330 | bool GrVkGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const { |
| 1331 | // Currently we don't support msaa |
| 1332 | if (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1) { |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | // This will support copying the dst as CopyImage since all of our surfaces require transferSrc |
| 1337 | // and transferDst usage flags in Vulkan. |
| 1338 | desc->fOrigin = src->origin(); |
| 1339 | desc->fConfig = src->config(); |
| 1340 | desc->fFlags = kNone_GrSurfaceFlags; |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1344 | void GrVkGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&, |
| 1345 | int* effectiveSampleCnt, SkAutoTDeleteArray<SkPoint>*) { |
| 1346 | // TODO: stub. |
| 1347 | SkASSERT(!this->caps()->sampleLocationsSupport()); |
| 1348 | *effectiveSampleCnt = rt->desc().fSampleCnt; |
| 1349 | } |
| 1350 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1351 | bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 1352 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 1353 | ReadPixelTempDrawInfo* tempDrawInfo) { |
| 1354 | // Currently we don't handle draws, so if the caller wants/needs to do a draw we need to fail |
| 1355 | if (kNoDraw_DrawPreference != *drawPreference) { |
| 1356 | return false; |
| 1357 | } |
| 1358 | |
| 1359 | if (srcSurface->config() != readConfig) { |
| 1360 | // TODO: This should fall back to drawing or copying to change config of srcSurface to match |
| 1361 | // that of readConfig. |
| 1362 | return false; |
| 1363 | } |
| 1364 | |
| 1365 | return true; |
| 1366 | } |
| 1367 | |
| 1368 | bool GrVkGpu::onReadPixels(GrSurface* surface, |
| 1369 | int left, int top, int width, int height, |
| 1370 | GrPixelConfig config, |
| 1371 | void* buffer, |
| 1372 | size_t rowBytes) { |
| 1373 | VkFormat pixelFormat; |
| 1374 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
| 1378 | GrVkTexture* tgt = static_cast<GrVkTexture*>(surface->asTexture()); |
| 1379 | if (!tgt) { |
| 1380 | return false; |
| 1381 | } |
| 1382 | |
| 1383 | // Change layout of our target so it can be used as copy |
| 1384 | VkImageLayout layout = tgt->currentLayout(); |
| 1385 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 1386 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 1387 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 1388 | VkAccessFlags dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; |
| 1389 | tgt->setImageLayout(this, |
| 1390 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1391 | srcAccessMask, |
| 1392 | dstAccessMask, |
| 1393 | srcStageMask, |
| 1394 | dstStageMask, |
| 1395 | false); |
| 1396 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1397 | GrVkTransferBuffer* transferBuffer = |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1398 | static_cast<GrVkTransferBuffer*>(this->createBuffer(rowBytes * height, |
| 1399 | kXferGpuToCpu_GrBufferType, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 1400 | kStream_GrAccessPattern)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1401 | |
| 1402 | bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin(); |
| 1403 | VkOffset3D offset = { |
| 1404 | left, |
| 1405 | flipY ? surface->height() - top - height : top, |
| 1406 | 0 |
| 1407 | }; |
| 1408 | |
| 1409 | // Copy the image to a buffer so we can map it to cpu memory |
| 1410 | VkBufferImageCopy region; |
| 1411 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 1412 | region.bufferOffset = 0; |
| 1413 | region.bufferRowLength = 0; // Forces RowLength to be imageExtent.width |
| 1414 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 1415 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1416 | region.imageOffset = offset; |
| 1417 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 1418 | |
| 1419 | fCurrentCmdBuffer->copyImageToBuffer(this, |
| 1420 | tgt, |
| 1421 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1422 | transferBuffer, |
| 1423 | 1, |
| 1424 | ®ion); |
| 1425 | |
| 1426 | // make sure the copy to buffer has finished |
| 1427 | transferBuffer->addMemoryBarrier(this, |
| 1428 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1429 | VK_ACCESS_HOST_READ_BIT, |
| 1430 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1431 | VK_PIPELINE_STAGE_HOST_BIT, |
| 1432 | false); |
| 1433 | |
| 1434 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 1435 | // we can copy the data out of the buffer. |
| 1436 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1437 | |
| 1438 | void* mappedMemory = transferBuffer->map(); |
| 1439 | |
| 1440 | memcpy(buffer, mappedMemory, rowBytes*height); |
| 1441 | |
| 1442 | transferBuffer->unmap(); |
| 1443 | transferBuffer->unref(); |
| 1444 | |
| 1445 | if (flipY) { |
| 1446 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
| 1447 | size_t tightRowBytes = GrBytesPerPixel(config) * width; |
| 1448 | scratch.reset(tightRowBytes); |
| 1449 | void* tmpRow = scratch.get(); |
| 1450 | // flip y in-place by rows |
| 1451 | const int halfY = height >> 1; |
| 1452 | char* top = reinterpret_cast<char*>(buffer); |
| 1453 | char* bottom = top + (height - 1) * rowBytes; |
| 1454 | for (int y = 0; y < halfY; y++) { |
| 1455 | memcpy(tmpRow, top, tightRowBytes); |
| 1456 | memcpy(top, bottom, tightRowBytes); |
| 1457 | memcpy(bottom, tmpRow, tightRowBytes); |
| 1458 | top += rowBytes; |
| 1459 | bottom -= rowBytes; |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | return true; |
| 1464 | } |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1465 | sk_sp<GrVkPipelineState> GrVkGpu::prepareDrawState(const GrPipeline& pipeline, |
| 1466 | const GrPrimitiveProcessor& primProc, |
| 1467 | GrPrimitiveType primitiveType, |
| 1468 | const GrVkRenderPass& renderPass) { |
| 1469 | sk_sp<GrVkPipelineState> pipelineState = |
| 1470 | fResourceProvider.findOrCreateCompatiblePipelineState(pipeline, |
| 1471 | primProc, |
| 1472 | primitiveType, |
| 1473 | renderPass); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1474 | if (!pipelineState) { |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1475 | return pipelineState; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1478 | pipelineState->setData(this, primProc, pipeline); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1479 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1480 | pipelineState->bind(this, fCurrentCmdBuffer); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 1481 | |
| 1482 | GrVkPipeline::SetDynamicState(this, fCurrentCmdBuffer, pipeline); |
| 1483 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1484 | return pipelineState; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | void GrVkGpu::onDraw(const GrPipeline& pipeline, |
| 1488 | const GrPrimitiveProcessor& primProc, |
| 1489 | const GrMesh* meshes, |
| 1490 | int meshCount) { |
| 1491 | if (!meshCount) { |
| 1492 | return; |
| 1493 | } |
| 1494 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1495 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt); |
| 1496 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1497 | SkASSERT(renderPass); |
| 1498 | |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 1499 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1500 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1501 | sk_sp<GrVkPipelineState> pipelineState = this->prepareDrawState(pipeline, |
| 1502 | primProc, |
| 1503 | primitiveType, |
| 1504 | *renderPass); |
| 1505 | if (!pipelineState) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1506 | return; |
| 1507 | } |
| 1508 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1509 | // Change layout of our render target so it can be used as the color attachment |
| 1510 | VkImageLayout layout = vkRT->currentLayout(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1511 | VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout); |
| 1512 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1513 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout); |
| 1514 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 1515 | vkRT->setImageLayout(this, |
| 1516 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
| 1517 | srcAccessMask, |
| 1518 | dstAccessMask, |
| 1519 | srcStageMask, |
| 1520 | dstStageMask, |
| 1521 | false); |
| 1522 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1523 | // If we are using a stencil attachment we also need to update its layout |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1524 | if (GrStencilAttachment* stencil = vkRT->renderTargetPriv().getStencilAttachment()) { |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1525 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1526 | VkImageLayout origDstLayout = vkStencil->currentLayout(); |
| 1527 | VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(origDstLayout); |
| 1528 | VkAccessFlags dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1529 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1530 | VkPipelineStageFlags srcStageMask = |
| 1531 | GrVkMemory::LayoutToPipelineStageFlags(origDstLayout); |
| 1532 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 1533 | vkStencil->setImageLayout(this, |
| 1534 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
| 1535 | srcAccessMask, |
| 1536 | dstAccessMask, |
| 1537 | srcStageMask, |
| 1538 | dstStageMask, |
| 1539 | false); |
| 1540 | } |
| 1541 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1542 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1543 | |
| 1544 | for (int i = 0; i < meshCount; ++i) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1545 | const GrMesh& mesh = meshes[i]; |
| 1546 | GrMesh::Iterator iter; |
| 1547 | const GrNonInstancedMesh* nonIdxMesh = iter.init(mesh); |
| 1548 | do { |
| 1549 | if (nonIdxMesh->primitiveType() != primitiveType) { |
| 1550 | // 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] | 1551 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 1552 | // pipelineState sits in a cache for a while. |
| 1553 | pipelineState->freeTempResources(this); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1554 | SkDEBUGCODE(pipelineState = nullptr); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1555 | primitiveType = nonIdxMesh->primitiveType(); |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1556 | // It is illegal for us to have the necessary memory barriers for when we write and |
| 1557 | // update the uniform buffers in prepareDrawState while in an active render pass. |
| 1558 | // Thus we must end the current one and then start it up again. |
| 1559 | fCurrentCmdBuffer->endRenderPass(this); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1560 | pipelineState = this->prepareDrawState(pipeline, |
| 1561 | primProc, |
| 1562 | primitiveType, |
| 1563 | *renderPass); |
| 1564 | if (!pipelineState) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1565 | return; |
| 1566 | } |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1567 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1568 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1569 | SkASSERT(pipelineState); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1570 | this->bindGeometry(primProc, *nonIdxMesh); |
| 1571 | |
| 1572 | if (nonIdxMesh->isIndexed()) { |
| 1573 | fCurrentCmdBuffer->drawIndexed(this, |
| 1574 | nonIdxMesh->indexCount(), |
| 1575 | 1, |
| 1576 | nonIdxMesh->startIndex(), |
| 1577 | nonIdxMesh->startVertex(), |
| 1578 | 0); |
| 1579 | } else { |
| 1580 | fCurrentCmdBuffer->draw(this, |
| 1581 | nonIdxMesh->vertexCount(), |
| 1582 | 1, |
| 1583 | nonIdxMesh->startVertex(), |
| 1584 | 0); |
| 1585 | } |
| 1586 | |
| 1587 | fStats.incNumDraws(); |
| 1588 | } while ((nonIdxMesh = iter.next())); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | fCurrentCmdBuffer->endRenderPass(this); |
| 1592 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1593 | // Technically we don't have to call this here (since there is a safety check in |
| 1594 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 1595 | // pipelineState sits in a cache for a while. |
| 1596 | pipelineState->freeTempResources(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1597 | |
| 1598 | #if SWAP_PER_DRAW |
| 1599 | glFlush(); |
| 1600 | #if defined(SK_BUILD_FOR_MAC) |
| 1601 | aglSwapBuffers(aglGetCurrentContext()); |
| 1602 | int set_a_break_pt_here = 9; |
| 1603 | aglSwapBuffers(aglGetCurrentContext()); |
| 1604 | #elif defined(SK_BUILD_FOR_WIN32) |
| 1605 | SwapBuf(); |
| 1606 | int set_a_break_pt_here = 9; |
| 1607 | SwapBuf(); |
| 1608 | #endif |
| 1609 | #endif |
| 1610 | } |