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" |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 20 | #include "GrVkGpuCommandBuffer.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 21 | #include "GrVkImage.h" |
| 22 | #include "GrVkIndexBuffer.h" |
| 23 | #include "GrVkMemory.h" |
| 24 | #include "GrVkPipeline.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 25 | #include "GrVkPipelineState.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 26 | #include "GrVkRenderPass.h" |
| 27 | #include "GrVkResourceProvider.h" |
| 28 | #include "GrVkTexture.h" |
| 29 | #include "GrVkTextureRenderTarget.h" |
| 30 | #include "GrVkTransferBuffer.h" |
| 31 | #include "GrVkVertexBuffer.h" |
| 32 | |
| 33 | #include "SkConfig8888.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 34 | #include "SkMipMap.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 35 | |
| 36 | #include "vk/GrVkInterface.h" |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 37 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | #if USE_SKSL |
| 40 | #include "SkSLCompiler.h" |
| 41 | #endif |
| 42 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 43 | #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) |
| 44 | #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) |
| 45 | #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) |
| 46 | |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 47 | #ifdef SK_ENABLE_VK_LAYERS |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 48 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback( |
| 49 | VkDebugReportFlagsEXT flags, |
| 50 | VkDebugReportObjectTypeEXT objectType, |
| 51 | uint64_t object, |
| 52 | size_t location, |
| 53 | int32_t messageCode, |
| 54 | const char* pLayerPrefix, |
| 55 | const char* pMessage, |
| 56 | void* pUserData) { |
| 57 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
| 58 | SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 59 | } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
| 60 | SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 61 | } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { |
| 62 | SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 63 | } else { |
| 64 | SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 65 | } |
| 66 | return VK_FALSE; |
| 67 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 68 | #endif |
| 69 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 70 | GrGpu* GrVkGpu::Create(GrBackendContext backendContext, const GrContextOptions& options, |
| 71 | GrContext* context) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 72 | const GrVkBackendContext* vkBackendContext = |
| 73 | reinterpret_cast<const GrVkBackendContext*>(backendContext); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 74 | if (!vkBackendContext) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 75 | vkBackendContext = GrVkBackendContext::Create(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 76 | if (!vkBackendContext) { |
| 77 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 78 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 79 | } else { |
| 80 | vkBackendContext->ref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Greg Daniel | fe2965a | 2016-10-11 16:31:05 -0400 | [diff] [blame] | 83 | if (!vkBackendContext->fInterface->validate(vkBackendContext->fExtensions)) { |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 87 | return new GrVkGpu(context, options, vkBackendContext); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | //////////////////////////////////////////////////////////////////////////////// |
| 91 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 92 | GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 93 | const GrVkBackendContext* backendCtx) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 94 | : INHERITED(context) |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 95 | , fDevice(backendCtx->fDevice) |
| 96 | , fQueue(backendCtx->fQueue) |
| 97 | , fResourceProvider(this) { |
| 98 | fBackendContext.reset(backendCtx); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 99 | |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 100 | #ifdef SK_ENABLE_VK_LAYERS |
brianosman | 419ca64 | 2016-05-04 08:19:44 -0700 | [diff] [blame] | 101 | fCallback = VK_NULL_HANDLE; |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 102 | if (backendCtx->fExtensions & kEXT_debug_report_GrVkExtensionFlag) { |
| 103 | // Setup callback creation information |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 104 | VkDebugReportCallbackCreateInfoEXT callbackCreateInfo; |
| 105 | callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 106 | callbackCreateInfo.pNext = nullptr; |
| 107 | callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | |
egdaniel | ef0c10c | 2016-04-07 07:51:22 -0700 | [diff] [blame] | 108 | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 109 | //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | |
| 110 | //VK_DEBUG_REPORT_DEBUG_BIT_EXT | |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 111 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 112 | callbackCreateInfo.pfnCallback = &DebugReportCallback; |
| 113 | callbackCreateInfo.pUserData = nullptr; |
| 114 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 115 | // Register the callback |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 116 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateDebugReportCallbackEXT( |
| 117 | backendCtx->fInstance, &callbackCreateInfo, nullptr, &fCallback)); |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 118 | } |
| 119 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 120 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 121 | #if USE_SKSL |
| 122 | fCompiler = new SkSL::Compiler(); |
| 123 | #else |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 124 | fCompiler = shaderc_compiler_initialize(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 125 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 126 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 127 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice, |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 128 | backendCtx->fFeatures, backendCtx->fExtensions)); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 129 | fCaps.reset(SkRef(fVkCaps.get())); |
| 130 | |
| 131 | VK_CALL(GetPhysicalDeviceMemoryProperties(backendCtx->fPhysicalDevice, &fPhysDevMemProps)); |
| 132 | |
| 133 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 134 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 135 | nullptr, // pNext |
| 136 | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | |
| 137 | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, // CmdPoolCreateFlags |
| 138 | backendCtx->fGraphicsQueueIndex, // queueFamilyIndex |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 139 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 140 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 141 | &fCmdPool)); |
| 142 | |
| 143 | // must call this after creating the CommandPool |
| 144 | fResourceProvider.init(); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 145 | fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 146 | SkASSERT(fCurrentCmdBuffer); |
| 147 | fCurrentCmdBuffer->begin(this); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 148 | |
| 149 | // set up our heaps |
| 150 | fHeaps[kLinearImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024)); |
egdaniel | 05dceab | 2016-06-22 07:45:50 -0700 | [diff] [blame] | 151 | // We want the OptimalImage_Heap to use a SubAlloc_strategy but it occasionally causes the |
| 152 | // device to run out of memory. Most likely this is caused by fragmentation in the device heap |
| 153 | // and we can't allocate more. Until we get a fix moving this to SingleAlloc. |
| 154 | fHeaps[kOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 64*1024*1024)); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 155 | fHeaps[kSmallOptimalImage_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 2*1024*1024)); |
| 156 | fHeaps[kVertexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
| 157 | fHeaps[kIndexBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
jvanverth | 4c6e47a | 2016-07-22 10:34:52 -0700 | [diff] [blame] | 158 | fHeaps[kUniformBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 256*1024)); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 159 | fHeaps[kCopyReadBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSingleAlloc_Strategy, 0)); |
| 160 | fHeaps[kCopyWriteBuffer_Heap].reset(new GrVkHeap(this, GrVkHeap::kSubAlloc_Strategy, 16*1024*1024)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | GrVkGpu::~GrVkGpu() { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 164 | fCurrentCmdBuffer->end(this); |
| 165 | fCurrentCmdBuffer->unref(this); |
| 166 | |
| 167 | // wait for all commands to finish |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 168 | fResourceProvider.checkCommandBuffers(); |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 169 | SkDEBUGCODE(VkResult res = ) VK_CALL(QueueWaitIdle(fQueue)); |
egdaniel | f8c2be3 | 2016-06-24 13:18:27 -0700 | [diff] [blame] | 170 | |
| 171 | // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences |
| 172 | // on the command buffers even though they have completed. This causes an assert to fire when |
| 173 | // destroying the command buffers. Currently this ony seems to happen on windows, so we add a |
| 174 | // sleep to make sure the fence singals. |
| 175 | #ifdef SK_DEBUG |
| 176 | #if defined(SK_BUILD_FOR_WIN) |
| 177 | Sleep(10); // In milliseconds |
| 178 | #else |
| 179 | // Uncomment if above bug happens on non windows build. |
| 180 | // sleep(1); // In seconds |
| 181 | #endif |
| 182 | #endif |
| 183 | |
egdaniel | be9d821 | 2016-09-20 08:54:23 -0700 | [diff] [blame] | 184 | #ifdef SK_DEBUG |
| 185 | if (this->vkCaps().allowInitializationErrorOnTearDown()) { |
| 186 | SkASSERT(VK_SUCCESS == res || |
| 187 | VK_ERROR_DEVICE_LOST == res || |
| 188 | VK_ERROR_INITIALIZATION_FAILED == res); |
| 189 | } else { |
| 190 | SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res); |
| 191 | } |
| 192 | #endif |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 193 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 194 | fCopyManager.destroyResources(this); |
| 195 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 196 | // must call this just before we destroy the VkDevice |
| 197 | fResourceProvider.destroyResources(); |
| 198 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 199 | VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); |
| 200 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 201 | #if USE_SKSL |
| 202 | delete fCompiler; |
| 203 | #else |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 204 | shaderc_compiler_release(fCompiler); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 205 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 206 | |
egdaniel | 735109c | 2016-07-27 08:03:57 -0700 | [diff] [blame] | 207 | #ifdef SK_ENABLE_VK_LAYERS |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 208 | if (fCallback) { |
| 209 | VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr)); |
brianosman | 419ca64 | 2016-05-04 08:19:44 -0700 | [diff] [blame] | 210 | fCallback = VK_NULL_HANDLE; |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 211 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 212 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /////////////////////////////////////////////////////////////////////////////// |
| 216 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 217 | GrGpuCommandBuffer* GrVkGpu::createCommandBuffer( |
| 218 | GrRenderTarget* target, |
| 219 | const GrGpuCommandBuffer::LoadAndStoreInfo& colorInfo, |
| 220 | const GrGpuCommandBuffer::LoadAndStoreInfo& stencilInfo) { |
| 221 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
| 222 | return new GrVkGpuCommandBuffer(this, vkRT, colorInfo, stencilInfo); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 225 | void GrVkGpu::submitCommandBuffer(SyncQueue sync) { |
| 226 | SkASSERT(fCurrentCmdBuffer); |
| 227 | fCurrentCmdBuffer->end(this); |
| 228 | |
| 229 | fCurrentCmdBuffer->submitToQueue(this, fQueue, sync); |
| 230 | fResourceProvider.checkCommandBuffers(); |
| 231 | |
| 232 | // Release old command buffer and create a new one |
| 233 | fCurrentCmdBuffer->unref(this); |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 234 | fCurrentCmdBuffer = fResourceProvider.findOrCreatePrimaryCommandBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 235 | SkASSERT(fCurrentCmdBuffer); |
| 236 | |
| 237 | fCurrentCmdBuffer->begin(this); |
| 238 | } |
| 239 | |
| 240 | /////////////////////////////////////////////////////////////////////////////// |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 241 | GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern, |
| 242 | const void* data) { |
| 243 | GrBuffer* buff; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 244 | switch (type) { |
| 245 | case kVertex_GrBufferType: |
| 246 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 247 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 248 | buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 249 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 250 | case kIndex_GrBufferType: |
| 251 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 252 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 253 | buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 254 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 255 | case kXferCpuToGpu_GrBufferType: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 256 | SkASSERT(kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 257 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 258 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 259 | case kXferGpuToCpu_GrBufferType: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 260 | SkASSERT(kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 261 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 262 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 263 | default: |
| 264 | SkFAIL("Unknown buffer type."); |
| 265 | return nullptr; |
| 266 | } |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 267 | if (data && buff) { |
| 268 | buff->updateData(data, size); |
| 269 | } |
| 270 | return buff; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | //////////////////////////////////////////////////////////////////////////////// |
| 274 | bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
| 275 | GrPixelConfig srcConfig, DrawPreference* drawPreference, |
| 276 | WritePixelTempDrawInfo* tempDrawInfo) { |
| 277 | if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) { |
| 278 | return false; |
| 279 | } |
| 280 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 281 | GrRenderTarget* renderTarget = dstSurface->asRenderTarget(); |
| 282 | |
| 283 | // Start off assuming no swizzling |
| 284 | tempDrawInfo->fSwizzle = GrSwizzle::RGBA(); |
| 285 | tempDrawInfo->fWriteConfig = srcConfig; |
| 286 | |
| 287 | // These settings we will always want if a temp draw is performed. Initially set the config |
| 288 | // to srcConfig, though that may be modified if we decide to do a R/B swap |
| 289 | tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags; |
| 290 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcConfig; |
| 291 | tempDrawInfo->fTempSurfaceDesc.fWidth = width; |
| 292 | tempDrawInfo->fTempSurfaceDesc.fHeight = height; |
| 293 | tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0; |
| 294 | tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 295 | |
egdaniel | d66110f | 2016-06-28 13:38:26 -0700 | [diff] [blame] | 296 | if (dstSurface->config() == srcConfig) { |
| 297 | return true; |
| 298 | } |
| 299 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 300 | if (renderTarget && this->vkCaps().isConfigRenderable(renderTarget->config(), |
| 301 | renderTarget->numColorSamples() > 1)) { |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 302 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 303 | |
| 304 | bool configsAreRBSwaps = GrPixelConfigSwapRAndB(srcConfig) == dstSurface->config(); |
| 305 | |
| 306 | if (!this->vkCaps().isConfigTexturable(srcConfig) && configsAreRBSwaps) { |
| 307 | if (!this->vkCaps().isConfigTexturable(dstSurface->config())) { |
| 308 | return false; |
| 309 | } |
| 310 | tempDrawInfo->fTempSurfaceDesc.fConfig = dstSurface->config(); |
| 311 | tempDrawInfo->fSwizzle = GrSwizzle::BGRA(); |
| 312 | tempDrawInfo->fWriteConfig = dstSurface->config(); |
| 313 | } |
| 314 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 315 | } |
| 316 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 317 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | bool GrVkGpu::onWritePixels(GrSurface* surface, |
| 321 | int left, int top, int width, int height, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 322 | GrPixelConfig config, |
| 323 | const SkTArray<GrMipLevel>& texels) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 324 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture()); |
| 325 | if (!vkTex) { |
| 326 | return false; |
| 327 | } |
| 328 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 329 | // Make sure we have at least the base level |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 330 | if (texels.empty() || !texels.begin()->fPixels) { |
| 331 | return false; |
| 332 | } |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 333 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 334 | // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels. |
| 335 | if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | bool success = false; |
| 340 | if (GrPixelConfigIsCompressed(vkTex->desc().fConfig)) { |
| 341 | // We check that config == desc.fConfig in GrGpu::getWritePixelsInfo() |
| 342 | SkASSERT(config == vkTex->desc().fConfig); |
| 343 | // TODO: add compressed texture support |
| 344 | // delete the following two lines and uncomment the two after that when ready |
| 345 | vkTex->unref(); |
| 346 | return false; |
| 347 | //success = this->uploadCompressedTexData(vkTex->desc(), buffer, false, left, top, width, |
| 348 | // height); |
| 349 | } else { |
| 350 | bool linearTiling = vkTex->isLinearTiled(); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 351 | if (linearTiling) { |
| 352 | if (texels.count() > 1) { |
| 353 | SkDebugf("Can't upload mipmap data to linear tiled texture"); |
| 354 | return false; |
| 355 | } |
| 356 | if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) { |
| 357 | // Need to change the layout to general in order to perform a host write |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 358 | vkTex->setImageLayout(this, |
| 359 | VK_IMAGE_LAYOUT_GENERAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 360 | VK_ACCESS_HOST_WRITE_BIT, |
| 361 | VK_PIPELINE_STAGE_HOST_BIT, |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 362 | false); |
egdaniel | bdf8811 | 2016-05-03 07:25:56 -0700 | [diff] [blame] | 363 | this->submitCommandBuffer(kForce_SyncQueue); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 364 | } |
| 365 | success = this->uploadTexDataLinear(vkTex, left, top, width, height, config, |
| 366 | texels.begin()->fPixels, texels.begin()->fRowBytes); |
| 367 | } else { |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 368 | int newMipLevels = texels.count(); |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 369 | int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1; |
| 370 | if (newMipLevels != currentMipLevels) { |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 371 | if (!vkTex->reallocForMipmap(this, newMipLevels)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | } |
| 375 | success = this->uploadTexDataOptimal(vkTex, left, top, width, height, config, texels); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 376 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 377 | } |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 378 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 379 | return success; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 380 | } |
| 381 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 382 | void GrVkGpu::resolveImage(GrVkRenderTarget* dst, GrVkRenderTarget* src, const SkIRect& srcRect, |
| 383 | const SkIPoint& dstPoint) { |
| 384 | SkASSERT(dst); |
| 385 | SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage()); |
| 386 | |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 387 | if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) { |
| 388 | this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue); |
| 389 | } |
| 390 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 391 | // Flip rect if necessary |
| 392 | SkIRect srcVkRect = srcRect; |
| 393 | int32_t dstY = dstPoint.fY; |
| 394 | |
| 395 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 396 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin()); |
| 397 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 398 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 399 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 400 | } |
| 401 | |
| 402 | VkImageResolve resolveInfo; |
| 403 | resolveInfo.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 404 | resolveInfo.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 405 | resolveInfo.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 406 | resolveInfo.dstOffset = { dstPoint.fX, dstY, 0 }; |
| 407 | // By the spec the depth of the extent should be ignored for 2D images, but certain devices |
| 408 | // (e.g. nexus 5x) currently fail if it is not 1 |
| 409 | resolveInfo.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 }; |
| 410 | |
| 411 | dst->setImageLayout(this, |
| 412 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 413 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 414 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 415 | false); |
| 416 | |
| 417 | src->msaaImage()->setImageLayout(this, |
| 418 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 419 | VK_ACCESS_TRANSFER_READ_BIT, |
| 420 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 421 | false); |
| 422 | |
| 423 | fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dst, 1, &resolveInfo); |
| 424 | } |
| 425 | |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 426 | void GrVkGpu::onResolveRenderTarget(GrRenderTarget* target) { |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 427 | if (target->needsResolve()) { |
| 428 | SkASSERT(target->numColorSamples() > 1); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 429 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target); |
| 430 | SkASSERT(rt->msaaImage()); |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 431 | |
| 432 | const SkIRect& srcRect = rt->getResolveRect(); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 433 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 434 | this->resolveImage(rt, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop)); |
egdaniel | 52ad251 | 2016-08-04 12:50:01 -0700 | [diff] [blame] | 435 | |
| 436 | rt->flagAsResolved(); |
| 437 | } |
| 438 | } |
| 439 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 440 | bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, |
| 441 | int left, int top, int width, int height, |
| 442 | GrPixelConfig dataConfig, |
| 443 | const void* data, |
| 444 | size_t rowBytes) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 445 | SkASSERT(data); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 446 | SkASSERT(tex->isLinearTiled()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 447 | |
| 448 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 449 | SkASSERT(!GrPixelConfigIsCompressed(dataConfig)); |
| 450 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 451 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 452 | |
| 453 | const GrSurfaceDesc& desc = tex->desc(); |
| 454 | |
| 455 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 456 | &width, &height, &data, &rowBytes)) { |
| 457 | return false; |
| 458 | } |
| 459 | size_t trimRowBytes = width * bpp; |
| 460 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 461 | SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() || |
| 462 | VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout()); |
| 463 | const VkImageSubresource subres = { |
| 464 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 465 | 0, // mipLevel |
| 466 | 0, // arraySlice |
| 467 | }; |
| 468 | VkSubresourceLayout layout; |
| 469 | VkResult err; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 470 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 471 | const GrVkInterface* interface = this->vkInterface(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 472 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 473 | GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 474 | tex->image(), |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 475 | &subres, |
| 476 | &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 477 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 478 | int texTop = kBottomLeft_GrSurfaceOrigin == desc.fOrigin ? tex->height() - top - height : top; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 479 | const GrVkAlloc& alloc = tex->alloc(); |
| 480 | VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 481 | VkDeviceSize size = height*layout.rowPitch; |
| 482 | void* mapPtr; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 483 | err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 484 | if (err) { |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) { |
| 489 | // copy into buffer by rows |
| 490 | const char* srcRow = reinterpret_cast<const char*>(data); |
| 491 | char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch; |
| 492 | for (int y = 0; y < height; y++) { |
| 493 | memcpy(dstRow, srcRow, trimRowBytes); |
| 494 | srcRow += rowBytes; |
| 495 | dstRow -= layout.rowPitch; |
| 496 | } |
| 497 | } else { |
| 498 | // If there is no padding on the src (rowBytes) or dst (layout.rowPitch) we can memcpy |
| 499 | if (trimRowBytes == rowBytes && trimRowBytes == layout.rowPitch) { |
| 500 | memcpy(mapPtr, data, trimRowBytes * height); |
| 501 | } else { |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 502 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes, |
| 503 | height); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 507 | GrVkMemory::FlushMappedAlloc(this, alloc); |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 508 | GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 509 | |
| 510 | return true; |
| 511 | } |
| 512 | |
| 513 | bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 514 | int left, int top, int width, int height, |
| 515 | GrPixelConfig dataConfig, |
| 516 | const SkTArray<GrMipLevel>& texels) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 517 | SkASSERT(!tex->isLinearTiled()); |
| 518 | // The assumption is either that we have no mipmaps, or that our rect is the entire texture |
| 519 | SkASSERT(1 == texels.count() || |
| 520 | (0 == left && 0 == top && width == tex->width() && height == tex->height())); |
| 521 | |
| 522 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 523 | SkASSERT(!GrPixelConfigIsCompressed(dataConfig)); |
| 524 | |
| 525 | if (width == 0 || height == 0) { |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | const GrSurfaceDesc& desc = tex->desc(); |
| 530 | SkASSERT(this->caps()->isConfigTexturable(desc.fConfig)); |
| 531 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 532 | |
| 533 | // texels is const. |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 534 | // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes. |
| 535 | // Because of this we need to make a non-const shallow copy of texels. |
| 536 | SkTArray<GrMipLevel> texelsShallowCopy(texels); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 537 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 538 | for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0; |
| 539 | currentMipLevel--) { |
| 540 | SkASSERT(texelsShallowCopy[currentMipLevel].fPixels); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 541 | } |
| 542 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 543 | // Determine whether we need to flip when we copy into the buffer |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 544 | bool flipY = (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 545 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 546 | // adjust any params (left, top, currentWidth, currentHeight |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 547 | // find the combined size of all the mip levels and the relative offset of |
| 548 | // each into the collective buffer |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 549 | // Do the first level separately because we may need to adjust width and height |
| 550 | // (for the non-mipped case). |
| 551 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 552 | &width, |
| 553 | &height, |
| 554 | &texelsShallowCopy[0].fPixels, |
| 555 | &texelsShallowCopy[0].fRowBytes)) { |
| 556 | return false; |
| 557 | } |
| 558 | SkTArray<size_t> individualMipOffsets(texelsShallowCopy.count()); |
| 559 | individualMipOffsets.push_back(0); |
| 560 | size_t combinedBufferSize = width * bpp * height; |
| 561 | int currentWidth = width; |
| 562 | int currentHeight = height; |
| 563 | for (int currentMipLevel = 1; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) { |
| 564 | currentWidth = SkTMax(1, currentWidth/2); |
| 565 | currentHeight = SkTMax(1, currentHeight/2); |
| 566 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 567 | ¤tWidth, |
| 568 | ¤tHeight, |
| 569 | &texelsShallowCopy[currentMipLevel].fPixels, |
| 570 | &texelsShallowCopy[currentMipLevel].fRowBytes)) { |
| 571 | return false; |
| 572 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 573 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 574 | individualMipOffsets.push_back(combinedBufferSize); |
| 575 | combinedBufferSize += trimmedSize; |
| 576 | } |
| 577 | |
| 578 | // allocate buffer to hold our mip data |
| 579 | GrVkTransferBuffer* transferBuffer = |
| 580 | GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type); |
| 581 | |
| 582 | char* buffer = (char*) transferBuffer->map(); |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 583 | SkTArray<VkBufferImageCopy> regions(texelsShallowCopy.count()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 584 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 585 | currentWidth = width; |
| 586 | currentHeight = height; |
| 587 | for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 588 | const size_t trimRowBytes = currentWidth * bpp; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 589 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 590 | |
| 591 | // copy data into the buffer, skipping the trailing bytes |
| 592 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 593 | const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 594 | if (flipY) { |
| 595 | src += (currentHeight - 1) * rowBytes; |
| 596 | for (int y = 0; y < currentHeight; y++) { |
| 597 | memcpy(dst, src, trimRowBytes); |
| 598 | src -= rowBytes; |
| 599 | dst += trimRowBytes; |
| 600 | } |
| 601 | } else if (trimRowBytes == rowBytes) { |
| 602 | memcpy(dst, src, trimRowBytes * currentHeight); |
| 603 | } else { |
| 604 | SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight); |
| 605 | } |
| 606 | |
| 607 | VkBufferImageCopy& region = regions.push_back(); |
| 608 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 609 | region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel]; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 610 | region.bufferRowLength = currentWidth; |
| 611 | region.bufferImageHeight = currentHeight; |
bsalomon | cf942c4 | 2016-04-29 18:30:06 -0700 | [diff] [blame] | 612 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 }; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 613 | region.imageOffset = { left, flipY ? tex->height() - top - currentHeight : top, 0 }; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 614 | region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 }; |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 615 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 616 | currentWidth = SkTMax(1, currentWidth/2); |
| 617 | currentHeight = SkTMax(1, currentHeight/2); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 618 | } |
| 619 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 620 | // no need to flush non-coherent memory, unmap will do that for us |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 621 | transferBuffer->unmap(); |
| 622 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 623 | // Change layout of our target so it can be copied to |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 624 | tex->setImageLayout(this, |
| 625 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 626 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 627 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 628 | false); |
| 629 | |
| 630 | // Copy the buffer to the image |
| 631 | fCurrentCmdBuffer->copyBufferToImage(this, |
| 632 | transferBuffer, |
| 633 | tex, |
| 634 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 635 | regions.count(), |
| 636 | regions.begin()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 637 | transferBuffer->unref(); |
| 638 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 639 | return true; |
| 640 | } |
| 641 | |
| 642 | //////////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 643 | GrTexture* GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 644 | const SkTArray<GrMipLevel>& texels) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 645 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 646 | |
| 647 | VkFormat pixelFormat; |
| 648 | if (!GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)) { |
| 649 | return nullptr; |
| 650 | } |
| 651 | |
| 652 | if (!fVkCaps->isConfigTexturable(desc.fConfig)) { |
| 653 | return nullptr; |
| 654 | } |
| 655 | |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 656 | if (renderTarget && !fVkCaps->isConfigRenderable(desc.fConfig, false)) { |
| 657 | return nullptr; |
| 658 | } |
| 659 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 660 | bool linearTiling = false; |
| 661 | if (SkToBool(desc.fFlags & kZeroCopy_GrSurfaceFlag)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 662 | // we can't have a linear texture with a mipmap |
| 663 | if (texels.count() > 1) { |
| 664 | SkDebugf("Trying to create linear tiled texture with mipmap"); |
| 665 | return nullptr; |
| 666 | } |
egdaniel | a95d46b | 2016-08-15 08:06:29 -0700 | [diff] [blame] | 667 | if (fVkCaps->isConfigTexturableLinearly(desc.fConfig) && |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 668 | (!renderTarget || fVkCaps->isConfigRenderableLinearly(desc.fConfig, false))) { |
| 669 | linearTiling = true; |
| 670 | } else { |
| 671 | return nullptr; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 676 | if (renderTarget) { |
| 677 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 678 | } |
| 679 | |
| 680 | // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and |
| 681 | // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we |
| 682 | // will be using this texture in some copy or not. Also this assumes, as is the current case, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 683 | // that all render targets in vulkan are also textures. If we change this practice of setting |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 684 | // both bits, we must make sure to set the destination bit if we are uploading srcData to the |
| 685 | // texture. |
| 686 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 687 | |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 688 | VkFlags memProps = (!texels.empty() && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT : |
| 689 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 690 | |
| 691 | // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 692 | // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 693 | // to 1. |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 694 | int mipLevels = texels.empty() ? 1 : texels.count(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 695 | GrVkImage::ImageDesc imageDesc; |
| 696 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 697 | imageDesc.fFormat = pixelFormat; |
| 698 | imageDesc.fWidth = desc.fWidth; |
| 699 | imageDesc.fHeight = desc.fHeight; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 700 | imageDesc.fLevels = linearTiling ? 1 : mipLevels; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 701 | imageDesc.fSamples = 1; |
| 702 | imageDesc.fImageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 703 | imageDesc.fUsageFlags = usageFlags; |
| 704 | imageDesc.fMemProps = memProps; |
| 705 | |
| 706 | GrVkTexture* tex; |
| 707 | if (renderTarget) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 708 | tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 709 | imageDesc); |
| 710 | } else { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 711 | tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | if (!tex) { |
| 715 | return nullptr; |
| 716 | } |
| 717 | |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 718 | if (!texels.empty()) { |
| 719 | SkASSERT(texels.begin()->fPixels); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 720 | bool success; |
| 721 | if (linearTiling) { |
| 722 | success = this->uploadTexDataLinear(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 723 | texels.begin()->fPixels, texels.begin()->fRowBytes); |
| 724 | } else { |
| 725 | success = this->uploadTexDataOptimal(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 726 | texels); |
| 727 | } |
| 728 | if (!success) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 729 | tex->unref(); |
| 730 | return nullptr; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return tex; |
| 735 | } |
| 736 | |
| 737 | //////////////////////////////////////////////////////////////////////////////// |
| 738 | |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 739 | bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src, |
| 740 | VkDeviceSize offset, VkDeviceSize size) { |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 741 | |
| 742 | // Update the buffer |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 743 | fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src); |
jvanverth | a584de9 | 2016-06-30 09:10:52 -0700 | [diff] [blame] | 744 | |
| 745 | return true; |
| 746 | } |
| 747 | |
| 748 | //////////////////////////////////////////////////////////////////////////////// |
| 749 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 750 | static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin) { |
| 751 | // By default, all textures in Vk use TopLeft |
| 752 | if (kDefault_GrSurfaceOrigin == origin) { |
| 753 | return kTopLeft_GrSurfaceOrigin; |
| 754 | } else { |
| 755 | return origin; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | GrTexture* GrVkGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc, |
| 760 | GrWrapOwnership ownership) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 761 | if (0 == desc.fTextureHandle) { |
| 762 | return nullptr; |
| 763 | } |
| 764 | |
| 765 | int maxSize = this->caps()->maxTextureSize(); |
| 766 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 767 | return nullptr; |
| 768 | } |
| 769 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 770 | const GrVkImageInfo* info = reinterpret_cast<const GrVkImageInfo*>(desc.fTextureHandle); |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 771 | if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 772 | return nullptr; |
| 773 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 774 | #ifdef SK_DEBUG |
| 775 | VkFormat format; |
| 776 | if (!GrPixelConfigToVkFormat(desc.fConfig, &format)) { |
| 777 | return nullptr; |
| 778 | } |
| 779 | SkASSERT(format == info->fFormat); |
| 780 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 781 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 782 | GrSurfaceDesc surfDesc; |
| 783 | // next line relies on GrBackendTextureDesc's flags matching GrTexture's |
| 784 | surfDesc.fFlags = (GrSurfaceFlags)desc.fFlags; |
| 785 | surfDesc.fWidth = desc.fWidth; |
| 786 | surfDesc.fHeight = desc.fHeight; |
| 787 | surfDesc.fConfig = desc.fConfig; |
| 788 | surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); |
| 789 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 790 | // In GL, Chrome assumes all textures are BottomLeft |
| 791 | // In VK, we don't have this restriction |
| 792 | surfDesc.fOrigin = resolve_origin(desc.fOrigin); |
| 793 | |
| 794 | GrVkTexture* texture = nullptr; |
| 795 | if (renderTarget) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 796 | texture = GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(this, surfDesc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 797 | ownership, info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 798 | } else { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 799 | texture = GrVkTexture::CreateWrappedTexture(this, surfDesc, ownership, info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 800 | } |
| 801 | if (!texture) { |
| 802 | return nullptr; |
| 803 | } |
| 804 | |
| 805 | return texture; |
| 806 | } |
| 807 | |
| 808 | GrRenderTarget* GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc, |
| 809 | GrWrapOwnership ownership) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 810 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 811 | const GrVkImageInfo* info = |
| 812 | reinterpret_cast<const GrVkImageInfo*>(wrapDesc.fRenderTargetHandle); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 813 | if (VK_NULL_HANDLE == info->fImage || |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 814 | (VK_NULL_HANDLE == info->fAlloc.fMemory && kAdopt_GrWrapOwnership == ownership)) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 815 | return nullptr; |
| 816 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 817 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 818 | GrSurfaceDesc desc; |
| 819 | desc.fConfig = wrapDesc.fConfig; |
| 820 | desc.fFlags = kCheckAllocation_GrSurfaceFlag; |
| 821 | desc.fWidth = wrapDesc.fWidth; |
| 822 | desc.fHeight = wrapDesc.fHeight; |
| 823 | desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount()); |
| 824 | |
| 825 | desc.fOrigin = resolve_origin(wrapDesc.fOrigin); |
| 826 | |
| 827 | GrVkRenderTarget* tgt = GrVkRenderTarget::CreateWrappedRenderTarget(this, desc, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 828 | ownership, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 829 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 830 | if (tgt && wrapDesc.fStencilBits) { |
| 831 | if (!createStencilAttachmentForRenderTarget(tgt, desc.fWidth, desc.fHeight)) { |
| 832 | tgt->unref(); |
| 833 | return nullptr; |
| 834 | } |
| 835 | } |
| 836 | return tgt; |
| 837 | } |
| 838 | |
egdaniel | 50ead53 | 2016-07-13 14:23:26 -0700 | [diff] [blame] | 839 | void GrVkGpu::generateMipmap(GrVkTexture* tex) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 840 | // don't do anything for linearly tiled textures (can't have mipmaps) |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 841 | if (tex->isLinearTiled()) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 842 | SkDebugf("Trying to create mipmap for linear tiled texture"); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 843 | return; |
| 844 | } |
| 845 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 846 | // determine if we can blit to and from this format |
| 847 | const GrVkCaps& caps = this->vkCaps(); |
| 848 | if (!caps.configCanBeDstofBlit(tex->config(), false) || |
egdaniel | 2f5792a | 2016-07-06 08:51:23 -0700 | [diff] [blame] | 849 | !caps.configCanBeSrcofBlit(tex->config(), false) || |
| 850 | !caps.mipMapSupport()) { |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 851 | return; |
| 852 | } |
| 853 | |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 854 | if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) { |
| 855 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 856 | } |
| 857 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 858 | // We may need to resolve the texture first if it is also a render target |
| 859 | GrVkRenderTarget* texRT = static_cast<GrVkRenderTarget*>(tex->asRenderTarget()); |
| 860 | if (texRT) { |
| 861 | this->onResolveRenderTarget(texRT); |
| 862 | } |
| 863 | |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 864 | int width = tex->width(); |
| 865 | int height = tex->height(); |
| 866 | VkImageBlit blitRegion; |
| 867 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 868 | |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 869 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 870 | uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1; |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 871 | if (levelCount != tex->mipLevels()) { |
| 872 | const GrVkResource* oldResource = tex->resource(); |
| 873 | oldResource->ref(); |
| 874 | // grab handle to the original image resource |
| 875 | VkImage oldImage = tex->image(); |
| 876 | |
| 877 | // change the original image's layout so we can copy from it |
| 878 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 879 | VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
| 880 | |
| 881 | if (!tex->reallocForMipmap(this, levelCount)) { |
| 882 | oldResource->unref(this); |
| 883 | return; |
| 884 | } |
| 885 | // change the new image's layout so we can blit to it |
| 886 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL, |
| 887 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
| 888 | |
| 889 | // Blit original image to top level of new image |
| 890 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 891 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
| 892 | blitRegion.srcOffsets[1] = { width, height, 1 }; |
| 893 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 894 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
| 895 | blitRegion.dstOffsets[1] = { width, height, 1 }; |
| 896 | |
| 897 | fCurrentCmdBuffer->blitImage(this, |
| 898 | oldResource, |
| 899 | oldImage, |
| 900 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 901 | tex->resource(), |
| 902 | tex->image(), |
| 903 | VK_IMAGE_LAYOUT_GENERAL, |
| 904 | 1, |
| 905 | &blitRegion, |
| 906 | VK_FILTER_LINEAR); |
| 907 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 908 | oldResource->unref(this); |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 909 | } else { |
| 910 | // change layout of the layers so we can write to them. |
| 911 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL, |
| 912 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 913 | } |
| 914 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 915 | // setup memory barrier |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 916 | SkASSERT(GrVkFormatToPixelConfig(tex->imageFormat(), nullptr)); |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 917 | VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT; |
| 918 | VkImageMemoryBarrier imageMemoryBarrier = { |
| 919 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 920 | NULL, // pNext |
egdaniel | 7ac5da8 | 2016-07-15 13:41:42 -0700 | [diff] [blame] | 921 | VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask |
| 922 | VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 923 | VK_IMAGE_LAYOUT_GENERAL, // oldLayout |
| 924 | VK_IMAGE_LAYOUT_GENERAL, // newLayout |
| 925 | VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex |
| 926 | VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 927 | tex->image(), // image |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 928 | { aspectFlags, 0, 1, 0, 1 } // subresourceRange |
| 929 | }; |
| 930 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 931 | // Blit the miplevels |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 932 | uint32_t mipLevel = 1; |
| 933 | while (mipLevel < levelCount) { |
| 934 | int prevWidth = width; |
| 935 | int prevHeight = height; |
| 936 | width = SkTMax(1, width / 2); |
| 937 | height = SkTMax(1, height / 2); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 938 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 939 | imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1; |
| 940 | this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 941 | false, &imageMemoryBarrier); |
| 942 | |
| 943 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 944 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
brianosman | e9906e7 | 2016-06-08 12:44:27 -0700 | [diff] [blame] | 945 | blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 946 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 }; |
| 947 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
brianosman | e9906e7 | 2016-06-08 12:44:27 -0700 | [diff] [blame] | 948 | blitRegion.dstOffsets[1] = { width, height, 1 }; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 949 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 950 | *tex, |
| 951 | *tex, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 952 | 1, |
| 953 | &blitRegion, |
| 954 | VK_FILTER_LINEAR); |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 955 | ++mipLevel; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 956 | } |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 959 | //////////////////////////////////////////////////////////////////////////////// |
| 960 | |
| 961 | GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 962 | int width, |
| 963 | int height) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 964 | SkASSERT(width >= rt->width()); |
| 965 | SkASSERT(height >= rt->height()); |
| 966 | |
| 967 | int samples = rt->numStencilSamples(); |
| 968 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 969 | const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 970 | |
| 971 | GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 972 | width, |
| 973 | height, |
| 974 | samples, |
| 975 | sFmt)); |
| 976 | fStats.incStencilAttachmentCreates(); |
| 977 | return stencil; |
| 978 | } |
| 979 | |
| 980 | //////////////////////////////////////////////////////////////////////////////// |
| 981 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 982 | bool copy_testing_data(GrVkGpu* gpu, void* srcData, const GrVkAlloc& alloc, |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 983 | size_t srcRowBytes, size_t dstRowBytes, int h) { |
| 984 | void* mapPtr; |
| 985 | VkResult err = GR_VK_CALL(gpu->vkInterface(), MapMemory(gpu->device(), |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 986 | alloc.fMemory, |
| 987 | alloc.fOffset, |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 988 | dstRowBytes * h, |
| 989 | 0, |
| 990 | &mapPtr)); |
| 991 | if (err) { |
| 992 | return false; |
| 993 | } |
| 994 | |
| 995 | // If there is no padding on dst we can do a single memcopy. |
| 996 | // This assumes the srcData comes in with no padding. |
| 997 | if (srcRowBytes == dstRowBytes) { |
| 998 | memcpy(mapPtr, srcData, srcRowBytes * h); |
| 999 | } else { |
| 1000 | SkRectMemcpy(mapPtr, static_cast<size_t>(dstRowBytes), srcData, srcRowBytes, |
| 1001 | srcRowBytes, h); |
| 1002 | } |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1003 | GrVkMemory::FlushMappedAlloc(gpu, alloc); |
| 1004 | GR_VK_CALL(gpu->vkInterface(), UnmapMemory(gpu->device(), alloc.fMemory)); |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1005 | return true; |
| 1006 | } |
| 1007 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1008 | GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h, |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 1009 | GrPixelConfig config, |
| 1010 | bool isRenderTarget) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1011 | |
| 1012 | VkFormat pixelFormat; |
| 1013 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | bool linearTiling = false; |
| 1018 | if (!fVkCaps->isConfigTexturable(config)) { |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 1022 | if (isRenderTarget && !fVkCaps->isConfigRenderable(config, false)) { |
| 1023 | return 0; |
| 1024 | } |
| 1025 | |
egdaniel | a95d46b | 2016-08-15 08:06:29 -0700 | [diff] [blame] | 1026 | if (fVkCaps->isConfigTexturableLinearly(config) && |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 1027 | (!isRenderTarget || fVkCaps->isConfigRenderableLinearly(config, false))) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1028 | linearTiling = true; |
| 1029 | } |
| 1030 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1031 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 1032 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 1033 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
egdaniel | 0a3a7f7 | 2016-06-24 09:22:31 -0700 | [diff] [blame] | 1034 | if (isRenderTarget) { |
| 1035 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 1036 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1037 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1038 | VkImage image = VK_NULL_HANDLE; |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1039 | GrVkAlloc alloc = { VK_NULL_HANDLE, 0, 0, 0 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1040 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1041 | VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 1042 | VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling) |
| 1043 | ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 1044 | : VK_IMAGE_LAYOUT_UNDEFINED; |
| 1045 | |
| 1046 | // Create Image |
| 1047 | VkSampleCountFlagBits vkSamples; |
| 1048 | if (!GrSampleCountToVkSampleCount(1, &vkSamples)) { |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | const VkImageCreateInfo imageCreateInfo = { |
| 1053 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 1054 | NULL, // pNext |
| 1055 | 0, // VkImageCreateFlags |
| 1056 | VK_IMAGE_TYPE_2D, // VkImageType |
| 1057 | pixelFormat, // VkFormat |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 1058 | { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1059 | 1, // mipLevels |
| 1060 | 1, // arrayLayers |
| 1061 | vkSamples, // samples |
| 1062 | imageTiling, // VkImageTiling |
| 1063 | usageFlags, // VkImageUsageFlags |
| 1064 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 1065 | 0, // queueFamilyCount |
| 1066 | 0, // pQueueFamilyIndices |
| 1067 | initialLayout // initialLayout |
| 1068 | }; |
| 1069 | |
| 1070 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image)); |
| 1071 | |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1072 | if (!GrVkMemory::AllocAndBindImageMemory(this, image, linearTiling, &alloc)) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1073 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | if (srcData) { |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1078 | size_t bpp = GrBytesPerPixel(config); |
| 1079 | size_t rowCopyBytes = bpp * w; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1080 | if (linearTiling) { |
| 1081 | const VkImageSubresource subres = { |
| 1082 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 1083 | 0, // mipLevel |
| 1084 | 0, // arraySlice |
| 1085 | }; |
| 1086 | VkSubresourceLayout layout; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1087 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1088 | VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1089 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1090 | if (!copy_testing_data(this, srcData, alloc, rowCopyBytes, |
brianosman | 583bc2e | 2016-08-15 14:06:31 -0700 | [diff] [blame] | 1091 | static_cast<size_t>(layout.rowPitch), h)) { |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1092 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
| 1093 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | } else { |
| 1097 | SkASSERT(w && h); |
| 1098 | |
| 1099 | VkBuffer buffer; |
| 1100 | VkBufferCreateInfo bufInfo; |
| 1101 | memset(&bufInfo, 0, sizeof(VkBufferCreateInfo)); |
| 1102 | bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; |
| 1103 | bufInfo.flags = 0; |
| 1104 | bufInfo.size = rowCopyBytes * h; |
| 1105 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; |
| 1106 | bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 1107 | bufInfo.queueFamilyIndexCount = 0; |
| 1108 | bufInfo.pQueueFamilyIndices = nullptr; |
| 1109 | VkResult err; |
| 1110 | err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer)); |
| 1111 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1112 | if (err) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1113 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1114 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1118 | GrVkAlloc bufferAlloc = { VK_NULL_HANDLE, 0, 0, 0 }; |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1119 | if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, |
| 1120 | true, &bufferAlloc)) { |
| 1121 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
| 1122 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1123 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1124 | return 0; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1125 | } |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1126 | |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1127 | if (!copy_testing_data(this, srcData, bufferAlloc, rowCopyBytes, rowCopyBytes, h)) { |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1128 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
| 1129 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1130 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1131 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | const VkCommandBufferAllocateInfo cmdInfo = { |
| 1136 | VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType |
| 1137 | NULL, // pNext |
| 1138 | fCmdPool, // commandPool |
| 1139 | VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level |
| 1140 | 1 // bufferCount |
| 1141 | }; |
| 1142 | |
| 1143 | VkCommandBuffer cmdBuffer; |
| 1144 | err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer)); |
| 1145 | if (err) { |
| 1146 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
| 1147 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1148 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1149 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | VkCommandBufferBeginInfo cmdBufferBeginInfo; |
| 1154 | memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo)); |
| 1155 | cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 1156 | cmdBufferBeginInfo.pNext = nullptr; |
| 1157 | cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 1158 | cmdBufferBeginInfo.pInheritanceInfo = nullptr; |
| 1159 | |
| 1160 | err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo)); |
| 1161 | SkASSERT(!err); |
| 1162 | |
| 1163 | // Set image layout and add barrier |
| 1164 | VkImageMemoryBarrier barrier; |
| 1165 | memset(&barrier, 0, sizeof(VkImageMemoryBarrier)); |
| 1166 | barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |
| 1167 | barrier.pNext = nullptr; |
| 1168 | barrier.srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(initialLayout); |
| 1169 | barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 1170 | barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1171 | barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1172 | barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |
| 1173 | barrier.image = image; |
| 1174 | barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0 , 1}; |
| 1175 | |
| 1176 | VK_CALL(CmdPipelineBarrier(cmdBuffer, |
| 1177 | GrVkMemory::LayoutToPipelineStageFlags(initialLayout), |
| 1178 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1179 | 0, |
| 1180 | 0, nullptr, |
| 1181 | 0, nullptr, |
| 1182 | 1, &barrier)); |
| 1183 | initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; |
| 1184 | |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1185 | // Submit copy command |
| 1186 | VkBufferImageCopy region; |
| 1187 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 1188 | region.bufferOffset = 0; |
egdaniel | a95d46b | 2016-08-15 08:06:29 -0700 | [diff] [blame] | 1189 | region.bufferRowLength = w; |
egdaniel | 3602d4f | 2016-08-12 11:58:53 -0700 | [diff] [blame] | 1190 | region.bufferImageHeight = h; |
| 1191 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1192 | region.imageOffset = { 0, 0, 0 }; |
| 1193 | region.imageExtent = { (uint32_t)w, (uint32_t)h, 1 }; |
| 1194 | |
| 1195 | VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, 1, ®ion)); |
| 1196 | |
| 1197 | // End CommandBuffer |
| 1198 | err = VK_CALL(EndCommandBuffer(cmdBuffer)); |
| 1199 | SkASSERT(!err); |
| 1200 | |
| 1201 | // Create Fence for queue |
| 1202 | VkFence fence; |
| 1203 | VkFenceCreateInfo fenceInfo; |
| 1204 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 1205 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 1206 | |
| 1207 | err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence)); |
| 1208 | SkASSERT(!err); |
| 1209 | |
| 1210 | VkSubmitInfo submitInfo; |
| 1211 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 1212 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 1213 | submitInfo.pNext = nullptr; |
| 1214 | submitInfo.waitSemaphoreCount = 0; |
| 1215 | submitInfo.pWaitSemaphores = nullptr; |
| 1216 | submitInfo.pWaitDstStageMask = 0; |
| 1217 | submitInfo.commandBufferCount = 1; |
| 1218 | submitInfo.pCommandBuffers = &cmdBuffer; |
| 1219 | submitInfo.signalSemaphoreCount = 0; |
| 1220 | submitInfo.pSignalSemaphores = nullptr; |
| 1221 | err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence)); |
| 1222 | SkASSERT(!err); |
| 1223 | |
| 1224 | err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX)); |
| 1225 | if (VK_TIMEOUT == err) { |
| 1226 | GrVkMemory::FreeImageMemory(this, linearTiling, alloc); |
| 1227 | VK_CALL(DestroyImage(fDevice, image, nullptr)); |
| 1228 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1229 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1230 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1231 | VK_CALL(DestroyFence(fDevice, fence, nullptr)); |
| 1232 | SkDebugf("Fence failed to signal: %d\n", err); |
| 1233 | SkFAIL("failing"); |
| 1234 | } |
| 1235 | SkASSERT(!err); |
| 1236 | |
| 1237 | // Clean up transfer resources |
| 1238 | GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc); |
| 1239 | VK_CALL(DestroyBuffer(fDevice, buffer, nullptr)); |
| 1240 | VK_CALL(FreeCommandBuffers(fDevice, fCmdPool, 1, &cmdBuffer)); |
| 1241 | VK_CALL(DestroyFence(fDevice, fence, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1245 | GrVkImageInfo* info = new GrVkImageInfo; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1246 | info->fImage = image; |
| 1247 | info->fAlloc = alloc; |
| 1248 | info->fImageTiling = imageTiling; |
| 1249 | info->fImageLayout = initialLayout; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1250 | info->fFormat = pixelFormat; |
jvanverth | 2af0f1b | 2016-05-03 10:36:49 -0700 | [diff] [blame] | 1251 | info->fLevelCount = 1; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1252 | |
| 1253 | return (GrBackendObject)info; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1257 | const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1258 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 1259 | if (backend && backend->fImage && backend->fAlloc.fMemory) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1260 | VkMemoryRequirements req; |
| 1261 | memset(&req, 0, sizeof(req)); |
| 1262 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
| 1263 | backend->fImage, |
| 1264 | &req)); |
| 1265 | // TODO: find a better check |
| 1266 | // This will probably fail with a different driver |
| 1267 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 1268 | } |
| 1269 | |
| 1270 | return false; |
| 1271 | } |
| 1272 | |
| 1273 | void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) { |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1274 | GrVkImageInfo* backend = reinterpret_cast<GrVkImageInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1275 | if (backend) { |
| 1276 | if (!abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1277 | // something in the command buffer may still be using this, so force submit |
| 1278 | this->submitCommandBuffer(kForce_SyncQueue); |
jvanverth | 6b6ffc4 | 2016-06-13 14:28:07 -0700 | [diff] [blame] | 1279 | GrVkImage::DestroyImageInfo(this, backend); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1280 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 1281 | delete backend; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | //////////////////////////////////////////////////////////////////////////////// |
| 1286 | |
| 1287 | void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1288 | VkPipelineStageFlags dstStageMask, |
| 1289 | bool byRegion, |
| 1290 | VkMemoryBarrier* barrier) const { |
| 1291 | SkASSERT(fCurrentCmdBuffer); |
| 1292 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1293 | srcStageMask, |
| 1294 | dstStageMask, |
| 1295 | byRegion, |
| 1296 | GrVkCommandBuffer::kMemory_BarrierType, |
| 1297 | barrier); |
| 1298 | } |
| 1299 | |
| 1300 | void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1301 | VkPipelineStageFlags dstStageMask, |
| 1302 | bool byRegion, |
| 1303 | VkBufferMemoryBarrier* barrier) const { |
| 1304 | SkASSERT(fCurrentCmdBuffer); |
| 1305 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1306 | srcStageMask, |
| 1307 | dstStageMask, |
| 1308 | byRegion, |
| 1309 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 1310 | barrier); |
| 1311 | } |
| 1312 | |
| 1313 | void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1314 | VkPipelineStageFlags dstStageMask, |
| 1315 | bool byRegion, |
| 1316 | VkImageMemoryBarrier* barrier) const { |
| 1317 | SkASSERT(fCurrentCmdBuffer); |
| 1318 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1319 | srcStageMask, |
| 1320 | dstStageMask, |
| 1321 | byRegion, |
| 1322 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 1323 | barrier); |
| 1324 | } |
| 1325 | |
| 1326 | void GrVkGpu::finishDrawTarget() { |
| 1327 | // Submit the current command buffer to the Queue |
| 1328 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 1329 | } |
| 1330 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1331 | void GrVkGpu::clearStencil(GrRenderTarget* target) { |
| 1332 | if (nullptr == target) { |
| 1333 | return; |
| 1334 | } |
| 1335 | GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment(); |
| 1336 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1337 | |
| 1338 | |
| 1339 | VkClearDepthStencilValue vkStencilColor; |
| 1340 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 1341 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1342 | vkStencil->setImageLayout(this, |
| 1343 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1344 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1345 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1346 | false); |
| 1347 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1348 | VkImageSubresourceRange subRange; |
| 1349 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1350 | subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1351 | subRange.baseMipLevel = 0; |
| 1352 | subRange.levelCount = 1; |
| 1353 | subRange.baseArrayLayer = 0; |
| 1354 | subRange.layerCount = 1; |
| 1355 | |
| 1356 | // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a |
| 1357 | // draw. Thus we should look into using the load op functions on the render pass to clear out |
| 1358 | // the stencil there. |
| 1359 | fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange); |
| 1360 | } |
| 1361 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1362 | inline bool can_copy_image(const GrSurface* dst, |
| 1363 | const GrSurface* src, |
| 1364 | const GrVkGpu* gpu) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1365 | const GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 1366 | const GrRenderTarget* srcRT = src->asRenderTarget(); |
| 1367 | if (dstRT && srcRT) { |
| 1368 | if (srcRT->numColorSamples() != dstRT->numColorSamples()) { |
| 1369 | return false; |
| 1370 | } |
| 1371 | } else if (dstRT) { |
| 1372 | if (dstRT->numColorSamples() > 1) { |
| 1373 | return false; |
| 1374 | } |
| 1375 | } else if (srcRT) { |
| 1376 | if (srcRT->numColorSamples() > 1) { |
| 1377 | return false; |
| 1378 | } |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1382 | // as image usage flags. |
| 1383 | if (src->origin() == dst->origin() && |
| 1384 | GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1385 | return true; |
| 1386 | } |
| 1387 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1388 | return false; |
| 1389 | } |
| 1390 | |
| 1391 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, |
| 1392 | GrSurface* src, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1393 | GrVkImage* dstImage, |
| 1394 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1395 | const SkIRect& srcRect, |
| 1396 | const SkIPoint& dstPoint) { |
| 1397 | SkASSERT(can_copy_image(dst, src, this)); |
| 1398 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1399 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 1400 | // the cache is flushed since it is only being written to. |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1401 | dstImage->setImageLayout(this, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1402 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1403 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1404 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1405 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1406 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1407 | srcImage->setImageLayout(this, |
| 1408 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1409 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1410 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1411 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1412 | |
| 1413 | // Flip rect if necessary |
| 1414 | SkIRect srcVkRect = srcRect; |
| 1415 | int32_t dstY = dstPoint.fY; |
| 1416 | |
| 1417 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1418 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin()); |
| 1419 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1420 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1421 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1422 | } |
| 1423 | |
| 1424 | VkImageCopy copyRegion; |
| 1425 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 1426 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1427 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1428 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1429 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
egdaniel | c355bc8 | 2016-04-27 11:31:59 -0700 | [diff] [blame] | 1430 | // The depth value of the extent is ignored according the vulkan spec for 2D images. However, on |
| 1431 | // at least the nexus 5X it seems to be checking it. Thus as a working around we must have the |
| 1432 | // depth value be 1. |
| 1433 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1434 | |
| 1435 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1436 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1437 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1438 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1439 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1440 | 1, |
| 1441 | ©Region); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1442 | |
| 1443 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 1444 | srcRect.width(), srcRect.height()); |
| 1445 | this->didWriteToSurface(dst, &dstRect); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1446 | } |
| 1447 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1448 | inline bool can_copy_as_blit(const GrSurface* dst, |
| 1449 | const GrSurface* src, |
| 1450 | const GrVkImage* dstImage, |
| 1451 | const GrVkImage* srcImage, |
| 1452 | const GrVkGpu* gpu) { |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1453 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1454 | // as image usage flags. |
| 1455 | const GrVkCaps& caps = gpu->vkCaps(); |
| 1456 | if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) || |
| 1457 | !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) { |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | // We cannot blit images that are multisampled. Will need to figure out if we can blit the |
| 1462 | // resolved msaa though. |
| 1463 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1464 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, |
| 1472 | GrSurface* src, |
| 1473 | GrVkImage* dstImage, |
| 1474 | GrVkImage* srcImage, |
| 1475 | const SkIRect& srcRect, |
| 1476 | const SkIPoint& dstPoint) { |
| 1477 | SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this)); |
| 1478 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1479 | dstImage->setImageLayout(this, |
| 1480 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1481 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1482 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1483 | false); |
| 1484 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1485 | srcImage->setImageLayout(this, |
| 1486 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1487 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1488 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1489 | false); |
| 1490 | |
| 1491 | // Flip rect if necessary |
| 1492 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1493 | srcVkRect.fLeft = srcRect.fLeft; |
| 1494 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1495 | SkIRect dstRect; |
| 1496 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1497 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1498 | |
| 1499 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1500 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1501 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1502 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1503 | srcVkRect.fTop = srcRect.fTop; |
| 1504 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 1508 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1509 | } else { |
| 1510 | dstRect.fTop = dstPoint.fY; |
| 1511 | } |
| 1512 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 1513 | |
| 1514 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 1515 | // get the correct origintation of the copied data. |
| 1516 | if (src->origin() != dst->origin()) { |
| 1517 | SkTSwap(dstRect.fTop, dstRect.fBottom); |
| 1518 | } |
| 1519 | |
| 1520 | VkImageBlit blitRegion; |
| 1521 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 1522 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1523 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1524 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 0 }; |
| 1525 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1526 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
| 1527 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 0 }; |
| 1528 | |
| 1529 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1530 | *srcImage, |
| 1531 | *dstImage, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1532 | 1, |
| 1533 | &blitRegion, |
| 1534 | VK_FILTER_NEAREST); // We never scale so any filter works here |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1535 | |
| 1536 | this->didWriteToSurface(dst, &dstRect); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1539 | inline bool can_copy_as_resolve(const GrSurface* dst, |
| 1540 | const GrSurface* src, |
| 1541 | const GrVkGpu* gpu) { |
| 1542 | // Our src must be a multisampled render target |
| 1543 | if (!src->asRenderTarget() || src->asRenderTarget()->numColorSamples() <= 1) { |
| 1544 | return false; |
| 1545 | } |
| 1546 | |
| 1547 | // The dst must be a render target but not multisampled |
| 1548 | if (!dst->asRenderTarget() || dst->asRenderTarget()->numColorSamples() > 1) { |
| 1549 | return false; |
| 1550 | } |
| 1551 | |
| 1552 | // Surfaces must have the same origin. |
| 1553 | if (src->origin() != dst->origin()) { |
| 1554 | return false; |
| 1555 | } |
| 1556 | |
| 1557 | return true; |
| 1558 | } |
| 1559 | |
| 1560 | void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, |
| 1561 | GrSurface* src, |
| 1562 | const SkIRect& srcRect, |
| 1563 | const SkIPoint& dstPoint) { |
| 1564 | GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget()); |
| 1565 | GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
| 1566 | SkASSERT(dstRT && dstRT->numColorSamples() <= 1); |
| 1567 | this->resolveImage(dstRT, srcRT, srcRect, dstPoint); |
| 1568 | } |
| 1569 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1570 | inline bool can_copy_as_draw(const GrSurface* dst, |
| 1571 | const GrSurface* src, |
| 1572 | const GrVkGpu* gpu) { |
| 1573 | return false; |
| 1574 | } |
| 1575 | |
| 1576 | void GrVkGpu::copySurfaceAsDraw(GrSurface* dst, |
| 1577 | GrSurface* src, |
| 1578 | const SkIRect& srcRect, |
| 1579 | const SkIPoint& dstPoint) { |
| 1580 | SkASSERT(false); |
| 1581 | } |
| 1582 | |
| 1583 | bool GrVkGpu::onCopySurface(GrSurface* dst, |
| 1584 | GrSurface* src, |
| 1585 | const SkIRect& srcRect, |
| 1586 | const SkIPoint& dstPoint) { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1587 | if (can_copy_as_resolve(dst, src, this)) { |
| 1588 | this->copySurfaceAsResolve(dst, src, srcRect, dstPoint); |
egdaniel | ec44099 | 2016-09-13 09:54:11 -0700 | [diff] [blame] | 1589 | return true; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
egdaniel | fd016d7 | 2016-09-27 12:13:05 -0700 | [diff] [blame] | 1592 | if (this->vkCaps().mustSubmitCommandsBeforeCopyOp()) { |
| 1593 | this->submitCommandBuffer(GrVkGpu::kSkip_SyncQueue); |
| 1594 | } |
| 1595 | |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 1596 | if (fCopyManager.copySurfaceAsDraw(this, dst, src, srcRect, dstPoint)) { |
| 1597 | return true; |
| 1598 | } |
| 1599 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1600 | GrVkImage* dstImage; |
| 1601 | GrVkImage* srcImage; |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1602 | GrRenderTarget* dstRT = dst->asRenderTarget(); |
| 1603 | if (dstRT) { |
| 1604 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT); |
| 1605 | dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT; |
| 1606 | } else { |
| 1607 | SkASSERT(dst->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1608 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1609 | } |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1610 | GrRenderTarget* srcRT = src->asRenderTarget(); |
| 1611 | if (srcRT) { |
| 1612 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT); |
| 1613 | srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1614 | } else { |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1615 | SkASSERT(src->asTexture()); |
| 1616 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1619 | if (can_copy_image(dst, src, this)) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1620 | this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint); |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) { |
| 1625 | this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1626 | return true; |
| 1627 | } |
| 1628 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1629 | return false; |
| 1630 | } |
| 1631 | |
egdaniel | 4bcd62e | 2016-08-31 07:37:31 -0700 | [diff] [blame] | 1632 | bool GrVkGpu::initDescForDstCopy(const GrRenderTarget* src, GrSurfaceDesc* desc) const { |
| 1633 | // We can always succeed here with either a CopyImage (none msaa src) or ResolveImage (msaa). |
| 1634 | // For CopyImage we can make a simple texture, for ResolveImage we require the dst to be a |
| 1635 | // render target as well. |
egdaniel | 37798fb | 2016-04-12 07:31:49 -0700 | [diff] [blame] | 1636 | desc->fOrigin = src->origin(); |
| 1637 | desc->fConfig = src->config(); |
egdaniel | bc9b296 | 2016-09-27 08:00:53 -0700 | [diff] [blame] | 1638 | if (src->numColorSamples() > 1 || |
| 1639 | (src->asTexture() && this->vkCaps().supportsCopiesAsDraws())) { |
| 1640 | desc->fFlags = kRenderTarget_GrSurfaceFlag; |
| 1641 | } else { |
| 1642 | // Just going to use CopyImage here |
| 1643 | desc->fFlags = kNone_GrSurfaceFlags; |
| 1644 | } |
| 1645 | |
egdaniel | 37798fb | 2016-04-12 07:31:49 -0700 | [diff] [blame] | 1646 | return true; |
| 1647 | } |
| 1648 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1649 | void GrVkGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&, |
csmartdalton | 0d28e57 | 2016-07-06 09:59:43 -0700 | [diff] [blame] | 1650 | int* effectiveSampleCnt, SamplePattern*) { |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1651 | // TODO: stub. |
| 1652 | SkASSERT(!this->caps()->sampleLocationsSupport()); |
| 1653 | *effectiveSampleCnt = rt->desc().fSampleCnt; |
| 1654 | } |
| 1655 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1656 | bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 1657 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 1658 | ReadPixelTempDrawInfo* tempDrawInfo) { |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1659 | // These settings we will always want if a temp draw is performed. |
| 1660 | tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 1661 | tempDrawInfo->fTempSurfaceDesc.fWidth = width; |
| 1662 | tempDrawInfo->fTempSurfaceDesc.fHeight = height; |
| 1663 | tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 0; |
| 1664 | tempDrawInfo->fTempSurfaceDesc.fOrigin = kTopLeft_GrSurfaceOrigin; // no CPU y-flip for TL. |
bsalomon | b117ff1 | 2016-07-19 07:24:40 -0700 | [diff] [blame] | 1665 | tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox; |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1666 | |
| 1667 | // For now assume no swizzling, we may change that below. |
| 1668 | tempDrawInfo->fSwizzle = GrSwizzle::RGBA(); |
| 1669 | |
| 1670 | // Depends on why we need/want a temp draw. Start off assuming no change, the surface we read |
| 1671 | // from will be srcConfig and we will read readConfig pixels from it. |
| 1672 | // Not that if we require a draw and return a non-renderable format for the temp surface the |
| 1673 | // base class will fail for us. |
| 1674 | tempDrawInfo->fTempSurfaceDesc.fConfig = srcSurface->config(); |
| 1675 | tempDrawInfo->fReadConfig = readConfig; |
| 1676 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1677 | if (srcSurface->config() == readConfig) { |
| 1678 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1679 | } |
| 1680 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1681 | if (this->vkCaps().isConfigRenderable(readConfig, srcSurface->desc().fSampleCnt > 1)) { |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1682 | ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference); |
| 1683 | tempDrawInfo->fTempSurfaceDesc.fConfig = readConfig; |
| 1684 | tempDrawInfo->fReadConfig = readConfig; |
| 1685 | return true; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1686 | } |
| 1687 | |
egdaniel | 4583ec5 | 2016-06-27 12:57:00 -0700 | [diff] [blame] | 1688 | return false; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | bool GrVkGpu::onReadPixels(GrSurface* surface, |
| 1692 | int left, int top, int width, int height, |
| 1693 | GrPixelConfig config, |
| 1694 | void* buffer, |
| 1695 | size_t rowBytes) { |
| 1696 | VkFormat pixelFormat; |
| 1697 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1698 | return false; |
| 1699 | } |
| 1700 | |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1701 | GrVkImage* image = nullptr; |
| 1702 | GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget()); |
| 1703 | if (rt) { |
| 1704 | // resolve the render target if necessary |
| 1705 | switch (rt->getResolveType()) { |
| 1706 | case GrVkRenderTarget::kCantResolve_ResolveType: |
| 1707 | return false; |
| 1708 | case GrVkRenderTarget::kAutoResolves_ResolveType: |
| 1709 | break; |
| 1710 | case GrVkRenderTarget::kCanResolve_ResolveType: |
| 1711 | this->onResolveRenderTarget(rt); |
| 1712 | break; |
| 1713 | default: |
| 1714 | SkFAIL("Unknown resolve type"); |
| 1715 | } |
| 1716 | image = rt; |
| 1717 | } else { |
| 1718 | image = static_cast<GrVkTexture*>(surface->asTexture()); |
| 1719 | } |
| 1720 | |
| 1721 | if (!image) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1722 | return false; |
| 1723 | } |
| 1724 | |
| 1725 | // Change layout of our target so it can be used as copy |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1726 | image->setImageLayout(this, |
| 1727 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1728 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1729 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1730 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1731 | |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1732 | size_t bpp = GrBytesPerPixel(config); |
| 1733 | size_t tightRowBytes = bpp * width; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1734 | bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1735 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1736 | VkBufferImageCopy region; |
| 1737 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1738 | |
| 1739 | bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin(); |
| 1740 | if (copyFromOrigin) { |
| 1741 | region.imageOffset = { 0, 0, 0 }; |
| 1742 | region.imageExtent = { (uint32_t)(left + width), |
| 1743 | (uint32_t)(flipY ? surface->height() - top : top + height), |
| 1744 | 1 |
| 1745 | }; |
| 1746 | } else { |
| 1747 | VkOffset3D offset = { |
| 1748 | left, |
| 1749 | flipY ? surface->height() - top - height : top, |
| 1750 | 0 |
| 1751 | }; |
| 1752 | region.imageOffset = offset; |
| 1753 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 1754 | } |
| 1755 | |
| 1756 | size_t transBufferRowBytes = bpp * region.imageExtent.width; |
| 1757 | GrVkTransferBuffer* transferBuffer = |
| 1758 | static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * height, |
| 1759 | kXferGpuToCpu_GrBufferType, |
| 1760 | kStream_GrAccessPattern)); |
| 1761 | |
| 1762 | // Copy the image to a buffer so we can map it to cpu memory |
jvanverth | db37909 | 2016-07-07 11:18:46 -0700 | [diff] [blame] | 1763 | region.bufferOffset = transferBuffer->offset(); |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1764 | region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1765 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 1766 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1767 | |
| 1768 | fCurrentCmdBuffer->copyImageToBuffer(this, |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1769 | image, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1770 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1771 | transferBuffer, |
| 1772 | 1, |
| 1773 | ®ion); |
| 1774 | |
| 1775 | // make sure the copy to buffer has finished |
| 1776 | transferBuffer->addMemoryBarrier(this, |
| 1777 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1778 | VK_ACCESS_HOST_READ_BIT, |
| 1779 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1780 | VK_PIPELINE_STAGE_HOST_BIT, |
| 1781 | false); |
| 1782 | |
| 1783 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 1784 | // we can copy the data out of the buffer. |
| 1785 | this->submitCommandBuffer(kForce_SyncQueue); |
jvanverth | 9d54afc | 2016-09-20 09:20:03 -0700 | [diff] [blame] | 1786 | GrVkMemory::InvalidateMappedAlloc(this, transferBuffer->alloc()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1787 | void* mappedMemory = transferBuffer->map(); |
| 1788 | |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1789 | if (copyFromOrigin) { |
| 1790 | uint32_t skipRows = region.imageExtent.height - height; |
| 1791 | mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left; |
| 1792 | } |
| 1793 | |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1794 | if (flipY) { |
| 1795 | const char* srcRow = reinterpret_cast<const char*>(mappedMemory); |
| 1796 | char* dstRow = reinterpret_cast<char*>(buffer)+(height - 1) * rowBytes; |
| 1797 | for (int y = 0; y < height; y++) { |
| 1798 | memcpy(dstRow, srcRow, tightRowBytes); |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1799 | srcRow += transBufferRowBytes; |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1800 | dstRow -= rowBytes; |
| 1801 | } |
| 1802 | } else { |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1803 | if (transBufferRowBytes == rowBytes) { |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1804 | memcpy(buffer, mappedMemory, rowBytes*height); |
| 1805 | } else { |
egdaniel | 6fa0a91 | 2016-09-12 11:51:29 -0700 | [diff] [blame] | 1806 | SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, |
| 1807 | height); |
egdaniel | 88e8aef | 2016-06-27 14:34:55 -0700 | [diff] [blame] | 1808 | } |
| 1809 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1810 | |
| 1811 | transferBuffer->unmap(); |
| 1812 | transferBuffer->unref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1813 | return true; |
| 1814 | } |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 1815 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1816 | // The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple |
| 1817 | // of the granularity. The width must also be a multiple of the granularity or eaqual to the width |
| 1818 | // the the entire attachment. Similar requirements for the y and height components. |
| 1819 | void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds, |
| 1820 | const VkExtent2D& granularity, int maxWidth, int maxHeight) { |
| 1821 | // Adjust Width |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 1822 | if ((0 != granularity.width && 1 != granularity.width)) { |
| 1823 | // Start with the right side of rect so we know if we end up going pass the maxWidth. |
| 1824 | int rightAdj = srcBounds.fRight % granularity.width; |
| 1825 | if (rightAdj != 0) { |
| 1826 | rightAdj = granularity.width - rightAdj; |
| 1827 | } |
| 1828 | dstBounds->fRight = srcBounds.fRight + rightAdj; |
| 1829 | if (dstBounds->fRight > maxWidth) { |
| 1830 | dstBounds->fRight = maxWidth; |
| 1831 | dstBounds->fLeft = 0; |
| 1832 | } else { |
| 1833 | dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width; |
| 1834 | } |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1835 | } else { |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 1836 | dstBounds->fLeft = srcBounds.fLeft; |
| 1837 | dstBounds->fRight = srcBounds.fRight; |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Adjust height |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 1841 | if ((0 != granularity.height && 1 != granularity.height)) { |
| 1842 | // Start with the bottom side of rect so we know if we end up going pass the maxHeight. |
| 1843 | int bottomAdj = srcBounds.fBottom % granularity.height; |
| 1844 | if (bottomAdj != 0) { |
| 1845 | bottomAdj = granularity.height - bottomAdj; |
| 1846 | } |
| 1847 | dstBounds->fBottom = srcBounds.fBottom + bottomAdj; |
| 1848 | if (dstBounds->fBottom > maxHeight) { |
| 1849 | dstBounds->fBottom = maxHeight; |
| 1850 | dstBounds->fTop = 0; |
| 1851 | } else { |
| 1852 | dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height; |
| 1853 | } |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1854 | } else { |
egdaniel | d5797b3 | 2016-09-20 12:57:45 -0700 | [diff] [blame] | 1855 | dstBounds->fTop = srcBounds.fTop; |
| 1856 | dstBounds->fBottom = srcBounds.fBottom; |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1857 | } |
| 1858 | } |
| 1859 | |
jvanverth | 7ec9241 | 2016-07-06 09:24:57 -0700 | [diff] [blame] | 1860 | void GrVkGpu::submitSecondaryCommandBuffer(GrVkSecondaryCommandBuffer* buffer, |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1861 | const GrVkRenderPass* renderPass, |
| 1862 | const VkClearValue* colorClear, |
| 1863 | GrVkRenderTarget* target, |
| 1864 | const SkIRect& bounds) { |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 1865 | const SkIRect* pBounds = &bounds; |
| 1866 | SkIRect flippedBounds; |
| 1867 | if (kBottomLeft_GrSurfaceOrigin == target->origin()) { |
| 1868 | flippedBounds = bounds; |
| 1869 | flippedBounds.fTop = target->height() - bounds.fBottom; |
| 1870 | flippedBounds.fBottom = target->height() - bounds.fTop; |
| 1871 | pBounds = &flippedBounds; |
| 1872 | } |
| 1873 | |
egdaniel | 27bb284 | 2016-07-07 11:58:35 -0700 | [diff] [blame] | 1874 | // The bounds we use for the render pass should be of the granularity supported |
| 1875 | // by the device. |
| 1876 | const VkExtent2D& granularity = renderPass->granularity(); |
| 1877 | SkIRect adjustedBounds; |
| 1878 | if ((0 != granularity.width && 1 != granularity.width) || |
| 1879 | (0 != granularity.height && 1 != granularity.height)) { |
| 1880 | adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity, |
| 1881 | target->width(), target->height()); |
| 1882 | pBounds = &adjustedBounds; |
| 1883 | } |
| 1884 | |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1885 | // Currently it is fine for us to always pass in 1 for the clear count even if no attachment |
| 1886 | // uses it. In the current state, we also only use the LOAD_OP_CLEAR for the color attachment |
| 1887 | // which is always at the first attachment. |
egdaniel | e7d1b24 | 2016-07-01 08:06:45 -0700 | [diff] [blame] | 1888 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, 1, colorClear, *target, *pBounds, true); |
egdaniel | 066df7c | 2016-06-08 14:02:27 -0700 | [diff] [blame] | 1889 | fCurrentCmdBuffer->executeCommands(this, buffer); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1890 | fCurrentCmdBuffer->endRenderPass(this); |
egdaniel | 6693355 | 2016-08-24 07:22:19 -0700 | [diff] [blame] | 1891 | |
egdaniel | ce3bfb1 | 2016-08-26 11:05:13 -0700 | [diff] [blame] | 1892 | this->didWriteToSurface(target, &bounds); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1893 | } |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1894 | |
jvanverth | 84741b3 | 2016-09-30 08:39:02 -0700 | [diff] [blame] | 1895 | GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() const { |
| 1896 | VkFenceCreateInfo createInfo; |
| 1897 | memset(&createInfo, 0, sizeof(VkFenceCreateInfo)); |
| 1898 | createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 1899 | createInfo.pNext = nullptr; |
| 1900 | createInfo.flags = 0; |
| 1901 | VkFence fence = VK_NULL_HANDLE; |
| 1902 | VkResult result = GR_VK_CALL(this->vkInterface(), CreateFence(this->device(), &createInfo, |
| 1903 | nullptr, &fence)); |
| 1904 | // TODO: verify that all QueueSubmits before this will finish before this fence signals |
| 1905 | if (VK_SUCCESS == result) { |
| 1906 | GR_VK_CALL(this->vkInterface(), QueueSubmit(this->queue(), 0, nullptr, fence)); |
| 1907 | } |
| 1908 | return (GrFence)fence; |
| 1909 | } |
| 1910 | |
| 1911 | bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) const { |
| 1912 | VkResult result = GR_VK_CALL(this->vkInterface(), WaitForFences(this->device(), 1, |
| 1913 | (VkFence*)&fence, |
| 1914 | VK_TRUE, |
| 1915 | timeout)); |
| 1916 | return (VK_SUCCESS == result); |
| 1917 | } |
| 1918 | |
| 1919 | void GrVkGpu::deleteFence(GrFence fence) const { |
| 1920 | GR_VK_CALL(this->vkInterface(), DestroyFence(this->device(), (VkFence)fence, nullptr)); |
| 1921 | } |
| 1922 | |