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